LCOV - code coverage report
Current view: top level - ballet/bn254 - fd_bn254_internal.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 81 81 100.0 %
Date: 2026-08-18 04:36:42 Functions: 52 160 32.5 %

          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     7095636 : fd_bn254_fp_is_zero( fd_bn254_fp_t const * r ) {
      51     7095636 :   return r->limbs[0] == 0UL
      52     7095636 :       && r->limbs[1] == 0UL
      53     7095636 :       && r->limbs[2] == 0UL
      54     7095636 :       && r->limbs[3] == 0UL;
      55     7095636 : }
      56             : 
      57             : static inline int
      58       90894 : fd_bn254_fp_is_one( fd_bn254_fp_t const * r ) {
      59       90894 :   return r->limbs[0] == fd_bn254_const_one_mont->limbs[0]
      60       90894 :       && r->limbs[1] == fd_bn254_const_one_mont->limbs[1]
      61       90894 :       && r->limbs[2] == fd_bn254_const_one_mont->limbs[2]
      62       90894 :       && r->limbs[3] == fd_bn254_const_one_mont->limbs[3];
      63       90894 : }
      64             : 
      65             : static inline fd_bn254_fp_t *
      66      657186 : fd_bn254_fp_set_zero( fd_bn254_fp_t * r ) {
      67      657186 :   r->limbs[0] = 0UL;
      68      657186 :   r->limbs[1] = 0UL;
      69      657186 :   r->limbs[2] = 0UL;
      70      657186 :   r->limbs[3] = 0UL;
      71      657186 :   return r;
      72      657186 : }
      73             : 
      74             : static inline fd_bn254_fp_t *
      75      555582 : fd_bn254_fp_set_one( fd_bn254_fp_t * r ) {
      76      555582 :   r->limbs[0] = fd_bn254_const_one_mont->limbs[0];
      77      555582 :   r->limbs[1] = fd_bn254_const_one_mont->limbs[1];
      78      555582 :   r->limbs[2] = fd_bn254_const_one_mont->limbs[2];
      79      555582 :   r->limbs[3] = fd_bn254_const_one_mont->limbs[3];
      80      555582 :   return r;
      81      555582 : }
      82             : 
      83             : /* Extension fields utilities */
      84             : 
      85             : static inline int
      86      460551 : fd_bn254_fp2_is_zero( fd_bn254_fp2_t const * a ) {
      87      460551 :   return fd_bn254_fp_is_zero( &a->el[0] )
      88      460551 :       && fd_bn254_fp_is_zero( &a->el[1] );
      89      460551 : }
      90             : 
      91             : static inline int
      92       30729 : fd_bn254_fp2_is_one( fd_bn254_fp2_t const * a ) {
      93       30729 :   return fd_bn254_fp_is_one ( &a->el[0] )
      94       30729 :       && fd_bn254_fp_is_zero( &a->el[1] );
      95       30729 : }
      96             : 
      97             : static inline fd_bn254_fp2_t *
      98      235236 : fd_bn254_fp2_set_zero( fd_bn254_fp2_t * r ) {
      99      235236 :   fd_bn254_fp_set_zero( &r->el[0] );
     100      235236 :   fd_bn254_fp_set_zero( &r->el[1] );
     101      235236 :   return r;
     102      235236 : }
     103             : 
     104             : static inline fd_bn254_fp2_t *
     105      186660 : fd_bn254_fp2_set_one( fd_bn254_fp2_t * r ) {
     106      186660 :   fd_bn254_fp_set_one ( &r->el[0] );
     107      186660 :   fd_bn254_fp_set_zero( &r->el[1] );
     108      186660 :   return r;
     109      186660 : }
     110             : 
     111             : /* Fp6 */
     112             : 
     113             : static inline int
     114         381 : fd_bn254_fp6_is_zero( fd_bn254_fp6_t const * a ) {
     115         381 :   return fd_bn254_fp2_is_zero( &a->el[0] )
     116         381 :       && fd_bn254_fp2_is_zero( &a->el[1] )
     117         381 :       && fd_bn254_fp2_is_zero( &a->el[2] );
     118         381 : }
     119             : 
     120             : static inline int
     121         393 : fd_bn254_fp6_is_one( fd_bn254_fp6_t const * a ) {
     122         393 :   return fd_bn254_fp2_is_one ( &a->el[0] )
     123         393 :       && fd_bn254_fp2_is_zero( &a->el[1] )
     124         393 :       && fd_bn254_fp2_is_zero( &a->el[2] );
     125         393 : }
     126             : 
     127             : static inline fd_bn254_fp6_t *
     128         777 : fd_bn254_fp6_set_zero( fd_bn254_fp6_t * r ) {
     129         777 :   fd_bn254_fp2_set_zero( &r->el[0] );
     130         777 :   fd_bn254_fp2_set_zero( &r->el[1] );
     131         777 :   fd_bn254_fp2_set_zero( &r->el[2] );
     132         777 :   return r;
     133         777 : }
     134             : 
     135             : static inline fd_bn254_fp6_t *
     136         777 : fd_bn254_fp6_set_one( fd_bn254_fp6_t * r ) {
     137         777 :   fd_bn254_fp2_set_one ( &r->el[0] );
     138         777 :   fd_bn254_fp2_set_zero( &r->el[1] );
     139         777 :   fd_bn254_fp2_set_zero( &r->el[2] );
     140         777 :   return r;
     141         777 : }
     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         393 : fd_bn254_fp12_is_one( fd_bn254_fp12_t const * a ) {
     153         393 :   return fd_bn254_fp6_is_one ( &a->el[0] )
     154         393 :       && fd_bn254_fp6_is_zero( &a->el[1] );
     155         393 : }
     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         777 : fd_bn254_fp12_set_one( fd_bn254_fp12_t * r ) {
     166         777 :   fd_bn254_fp6_set_one ( &r->el[0] );
     167         777 :   fd_bn254_fp6_set_zero( &r->el[1] );
     168         777 :   return r;
     169         777 : }
     170             : 
     171             : /* Group utilities */
     172             : 
     173             : static inline int
     174     6500019 : fd_bn254_g1_is_zero( fd_bn254_g1_t const * p ) {
     175     6500019 :   return fd_bn254_fp_is_zero( &p->Z );
     176     6500019 : }
     177             : 
     178             : static inline int
     179      428310 : fd_bn254_g2_is_zero( fd_bn254_g2_t const * p ) {
     180      428310 :   return fd_bn254_fp2_is_zero( &p->Z );
     181      428310 : }
     182             : 
     183             : /* Extern fns, defined in fd_bn254_field.c */
     184             : 
     185             : fd_bn254_fp_t *
     186             : fd_bn254_fp_pow( fd_bn254_fp_t * restrict r,
     187             :                  fd_bn254_fp_t const *    a,
     188             :                  fd_uint256_t  const *    b );
     189             : 
     190             : /* Extern fns, defined in fd_bn254_field_ext.c */
     191             : 
     192             : fd_bn254_fp2_t *
     193             : fd_bn254_fp2_pow( fd_bn254_fp2_t * restrict r,
     194             :                   fd_bn254_fp2_t const *    a,
     195             :                   fd_uint256_t   const *    b );
     196             : 
     197             : fd_bn254_fp12_t *
     198             : fd_bn254_fp12_mul( fd_bn254_fp12_t * r,
     199             :                    fd_bn254_fp12_t const * a,
     200             :                    fd_bn254_fp12_t const * b );
     201             : 
     202             : fd_bn254_fp12_t *
     203             : fd_bn254_fp12_inv( fd_bn254_fp12_t * r,
     204             :                    fd_bn254_fp12_t const * a );
     205             : 
     206             : fd_bn254_fp12_t *
     207             : fd_bn254_fp12_mul_sparse( fd_bn254_fp12_t *       r,
     208             :                           fd_bn254_fp12_t const * a,
     209             :                           fd_bn254_fp12_t const * b );
     210             : 
     211             : fd_bn254_fp12_t *
     212             : fd_bn254_fp12_sqr( fd_bn254_fp12_t * r,
     213             :                    fd_bn254_fp12_t const * a );
     214             : 
     215             : /* Extern fns, defined in fd_bn254_g1.c */
     216             : 
     217             : uchar *
     218             : fd_bn254_g1_tobytes( uchar                 out[64],
     219             :                      fd_bn254_g1_t const * p,
     220             :                      int                   big_endian );
     221             : 
     222             : fd_bn254_g1_t *
     223             : fd_bn254_g1_affine_add( fd_bn254_g1_t *       r,
     224             :                         fd_bn254_g1_t const * p,
     225             :                         fd_bn254_g1_t const * q );
     226             : 
     227             : fd_bn254_g1_t *
     228             : fd_bn254_g1_dbl( fd_bn254_g1_t *       r,
     229             :                  fd_bn254_g1_t const * p );
     230             : 
     231             : fd_bn254_g1_t *
     232             : fd_bn254_g1_add_mixed( fd_bn254_g1_t *       r,
     233             :                        fd_bn254_g1_t const * p,
     234             :                        fd_bn254_g1_t const * q );
     235             : 
     236             : fd_bn254_g1_t *
     237             : fd_bn254_g1_scalar_mul( fd_bn254_g1_t *           r,
     238             :                         fd_bn254_g1_t const *     p,
     239             :                         fd_bn254_scalar_t const * s );
     240             : 
     241             : fd_bn254_g1_t *
     242             : fd_bn254_g1_frombytes_internal( fd_bn254_g1_t * p,
     243             :                                 uchar const     in[64],
     244             :                                 int             big_endian );
     245             : 
     246             : fd_bn254_g1_t *
     247             : fd_bn254_g1_frombytes_check_subgroup( fd_bn254_g1_t * p,
     248             :                                       uchar const     in[64],
     249             :                                       int             big_endian );
     250             : 
     251             : /* Extern fns, defined in fd_bn254_g2.c */
     252             : 
     253             : uchar *
     254             : fd_bn254_g2_tobytes( uchar                 out[128],
     255             :                      fd_bn254_g2_t const * p,
     256             :                      int                   big_endian );
     257             : 
     258             : fd_bn254_g2_t *
     259             : fd_bn254_g2_dbl( fd_bn254_g2_t *       r,
     260             :                  fd_bn254_g2_t const * p );
     261             : 
     262             : fd_bn254_g2_t *
     263             : fd_bn254_g2_add_mixed( fd_bn254_g2_t *       r,
     264             :                        fd_bn254_g2_t const * p,
     265             :                        fd_bn254_g2_t const * q );
     266             : 
     267             : fd_bn254_g2_t *
     268             : fd_bn254_g2_add( fd_bn254_g2_t *       r,
     269             :                  fd_bn254_g2_t const * p,
     270             :                  fd_bn254_g2_t const * q );
     271             : 
     272             : fd_bn254_g2_t *
     273             : fd_bn254_g2_affine_add( fd_bn254_g2_t *       r,
     274             :                         fd_bn254_g2_t const * p,
     275             :                         fd_bn254_g2_t const * q );
     276             : 
     277             : fd_bn254_g2_t *
     278             : fd_bn254_g2_scalar_mul( fd_bn254_g2_t *           r,
     279             :                         fd_bn254_g2_t const *     p,
     280             :                         fd_bn254_scalar_t const * s );
     281             : 
     282             : fd_bn254_g2_t *
     283             : fd_bn254_g2_frombytes_internal( fd_bn254_g2_t * p,
     284             :                                 uchar const     in[128],
     285             :                                 int             big_endian );
     286             : 
     287             : fd_bn254_g2_t *
     288             : fd_bn254_g2_frombytes_check_eq_only( fd_bn254_g2_t * p,
     289             :                                      uchar const     in[128],
     290             :                                      int             big_endian );
     291             : 
     292             : fd_bn254_g2_t *
     293             : fd_bn254_g2_frombytes_check_subgroup( fd_bn254_g2_t * p,
     294             :                                       uchar const     in[128],
     295             :                                       int             big_endian );
     296             : 
     297             : /* Extern fns, defined in fd_bn254_pairing.c */
     298             : 
     299             : fd_bn254_fp12_t *
     300             : fd_bn254_fp12_pow_x( fd_bn254_fp12_t * restrict r,
     301             :                      fd_bn254_fp12_t const *    a );
     302             : 
     303             : fd_bn254_fp12_t *
     304             : fd_bn254_final_exp( fd_bn254_fp12_t *       r,
     305             :                     fd_bn254_fp12_t * const x );
     306             : 
     307             : fd_bn254_fp12_t *
     308             : fd_bn254_miller_loop( fd_bn254_fp12_t *   r,
     309             :                       fd_bn254_g1_t const p[],
     310             :                       fd_bn254_g2_t const q[],
     311             :                       ulong               sz );
     312             : 
     313             : #endif /* HEADER_fd_src_ballet_bn254_fd_bn254_internal_h */

Generated by: LCOV version 1.14