Line data Source code
1 : #include "./fd_bn254_field_inl.h"
2 :
3 : /* Compress/Decompress */
4 :
5 : uchar *
6 : fd_bn254_g1_compress( uchar out[32],
7 : uchar const in [64],
8 264 : int big_endian ) {
9 264 : fd_bn254_g1_t p[1] = { 0 };
10 264 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_internal( p, in, big_endian ) ) ) {
11 164 : return NULL;
12 164 : }
13 100 : int is_inf = fd_bn254_g1_is_zero( p );
14 100 : int flag_inf = in[ big_endian ? 32 : 63 ] & FLAG_INF;
15 :
16 : /* Serialize compressed point:
17 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L122
18 :
19 : 1. If the infinity flags is set, return point at infinity
20 : 2. Else, copy x and set neg_y flag */
21 :
22 100 : if( FD_UNLIKELY( is_inf ) ) {
23 23 : fd_memset( out, 0, 32 );
24 : /* The infinity flag in the result is set iff the infinity flag is set in the Y coordinate */
25 23 : out[ big_endian ? 0 : 31 ] |= (uchar)flag_inf;
26 23 : return out;
27 23 : }
28 :
29 77 : int is_neg = fd_bn254_fp_is_neg_nm( &p->Y );
30 77 : fd_bn254_fp_tobytes_nm( out, &p->X, big_endian );
31 77 : if( is_neg ) {
32 41 : out[ big_endian ? 0 : 31 ] |= FLAG_NEG;
33 41 : }
34 77 : return out;
35 100 : }
36 :
37 : uchar *
38 : fd_bn254_g1_decompress( uchar out[64],
39 : uchar const in [32],
40 265 : int big_endian ) {
41 : /* Special case: all zeros in => all zeros out, no flags */
42 265 : const uchar zero[32] = { 0 };
43 265 : if( fd_memeq( in, zero, 32 ) ) {
44 11 : return fd_memset( out, 0, 64UL );
45 11 : }
46 :
47 254 : fd_bn254_fp_t x_nm[1], x[1], x2[1], x3_plus_b[1], y[1];
48 254 : int is_inf, is_neg;
49 254 : if( FD_UNLIKELY( !fd_bn254_fp_frombytes_nm( x_nm, in, big_endian, &is_inf, &is_neg ) ) ) {
50 78 : return NULL;
51 78 : }
52 :
53 : /* Point at infinity.
54 : If the point at infinity flag is set (bit 6), return the point at
55 : infinity with no check on coords.
56 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L156-L160
57 : */
58 176 : if( is_inf ) {
59 29 : fd_memset( out, 0, 64UL );
60 : /* no flags */
61 29 : return out;
62 29 : }
63 :
64 147 : fd_bn254_fp_to_mont( x, x_nm );
65 147 : fd_bn254_fp_sqr( x2, x );
66 147 : fd_bn254_fp_mul( x3_plus_b, x2, x );
67 147 : fd_bn254_fp_add( x3_plus_b, x3_plus_b, fd_bn254_const_b_mont );
68 147 : if( FD_UNLIKELY( !fd_bn254_fp_sqrt( y, x3_plus_b ) ) ) {
69 5 : return NULL;
70 5 : }
71 :
72 142 : fd_bn254_fp_from_mont( y, y );
73 142 : if( is_neg != fd_bn254_fp_is_neg_nm( y ) ) {
74 68 : fd_bn254_fp_neg_nm( y, y );
75 68 : }
76 :
77 142 : fd_bn254_fp_tobytes_nm( out, x_nm, big_endian );
78 142 : fd_bn254_fp_tobytes_nm( &out[32], y, big_endian );
79 : /* no flags */
80 142 : return out;
81 147 : }
82 :
83 : uchar *
84 : fd_bn254_g2_compress( uchar out[64],
85 : uchar const in[128],
86 322 : int big_endian ) {
87 322 : fd_bn254_g2_t p[1] = { 0 };
88 322 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_internal( p, in, big_endian ) ) ) {
89 251 : return NULL;
90 251 : }
91 71 : int is_inf = fd_bn254_g2_is_zero( p );
92 71 : int flag_inf = in[ big_endian ? 64 : 127 ] & FLAG_INF;
93 :
94 : /* Serialize compressed point */
95 :
96 71 : if( FD_UNLIKELY( is_inf ) ) {
97 16 : fd_memset( out, 0, 64 );
98 : /* The infinity flag in the result is set iff the infinity flag is set in the Y coordinate */
99 16 : out[ big_endian ? 0 : 63 ] |= (uchar)flag_inf;
100 16 : return out;
101 16 : }
102 :
103 : /* Serialize x coordinate. The flags are on the 2nd element.
104 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ff/src/fields/models/quadratic_extension.rs#L700-L702 */
105 55 : int is_neg = fd_bn254_fp2_is_neg_nm( &p->Y );
106 55 : fd_bn254_fp2_tobytes_nm( out, &p->X, big_endian );
107 55 : if( is_neg ) {
108 25 : out[ big_endian ? 0 : 63 ] |= FLAG_NEG;
109 25 : }
110 55 : return out;
111 71 : }
112 :
113 : uchar *
114 : fd_bn254_g2_decompress( uchar out[128],
115 : uchar const in [64],
116 329 : int big_endian ) {
117 : /* Special case: all zeros in => all zeros out, no flags */
118 329 : const uchar zero[64] = { 0 };
119 329 : if( fd_memeq( in, zero, 64 ) ) {
120 33 : return fd_memset( out, 0, 128UL );
121 33 : }
122 :
123 296 : fd_bn254_fp2_t x_nm[1], x[1], x2[1], x3_plus_b[1], y[1];
124 296 : int is_inf, is_neg;
125 296 : if( FD_UNLIKELY( !fd_bn254_fp2_frombytes_nm( x_nm, in, big_endian, &is_inf, &is_neg ) ) ) {
126 145 : return NULL;
127 145 : }
128 :
129 : /* Point at infinity.
130 : If the point at infinity flag is set (bit 6), return the point at
131 : infinity with no check on coords.
132 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L156-L160 */
133 151 : if( is_inf ) {
134 20 : fd_memset( out, 0, 128UL );
135 : /* no flags */
136 20 : return out;
137 20 : }
138 :
139 131 : fd_bn254_fp2_to_mont( x, x_nm );
140 131 : fd_bn254_fp2_sqr( x2, x );
141 131 : fd_bn254_fp2_mul( x3_plus_b, x2, x );
142 131 : fd_bn254_fp2_add( x3_plus_b, x3_plus_b, fd_bn254_const_twist_b_mont );
143 131 : if( FD_UNLIKELY( !fd_bn254_fp2_sqrt( y, x3_plus_b ) ) ) {
144 14 : return NULL;
145 14 : }
146 :
147 117 : fd_bn254_fp2_from_mont( y, y );
148 117 : if( is_neg != fd_bn254_fp2_is_neg_nm( y ) ) {
149 29 : fd_bn254_fp2_neg_nm( y, y );
150 29 : }
151 :
152 117 : fd_bn254_fp2_tobytes_nm( out, x_nm, big_endian );
153 117 : fd_bn254_fp2_tobytes_nm( &out[64], y, big_endian );
154 : /* no flags */
155 117 : return out;
156 131 : }
157 :
158 : /* Ops */
159 :
160 : int
161 : fd_bn254_g1_add_syscall( uchar out[64],
162 : uchar const in[],
163 : ulong in_sz,
164 92 : int big_endian ) {
165 : /* Expected 128-byte input (2 points). Pad input with 0s (only big endian). */
166 92 : if( FD_UNLIKELY( in_sz > 128UL ) ) {
167 2 : return -1;
168 2 : }
169 90 : if( FD_UNLIKELY( !big_endian && in_sz != 128UL ) ) {
170 0 : return -1;
171 0 : }
172 90 : uchar FD_ALIGNED buf[128] = { 0 };
173 90 : fd_memcpy( buf, in, in_sz );
174 :
175 : /* Validate inputs */
176 90 : fd_bn254_g1_t r[1], a[1], b[1];
177 90 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( a, &buf[ 0], big_endian ) ) ) {
178 25 : return -1;
179 25 : }
180 65 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( b, &buf[64], big_endian ) ) ) {
181 8 : return -1;
182 8 : }
183 :
184 : /* Compute point add and serialize result */
185 57 : fd_bn254_g1_affine_add( r, a, b );
186 57 : fd_bn254_g1_tobytes( out, r, big_endian );
187 57 : return 0;
188 65 : }
189 :
190 : int
191 : fd_bn254_g2_add_syscall( uchar out[128],
192 : uchar const in[],
193 : ulong in_sz,
194 61 : int big_endian ) {
195 : /* Expected 256-byte input (2 points).
196 : https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/addition.rs#L234-L236 */
197 61 : if( FD_UNLIKELY( in_sz != 256UL ) ) {
198 1 : return -1;
199 1 : }
200 60 : uchar FD_ALIGNED buf[256] = { 0 };
201 60 : fd_memcpy( buf, in, in_sz );
202 :
203 : /* Validate inputs (curve eq only, no subgroup)
204 : https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/addition.rs#L238-L250 */
205 60 : fd_bn254_g2_t r[1], a[1], b[1];
206 60 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_eq_only( a, &buf[ 0], big_endian ) ) ) {
207 28 : return -1;
208 28 : }
209 32 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_eq_only( b, &buf[128], big_endian ) ) ) {
210 5 : return -1;
211 5 : }
212 :
213 : /* Compute point add and serialize result */
214 27 : fd_bn254_g2_affine_add( r, a, b );
215 27 : fd_bn254_g2_tobytes( out, r, big_endian );
216 27 : return 0;
217 32 : }
218 :
219 : int
220 : fd_bn254_g1_scalar_mul_syscall( uchar out[64],
221 : uchar const in[],
222 : ulong in_sz,
223 1855 : int big_endian ) {
224 : /* Expected 96-byte input (1 point + 1 scalar). Pad input with 0s (only big endian). */
225 1855 : if( FD_UNLIKELY( in_sz > 96UL ) ) {
226 2 : return -1;
227 2 : }
228 1853 : if( FD_UNLIKELY( !big_endian && in_sz != 96UL ) ) {
229 0 : return -1;
230 0 : }
231 1853 : uchar FD_ALIGNED buf[96] = { 0 };
232 1853 : fd_memcpy( buf, in, fd_ulong_min( in_sz, 96UL ) );
233 :
234 : /* Validate inputs */
235 1853 : fd_bn254_g1_t r[1], a[1];
236 1853 : fd_bn254_scalar_t s[1];
237 1853 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( a, &buf[ 0], big_endian ) ) ) {
238 1345 : return -1;
239 1345 : }
240 :
241 : /* Scalar is big endian and NOT validated
242 : https://github.com/anza-xyz/agave/blob/v1.18.6/sdk/program/src/alt_bn128/mod.rs#L211-L214 */
243 508 : if( FD_BIG_ENDIAN_LIKELY( big_endian ) ) {
244 492 : fd_uint256_bswap( s, fd_type_pun_const( &buf[64] ) ); /* &buf[64] is always FD_ALIGNED */
245 492 : } else {
246 16 : memcpy( s, &buf[64], 32 );
247 16 : }
248 : // no: if( FD_UNLIKELY( !fd_bn254_scalar_validate( s ) ) ) return -1;
249 :
250 : /* Compute scalar mul and serialize result */
251 508 : fd_bn254_g1_scalar_mul( r, a, s );
252 508 : fd_bn254_g1_tobytes( out, r, big_endian );
253 508 : return 0;
254 1853 : }
255 :
256 : int
257 : fd_bn254_g2_scalar_mul_syscall( uchar out[128],
258 : uchar const in[],
259 : ulong in_sz,
260 52 : int big_endian ) {
261 : /* Expected 160-byte input (1 point + 1 scalar).
262 : https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/multiplication.rs#L248-L250 */
263 52 : if( FD_UNLIKELY( in_sz != 160UL ) ) {
264 0 : return -1;
265 0 : }
266 52 : uchar FD_ALIGNED buf[160] = { 0 };
267 52 : fd_memcpy( buf, in, 160UL );
268 :
269 : /* Validate point (curve equation and subgroup membership)
270 : https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/multiplication.rs#L252-L255 */
271 52 : fd_bn254_g2_t r[1], a[1];
272 52 : fd_bn254_scalar_t s[1];
273 52 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_subgroup( a, &buf[ 0], big_endian ) ) ) {
274 22 : return -1;
275 22 : }
276 :
277 : /* Scalar is little endian and NOT validated
278 : https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/multiplication.rs#L256-L272 */
279 30 : if( FD_BIG_ENDIAN_LIKELY( big_endian ) ) {
280 16 : fd_uint256_bswap( s, fd_type_pun_const( &buf[128] ) ); /* &buf[128] is always FD_ALIGNED */
281 16 : } else {
282 14 : memcpy( s, &buf[128], 32 );
283 14 : }
284 : // no: if( FD_UNLIKELY( !fd_bn254_scalar_validate( s ) ) ) return -1;
285 :
286 : /* Compute scalar mul and serialize result */
287 30 : fd_bn254_g2_scalar_mul( r, a, s );
288 30 : fd_bn254_g2_tobytes( out, r, big_endian );
289 30 : return 0;
290 52 : }
291 :
292 : int
293 : fd_bn254_pairing_is_one_syscall( uchar out[32],
294 : uchar const in[],
295 : ulong in_sz,
296 549 : int big_endian ) {
297 : /* https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/pairing.rs#L79 */
298 549 : if( FD_UNLIKELY( (in_sz % 192UL) != 0 ) ) {
299 5 : return -1; /* Invalid input length */
300 5 : }
301 544 : ulong elements_len = in_sz / 192UL;
302 544 : fd_bn254_g1_t p[FD_BN254_PAIRING_BATCH_MAX];
303 544 : fd_bn254_g2_t q[FD_BN254_PAIRING_BATCH_MAX];
304 :
305 : /* Important: set r=1 so that the result of 0 pairings is 1. */
306 544 : fd_bn254_fp12_t r[1];
307 544 : fd_bn254_fp12_set_one( r );
308 :
309 544 : ulong sz=0;
310 989 : for( ulong i=0; i<elements_len; i++ ) {
311 : /* G1: deserialize and check subgroup membership */
312 856 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( &p[sz], &in[i*192 ], big_endian ) ) ) {
313 170 : return -1;
314 170 : }
315 : /* G2: deserialize and check subgroup membership */
316 686 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_subgroup( &q[sz], &in[i*192+64], big_endian ) ) ) {
317 241 : return -1;
318 241 : }
319 : /* Skip any pair where either P or Q is the point at infinity */
320 445 : if( FD_UNLIKELY( fd_bn254_g1_is_zero(&p[sz]) || fd_bn254_g2_is_zero(&q[sz]) ) ) {
321 49 : continue;
322 49 : }
323 396 : ++sz;
324 : /* Compute the Miller loop and aggregate into r */
325 396 : if( sz==FD_BN254_PAIRING_BATCH_MAX ) {
326 8 : fd_bn254_fp12_t tmp[1];
327 8 : fd_bn254_miller_loop( tmp, p, q, sz );
328 8 : fd_bn254_fp12_mul( r, r, tmp );
329 8 : sz = 0;
330 8 : }
331 396 : }
332 133 : if( sz>0 ) {
333 86 : fd_bn254_fp12_t tmp[1];
334 86 : fd_bn254_miller_loop( tmp, p, q, sz );
335 86 : fd_bn254_fp12_mul( r, r, tmp );
336 86 : sz = 0;
337 86 : }
338 :
339 : /* Compute the final exponentiation */
340 133 : fd_bn254_final_exp( r, r );
341 :
342 : /* Output is 0 or 1, serialized as big endian uint256. */
343 133 : fd_memset( out, 0, 32 );
344 133 : if( FD_LIKELY( fd_bn254_fp12_is_one( r ) ) ) {
345 88 : out[ big_endian ? 31 : 0 ] = 1;
346 88 : }
347 133 : return 0;
348 544 : }
|