Line data Source code
1 : #include "./fd_bn254_field_inl.h"
2 :
3 : /* const 0. */
4 : const fd_bn254_fp_t fd_bn254_const_zero[1] = {{{
5 : 0x0UL, 0x0UL, 0x0UL, 0x0UL,
6 : }}};
7 :
8 : /* const p, used to validate a field element. NOT Montgomery.
9 : 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 */
10 : const fd_bn254_fp_t fd_bn254_const_p[1] = {{{
11 : 0x3c208c16d87cfd47, 0x97816a916871ca8d, 0xb85045b68181585d, 0x30644e72e131a029,
12 : }}};
13 :
14 : /* const 1. Montgomery.
15 : 0x0e0a77c19a07df2f666ea36f7879462c0a78eb28f5c70b3dd35d438dc58f0d9d */
16 : const fd_bn254_fp_t fd_bn254_const_one_mont[1] = {{{
17 : 0xd35d438dc58f0d9d, 0x0a78eb28f5c70b3d, 0x666ea36f7879462c, 0x0e0a77c19a07df2f
18 : }}};
19 :
20 : /* const x, used by fd_bn254_g2_frombytes_check(). scalar (NOT Montgomery)
21 : 0x44e992b44a6909f1 (64-bit) */
22 : const fd_bn254_scalar_t fd_bn254_const_x[1] = {{{
23 : 0x44e992b44a6909f1, 0x0, 0x0, 0x0,
24 : }}};
25 :
26 : /* const b=3, in curve equation y^2 = x^3 + b. Montgomery.
27 : 0x2a1f6744ce179d8e334bea4e696bd2841f6ac17ae15521b97a17caa950ad28d7 */
28 : const fd_bn254_fp_t fd_bn254_const_b_mont[1] = {{{
29 : 0x7a17caa950ad28d7, 0x1f6ac17ae15521b9, 0x334bea4e696bd284, 0x2a1f6744ce179d8e
30 : // 0x3UL, 0x0UL, 0x0UL, 0x0UL,
31 : }}};
32 :
33 : /* const p-1, to check if sqrt exists. Montgomery.
34 : 0x2259d6b14729c0fa51e1a247090812318d087f6872aabf4f68c3488912edefaa */
35 : const fd_bn254_fp_t fd_bn254_const_p_minus_one_mont[1] = {{{
36 : 0x68c3488912edefaa, 0x8d087f6872aabf4f, 0x51e1a24709081231, 0x2259d6b14729c0fa,
37 : }}};
38 :
39 : /* const (p-1)/2, used to check if an element is positive or negative,
40 : and to calculate sqrt() in Fp2. NOT Montgomery.
41 : 0x183227397098d014dc2822db40c0ac2ecbc0b548b438e5469e10460b6c3e7ea3 */
42 : const fd_bn254_fp_t fd_bn254_const_p_minus_one_half[1] = {{{
43 : 0x9e10460b6c3e7ea3, 0xcbc0b548b438e546, 0xdc2822db40c0ac2e, 0x183227397098d014,
44 : }}};
45 :
46 : /* const (p-3)/4, used to calculate sqrt() in Fp and Fp2. bigint (NOT Montgomery)
47 : 0x0c19139cb84c680a6e14116da060561765e05aa45a1c72a34f082305b61f3f51 */
48 : const fd_uint256_t fd_bn254_const_sqrt_exp[1] = {{{
49 : 0x4f082305b61f3f51, 0x65e05aa45a1c72a3, 0x6e14116da0605617, 0x0c19139cb84c680a,
50 : }}};
51 :
52 : fd_bn254_fp_t *
53 : fd_bn254_fp_pow( fd_bn254_fp_t * restrict r,
54 : fd_bn254_fp_t const * a,
55 36309 : fd_uint256_t const * b ) {
56 36309 : fd_bn254_fp_set_one( r );
57 36309 : if( fd_uint256_is_zero( b ) ) return r; /* x^0 = 1 */
58 :
59 : /* There must be a bit set, as b>0, so if we reach i==0, it must be set. */
60 36309 : int i = 255;
61 181545 : while( !fd_uint256_bit( b, i ) ) i--;
62 :
63 9186177 : for( ; i>=0; i--) {
64 9149868 : fd_bn254_fp_sqr( r, r );
65 9149868 : if( fd_uint256_bit( b, i ) ) {
66 3957681 : fd_bn254_fp_mul( r, r, a );
67 3957681 : }
68 9149868 : }
69 36309 : return r;
70 36309 : }
|