LCOV - code coverage report
Current view: top level - ballet/bn254 - fd_bn254_internal.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 75 75 100.0 %
Date: 2025-07-06 04:53:02 Functions: 21 28 75.0 %

          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      769764 : fd_bn254_fp_is_zero( fd_bn254_fp_t const * r ) {
      51      769764 :   return r->limbs[0] == 0UL
      52      769764 :       && r->limbs[1] == 0UL
      53      769764 :       && r->limbs[2] == 0UL
      54      769764 :       && r->limbs[3] == 0UL;
      55      769764 : }
      56             : 
      57             : static inline int
      58       33480 : fd_bn254_fp_is_one( fd_bn254_fp_t const * r ) {
      59       33480 :   return r->limbs[0] == fd_bn254_const_one_mont->limbs[0]
      60       33480 :       && r->limbs[1] == fd_bn254_const_one_mont->limbs[1]
      61       33480 :       && r->limbs[2] == fd_bn254_const_one_mont->limbs[2]
      62       33480 :       && r->limbs[3] == fd_bn254_const_one_mont->limbs[3];
      63       33480 : }
      64             : 
      65             : static inline fd_bn254_fp_t *
      66      466281 : fd_bn254_fp_set_zero( fd_bn254_fp_t * r ) {
      67      466281 :   r->limbs[0] = 0UL;
      68      466281 :   r->limbs[1] = 0UL;
      69      466281 :   r->limbs[2] = 0UL;
      70      466281 :   r->limbs[3] = 0UL;
      71      466281 :   return r;
      72      466281 : }
      73             : 
      74             : static inline fd_bn254_fp_t *
      75      176043 : fd_bn254_fp_set_one( fd_bn254_fp_t * r ) {
      76      176043 :   r->limbs[0] = fd_bn254_const_one_mont->limbs[0];
      77      176043 :   r->limbs[1] = fd_bn254_const_one_mont->limbs[1];
      78      176043 :   r->limbs[2] = fd_bn254_const_one_mont->limbs[2];
      79      176043 :   r->limbs[3] = fd_bn254_const_one_mont->limbs[3];
      80      176043 :   return r;
      81      176043 : }
      82             : 
      83             : /* Extension fields utilities */
      84             : 
      85             : static inline int
      86      103032 : fd_bn254_fp2_is_zero( fd_bn254_fp2_t const * a ) {
      87      103032 :   return fd_bn254_fp_is_zero( &a->el[0] )
      88      103032 :       && fd_bn254_fp_is_zero( &a->el[1] );
      89      103032 : }
      90             : 
      91             : static inline int
      92         345 : fd_bn254_fp2_is_one( fd_bn254_fp2_t const * a ) {
      93         345 :   return fd_bn254_fp_is_one ( &a->el[0] )
      94         345 :       && fd_bn254_fp_is_zero( &a->el[1] );
      95         345 : }
      96             : 
      97             : static inline fd_bn254_fp2_t *
      98      197460 : fd_bn254_fp2_set_zero( fd_bn254_fp2_t * r ) {
      99      197460 :   fd_bn254_fp_set_zero( &r->el[0] );
     100      197460 :   fd_bn254_fp_set_zero( &r->el[1] );
     101      197460 :   return r;
     102      197460 : }
     103             : 
     104             : static inline fd_bn254_fp2_t *
     105       41322 : fd_bn254_fp2_set_one( fd_bn254_fp2_t * r ) {
     106       41322 :   fd_bn254_fp_set_one ( &r->el[0] );
     107       41322 :   fd_bn254_fp_set_zero( &r->el[1] );
     108       41322 :   return r;
     109       41322 : }
     110             : 
     111             : /* Fp6 */
     112             : 
     113             : static inline int
     114         339 : fd_bn254_fp6_is_zero( fd_bn254_fp6_t const * a ) {
     115         339 :   return fd_bn254_fp2_is_zero( &a->el[0] )
     116         339 :       && fd_bn254_fp2_is_zero( &a->el[1] )
     117         339 :       && fd_bn254_fp2_is_zero( &a->el[2] );
     118         339 : }
     119             : 
     120             : static inline int
     121         345 : fd_bn254_fp6_is_one( fd_bn254_fp6_t const * a ) {
     122         345 :   return fd_bn254_fp2_is_one ( &a->el[0] )
     123         345 :       && fd_bn254_fp2_is_zero( &a->el[1] )
     124         345 :       && fd_bn254_fp2_is_zero( &a->el[2] );
     125         345 : }
     126             : 
     127             : static inline fd_bn254_fp6_t *
     128         684 : fd_bn254_fp6_set_zero( fd_bn254_fp6_t * r ) {
     129         684 :   fd_bn254_fp2_set_zero( &r->el[0] );
     130         684 :   fd_bn254_fp2_set_zero( &r->el[1] );
     131         684 :   fd_bn254_fp2_set_zero( &r->el[2] );
     132         684 :   return r;
     133         684 : }
     134             : 
     135             : static inline fd_bn254_fp6_t *
     136         684 : fd_bn254_fp6_set_one( fd_bn254_fp6_t * r ) {
     137         684 :   fd_bn254_fp2_set_one ( &r->el[0] );
     138         684 :   fd_bn254_fp2_set_zero( &r->el[1] );
     139         684 :   fd_bn254_fp2_set_zero( &r->el[2] );
     140         684 :   return r;
     141         684 : }
     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         345 : fd_bn254_fp12_is_one( fd_bn254_fp12_t const * a ) {
     153         345 :   return fd_bn254_fp6_is_one ( &a->el[0] )
     154         345 :       && fd_bn254_fp6_is_zero( &a->el[1] );
     155         345 : }
     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         684 : fd_bn254_fp12_set_one( fd_bn254_fp12_t * r ) {
     166         684 :   fd_bn254_fp6_set_one ( &r->el[0] );
     167         684 :   fd_bn254_fp6_set_zero( &r->el[1] );
     168         684 :   return r;
     169         684 : }
     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 */

Generated by: LCOV version 1.14