Line data Source code
1 : #include "./fd_bn254_internal.h"
2 :
3 : #include "./fd_bn254_field.c"
4 : #include "./fd_bn254_field_ext.c"
5 : #include "./fd_bn254_g1.c"
6 : #include "./fd_bn254_g2.c"
7 : #include "./fd_bn254_pairing.c"
8 :
9 : /* Compress/Decompress */
10 :
11 : uchar *
12 : fd_bn254_g1_compress( uchar out[32],
13 594 : uchar const in [64] ) {
14 594 : fd_bn254_g1_t p[1] = { 0 };
15 594 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_internal( p, in ) ) ) {
16 408 : return NULL;
17 408 : }
18 186 : int is_inf = fd_bn254_g1_is_zero( p );
19 186 : int flag_inf = in[32] & FLAG_INF;
20 :
21 : /* Serialize compressed point:
22 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L122
23 :
24 : 1. If the infinity flags is set, return point at infinity
25 : 2. Else, copy x and set neg_y flag */
26 :
27 186 : if( FD_UNLIKELY( is_inf ) ) {
28 63 : fd_memset( out, 0, 32 );
29 : /* The infinity flag in the result is set iff the infinity flag is set in the Y coordinate */
30 63 : out[0] = (uchar)( out[0] | flag_inf );
31 63 : return out;
32 63 : }
33 :
34 123 : int is_neg = fd_bn254_fp_is_neg_nm( &p->Y );
35 123 : memmove( out, in, 32 );
36 123 : if( is_neg ) {
37 51 : out[0] = (uchar)( out[0] | FLAG_NEG );
38 51 : }
39 123 : return out;
40 186 : }
41 :
42 : uchar *
43 : fd_bn254_g1_decompress( uchar out[64],
44 594 : uchar const in [32] ) {
45 : /* Special case: all zeros in => all zeros out, no flags */
46 594 : const uchar zero[32] = { 0 };
47 594 : if( fd_memeq( in, zero, 32 ) ) {
48 24 : return fd_memset( out, 0, 64UL );
49 24 : }
50 :
51 570 : fd_bn254_fp_t x[1], x2[1], x3_plus_b[1], y[1];
52 570 : int is_inf, is_neg;
53 570 : if( FD_UNLIKELY( !fd_bn254_fp_frombytes_be_nm( x, in, &is_inf, &is_neg ) ) ) {
54 174 : return NULL;
55 174 : }
56 :
57 : /* Point at infinity.
58 : If the point at infinity flag is set (bit 6), return the point at
59 : infinity with no check on coords.
60 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L156-L160
61 : */
62 396 : if( is_inf ) {
63 69 : fd_memset( out, 0, 64UL );
64 : /* no flags */
65 69 : return out;
66 69 : }
67 :
68 327 : fd_bn254_fp_to_mont( x, x );
69 327 : fd_bn254_fp_sqr( x2, x );
70 327 : fd_bn254_fp_mul( x3_plus_b, x2, x );
71 327 : fd_bn254_fp_add( x3_plus_b, x3_plus_b, fd_bn254_const_b_mont );
72 327 : if( FD_UNLIKELY( !fd_bn254_fp_sqrt( y, x3_plus_b ) ) ) {
73 6 : return NULL;
74 6 : }
75 :
76 321 : fd_bn254_fp_from_mont( y, y );
77 321 : if( is_neg != fd_bn254_fp_is_neg_nm( y ) ) {
78 147 : fd_bn254_fp_neg_nm( y, y );
79 147 : }
80 :
81 321 : memmove( out, in, 32 ); out[0] &= FLAG_MASK;
82 321 : fd_bn254_fp_tobytes_be_nm( &out[32], y );
83 : /* no flags */
84 321 : return out;
85 327 : }
86 :
87 : uchar *
88 : fd_bn254_g2_compress( uchar out[64],
89 786 : uchar const in[128] ) {
90 786 : fd_bn254_g2_t p[1] = { 0 };
91 786 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_internal( p, in ) ) ) {
92 672 : return NULL;
93 672 : }
94 114 : int is_inf = fd_bn254_g2_is_zero( p );
95 114 : int flag_inf = in[64] & FLAG_INF;
96 :
97 : /* Serialize compressed point */
98 :
99 114 : if( FD_UNLIKELY( is_inf ) ) {
100 39 : fd_memset( out, 0, 64 );
101 : /* The infinity flag in the result is set iff the infinity flag is set in the Y coordinate */
102 39 : out[0] = (uchar)( out[0] | flag_inf );
103 39 : return out;
104 39 : }
105 :
106 : /* Serialize x coordinate. The flags are on the 2nd element.
107 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ff/src/fields/models/quadratic_extension.rs#L700-L702 */
108 75 : int is_neg = fd_bn254_fp2_is_neg_nm( &p->Y );
109 75 : memmove( out, in, 64 );
110 75 : if( is_neg ) {
111 27 : out[0] = (uchar)( out[0] | FLAG_NEG );
112 27 : }
113 75 : return out;
114 114 : }
115 :
116 : uchar *
117 : fd_bn254_g2_decompress( uchar out[128],
118 786 : uchar const in [64] ) {
119 : /* Special case: all zeros in => all zeros out, no flags */
120 786 : const uchar zero[64] = { 0 };
121 786 : if( fd_memeq( in, zero, 64 ) ) {
122 84 : return fd_memset( out, 0, 128UL );
123 84 : }
124 :
125 702 : fd_bn254_fp2_t x[1], x2[1], x3_plus_b[1], y[1];
126 702 : int is_inf, is_neg;
127 702 : if( FD_UNLIKELY( !fd_bn254_fp2_frombytes_be_nm( x, in, &is_inf, &is_neg ) ) ) {
128 348 : return NULL;
129 348 : }
130 :
131 : /* Point at infinity.
132 : If the point at infinity flag is set (bit 6), return the point at
133 : infinity with no check on coords.
134 : https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L156-L160 */
135 354 : if( is_inf ) {
136 48 : fd_memset( out, 0, 128UL );
137 : /* no flags */
138 48 : return out;
139 48 : }
140 :
141 306 : fd_bn254_fp2_to_mont( x, x );
142 306 : fd_bn254_fp2_sqr( x2, x );
143 306 : fd_bn254_fp2_mul( x3_plus_b, x2, x );
144 306 : fd_bn254_fp2_add( x3_plus_b, x3_plus_b, fd_bn254_const_twist_b_mont );
145 306 : if( FD_UNLIKELY( !fd_bn254_fp2_sqrt( y, x3_plus_b ) ) ) {
146 36 : return NULL;
147 36 : }
148 :
149 270 : fd_bn254_fp2_from_mont( y, y );
150 270 : if( is_neg != fd_bn254_fp2_is_neg_nm( y ) ) {
151 36 : fd_bn254_fp2_neg_nm( y, y );
152 36 : }
153 :
154 270 : memmove( out, in, 64 ); out[0] &= FLAG_MASK;
155 270 : fd_bn254_fp2_tobytes_be_nm( &out[64], y );
156 : /* no flags */
157 270 : return out;
158 306 : }
159 :
160 : /* Ops */
161 :
162 : int
163 : fd_bn254_g1_add_syscall( uchar out[64],
164 : uchar const in[],
165 45 : ulong in_sz ) {
166 : /* Expected 128-byte input (2 points). Pad input with 0s. */
167 45 : if( FD_UNLIKELY( in_sz > 128UL ) ) {
168 3 : return -1;
169 3 : }
170 42 : uchar FD_ALIGNED buf[128] = { 0 };
171 42 : fd_memcpy( buf, in, in_sz );
172 :
173 : /* Validate inputs */
174 42 : fd_bn254_g1_t r[1], a[1], b[1];
175 42 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( a, &buf[ 0] ) ) ) {
176 3 : return -1;
177 3 : }
178 39 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( b, &buf[64] ) ) ) {
179 3 : return -1;
180 3 : }
181 :
182 : /* Compute point add and serialize result */
183 36 : fd_bn254_g1_affine_add( r, a, b );
184 36 : fd_bn254_g1_tobytes( out, r );
185 36 : return 0;
186 39 : }
187 :
188 : int
189 : fd_bn254_g1_scalar_mul_syscall( uchar out[64],
190 : uchar const in[],
191 5367 : ulong in_sz ) {
192 : /* Expected 96-byte input (1 point + 1 scalar). Pad input with 0s.
193 : Note: Agave checks for 128 bytes instead of 96. We have to do the same check.
194 : https://github.com/anza-xyz/agave/blob/master/sdk/program/src/alt_bn128/mod.rs#L17 */
195 5367 : if( FD_UNLIKELY( in_sz > 128UL ) ) {
196 3 : return -1;
197 3 : }
198 5364 : uchar FD_ALIGNED buf[96] = { 0 };
199 5364 : fd_memcpy( buf, in, fd_ulong_min( in_sz, 96UL ) );
200 :
201 : /* Validate inputs */
202 5364 : fd_bn254_g1_t r[1], a[1];
203 5364 : fd_bn254_scalar_t s[1];
204 5364 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( a, &buf[ 0] ) ) ) {
205 3972 : return -1;
206 3972 : }
207 :
208 : /* Scalar is big endian and NOT validated
209 : https://github.com/anza-xyz/agave/blob/v1.18.6/sdk/program/src/alt_bn128/mod.rs#L211-L214 */
210 1392 : fd_uint256_bswap( s, fd_type_pun_const( &buf[64] ) ); /* &buf[64] is always FD_ALIGNED */
211 : // no: if( FD_UNLIKELY( !fd_bn254_scalar_validate( s ) ) ) return -1;
212 :
213 : /* Compute scalar mul and serialize result */
214 1392 : fd_bn254_g1_scalar_mul( r, a, s );
215 1392 : fd_bn254_g1_tobytes( out, r );
216 1392 : return 0;
217 5364 : }
218 :
219 : int
220 : fd_bn254_pairing_is_one_syscall( uchar out[32],
221 : uchar const in[],
222 1428 : ulong in_sz ) {
223 : /* https://github.com/anza-xyz/agave/blob/master/sdk/program/src/alt_bn128/mod.rs#L242
224 : Note: this check doesn't do anything because
225 : 192usize.checked_rem(192) = Some(0)
226 : i.e., this is NOT checking that in_sz % 192 == 0.
227 : i.e., this is NOT needed:
228 : if( FD_UNLIKELY( (in_sz%192UL)!=0 ) ) return -1; */
229 1428 : ulong elements_len = in_sz / 192UL;
230 1428 : fd_bn254_g1_t p[FD_BN254_PAIRING_BATCH_MAX];
231 1428 : fd_bn254_g2_t q[FD_BN254_PAIRING_BATCH_MAX];
232 :
233 : /* Important: set r=1 so that the result of 0 pairings is 1. */
234 1428 : fd_bn254_fp12_t r[1];
235 1428 : fd_bn254_fp12_set_one( r );
236 :
237 1428 : ulong sz=0;
238 1785 : for( ulong i=0; i<elements_len; i++ ) {
239 : /* G1: deserialize and check subgroup membership */
240 1521 : if( FD_UNLIKELY( !fd_bn254_g1_frombytes_check_subgroup( &p[sz], &in[i*192 ] ) ) ) {
241 444 : return -1;
242 444 : }
243 : /* G2: deserialize and check subgroup membership */
244 1077 : if( FD_UNLIKELY( !fd_bn254_g2_frombytes_check_subgroup( &q[sz], &in[i*192+64] ) ) ) {
245 720 : return -1;
246 720 : }
247 : /* Skip any pair where either P or Q is the point at infinity */
248 357 : if( FD_UNLIKELY( fd_bn254_g1_is_zero(&p[sz]) || fd_bn254_g2_is_zero(&q[sz]) ) ) {
249 102 : continue;
250 102 : }
251 255 : ++sz;
252 : /* Compute the Miller loop and aggegate into r */
253 255 : if( sz==FD_BN254_PAIRING_BATCH_MAX || i==elements_len-1 ) {
254 156 : fd_bn254_fp12_t tmp[1];
255 156 : fd_bn254_miller_loop( tmp, p, q, sz );
256 156 : fd_bn254_fp12_mul( r, r, tmp );
257 156 : sz = 0;
258 156 : }
259 255 : }
260 264 : if( sz>0 ) {
261 0 : fd_bn254_fp12_t tmp[1];
262 0 : fd_bn254_miller_loop( tmp, p, q, sz );
263 0 : fd_bn254_fp12_mul( r, r, tmp );
264 0 : sz = 0;
265 0 : }
266 :
267 : /* Compute the final exponentiation */
268 264 : fd_bn254_final_exp( r, r );
269 :
270 : /* Output is 0 or 1, serialized as big endian uint256. */
271 264 : fd_memset( out, 0, 32 );
272 264 : if( FD_LIKELY( fd_bn254_fp12_is_one( r ) ) ) {
273 147 : out[31] = 1;
274 147 : }
275 264 : return 0;
276 1428 : }
|