LCOV - code coverage report
Current view: top level - ballet/bn254 - fd_bn254_g1.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 198 0.0 %
Date: 2025-02-18 12:28:12 Functions: 0 11 0.0 %

          Line data    Source code
       1             : #include "./fd_bn254.h"
       2             : 
       3             : /* G1 */
       4             : 
       5             : static inline int
       6           0 : fd_bn254_g1_is_zero( fd_bn254_g1_t const * p ) {
       7           0 :   return fd_bn254_fp_is_zero( &p->Z );
       8           0 : }
       9             : 
      10             : static inline fd_bn254_g1_t *
      11             : fd_bn254_g1_set( fd_bn254_g1_t *       r,
      12           0 :                  fd_bn254_g1_t const * p ) {
      13           0 :   fd_bn254_fp_set( &r->X, &p->X );
      14           0 :   fd_bn254_fp_set( &r->Y, &p->Y );
      15           0 :   fd_bn254_fp_set( &r->Z, &p->Z );
      16           0 :   return r;
      17           0 : }
      18             : 
      19             : static inline fd_bn254_g1_t *
      20           0 : fd_bn254_g1_set_zero( fd_bn254_g1_t * r ) {
      21             :   // fd_bn254_fp_set_zero( &r->X );
      22             :   // fd_bn254_fp_set_zero( &r->Y );
      23           0 :   fd_bn254_fp_set_zero( &r->Z );
      24           0 :   return r;
      25           0 : }
      26             : 
      27             : static inline fd_bn254_g1_t *
      28             : fd_bn254_g1_to_affine( fd_bn254_g1_t *       r,
      29           0 :                        fd_bn254_g1_t const * p ) {
      30           0 :   if( FD_UNLIKELY( fd_bn254_fp_is_zero( &p->Z ) || fd_bn254_fp_is_one( &p->Z ) ) ) {
      31           0 :     return fd_bn254_g1_set( r, p );
      32           0 :   }
      33             : 
      34           0 :   fd_bn254_fp_t iz[1], iz2[1];
      35           0 :   fd_bn254_fp_inv( iz, &p->Z );
      36           0 :   fd_bn254_fp_sqr( iz2, iz );
      37             : 
      38             :   /* X / Z^2, Y / Z^3 */
      39           0 :   fd_bn254_fp_mul( &r->X, &p->X, iz2 );
      40           0 :   fd_bn254_fp_mul( &r->Y, &p->Y, iz2 );
      41           0 :   fd_bn254_fp_mul( &r->Y, &r->Y, iz );
      42           0 :   fd_bn254_fp_set_one( &r->Z );
      43           0 :   return r;
      44           0 : }
      45             : 
      46             : uchar *
      47             : fd_bn254_g1_tobytes( uchar                 out[64],
      48           0 :                      fd_bn254_g1_t const * p ) {
      49           0 :   if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
      50           0 :     fd_memset( out, 0, 64UL );
      51             :     /* no flags */
      52           0 :     return out;
      53           0 :   }
      54             : 
      55           0 :   fd_bn254_g1_t r[1];
      56           0 :   fd_bn254_g1_to_affine( r, p );
      57             : 
      58           0 :   fd_bn254_fp_from_mont( &r->X, &r->X );
      59           0 :   fd_bn254_fp_from_mont( &r->Y, &r->Y );
      60             : 
      61           0 :   fd_bn254_fp_tobytes_be_nm( &out[ 0], &r->X );
      62           0 :   fd_bn254_fp_tobytes_be_nm( &out[32], &r->Y );
      63             :   /* no flags */
      64           0 :   return out;
      65           0 : }
      66             : 
      67             : /* fd_bn254_g1_affine_add computes r = p + q.
      68             :    Both p, q are affine, i.e. Z==1. */
      69             : fd_bn254_g1_t *
      70             : fd_bn254_g1_affine_add( fd_bn254_g1_t *       r,
      71             :                         fd_bn254_g1_t const * p,
      72           0 :                         fd_bn254_g1_t const * q ) {
      73             :   /* p==0, return q */
      74           0 :   if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
      75           0 :     return fd_bn254_g1_set( r, q );
      76           0 :   }
      77             :   /* q==0, return p */
      78           0 :   if( FD_UNLIKELY( fd_bn254_g1_is_zero( q ) ) ) {
      79           0 :     return fd_bn254_g1_set( r, p );
      80           0 :   }
      81             : 
      82           0 :   fd_bn254_fp_t lambda[1], x[1], y[1];
      83             : 
      84             :   /* same X, either the points are equal or opposite */
      85           0 :   if( fd_bn254_fp_eq( &p->X, &q->X ) ) {
      86           0 :     if( fd_bn254_fp_eq( &p->Y, &q->Y ) ) {
      87             :       /* p==q => point double: lambda = 3 * x1^2 / (2 * y1) */
      88           0 :       fd_bn254_fp_sqr( x, &p->X ); /* x =   x1^2 */
      89           0 :       fd_bn254_fp_add( y, x, x );  /* y = 2 x1^2 */
      90           0 :       fd_bn254_fp_add( x, x, y );  /* x = 3 x1^2 */
      91           0 :       fd_bn254_fp_add( y, &p->Y, &p->Y );
      92           0 :       fd_bn254_fp_inv( lambda, y );
      93           0 :       fd_bn254_fp_mul( lambda, lambda, x );
      94           0 :     } else {
      95             :       /* p==-q => r=0 */
      96             :       /* COV: this may never happen with real data */
      97           0 :       return fd_bn254_g1_set_zero( r );
      98           0 :     }
      99           0 :   } else {
     100             :     /* point add: lambda = (y1 - y2) / (x1 - x2) */
     101           0 :     fd_bn254_fp_sub( x, &p->X, &q->X );
     102           0 :     fd_bn254_fp_sub( y, &p->Y, &q->Y );
     103           0 :     fd_bn254_fp_inv( lambda, x );
     104           0 :     fd_bn254_fp_mul( lambda, lambda, y );
     105           0 :   }
     106             : 
     107             :   /* x3 = lambda^2 - x1 - x2 */
     108           0 :   fd_bn254_fp_sqr( x, lambda );
     109           0 :   fd_bn254_fp_sub( x, x, &p->X );
     110           0 :   fd_bn254_fp_sub( x, x, &q->X );
     111             : 
     112             :   /* y3 = lambda * (x1 - x3) - y1 */
     113           0 :   fd_bn254_fp_sub( y, &p->X, x );
     114           0 :   fd_bn254_fp_mul( y, y, lambda );
     115           0 :   fd_bn254_fp_sub( y, y, &p->Y );
     116             : 
     117           0 :   fd_bn254_fp_set( &r->X, x );
     118           0 :   fd_bn254_fp_set( &r->Y, y );
     119           0 :   fd_bn254_fp_set_one( &r->Z );
     120           0 :   return r;
     121           0 : }
     122             : 
     123             : /* fd_bn254_g1_dbl computes r = 2p.
     124             :    https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2007-bl */
     125             : fd_bn254_g1_t *
     126             : fd_bn254_g1_dbl( fd_bn254_g1_t *       r,
     127           0 :                  fd_bn254_g1_t const * p ) {
     128             :   /* p==0, return 0 */
     129           0 :   if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
     130           0 :     return fd_bn254_g1_set_zero( r );
     131           0 :   }
     132             : 
     133           0 :   fd_bn254_fp_t xx[1], yy[1], zz[1];
     134           0 :   fd_bn254_fp_t y4[1], s[1], m[1];
     135             :   /* XX = X1^2 */
     136           0 :   fd_bn254_fp_sqr( xx, &p->X );
     137             :   /* YY = Y1^2 */
     138           0 :   fd_bn254_fp_sqr( yy, &p->Y );
     139             :   /* YYYY = YY^2 */
     140           0 :   fd_bn254_fp_sqr( y4, yy );
     141             :   /* ZZ = Z1^2 */
     142           0 :   fd_bn254_fp_sqr( zz, &p->Z );
     143             :   /* S = 2*((X1+YY)^2-XX-YYYY) */
     144           0 :   fd_bn254_fp_add( s, &p->X, yy );
     145           0 :   fd_bn254_fp_sqr( s, s );
     146           0 :   fd_bn254_fp_sub( s, s, xx );
     147           0 :   fd_bn254_fp_sub( s, s, y4 );
     148           0 :   fd_bn254_fp_add( s, s, s );
     149             :   /* M = 3*XX+a*ZZ^2, a=0 */
     150           0 :   fd_bn254_fp_add( m, xx, xx );
     151           0 :   fd_bn254_fp_add( m, m, xx );
     152             :   /* T = M^2-2*S
     153             :      X3 = T */
     154           0 :   fd_bn254_fp_sqr( &r->X, m );
     155           0 :   fd_bn254_fp_sub( &r->X, &r->X, s );
     156           0 :   fd_bn254_fp_sub( &r->X, &r->X, s );
     157             :   /* Z3 = (Y1+Z1)^2-YY-ZZ
     158             :      note: compute Z3 before Y3 because it depends on p->Y,
     159             :      that might be overwritten if r==p. */
     160           0 :   fd_bn254_fp_add( &r->Z, &p->Z, &p->Y );
     161           0 :   fd_bn254_fp_sqr( &r->Z, &r->Z );
     162           0 :   fd_bn254_fp_sub( &r->Z, &r->Z, yy );
     163           0 :   fd_bn254_fp_sub( &r->Z, &r->Z, zz );
     164             :   /* Y3 = M*(S-T)-8*YYYY */
     165           0 :   fd_bn254_fp_sub( &r->Y, s, &r->X );
     166           0 :   fd_bn254_fp_mul( &r->Y, &r->Y, m );
     167           0 :   fd_bn254_fp_add( y4, y4, y4 ); /* 2 y^4 */
     168           0 :   fd_bn254_fp_add( y4, y4, y4 ); /* 4 y^4 */
     169           0 :   fd_bn254_fp_add( y4, y4, y4 ); /* 8 y^4 */
     170           0 :   fd_bn254_fp_sub( &r->Y, &r->Y, y4 );
     171           0 :   return r;
     172           0 : }
     173             : 
     174             : /* fd_bn254_g1_add_mixed computes r = p + q, when q->Z==1.
     175             :    http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl */
     176             : fd_bn254_g1_t *
     177             : fd_bn254_g1_add_mixed( fd_bn254_g1_t *       r,
     178             :                        fd_bn254_g1_t const * p,
     179           0 :                        fd_bn254_g1_t const * q ) {
     180             :   /* p==0, return q */
     181           0 :   if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
     182           0 :     return fd_bn254_g1_set( r, q );
     183           0 :   }
     184           0 :   fd_bn254_fp_t zz[1], u2[1], s2[1];
     185           0 :   fd_bn254_fp_t h[1], hh[1];
     186           0 :   fd_bn254_fp_t i[1], j[1];
     187           0 :   fd_bn254_fp_t rr[1], v[1];
     188             :   /* Z1Z1 = Z1^2 */
     189           0 :   fd_bn254_fp_sqr( zz, &p->Z );
     190             :   /* U2 = X2*Z1Z1 */
     191           0 :   fd_bn254_fp_mul( u2, &q->X, zz );
     192             :   /* S2 = Y2*Z1*Z1Z1 */
     193           0 :   fd_bn254_fp_mul( s2, &q->Y, &p->Z );
     194           0 :   fd_bn254_fp_mul( s2, s2, zz );
     195             : 
     196             :   /* if p==q, call fd_bn254_g1_dbl */
     197           0 :   if( FD_UNLIKELY( fd_bn254_fp_eq( u2, &p->X ) && fd_bn254_fp_eq( s2, &p->Y ) ) ) {
     198             :     /* COV: this may never happen with real data */
     199           0 :     return fd_bn254_g1_dbl( r, p );
     200           0 :   }
     201             : 
     202             :   /* H = U2-X1 */
     203           0 :   fd_bn254_fp_sub( h, u2, &p->X );
     204             :   /* HH = H^2 */
     205           0 :   fd_bn254_fp_sqr( hh, h );
     206             :   /* I = 4*HH */
     207           0 :   fd_bn254_fp_add( i, hh, hh );
     208           0 :   fd_bn254_fp_add( i, i, i );
     209             :   /* J = H*I */
     210           0 :   fd_bn254_fp_mul( j, h, i );
     211             :   /* r = 2*(S2-Y1) */
     212           0 :   fd_bn254_fp_sub( rr, s2, &p->Y );
     213           0 :   fd_bn254_fp_add( rr, rr, rr );
     214             :   /* V = X1*I */
     215           0 :   fd_bn254_fp_mul( v, &p->X, i );
     216             :   /* X3 = r^2-J-2*V */
     217           0 :   fd_bn254_fp_sqr( &r->X, rr );
     218           0 :   fd_bn254_fp_sub( &r->X, &r->X, j );
     219           0 :   fd_bn254_fp_sub( &r->X, &r->X, v );
     220           0 :   fd_bn254_fp_sub( &r->X, &r->X, v );
     221             :   /* Y3 = r*(V-X3)-2*Y1*J
     222             :      note: i no longer used */
     223           0 :   fd_bn254_fp_mul( i, &p->Y, j ); /* i =   Y1*J */
     224           0 :   fd_bn254_fp_add( i, i, i );     /* i = 2*Y1*J */
     225           0 :   fd_bn254_fp_sub( &r->Y, v, &r->X );
     226           0 :   fd_bn254_fp_mul( &r->Y, &r->Y, rr );
     227           0 :   fd_bn254_fp_sub( &r->Y, &r->Y, i );
     228             :   /* Z3 = (Z1+H)^2-Z1Z1-HH */
     229           0 :   fd_bn254_fp_add( &r->Z, &p->Z, h );
     230           0 :   fd_bn254_fp_sqr( &r->Z, &r->Z );
     231           0 :   fd_bn254_fp_sub( &r->Z, &r->Z, zz );
     232           0 :   fd_bn254_fp_sub( &r->Z, &r->Z, hh );
     233           0 :   return r;
     234           0 : }
     235             : 
     236             : /* fd_bn254_g1_scalar_mul computes r = s * p.
     237             :    This assumes that p is affine, i.e. p->Z==1. */
     238             : fd_bn254_g1_t *
     239             : fd_bn254_g1_scalar_mul( fd_bn254_g1_t *           r,
     240             :                         fd_bn254_g1_t const *     p,
     241           0 :                         fd_bn254_scalar_t const * s ) {
     242             :   /* TODO: wNAF, GLV */
     243           0 :   int i = 255;
     244           0 :   for( ; i>=0 && !fd_uint256_bit( s, i ); i-- ) ; /* do nothing, just i-- */
     245           0 :   if( FD_UNLIKELY( i<0 ) ) {
     246           0 :     return fd_bn254_g1_set_zero( r );
     247           0 :   }
     248           0 :   fd_bn254_g1_set( r, p );
     249           0 :   for( i--; i>=0; i-- ) {
     250           0 :     fd_bn254_g1_dbl( r, r );
     251           0 :     if( fd_uint256_bit( s, i ) ) {
     252           0 :       fd_bn254_g1_add_mixed( r, r, p );
     253           0 :     }
     254           0 :   }
     255           0 :   return r;
     256           0 : }
     257             : 
     258             : /* fd_bn254_g1_frombytes_internal extracts (x, y) and performs basic checks.
     259             :    This is used by fd_bn254_g1_compress() and fd_bn254_g1_frombytes_check_subgroup().
     260             :    https://github.com/arkworks-rs/algebra/blob/v0.4.2/ec/src/models/short_weierstrass/mod.rs#L173-L178 */
     261             : static inline fd_bn254_g1_t *
     262             : fd_bn254_g1_frombytes_internal( fd_bn254_g1_t * p,
     263           0 :                                 uchar const     in[64] ) {
     264             :   /* Special case: all zeros => point at infinity */
     265           0 :   const uchar zero[64] = { 0 };
     266           0 :   if( FD_UNLIKELY( fd_memeq( in, zero, 64 ) ) ) {
     267           0 :     return fd_bn254_g1_set_zero( p );
     268           0 :   }
     269             : 
     270             :   /* Check x < p */
     271           0 :   if( FD_UNLIKELY( !fd_bn254_fp_frombytes_be_nm( &p->X, &in[0], NULL, NULL ) ) ) {
     272           0 :     return NULL;
     273           0 :   }
     274             : 
     275             :   /* Check flags and y < p */
     276           0 :   int is_inf, is_neg;
     277           0 :   if( FD_UNLIKELY( !fd_bn254_fp_frombytes_be_nm( &p->Y, &in[32], &is_inf, &is_neg ) ) ) {
     278           0 :     return NULL;
     279           0 :   }
     280             : 
     281           0 :   if( FD_UNLIKELY( is_inf ) ) {
     282           0 :     return fd_bn254_g1_set_zero( p );
     283           0 :   }
     284             : 
     285           0 :   fd_bn254_fp_set_one( &p->Z );
     286           0 :   return p;
     287           0 : }
     288             : 
     289             : /* fd_bn254_g1_frombytes_check_subgroup performs frombytes AND checks subgroup membership. */
     290             : static inline fd_bn254_g1_t *
     291             : fd_bn254_g1_frombytes_check_subgroup( fd_bn254_g1_t * p,
     292           0 :                                       uchar const     in[64] ) {
     293           0 :   if( FD_UNLIKELY( !fd_bn254_g1_frombytes_internal( p, in ) ) ) {
     294           0 :     return NULL;
     295           0 :   }
     296           0 :   if( FD_UNLIKELY( fd_bn254_g1_is_zero( p ) ) ) {
     297           0 :     return p;
     298           0 :   }
     299             : 
     300           0 :   fd_bn254_fp_to_mont( &p->X, &p->X );
     301           0 :   fd_bn254_fp_to_mont( &p->Y, &p->Y );
     302           0 :   fd_bn254_fp_set_one( &p->Z );
     303             : 
     304             :   /* Check that y^2 = x^3 + b */
     305           0 :   fd_bn254_fp_t y2[1], x3b[1];
     306           0 :   fd_bn254_fp_sqr( y2, &p->Y );
     307           0 :   fd_bn254_fp_sqr( x3b, &p->X );
     308           0 :   fd_bn254_fp_mul( x3b, x3b, &p->X );
     309           0 :   fd_bn254_fp_add( x3b, x3b, fd_bn254_const_b_mont );
     310           0 :   if( FD_UNLIKELY( !fd_bn254_fp_eq( y2, x3b ) ) ) {
     311           0 :     return NULL;
     312           0 :   }
     313             : 
     314             :   /* G1 has prime order, so we don't need to do any further checks. */
     315             : 
     316           0 :   return p;
     317           0 : }

Generated by: LCOV version 1.14