Line data Source code
1 : #include "./fd_bn254.h"
2 :
3 : /* G1 */
4 :
5 : static inline int
6 598422 : fd_bn254_g1_is_zero( fd_bn254_g1_t const * p ) {
7 598422 : return fd_bn254_fp_is_zero( &p->Z );
8 598422 : }
9 :
10 : static inline fd_bn254_g1_t *
11 : fd_bn254_g1_set( fd_bn254_g1_t * r,
12 63174 : fd_bn254_g1_t const * p ) {
13 63174 : fd_bn254_fp_set( &r->X, &p->X );
14 63174 : fd_bn254_fp_set( &r->Y, &p->Y );
15 63174 : fd_bn254_fp_set( &r->Z, &p->Z );
16 63174 : return r;
17 63174 : }
18 :
19 : static inline fd_bn254_g1_t *
20 30039 : fd_bn254_g1_set_zero( fd_bn254_g1_t * r ) {
21 : // fd_bn254_fp_set_zero( &r->X );
22 : // fd_bn254_fp_set_zero( &r->Y );
23 30039 : fd_bn254_fp_set_zero( &r->Z );
24 30039 : return r;
25 30039 : }
26 :
27 : static inline fd_bn254_g1_t *
28 : fd_bn254_g1_to_affine( fd_bn254_g1_t * r,
29 33135 : fd_bn254_g1_t const * p ) {
30 33135 : if( FD_UNLIKELY( fd_bn254_fp_is_zero( &p->Z ) || fd_bn254_fp_is_one( &p->Z ) ) ) {
31 30039 : return fd_bn254_g1_set( r, p );
32 30039 : }
33 :
34 3096 : fd_bn254_fp_t iz[1], iz2[1];
35 3096 : fd_bn254_fp_inv( iz, &p->Z );
36 3096 : fd_bn254_fp_sqr( iz2, iz );
37 :
38 : /* X / Z^2, Y / Z^3 */
39 3096 : fd_bn254_fp_mul( &r->X, &p->X, iz2 );
40 3096 : fd_bn254_fp_mul( &r->Y, &p->Y, iz2 );
41 3096 : fd_bn254_fp_mul( &r->Y, &r->Y, iz );
42 3096 : fd_bn254_fp_set_one( &r->Z );
43 3096 : return r;
44 33135 : }
45 :
46 : uchar *
47 : fd_bn254_g1_tobytes( uchar out[64],
48 33153 : fd_bn254_g1_t const * p ) {
49 33153 : if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
50 18 : fd_memset( out, 0, 64UL );
51 : /* no flags */
52 18 : return out;
53 18 : }
54 :
55 33135 : fd_bn254_g1_t r[1];
56 33135 : fd_bn254_g1_to_affine( r, p );
57 :
58 33135 : fd_bn254_fp_from_mont( &r->X, &r->X );
59 33135 : fd_bn254_fp_from_mont( &r->Y, &r->Y );
60 :
61 33135 : fd_bn254_fp_tobytes_be_nm( &out[ 0], &r->X );
62 33135 : fd_bn254_fp_tobytes_be_nm( &out[32], &r->Y );
63 : /* no flags */
64 33135 : return out;
65 33153 : }
66 :
67 : /* fd_bn254_g1_affine_add computes r = p + q.
68 : Both p, q are affine, i.e. Z==1. */
69 : fd_bn254_g1_t *
70 : fd_bn254_g1_affine_add( fd_bn254_g1_t * r,
71 : fd_bn254_g1_t const * p,
72 30033 : fd_bn254_g1_t const * q ) {
73 : /* p==0, return q */
74 30033 : if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
75 15 : return fd_bn254_g1_set( r, q );
76 15 : }
77 : /* q==0, return p */
78 30018 : if( FD_UNLIKELY( fd_bn254_g1_is_zero( q ) ) ) {
79 30006 : return fd_bn254_g1_set( r, p );
80 30006 : }
81 :
82 12 : fd_bn254_fp_t lambda[1], x[1], y[1];
83 :
84 : /* same X, either the points are equal or opposite */
85 12 : if( fd_bn254_fp_eq( &p->X, &q->X ) ) {
86 3 : if( fd_bn254_fp_eq( &p->Y, &q->Y ) ) {
87 : /* p==q => point double: lambda = 3 * x1^2 / (2 * y1) */
88 3 : fd_bn254_fp_sqr( x, &p->X ); /* x = x1^2 */
89 3 : fd_bn254_fp_add( y, x, x ); /* y = 2 x1^2 */
90 3 : fd_bn254_fp_add( x, x, y ); /* x = 3 x1^2 */
91 3 : fd_bn254_fp_add( y, &p->Y, &p->Y );
92 3 : fd_bn254_fp_inv( lambda, y );
93 3 : fd_bn254_fp_mul( lambda, lambda, x );
94 3 : } else {
95 : /* p==-q => r=0 */
96 : /* COV: this may never happen with real data */
97 0 : return fd_bn254_g1_set_zero( r );
98 0 : }
99 9 : } else {
100 : /* point add: lambda = (y1 - y2) / (x1 - x2) */
101 9 : fd_bn254_fp_sub( x, &p->X, &q->X );
102 9 : fd_bn254_fp_sub( y, &p->Y, &q->Y );
103 9 : fd_bn254_fp_inv( lambda, x );
104 9 : fd_bn254_fp_mul( lambda, lambda, y );
105 9 : }
106 :
107 : /* x3 = lambda^2 - x1 - x2 */
108 12 : fd_bn254_fp_sqr( x, lambda );
109 12 : fd_bn254_fp_sub( x, x, &p->X );
110 12 : fd_bn254_fp_sub( x, x, &q->X );
111 :
112 : /* y3 = lambda * (x1 - x3) - y1 */
113 12 : fd_bn254_fp_sub( y, &p->X, x );
114 12 : fd_bn254_fp_mul( y, y, lambda );
115 12 : fd_bn254_fp_sub( y, y, &p->Y );
116 :
117 12 : fd_bn254_fp_set( &r->X, x );
118 12 : fd_bn254_fp_set( &r->Y, y );
119 12 : fd_bn254_fp_set_one( &r->Z );
120 12 : return r;
121 12 : }
122 :
123 : /* fd_bn254_g1_dbl computes r = 2p.
124 : https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l */
125 : fd_bn254_g1_t *
126 : fd_bn254_g1_dbl( fd_bn254_g1_t * r,
127 399660 : fd_bn254_g1_t const * p ) {
128 : /* p==0, return 0 */
129 399660 : if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
130 0 : return fd_bn254_g1_set_zero( r );
131 0 : }
132 :
133 399660 : fd_bn254_fp_t a[1], b[1], c[1];
134 399660 : fd_bn254_fp_t d[1], e[1], f[1];
135 :
136 : /* A = X1^2 */
137 399660 : fd_bn254_fp_sqr( a, &p->X );
138 : /* B = Y1^2 */
139 399660 : fd_bn254_fp_sqr( b, &p->Y );
140 : /* C = B^2 */
141 399660 : fd_bn254_fp_sqr( c, b );
142 : /* D = 2*((X1+B)^2-A-C)
143 : (X1+B)^2 = X1^2 + 2*X1*B + B^2
144 : D = 2*(X1^2 + 2*X1*B + B^2 - A - C)
145 : D = 2*(X1^2 + 2*X1*B + B^2 - X1^2 - B^2)
146 : ^ ^ ^ ^
147 : |---------------|-----| |
148 : |------------|
149 : These terms cancel each other out, and we're left with:
150 : D = 2*(2*X1*B) */
151 399660 : fd_bn254_fp_mul( d, &p->X, b );
152 399660 : fd_bn254_fp_add( d, d, d );
153 399660 : fd_bn254_fp_add( d, d, d );
154 : /* E = 3*A */
155 399660 : fd_bn254_fp_add( e, a, a );
156 399660 : fd_bn254_fp_add( e, a, e );
157 : /* F = E^2 */
158 399660 : fd_bn254_fp_sqr( f, e );
159 : /* X3 = F-2*D */
160 399660 : fd_bn254_fp_add( &r->X, d, d );
161 399660 : fd_bn254_fp_sub( &r->X, f, &r->X );
162 : /* Z3 = (Y1+Z1)^2-YY-ZZ
163 : note: compute Z3 before Y3 because it depends on p->Y,
164 : that might be overwritten if r==p. */
165 : /* Z3 = 2*Y1*Z1 */
166 399660 : fd_bn254_fp_mul( &r->Z, &p->Y, &p->Z );
167 399660 : fd_bn254_fp_add( &r->Z, &r->Z, &r->Z );
168 : /* Y3 = E*(D-X3)-8*C */
169 399660 : fd_bn254_fp_sub( &r->Y, d, &r->X );
170 399660 : fd_bn254_fp_mul( &r->Y, e, &r->Y );
171 399660 : fd_bn254_fp_add( c, c, c ); /* 2*c */
172 399660 : fd_bn254_fp_add( c, c, c ); /* 4*y */
173 399660 : fd_bn254_fp_add( c, c, c ); /* 8*y */
174 399660 : fd_bn254_fp_sub( &r->Y, &r->Y, c );
175 399660 : return r;
176 399660 : }
177 :
178 : /* fd_bn254_g1_add_mixed computes r = p + q, when q->Z==1.
179 : http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl */
180 : fd_bn254_g1_t *
181 : fd_bn254_g1_add_mixed( fd_bn254_g1_t * r,
182 : fd_bn254_g1_t const * p,
183 10860 : fd_bn254_g1_t const * q ) {
184 : /* p==0, return q */
185 10860 : if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
186 0 : return fd_bn254_g1_set( r, q );
187 0 : }
188 10860 : fd_bn254_fp_t zz[1], u2[1], s2[1];
189 10860 : fd_bn254_fp_t h[1], hh[1];
190 10860 : fd_bn254_fp_t i[1], j[1];
191 10860 : fd_bn254_fp_t rr[1], v[1];
192 : /* Z1Z1 = Z1^2 */
193 10860 : fd_bn254_fp_sqr( zz, &p->Z );
194 : /* U2 = X2*Z1Z1 */
195 10860 : fd_bn254_fp_mul( u2, &q->X, zz );
196 : /* S2 = Y2*Z1*Z1Z1 */
197 10860 : fd_bn254_fp_mul( s2, &q->Y, &p->Z );
198 10860 : fd_bn254_fp_mul( s2, s2, zz );
199 :
200 : /* if p==q, call fd_bn254_g1_dbl */
201 10860 : if( FD_UNLIKELY( fd_bn254_fp_eq( u2, &p->X ) && fd_bn254_fp_eq( s2, &p->Y ) ) ) {
202 : /* COV: this may never happen with real data */
203 0 : return fd_bn254_g1_dbl( r, p );
204 0 : }
205 :
206 : /* H = U2-X1 */
207 10860 : fd_bn254_fp_sub( h, u2, &p->X );
208 : /* HH = H^2 */
209 10860 : fd_bn254_fp_sqr( hh, h );
210 : /* I = 4*HH */
211 10860 : fd_bn254_fp_add( i, hh, hh );
212 10860 : fd_bn254_fp_add( i, i, i );
213 : /* J = H*I */
214 10860 : fd_bn254_fp_mul( j, h, i );
215 : /* r = 2*(S2-Y1) */
216 10860 : fd_bn254_fp_sub( rr, s2, &p->Y );
217 10860 : fd_bn254_fp_add( rr, rr, rr );
218 : /* V = X1*I */
219 10860 : fd_bn254_fp_mul( v, &p->X, i );
220 : /* X3 = r^2-J-2*V */
221 10860 : fd_bn254_fp_sqr( &r->X, rr );
222 10860 : fd_bn254_fp_sub( &r->X, &r->X, j );
223 10860 : fd_bn254_fp_sub( &r->X, &r->X, v );
224 10860 : fd_bn254_fp_sub( &r->X, &r->X, v );
225 : /* Y3 = r*(V-X3)-2*Y1*J
226 : note: i no longer used */
227 10860 : fd_bn254_fp_mul( i, &p->Y, j ); /* i = Y1*J */
228 10860 : fd_bn254_fp_add( i, i, i ); /* i = 2*Y1*J */
229 10860 : fd_bn254_fp_sub( &r->Y, v, &r->X );
230 10860 : fd_bn254_fp_mul( &r->Y, &r->Y, rr );
231 10860 : fd_bn254_fp_sub( &r->Y, &r->Y, i );
232 : /* Z3 = (Z1+H)^2-Z1Z1-HH */
233 10860 : fd_bn254_fp_add( &r->Z, &p->Z, h );
234 10860 : fd_bn254_fp_sqr( &r->Z, &r->Z );
235 10860 : fd_bn254_fp_sub( &r->Z, &r->Z, zz );
236 10860 : fd_bn254_fp_sub( &r->Z, &r->Z, hh );
237 10860 : return r;
238 10860 : }
239 :
240 : /* fd_bn254_g1_scalar_mul computes r = s * p.
241 : This assumes that p is affine, i.e. p->Z==1. */
242 : fd_bn254_g1_t *
243 : fd_bn254_g1_scalar_mul( fd_bn254_g1_t * r,
244 : fd_bn254_g1_t const * p,
245 3120 : fd_bn254_scalar_t const * s ) {
246 : /* TODO: wNAF, GLV */
247 3120 : int i = 255;
248 399066 : for( ; i>=0 && !fd_uint256_bit( s, i ); i-- ) ; /* do nothing, just i-- */
249 3120 : if( FD_UNLIKELY( i<0 ) ) {
250 6 : return fd_bn254_g1_set_zero( r );
251 6 : }
252 3114 : fd_bn254_g1_set( r, p );
253 402774 : for( i--; i>=0; i-- ) {
254 399660 : fd_bn254_g1_dbl( r, r );
255 399660 : if( fd_uint256_bit( s, i ) ) {
256 10860 : fd_bn254_g1_add_mixed( r, r, p );
257 10860 : }
258 399660 : }
259 3114 : return r;
260 3120 : }
261 :
262 : /* fd_bn254_g1_frombytes_internal extracts (x, y) and performs basic checks.
263 : This is used by fd_bn254_g1_compress() and fd_bn254_g1_frombytes_check_subgroup().
264 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L173-L178 */
265 : static inline fd_bn254_g1_t *
266 : fd_bn254_g1_frombytes_internal( fd_bn254_g1_t * p,
267 93963 : uchar const in[64] ) {
268 : /* Special case: all zeros => point at infinity */
269 93963 : const uchar zero[64] = { 0 };
270 93963 : if( FD_UNLIKELY( fd_memeq( in, zero, 64 ) ) ) {
271 30033 : return fd_bn254_g1_set_zero( p );
272 30033 : }
273 :
274 : /* Check x < p */
275 63930 : if( FD_UNLIKELY( !fd_bn254_fp_frombytes_be_nm( &p->X, &in[0], NULL, NULL ) ) ) {
276 0 : return NULL;
277 0 : }
278 :
279 : /* Check flags and y < p */
280 63930 : int is_inf, is_neg;
281 63930 : if( FD_UNLIKELY( !fd_bn254_fp_frombytes_be_nm( &p->Y, &in[32], &is_inf, &is_neg ) ) ) {
282 0 : return NULL;
283 0 : }
284 :
285 63930 : if( FD_UNLIKELY( is_inf ) ) {
286 0 : return fd_bn254_g1_set_zero( p );
287 0 : }
288 :
289 63930 : fd_bn254_fp_set_one( &p->Z );
290 63930 : return p;
291 63930 : }
292 :
293 : /* fd_bn254_g1_frombytes_check_subgroup performs frombytes AND checks subgroup membership. */
294 : static inline fd_bn254_g1_t *
295 : fd_bn254_g1_frombytes_check_subgroup( fd_bn254_g1_t * p,
296 63921 : uchar const in[64] ) {
297 63921 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_internal( p, in ) ) ) {
298 0 : return NULL;
299 0 : }
300 63921 : if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
301 30033 : return p;
302 30033 : }
303 :
304 33888 : fd_bn254_fp_to_mont( &p->X, &p->X );
305 33888 : fd_bn254_fp_to_mont( &p->Y, &p->Y );
306 33888 : fd_bn254_fp_set_one( &p->Z );
307 :
308 : /* Check that y^2 = x^3 + b */
309 33888 : fd_bn254_fp_t y2[1], x3b[1];
310 33888 : fd_bn254_fp_sqr( y2, &p->Y );
311 33888 : fd_bn254_fp_sqr( x3b, &p->X );
312 33888 : fd_bn254_fp_mul( x3b, x3b, &p->X );
313 33888 : fd_bn254_fp_add( x3b, x3b, fd_bn254_const_b_mont );
314 33888 : if( FD_UNLIKELY( !fd_bn254_fp_eq( y2, x3b ) ) ) {
315 0 : return NULL;
316 0 : }
317 :
318 : /* G1 has prime order, so we don't need to do any further checks. */
319 :
320 33888 : return p;
321 33888 : }
|