3
3
* Distributed under the MIT software license, see the accompanying *
4
4
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5
5
**********************************************************************/
6
+ /*For future reference: https://reviews.bitcoinabc.org/D1072 */
6
7
7
8
#ifndef _SECP256K1_MODULE_MULTISET_MAIN_
8
9
#define _SECP256K1_MODULE_MULTISET_MAIN_
9
10
10
-
11
11
#include "include/secp256k1_multiset.h"
12
12
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.
20
17
*/
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 ) ) {
23
20
memset (& target -> d , 0 , sizeof (target -> d ));
24
21
} else {
22
+ secp256k1_fe_normalize (& input -> x );
23
+ secp256k1_fe_normalize (& input -> y );
24
+ secp256k1_fe_normalize (& input -> z );
25
+
25
26
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 );
28
29
}
29
30
}
30
31
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 ) {
35
35
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 );
38
38
39
39
target -> infinity = secp256k1_fe_is_zero (& target -> z ) ? 1 : 0 ;
40
40
}
41
41
42
- /** Converts a data element to a group element (affine)
42
+ /* Converts a data element to a group element (affine)
43
43
*
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
47
47
*
48
48
* Pass inverse=0 to generate the group element, or inverse=1 to generate its inverse
49
49
*/
@@ -89,13 +89,12 @@ static void ge_from_data_var(secp256k1_ge *target, const unsigned char *input, s
89
89
}
90
90
91
91
VERIFY_CHECK (secp256k1_ge_is_valid_var (target ));
92
- VERIFY_CHECK (!secp256k1_ge_is_infinity (target ));
93
92
break ;
94
93
}
95
94
}
96
95
97
96
/** 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 ) {
99
98
secp256k1_ge newelm ;
100
99
secp256k1_gej source , target ;
101
100
@@ -108,9 +107,6 @@ static int multiset_add_remove(const secp256k1_context* ctx, secp256k1_multiset
108
107
109
108
secp256k1_gej_add_ge_var (& target , & source , & newelm , NULL );
110
109
111
- secp256k1_fe_normalize (& target .x );
112
- secp256k1_fe_normalize (& target .y );
113
- secp256k1_fe_normalize (& target .z );
114
110
multiset_from_gej_var (multiset , & target );
115
111
116
112
return 1 ;
@@ -139,15 +135,13 @@ int secp256k1_multiset_combine(const secp256k1_context* ctx, secp256k1_multiset
139
135
140
136
secp256k1_gej_add_var (& gej_result , & gej_multiset , & gej_input , NULL );
141
137
142
- secp256k1_fe_normalize (& gej_result .x );
143
- secp256k1_fe_normalize (& gej_result .y );
144
- secp256k1_fe_normalize (& gej_result .z );
145
138
multiset_from_gej_var (multiset , & gej_result );
146
139
147
140
return 1 ;
148
141
}
149
142
150
143
/** Hash the multiset into resultHash */
144
+ /* TODO: Add hash function pointer to optionally replace the hash function */
151
145
int secp256k1_multiset_finalize (const secp256k1_context * ctx , unsigned char * resultHash , const secp256k1_multiset * multiset ) {
152
146
secp256k1_sha256 hasher ;
153
147
unsigned char buffer [64 ];
@@ -159,13 +153,11 @@ int secp256k1_multiset_finalize(const secp256k1_context* ctx, unsigned char *res
159
153
ARG_CHECK (multiset != NULL );
160
154
161
155
gej_from_multiset_var (& gej , multiset );
162
-
163
- if (gej .infinity ) {
156
+ if (secp256k1_gej_is_infinity (& gej )) {
164
157
/* empty set is encoded as zeros */
165
158
memset (resultHash , 0x00 , 32 );
166
159
return 1 ;
167
160
}
168
-
169
161
/* we must normalize to affine first */
170
162
secp256k1_ge_set_gej (& ge , & gej );
171
163
secp256k1_fe_normalize (& ge .x );
@@ -180,13 +172,13 @@ int secp256k1_multiset_finalize(const secp256k1_context* ctx, unsigned char *res
180
172
return 1 ;
181
173
}
182
174
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 ;
188
179
189
180
VERIFY_CHECK (ctx != NULL );
181
+ VERIFY_CHECK (multiset != NULL );
190
182
191
183
multiset_from_gej_var (multiset , & inf );
192
184
0 commit comments