Skip to content
This repository was archived by the owner on Mar 24, 2021. It is now read-only.

Commit b8c5c3f

Browse files
committed
Fix docs and small code problems in multiset
1 parent 803cbc8 commit b8c5c3f

File tree

4 files changed

+41
-50
lines changed

4 files changed

+41
-50
lines changed

Makefile.am

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ if ENABLE_MODULE_ECDH
137137
include src/modules/ecdh/Makefile.am.include
138138
endif
139139

140-
if ENABLE_MODULE_RECOVERY
141-
include src/modules/recovery/Makefile.am.include
142-
endif
143-
144140
if ENABLE_MODULE_MULTISET
145141
include src/modules/multiset/Makefile.am.include
146142
endif
143+
144+
if ENABLE_MODULE_RECOVERY
145+
include src/modules/recovery/Makefile.am.include
146+
endif

include/secp256k1_multiset.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct {
2929
* Returns: 1: success
3030
* 0: invalid parameter
3131
* Args: ctx: pointer to a context object (cannot be NULL)
32-
* Out: multiset: the resulting multiset
32+
* Out: multiset: the resulting multiset (cannot be NULL)
3333
*/
3434
SECP256K1_API int secp256k1_multiset_init(
3535
const secp256k1_context* ctx,
@@ -68,8 +68,8 @@ SECP256K1_API int secp256k1_multiset_parse(
6868
* Returns: 1: success
6969
* 0: invalid parameter
7070
* Args: ctx: pointer to a context object (cannot be NULL)
71-
* Out: multiset: the multiset to update
72-
* In: input: the data to add
71+
* Out: multiset: the multiset to update (cannot be NULL)
72+
* In: input: the data to add (cannot be NULL)
7373
* inputLen: the size of the data to add
7474
*/
7575
SECP256K1_API int secp256k1_multiset_add(
@@ -84,8 +84,8 @@ SECP256K1_API int secp256k1_multiset_add(
8484
* Returns: 1: success
8585
* 0: invalid parameter
8686
* Args: ctx: pointer to a context object (cannot be NULL)
87-
* Out: multiset: the multiset to update
88-
* In: input: the data to remove
87+
* Out: multiset: the multiset to update (cannot be NULL)
88+
* In: input: the data to remove (cannot be NULL)
8989
* inputLen: the size of the data to remove
9090
*/
9191
SECP256K1_API int secp256k1_multiset_remove(
@@ -102,7 +102,7 @@ SECP256K1_API int secp256k1_multiset_remove(
102102
* Returns: 1: success
103103
* 0: invalid parameter
104104
* Args: ctx: pointer to a context object (cannot be NULL)
105-
* In/Out: multiset: the multiset to which the input must be added
105+
* In/Out: multiset: the multiset to which the input must be added (cannot be NULL)
106106
* In: input: the multiset to add
107107
*/
108108
SECP256K1_API int secp256k1_multiset_combine(

src/modules/multiset/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Algorithm
5353

5454
Using Elliptic Curves as multisets is described in [3].
5555

56-
This implementation uses trial-and-hash [4] to convert the hash into
56+
This implementation uses Try and Increment [4] to convert the hash into
5757
point on the secp256k1 curve which serves as multiset. The curve's
5858
group operations are then used to add and remove multisets.
5959
Associativity and Commutativity then follow.
@@ -67,7 +67,6 @@ not securely conceal the underlying data being hashed.
6767

6868
For the purpose of UTXO commitments this is not relevant.
6969

70-
Faster and constant time algorithms exists [3] but only for char-2 curves.
7170

7271
References
7372
----------

src/modules/multiset/main_impl.h

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,47 @@
33
* Distributed under the MIT software license, see the accompanying *
44
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
55
**********************************************************************/
6+
/*For future reference: https://reviews.bitcoinabc.org/D1072 */
67

78
#ifndef _SECP256K1_MODULE_MULTISET_MAIN_
89
#define _SECP256K1_MODULE_MULTISET_MAIN_
910

10-
1111
#include "include/secp256k1_multiset.h"
1212

13-
#include "hash.h"
14-
#include "field.h"
15-
#include "group.h"
16-
17-
/** Converts a group element (Jacobian) to a multiset.
18-
* Requires the field elements to be normalized
19-
* Infinite uses special value, z = 0
13+
/* Converts a group element (Jacobian) to a multiset.
14+
* Requires the field elements to be normalized
15+
* Infinite uses special value, z = 0
16+
* Will also normalize the input.
2017
*/
21-
static void multiset_from_gej_var(secp256k1_multiset *target, const secp256k1_gej *input) {
22-
if (input->infinity) {
18+
static void multiset_from_gej_var(secp256k1_multiset *target, secp256k1_gej *input) {
19+
if (secp256k1_gej_is_infinity(input)) {
2320
memset(&target->d, 0, sizeof(target->d));
2421
} else {
22+
secp256k1_fe_normalize(&input->x);
23+
secp256k1_fe_normalize(&input->y);
24+
secp256k1_fe_normalize(&input->z);
25+
2526
secp256k1_fe_get_b32(target->d, &input->x);
26-
secp256k1_fe_get_b32(target->d+32, &input->y);
27-
secp256k1_fe_get_b32(target->d+64, &input->z);
27+
secp256k1_fe_get_b32(target->d + 32, &input->y);
28+
secp256k1_fe_get_b32(target->d + 64, &input->z);
2829
}
2930
}
3031

31-
/** Converts a multiset to group element (Jacobian)
32-
* Infinite uses special value, z = 0
33-
*/
34-
static void gej_from_multiset_var(secp256k1_gej *target, const secp256k1_multiset *input) {
32+
/* Converts a multiset to group element (Jacobian)
33+
* Infinite uses special value, z = 0 */
34+
static void gej_from_multiset_var(secp256k1_gej *target, const secp256k1_multiset *input) {
3535
secp256k1_fe_set_b32(&target->x, input->d);
36-
secp256k1_fe_set_b32(&target->y, input->d+32);
37-
secp256k1_fe_set_b32(&target->z, input->d+64);
36+
secp256k1_fe_set_b32(&target->y, input->d + 32);
37+
secp256k1_fe_set_b32(&target->z, input->d + 64);
3838

3939
target->infinity = secp256k1_fe_is_zero(&target->z) ? 1 : 0;
4040
}
4141

42-
/** Converts a data element to a group element (affine)
42+
/* Converts a data element to a group element (affine)
4343
*
44-
* We use trial-and-rehash which is fast but non-constant time.
45-
* Though constant time algo's exist we are not concerned with timing attacks
46-
* as we make no attempt to hide the underlying data
44+
* We use Try and Increment which is fast but non-constant time.
45+
* Though constant time algo's exist we are not concerned with timing attacks
46+
* as we make no attempt to hide the underlying data
4747
*
4848
* Pass inverse=0 to generate the group element, or inverse=1 to generate its inverse
4949
*/
@@ -89,13 +89,12 @@ static void ge_from_data_var(secp256k1_ge *target, const unsigned char *input, s
8989
}
9090

9191
VERIFY_CHECK(secp256k1_ge_is_valid_var(target));
92-
VERIFY_CHECK(!secp256k1_ge_is_infinity(target));
9392
break;
9493
}
9594
}
9695

9796
/** Adds or removes a data element */
98-
static int multiset_add_remove(const secp256k1_context* ctx, secp256k1_multiset *multiset, const unsigned char *input, size_t inputLen, int remove) {
97+
static int multiset_add_remove(const secp256k1_context *ctx, secp256k1_multiset *multiset, const unsigned char *input, size_t inputLen, int remove) {
9998
secp256k1_ge newelm;
10099
secp256k1_gej source, target;
101100

@@ -108,9 +107,6 @@ static int multiset_add_remove(const secp256k1_context* ctx, secp256k1_multiset
108107

109108
secp256k1_gej_add_ge_var(&target, &source, &newelm, NULL);
110109

111-
secp256k1_fe_normalize(&target.x);
112-
secp256k1_fe_normalize(&target.y);
113-
secp256k1_fe_normalize(&target.z);
114110
multiset_from_gej_var(multiset, &target);
115111

116112
return 1;
@@ -139,15 +135,13 @@ int secp256k1_multiset_combine(const secp256k1_context* ctx, secp256k1_multiset
139135

140136
secp256k1_gej_add_var(&gej_result, &gej_multiset, &gej_input, NULL);
141137

142-
secp256k1_fe_normalize(&gej_result.x);
143-
secp256k1_fe_normalize(&gej_result.y);
144-
secp256k1_fe_normalize(&gej_result.z);
145138
multiset_from_gej_var(multiset, &gej_result);
146139

147140
return 1;
148141
}
149142

150143
/** Hash the multiset into resultHash */
144+
/* TODO: Add hash function pointer to optionally replace the hash function */
151145
int secp256k1_multiset_finalize(const secp256k1_context* ctx, unsigned char *resultHash, const secp256k1_multiset *multiset) {
152146
secp256k1_sha256 hasher;
153147
unsigned char buffer[64];
@@ -159,13 +153,11 @@ int secp256k1_multiset_finalize(const secp256k1_context* ctx, unsigned char *res
159153
ARG_CHECK(multiset != NULL);
160154

161155
gej_from_multiset_var(&gej, multiset);
162-
163-
if (gej.infinity) {
156+
if (secp256k1_gej_is_infinity(&gej)) {
164157
/* empty set is encoded as zeros */
165158
memset(resultHash, 0x00, 32);
166159
return 1;
167160
}
168-
169161
/* we must normalize to affine first */
170162
secp256k1_ge_set_gej(&ge, &gej);
171163
secp256k1_fe_normalize(&ge.x);
@@ -180,13 +172,13 @@ int secp256k1_multiset_finalize(const secp256k1_context* ctx, unsigned char *res
180172
return 1;
181173
}
182174

183-
/** Inits the multiset with the constant for empty data,
184-
* represented by the Jacobian GE infinite
185-
*/
186-
int secp256k1_multiset_init(const secp256k1_context* ctx, secp256k1_multiset *multiset) {
187-
const secp256k1_gej inf = SECP256K1_GEJ_CONST_INFINITY;
175+
/* Inits the multiset with the constant for empty data,
176+
represented by the Jacobian GE infinite */
177+
int secp256k1_multiset_init(const secp256k1_context *ctx, secp256k1_multiset *multiset) {
178+
secp256k1_gej inf = SECP256K1_GEJ_CONST_INFINITY;
188179

189180
VERIFY_CHECK(ctx != NULL);
181+
VERIFY_CHECK(multiset != NULL);
190182

191183
multiset_from_gej_var(multiset, &inf);
192184

0 commit comments

Comments
 (0)