Line data Source code
1 : #ifndef HEADER_fd_src_ballet_bn254_fd_bn254_internal_h 2 : #define HEADER_fd_src_ballet_bn254_fd_bn254_internal_h 3 : 4 : #include "./fd_bn254.h" 5 : 6 : /* Base field */ 7 : 8 : typedef fd_uint256_t fd_bn254_fp_t; 9 : 10 : /* Extension fields */ 11 : 12 : struct FD_ALIGNED fd_bn254_fp2 { 13 : fd_bn254_fp_t el[2]; 14 : }; 15 : typedef struct fd_bn254_fp2 fd_bn254_fp2_t; 16 : 17 : struct FD_ALIGNED fd_bn254_fp6 { 18 : fd_bn254_fp2_t el[3]; 19 : }; 20 : typedef struct fd_bn254_fp6 fd_bn254_fp6_t; 21 : 22 : struct FD_ALIGNED fd_bn254_fp12 { 23 : fd_bn254_fp6_t el[2]; 24 : }; 25 : typedef struct fd_bn254_fp12 fd_bn254_fp12_t; 26 : 27 : /* Point on G1, Jacobian coordinates */ 28 : struct FD_ALIGNED fd_bn254_g1 { 29 : fd_bn254_fp_t X; 30 : fd_bn254_fp_t Y; 31 : fd_bn254_fp_t Z; 32 : }; 33 : typedef struct fd_bn254_g1 fd_bn254_g1_t; 34 : 35 : /* Point on G2, Jacobian coordinates */ 36 : struct FD_ALIGNED fd_bn254_g2 { 37 : fd_bn254_fp2_t X; 38 : fd_bn254_fp2_t Y; 39 : fd_bn254_fp2_t Z; 40 : }; 41 : typedef struct fd_bn254_g2 fd_bn254_g2_t; 42 : 43 : /* Field utilities */ 44 : 45 : /* const 1. Montgomery. 46 : 0x0e0a77c19a07df2f666ea36f7879462c0a78eb28f5c70b3dd35d438dc58f0d9d */ 47 : extern const fd_bn254_fp_t fd_bn254_const_one_mont[1]; 48 : 49 : static inline int 50 0 : fd_bn254_fp_is_zero( fd_bn254_fp_t const * r ) { 51 0 : return r->limbs[0] == 0UL 52 0 : && r->limbs[1] == 0UL 53 0 : && r->limbs[2] == 0UL 54 0 : && r->limbs[3] == 0UL; 55 0 : } 56 : 57 : static inline int 58 0 : fd_bn254_fp_is_one( fd_bn254_fp_t const * r ) { 59 0 : return r->limbs[0] == fd_bn254_const_one_mont->limbs[0] 60 0 : && r->limbs[1] == fd_bn254_const_one_mont->limbs[1] 61 0 : && r->limbs[2] == fd_bn254_const_one_mont->limbs[2] 62 0 : && r->limbs[3] == fd_bn254_const_one_mont->limbs[3]; 63 0 : } 64 : 65 : static inline fd_bn254_fp_t * 66 0 : fd_bn254_fp_set_zero( fd_bn254_fp_t * r ) { 67 0 : r->limbs[0] = 0UL; 68 0 : r->limbs[1] = 0UL; 69 0 : r->limbs[2] = 0UL; 70 0 : r->limbs[3] = 0UL; 71 0 : return r; 72 0 : } 73 : 74 : static inline fd_bn254_fp_t * 75 0 : fd_bn254_fp_set_one( fd_bn254_fp_t * r ) { 76 0 : r->limbs[0] = fd_bn254_const_one_mont->limbs[0]; 77 0 : r->limbs[1] = fd_bn254_const_one_mont->limbs[1]; 78 0 : r->limbs[2] = fd_bn254_const_one_mont->limbs[2]; 79 0 : r->limbs[3] = fd_bn254_const_one_mont->limbs[3]; 80 0 : return r; 81 0 : } 82 : 83 : /* Extension fields utilities */ 84 : 85 : static inline int 86 0 : fd_bn254_fp2_is_zero( fd_bn254_fp2_t const * a ) { 87 0 : return fd_bn254_fp_is_zero( &a->el[0] ) 88 0 : && fd_bn254_fp_is_zero( &a->el[1] ); 89 0 : } 90 : 91 : static inline int 92 0 : fd_bn254_fp2_is_one( fd_bn254_fp2_t const * a ) { 93 0 : return fd_bn254_fp_is_one ( &a->el[0] ) 94 0 : && fd_bn254_fp_is_zero( &a->el[1] ); 95 0 : } 96 : 97 : static inline fd_bn254_fp2_t * 98 0 : fd_bn254_fp2_set_zero( fd_bn254_fp2_t * r ) { 99 0 : fd_bn254_fp_set_zero( &r->el[0] ); 100 0 : fd_bn254_fp_set_zero( &r->el[1] ); 101 0 : return r; 102 0 : } 103 : 104 : static inline fd_bn254_fp2_t * 105 0 : fd_bn254_fp2_set_one( fd_bn254_fp2_t * r ) { 106 0 : fd_bn254_fp_set_one ( &r->el[0] ); 107 0 : fd_bn254_fp_set_zero( &r->el[1] ); 108 0 : return r; 109 0 : } 110 : 111 : /* Fp6 */ 112 : 113 : static inline int 114 0 : fd_bn254_fp6_is_zero( fd_bn254_fp6_t const * a ) { 115 0 : return fd_bn254_fp2_is_zero( &a->el[0] ) 116 0 : && fd_bn254_fp2_is_zero( &a->el[1] ) 117 0 : && fd_bn254_fp2_is_zero( &a->el[2] ); 118 0 : } 119 : 120 : static inline int 121 0 : fd_bn254_fp6_is_one( fd_bn254_fp6_t const * a ) { 122 0 : return fd_bn254_fp2_is_one ( &a->el[0] ) 123 0 : && fd_bn254_fp2_is_zero( &a->el[1] ) 124 0 : && fd_bn254_fp2_is_zero( &a->el[2] ); 125 0 : } 126 : 127 : static inline fd_bn254_fp6_t * 128 0 : fd_bn254_fp6_set_zero( fd_bn254_fp6_t * r ) { 129 0 : fd_bn254_fp2_set_zero( &r->el[0] ); 130 0 : fd_bn254_fp2_set_zero( &r->el[1] ); 131 0 : fd_bn254_fp2_set_zero( &r->el[2] ); 132 0 : return r; 133 0 : } 134 : 135 : static inline fd_bn254_fp6_t * 136 0 : fd_bn254_fp6_set_one( fd_bn254_fp6_t * r ) { 137 0 : fd_bn254_fp2_set_one ( &r->el[0] ); 138 0 : fd_bn254_fp2_set_zero( &r->el[1] ); 139 0 : fd_bn254_fp2_set_zero( &r->el[2] ); 140 0 : return r; 141 0 : } 142 : 143 : /* Fp12 */ 144 : 145 : // static inline int 146 : // fd_bn254_fp12_is_zero( fd_bn254_fp12_t const * a ) { 147 : // return fd_bn254_fp6_is_zero( &a->el[0] ) 148 : // && fd_bn254_fp6_is_zero( &a->el[1] ); 149 : // } 150 : 151 : static inline int 152 0 : fd_bn254_fp12_is_one( fd_bn254_fp12_t const * a ) { 153 0 : return fd_bn254_fp6_is_one ( &a->el[0] ) 154 0 : && fd_bn254_fp6_is_zero( &a->el[1] ); 155 0 : } 156 : 157 : // static inline fd_bn254_fp12_t * 158 : // fd_bn254_fp12_set_zero( fd_bn254_fp12_t * r ) { 159 : // fd_bn254_fp6_set_zero( &r->el[0] ); 160 : // fd_bn254_fp6_set_zero( &r->el[1] ); 161 : // return r; 162 : // } 163 : 164 : static inline fd_bn254_fp12_t * 165 0 : fd_bn254_fp12_set_one( fd_bn254_fp12_t * r ) { 166 0 : fd_bn254_fp6_set_one ( &r->el[0] ); 167 0 : fd_bn254_fp6_set_zero( &r->el[1] ); 168 0 : return r; 169 0 : } 170 : 171 : fd_bn254_fp12_t * 172 : fd_bn254_fp12_mul( fd_bn254_fp12_t * r, 173 : fd_bn254_fp12_t const * a, 174 : fd_bn254_fp12_t const * b ); 175 : 176 : fd_bn254_fp12_t * 177 : fd_bn254_fp12_inv( fd_bn254_fp12_t * r, 178 : fd_bn254_fp12_t const * a ); 179 : 180 : fd_bn254_fp12_t * 181 : fd_bn254_final_exp( fd_bn254_fp12_t * r, 182 : fd_bn254_fp12_t * const x ); 183 : 184 : fd_bn254_fp12_t * 185 : fd_bn254_miller_loop( fd_bn254_fp12_t * r, 186 : fd_bn254_g1_t const p[], 187 : fd_bn254_g2_t const q[], 188 : ulong sz ); 189 : 190 : #endif /* HEADER_fd_src_ballet_bn254_fd_bn254_internal_h */