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 250 : int big_endian ) {
9 250 : fd_bn254_g1_t p[1] = { 0 };
10 250 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_internal( p, in, big_endian ) ) ) {
11 162 : return NULL;
12 162 : }
13 88 : int is_inf = fd_bn254_g1_is_zero( p );
14 88 : 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 88 : 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 65 : int is_neg = fd_bn254_fp_is_neg_nm( &p->Y );
30 65 : fd_bn254_fp_tobytes_nm( out, &p->X, big_endian );
31 65 : if( is_neg ) {
32 35 : out[ big_endian ? 0 : 31 ] |= FLAG_NEG;
33 35 : }
34 65 : return out;
35 88 : }
36 :
37 : uchar *
38 : fd_bn254_g1_decompress( uchar out[64],
39 : uchar const in [32],
40 252 : int big_endian ) {
41 : /* Special case: all zeros in => all zeros out, no flags */
42 252 : const uchar zero[32] = { 0 };
43 252 : if( fd_memeq( in, zero, 32 ) ) {
44 11 : return fd_memset( out, 0, 64UL );
45 11 : }
46 :
47 241 : fd_bn254_fp_t x_nm[1], x[1], x2[1], x3_plus_b[1], y[1];
48 241 : int is_inf, is_neg;
49 241 : if( FD_UNLIKELY( !fd_bn254_fp_frombytes_nm( x_nm, in, big_endian, &is_inf, &is_neg ) ) ) {
50 77 : return NULL;
51 77 : }
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 164 : if( is_inf ) {
59 29 : fd_memset( out, 0, 64UL );
60 : /* no flags */
61 29 : return out;
62 29 : }
63 :
64 135 : fd_bn254_fp_to_mont( x, x_nm );
65 135 : fd_bn254_fp_sqr( x2, x );
66 135 : fd_bn254_fp_mul( x3_plus_b, x2, x );
67 135 : fd_bn254_fp_add( x3_plus_b, x3_plus_b, fd_bn254_const_b_mont );
68 135 : if( FD_UNLIKELY( !fd_bn254_fp_sqrt( y, x3_plus_b ) ) ) {
69 5 : return NULL;
70 5 : }
71 :
72 130 : fd_bn254_fp_from_mont( y, y );
73 130 : if( is_neg != fd_bn254_fp_is_neg_nm( y ) ) {
74 63 : fd_bn254_fp_neg_nm( y, y );
75 63 : }
76 :
77 130 : fd_bn254_fp_tobytes_nm( out, x_nm, big_endian );
78 130 : fd_bn254_fp_tobytes_nm( &out[32], y, big_endian );
79 : /* no flags */
80 130 : return out;
81 135 : }
82 :
83 : uchar *
84 : fd_bn254_g2_compress( uchar out[64],
85 : uchar const in[128],
86 312 : int big_endian ) {
87 312 : fd_bn254_g2_t p[1] = { 0 };
88 312 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_internal( p, in, big_endian ) ) ) {
89 251 : return NULL;
90 251 : }
91 61 : int is_inf = fd_bn254_g2_is_zero( p );
92 61 : int flag_inf = in[ big_endian ? 64 : 127 ] & FLAG_INF;
93 :
94 : /* Serialize compressed point */
95 :
96 61 : if( FD_UNLIKELY( is_inf ) ) {
97 15 : 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 15 : out[ big_endian ? 0 : 63 ] |= (uchar)flag_inf;
100 15 : return out;
101 15 : }
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 46 : int is_neg = fd_bn254_fp2_is_neg_nm( &p->Y );
106 46 : fd_bn254_fp2_tobytes_nm( out, &p->X, big_endian );
107 46 : if( is_neg ) {
108 20 : out[ big_endian ? 0 : 63 ] |= FLAG_NEG;
109 20 : }
110 46 : return out;
111 61 : }
112 :
113 : uchar *
114 : fd_bn254_g2_decompress( uchar out[128],
115 : uchar const in [64],
116 313 : int big_endian ) {
117 : /* Special case: all zeros in => all zeros out, no flags */
118 313 : const uchar zero[64] = { 0 };
119 313 : if( fd_memeq( in, zero, 64 ) ) {
120 29 : return fd_memset( out, 0, 128UL );
121 29 : }
122 :
123 284 : fd_bn254_fp2_t x_nm[1], x[1], x2[1], x3_plus_b[1], y[1];
124 284 : int is_inf, is_neg;
125 284 : if( FD_UNLIKELY( !fd_bn254_fp2_frombytes_nm( x_nm, in, big_endian, &is_inf, &is_neg ) ) ) {
126 139 : return NULL;
127 139 : }
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 145 : if( is_inf ) {
134 19 : fd_memset( out, 0, 128UL );
135 : /* no flags */
136 19 : return out;
137 19 : }
138 :
139 126 : fd_bn254_fp2_to_mont( x, x_nm );
140 126 : fd_bn254_fp2_sqr( x2, x );
141 126 : fd_bn254_fp2_mul( x3_plus_b, x2, x );
142 126 : fd_bn254_fp2_add( x3_plus_b, x3_plus_b, fd_bn254_const_twist_b_mont );
143 126 : if( FD_UNLIKELY( !fd_bn254_fp2_sqrt( y, x3_plus_b ) ) ) {
144 14 : return NULL;
145 14 : }
146 :
147 112 : fd_bn254_fp2_from_mont( y, y );
148 112 : if( is_neg != fd_bn254_fp2_is_neg_nm( y ) ) {
149 26 : fd_bn254_fp2_neg_nm( y, y );
150 26 : }
151 :
152 112 : fd_bn254_fp2_tobytes_nm( out, x_nm, big_endian );
153 112 : fd_bn254_fp2_tobytes_nm( &out[64], y, big_endian );
154 : /* no flags */
155 112 : return out;
156 126 : }
157 :
158 : /* Ops */
159 :
160 : int
161 : fd_bn254_g1_add_syscall( uchar out[64],
162 : uchar const in[],
163 : ulong in_sz,
164 75 : int big_endian ) {
165 : /* Expected 128-byte input (2 points). Pad input with 0s (only big endian). */
166 75 : if( FD_UNLIKELY( in_sz > 128UL ) ) {
167 1 : return -1;
168 1 : }
169 74 : if( FD_UNLIKELY( !big_endian && in_sz != 128UL ) ) {
170 0 : return -1;
171 0 : }
172 74 : uchar FD_ALIGNED buf[128] = { 0 };
173 74 : fd_memcpy( buf, in, in_sz );
174 :
175 : /* Validate inputs */
176 74 : fd_bn254_g1_t r[1], a[1], b[1];
177 74 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( a, &buf[ 0], big_endian ) ) ) {
178 24 : return -1;
179 24 : }
180 50 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( b, &buf[64], big_endian ) ) ) {
181 5 : return -1;
182 5 : }
183 :
184 : /* Compute point add and serialize result */
185 45 : fd_bn254_g1_affine_add( r, a, b );
186 45 : fd_bn254_g1_tobytes( out, r, big_endian );
187 45 : return 0;
188 50 : }
189 :
190 : int
191 : fd_bn254_g2_add_syscall( uchar out[128],
192 : uchar const in[],
193 : ulong in_sz,
194 50 : 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 50 : if( FD_UNLIKELY( in_sz != 256UL ) ) {
198 1 : return -1;
199 1 : }
200 49 : uchar FD_ALIGNED buf[256] = { 0 };
201 49 : 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 49 : fd_bn254_g2_t r[1], a[1], b[1];
206 49 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_eq_only( a, &buf[ 0], big_endian ) ) ) {
207 28 : return -1;
208 28 : }
209 21 : 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 16 : fd_bn254_g2_affine_add( r, a, b );
215 16 : fd_bn254_g2_tobytes( out, r, big_endian );
216 16 : return 0;
217 21 : }
218 :
219 : int
220 : fd_bn254_g1_scalar_mul_syscall( uchar out[64],
221 : uchar const in[],
222 : ulong in_sz,
223 1836 : int big_endian ) {
224 : /* Expected 96-byte input (1 point + 1 scalar). Pad input with 0s (only big endian). */
225 1836 : if( FD_UNLIKELY( in_sz > 96UL ) ) {
226 2 : return -1;
227 2 : }
228 1834 : if( FD_UNLIKELY( !big_endian && in_sz != 96UL ) ) {
229 0 : return -1;
230 0 : }
231 1834 : uchar FD_ALIGNED buf[96] = { 0 };
232 1834 : fd_memcpy( buf, in, fd_ulong_min( in_sz, 96UL ) );
233 :
234 : /* Validate inputs */
235 1834 : fd_bn254_g1_t r[1], a[1];
236 1834 : fd_bn254_scalar_t s[1];
237 1834 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( a, &buf[ 0], big_endian ) ) ) {
238 1342 : return -1;
239 1342 : }
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 492 : if( FD_BIG_ENDIAN_LIKELY( big_endian ) ) {
244 484 : fd_uint256_bswap( s, fd_type_pun_const( &buf[64] ) ); /* &buf[64] is always FD_ALIGNED */
245 484 : } else {
246 8 : memcpy( s, &buf[64], 32 );
247 8 : }
248 : // no: if( FD_UNLIKELY( !fd_bn254_scalar_validate( s ) ) ) return -1;
249 :
250 : /* Compute scalar mul and serialize result */
251 492 : fd_bn254_g1_scalar_mul( r, a, s );
252 492 : fd_bn254_g1_tobytes( out, r, big_endian );
253 492 : return 0;
254 1834 : }
255 :
256 : int
257 : fd_bn254_g2_scalar_mul_syscall( uchar out[128],
258 : uchar const in[],
259 : ulong in_sz,
260 34 : 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 34 : if( FD_UNLIKELY( in_sz != 160UL ) ) {
264 0 : return -1;
265 0 : }
266 34 : uchar FD_ALIGNED buf[160] = { 0 };
267 34 : 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 34 : fd_bn254_g2_t r[1], a[1];
272 34 : fd_bn254_scalar_t s[1];
273 34 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_subgroup( a, &buf[ 0], big_endian ) ) ) {
274 19 : return -1;
275 19 : }
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 15 : if( FD_BIG_ENDIAN_LIKELY( big_endian ) ) {
280 8 : fd_uint256_bswap( s, fd_type_pun_const( &buf[128] ) ); /* &buf[128] is always FD_ALIGNED */
281 8 : } else {
282 7 : memcpy( s, &buf[128], 32 );
283 7 : }
284 : // no: if( FD_UNLIKELY( !fd_bn254_scalar_validate( s ) ) ) return -1;
285 :
286 : /* Compute scalar mul and serialize result */
287 15 : fd_bn254_g2_scalar_mul( r, a, s );
288 15 : fd_bn254_g2_tobytes( out, r, big_endian );
289 15 : return 0;
290 34 : }
291 :
292 : int
293 : fd_bn254_pairing_is_one_syscall( uchar out[32],
294 : uchar const in[],
295 : ulong in_sz,
296 530 : int big_endian ) {
297 : /* https://github.com/anza-xyz/solana-sdk/blob/bn254%40v3.2.1/bn254/src/pairing.rs#L79 */
298 530 : if( FD_UNLIKELY( (in_sz % 192UL) != 0 ) ) {
299 5 : return -1; /* Invalid input length */
300 5 : }
301 525 : ulong elements_len = in_sz / 192UL;
302 525 : fd_bn254_g1_t p[FD_BN254_PAIRING_BATCH_MAX];
303 525 : 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 525 : fd_bn254_fp12_t r[1];
307 525 : fd_bn254_fp12_set_one( r );
308 :
309 525 : ulong sz=0;
310 847 : for( ulong i=0; i<elements_len; i++ ) {
311 : /* G1: deserialize and check subgroup membership */
312 730 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( &p[sz], &in[i*192 ], big_endian ) ) ) {
313 168 : return -1;
314 168 : }
315 : /* G2: deserialize and check subgroup membership */
316 562 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_subgroup( &q[sz], &in[i*192+64], big_endian ) ) ) {
317 240 : return -1;
318 240 : }
319 : /* Skip any pair where either P or Q is the point at infinity */
320 322 : if( FD_UNLIKELY( fd_bn254_g1_is_zero(&p[sz]) || fd_bn254_g2_is_zero(&q[sz]) ) ) {
321 43 : continue;
322 43 : }
323 279 : ++sz;
324 : /* Compute the Miller loop and aggregate into r */
325 279 : if( sz==FD_BN254_PAIRING_BATCH_MAX ) {
326 5 : fd_bn254_fp12_t tmp[1];
327 5 : fd_bn254_miller_loop( tmp, p, q, sz );
328 5 : fd_bn254_fp12_mul( r, r, tmp );
329 5 : sz = 0;
330 5 : }
331 279 : }
332 117 : if( sz>0 ) {
333 78 : fd_bn254_fp12_t tmp[1];
334 78 : fd_bn254_miller_loop( tmp, p, q, sz );
335 78 : fd_bn254_fp12_mul( r, r, tmp );
336 78 : sz = 0;
337 78 : }
338 :
339 : /* Compute the final exponentiation */
340 117 : fd_bn254_final_exp( r, r );
341 :
342 : /* Output is 0 or 1, serialized as big endian uint256. */
343 117 : fd_memset( out, 0, 32 );
344 117 : if( FD_LIKELY( fd_bn254_fp12_is_one( r ) ) ) {
345 78 : out[ big_endian ? 31 : 0 ] = 1;
346 78 : }
347 117 : return 0;
348 525 : }
|