LCOV - code coverage report
Current view: top level - ballet/bn254 - fd_bn254_g2.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 299 362 82.6 %
Date: 2026-03-19 18:19:27 Functions: 15 17 88.2 %

          Line data    Source code
       1             : #include "./fd_bn254.h"
       2             : 
       3             : /* G2 */
       4             : 
       5             : /* COV: unlike g1, g2 operations are not exposed to users.
       6             :    So many edge cases and checks for zero are never triggered, e.g. by syscall tests. */
       7             : 
       8             : static inline int
       9       14709 : fd_bn254_g2_is_zero( fd_bn254_g2_t const * p ) {
      10       14709 :   return fd_bn254_fp2_is_zero( &p->Z );
      11       14709 : }
      12             : 
      13             : static inline int
      14             : fd_bn254_g2_eq( fd_bn254_g2_t const * p,
      15         127 :                 fd_bn254_g2_t const * q ) {
      16         127 :   if( fd_bn254_g2_is_zero( p ) ) {
      17          13 :     return fd_bn254_g2_is_zero( q );
      18          13 :   }
      19         114 :   if( fd_bn254_g2_is_zero( q ) ) {
      20           0 :     return 0;
      21           0 :   }
      22             : 
      23         114 :   fd_bn254_fp2_t pz2[1], qz2[1];
      24         114 :   fd_bn254_fp2_t l[1], r[1];
      25             : 
      26         114 :   fd_bn254_fp2_sqr( pz2, &p->Z );
      27         114 :   fd_bn254_fp2_sqr( qz2, &q->Z );
      28             : 
      29         114 :   fd_bn254_fp2_mul( l, &p->X, qz2 );
      30         114 :   fd_bn254_fp2_mul( r, &q->X, pz2 );
      31         114 :   if( !fd_bn254_fp2_eq( l, r ) ) {
      32           8 :     return 0;
      33           8 :   }
      34             : 
      35         106 :   fd_bn254_fp2_mul( l, &p->Y, qz2 );
      36         106 :   fd_bn254_fp2_mul( l, l, &q->Z );
      37         106 :   fd_bn254_fp2_mul( r, &q->Y, pz2 );
      38         106 :   fd_bn254_fp2_mul( r, r, &p->Z );
      39         106 :   return fd_bn254_fp2_eq( l, r );
      40         114 : }
      41             : 
      42             : static inline fd_bn254_g2_t *
      43             : fd_bn254_g2_set( fd_bn254_g2_t *       r,
      44         351 :                  fd_bn254_g2_t const * p ) {
      45         351 :   fd_bn254_fp2_set( &r->X, &p->X );
      46         351 :   fd_bn254_fp2_set( &r->Y, &p->Y );
      47         351 :   fd_bn254_fp2_set( &r->Z, &p->Z );
      48         351 :   return r;
      49         351 : }
      50             : 
      51             : static inline fd_bn254_g2_t *
      52             : fd_bn254_g2_neg( fd_bn254_g2_t *       r,
      53          84 :                  fd_bn254_g2_t const * p ) {
      54          84 :   fd_bn254_fp2_set( &r->X, &p->X );
      55          84 :   fd_bn254_fp2_neg( &r->Y, &p->Y );
      56          84 :   fd_bn254_fp2_set( &r->Z, &p->Z );
      57          84 :   return r;
      58          84 : }
      59             : 
      60             : static inline fd_bn254_g2_t *
      61          52 : fd_bn254_g2_set_zero( fd_bn254_g2_t * r ) {
      62             :   // fd_bn254_fp2_set_zero( &r->X );
      63             :   // fd_bn254_fp2_set_zero( &r->Y );
      64          52 :   fd_bn254_fp2_set_zero( &r->Z );
      65          52 :   return r;
      66          52 : }
      67             : 
      68             : static inline fd_bn254_g2_t *
      69             : fd_bn254_g2_to_affine( fd_bn254_g2_t *       r,
      70           0 :                        fd_bn254_g2_t const * p ) {
      71           0 :   if( FD_UNLIKELY( fd_bn254_fp2_is_zero( &p->Z ) || fd_bn254_fp2_is_one( &p->Z ) ) ) {
      72           0 :     return fd_bn254_g2_set( r, p );
      73           0 :   }
      74             : 
      75           0 :   fd_bn254_fp2_t iz[1], iz2[1];
      76           0 :   fd_bn254_fp2_inv( iz, &p->Z );
      77           0 :   fd_bn254_fp2_sqr( iz2, iz );
      78             : 
      79             :   /* X / Z^2, Y / Z^3 */
      80           0 :   fd_bn254_fp2_mul( &r->X, &p->X, iz2 );
      81           0 :   fd_bn254_fp2_mul( &r->Y, &p->Y, iz2 );
      82           0 :   fd_bn254_fp2_mul( &r->Y, &r->Y, iz );
      83           0 :   fd_bn254_fp2_set_one( &r->Z );
      84           0 :   return r;
      85           0 : }
      86             : 
      87             : uchar *
      88             : fd_bn254_g2_tobytes( uchar                 out[128],
      89             :                      fd_bn254_g2_t const * p,
      90           0 :                      int                   big_endian ) {
      91           0 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( p ) ) ) {
      92           0 :     fd_memset( out, 0, 128UL );
      93             :     /* no flags */
      94           0 :     return out;
      95           0 :   }
      96             : 
      97           0 :   fd_bn254_g2_t r[1];
      98           0 :   fd_bn254_g2_to_affine( r, p );
      99             : 
     100           0 :   fd_bn254_fp2_from_mont( &r->X, &r->X );
     101           0 :   fd_bn254_fp2_from_mont( &r->Y, &r->Y );
     102             : 
     103           0 :   fd_bn254_fp2_tobytes_nm( &out[ 0], &r->X, big_endian );
     104           0 :   fd_bn254_fp2_tobytes_nm( &out[64], &r->Y, big_endian );
     105             :   /* no flags */
     106           0 :   return out;
     107           0 : }
     108             : 
     109             : static inline fd_bn254_g2_t *
     110             : fd_bn254_g2_frob( fd_bn254_g2_t *       r,
     111         338 :                   fd_bn254_g2_t const * p ) {
     112         338 :   fd_bn254_fp2_conj( &r->X, &p->X );
     113         338 :   fd_bn254_fp2_mul ( &r->X, &r->X, &fd_bn254_const_frob_gamma1_mont[1] );
     114         338 :   fd_bn254_fp2_conj( &r->Y, &p->Y );
     115         338 :   fd_bn254_fp2_mul ( &r->Y, &r->Y, &fd_bn254_const_frob_gamma1_mont[2] );
     116         338 :   fd_bn254_fp2_conj( &r->Z, &p->Z );
     117         338 :   return r;
     118         338 : }
     119             : 
     120             : static inline fd_bn254_g2_t *
     121             : fd_bn254_g2_frob2( fd_bn254_g2_t *       r,
     122         211 :                    fd_bn254_g2_t const * p ) {
     123             :   /* X */
     124         211 :   fd_bn254_fp_mul( &r->X.el[0], &p->X.el[0], &fd_bn254_const_frob_gamma2_mont[1] );
     125         211 :   fd_bn254_fp_mul( &r->X.el[1], &p->X.el[1], &fd_bn254_const_frob_gamma2_mont[1] );
     126             :   /* Y */
     127         211 :   fd_bn254_fp_mul( &r->Y.el[0], &p->Y.el[0], &fd_bn254_const_frob_gamma2_mont[2] );
     128         211 :   fd_bn254_fp_mul( &r->Y.el[1], &p->Y.el[1], &fd_bn254_const_frob_gamma2_mont[2] );
     129             :   /* Z=1 */
     130         211 :   fd_bn254_fp2_set( &r->Z, &p->Z );
     131         211 :   return r;
     132         211 : }
     133             : 
     134             : /* fd_bn254_g2_dbl computes r = 2p.
     135             :    https://hyperelliptic.org/efd/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l */
     136             : fd_bn254_g2_t *
     137             : fd_bn254_g2_dbl( fd_bn254_g2_t *       r,
     138        7130 :                  fd_bn254_g2_t const * p ) {
     139             :   /* p==0, return 0 */
     140        7130 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( p ) ) ) {
     141          13 :     return fd_bn254_g2_set_zero( r );
     142          13 :   }
     143             : 
     144        7117 :   fd_bn254_fp2_t a[1], b[1], c[1];
     145        7117 :   fd_bn254_fp2_t d[1], e[1], f[1];
     146             : 
     147             :   /* A = X1^2 */
     148        7117 :   fd_bn254_fp2_sqr( a, &p->X );
     149             :   /* B = Y1^2 */
     150        7117 :   fd_bn254_fp2_sqr( b, &p->Y );
     151             :   /* C = B^2 */
     152        7117 :   fd_bn254_fp2_sqr( c, b );
     153             :   /* D = 2*((X1+B)^2-A-C)
     154             :      (X1+B)^2 = X1^2 + 2*X1*B + B^2
     155             :      D = 2*(X1^2 + 2*X1*B + B^2 - A    - C)
     156             :      D = 2*(X1^2 + 2*X1*B + B^2 - X1^2 - B^2)
     157             :             ^               ^     ^      ^
     158             :             |---------------|-----|      |
     159             :                             |------------|
     160             :      These terms cancel each other out, and we're left with:
     161             :      D = 2*(2*X1*B) */
     162        7117 :   fd_bn254_fp2_mul( d, &p->X, b );
     163        7117 :   fd_bn254_fp2_add( d, d, d );
     164        7117 :   fd_bn254_fp2_add( d, d, d );
     165             :   /* E = 3*A */
     166        7117 :   fd_bn254_fp2_add( e, a, a );
     167        7117 :   fd_bn254_fp2_add( e, a, e );
     168             :   /* F = E^2 */
     169        7117 :   fd_bn254_fp2_sqr( f, e );
     170             :   /* X3 = F-2*D */
     171        7117 :   fd_bn254_fp2_add( &r->X, d, d );
     172        7117 :   fd_bn254_fp2_sub( &r->X, f, &r->X );
     173             :   /* Z3 = (Y1+Z1)^2-YY-ZZ
     174             :      note: compute Z3 before Y3 because it depends on p->Y,
     175             :      that might be overwritten if r==p. */
     176             :   /* Z3 = 2*Y1*Z1 */
     177        7117 :   fd_bn254_fp2_mul( &r->Z, &p->Y, &p->Z );
     178        7117 :   fd_bn254_fp2_add( &r->Z, &r->Z, &r->Z );
     179             :   /* Y3 = E*(D-X3)-8*C */
     180        7117 :   fd_bn254_fp2_sub( &r->Y, d, &r->X );
     181        7117 :   fd_bn254_fp2_mul( &r->Y, e, &r->Y );
     182        7117 :   fd_bn254_fp2_add( c, c, c ); /* 2*c */
     183        7117 :   fd_bn254_fp2_add( c, c, c ); /* 4*y */
     184        7117 :   fd_bn254_fp2_add( c, c, c ); /* 8*y */
     185        7117 :   fd_bn254_fp2_sub( &r->Y, &r->Y, c );
     186        7117 :   return r;
     187        7130 : }
     188             : 
     189             : /* fd_bn254_g2_add_mixed computes r = p + q, when q->Z==1.
     190             :    http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl */
     191             : fd_bn254_g2_t *
     192             : fd_bn254_g2_add_mixed( fd_bn254_g2_t *       r,
     193             :                        fd_bn254_g2_t const * p,
     194        3184 :                        fd_bn254_g2_t const * q ) {
     195             :   /* p==0, return q */
     196        3184 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( p ) ) ) {
     197          13 :     return fd_bn254_g2_set( r, q );
     198          13 :   }
     199             :   /* q==0, return p */
     200        3171 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( q ) ) ) {
     201           0 :     return fd_bn254_g2_set( r, p );
     202           0 :   }
     203        3171 :   fd_bn254_fp2_t zz[1], u2[1], s2[1];
     204        3171 :   fd_bn254_fp2_t h[1], hh[1];
     205        3171 :   fd_bn254_fp2_t i[1], j[1];
     206        3171 :   fd_bn254_fp2_t rr[1], v[1];
     207             :   /* Z1Z1 = Z1^2 */
     208        3171 :   fd_bn254_fp2_sqr( zz, &p->Z );
     209             :   /* U2 = X2*Z1Z1 */
     210        3171 :   fd_bn254_fp2_mul( u2, &q->X, zz );
     211             :   /* S2 = Y2*Z1*Z1Z1 */
     212        3171 :   fd_bn254_fp2_mul( s2, &q->Y, &p->Z );
     213        3171 :   fd_bn254_fp2_mul( s2, s2, zz );
     214             : 
     215             :   /* if p==q, call fd_bn254_g2_dbl */
     216        3171 :   if( FD_UNLIKELY( fd_bn254_fp2_eq( u2, &p->X ) && fd_bn254_fp2_eq( s2, &p->Y ) ) ) {
     217           0 :     return fd_bn254_g2_dbl( r, p );
     218           0 :   }
     219             : 
     220             :   /* H = U2-X1 */
     221        3171 :   fd_bn254_fp2_sub( h, u2, &p->X );
     222             :   /* HH = H^2 */
     223        3171 :   fd_bn254_fp2_sqr( hh, h );
     224             :   /* I = 4*HH */
     225        3171 :   fd_bn254_fp2_add( i, hh, hh );
     226        3171 :   fd_bn254_fp2_add( i, i, i );
     227             :   /* J = H*I */
     228        3171 :   fd_bn254_fp2_mul( j, h, i );
     229             :   /* r = 2*(S2-Y1) */
     230        3171 :   fd_bn254_fp2_sub( rr, s2, &p->Y );
     231        3171 :   fd_bn254_fp2_add( rr, rr, rr );
     232             :   /* V = X1*I */
     233        3171 :   fd_bn254_fp2_mul( v, &p->X, i );
     234             :   /* X3 = r^2-J-2*V */
     235        3171 :   fd_bn254_fp2_sqr( &r->X, rr );
     236        3171 :   fd_bn254_fp2_sub( &r->X, &r->X, j );
     237        3171 :   fd_bn254_fp2_sub( &r->X, &r->X, v );
     238        3171 :   fd_bn254_fp2_sub( &r->X, &r->X, v );
     239             :   /* Y3 = r*(V-X3)-2*Y1*J
     240             :      note: i no longer used */
     241        3171 :   fd_bn254_fp2_mul( i, &p->Y, j ); /* i =   Y1*J */
     242        3171 :   fd_bn254_fp2_add( i, i, i );     /* i = 2*Y1*J */
     243        3171 :   fd_bn254_fp2_sub( &r->Y, v, &r->X );
     244        3171 :   fd_bn254_fp2_mul( &r->Y, &r->Y, rr );
     245        3171 :   fd_bn254_fp2_sub( &r->Y, &r->Y, i );
     246             :   /* Z3 = (Z1+H)^2-Z1Z1-HH */
     247        3171 :   fd_bn254_fp2_add( &r->Z, &p->Z, h );
     248        3171 :   fd_bn254_fp2_sqr( &r->Z, &r->Z );
     249        3171 :   fd_bn254_fp2_sub( &r->Z, &r->Z, zz );
     250        3171 :   fd_bn254_fp2_sub( &r->Z, &r->Z, hh );
     251        3171 :   return r;
     252        3171 : }
     253             : 
     254             : /* fd_bn254_g2_add computes r = p + q.
     255             :    http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl */
     256             : fd_bn254_g2_t *
     257             : fd_bn254_g2_add( fd_bn254_g2_t *       r,
     258             :                  fd_bn254_g2_t const * p,
     259         253 :                  fd_bn254_g2_t const * q ) {
     260             :   /* p==0, return q */
     261         253 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( p ) ) ) {
     262          26 :     return fd_bn254_g2_set( r, q );
     263          26 :   }
     264             :   /* q==0, return p */
     265         227 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( q ) ) ) {
     266           0 :     return fd_bn254_g2_set( r, p );
     267           0 :   }
     268         227 :   fd_bn254_fp2_t zz1[1], zz2[1];
     269         227 :   fd_bn254_fp2_t u1[1], s1[1];
     270         227 :   fd_bn254_fp2_t u2[1], s2[1];
     271         227 :   fd_bn254_fp2_t h[1];
     272         227 :   fd_bn254_fp2_t i[1], j[1];
     273         227 :   fd_bn254_fp2_t rr[1], v[1];
     274             :   /* Z1Z1 = Z1^2 */
     275         227 :   fd_bn254_fp2_sqr( zz1, &p->Z );
     276             :   /* Z2Z2 = Z2^2 */
     277         227 :   fd_bn254_fp2_sqr( zz2, &q->Z );
     278             :   /* U1 = X1*Z2Z2 */
     279         227 :   fd_bn254_fp2_mul( u1, &p->X, zz2 );
     280             :   /* U2 = X2*Z1Z1 */
     281         227 :   fd_bn254_fp2_mul( u2, &q->X, zz1 );
     282             :   /* S1 = Y1*Z2*Z2Z2 */
     283         227 :   fd_bn254_fp2_mul( s1, &p->Y, &q->Z );
     284         227 :   fd_bn254_fp2_mul( s1, s1, zz2 );
     285             :   /* S2 = Y2*Z1*Z1Z1 */
     286         227 :   fd_bn254_fp2_mul( s2, &q->Y, &p->Z );
     287         227 :   fd_bn254_fp2_mul( s2, s2, zz1 );
     288             : 
     289             :   /* if p==q, call fd_bn254_g2_dbl */
     290             :   // if( FD_UNLIKELY( fd_bn254_fp2_eq( u2, &p->X ) && fd_bn254_fp2_eq( s2, &p->Y ) ) ) {
     291             :   //   return fd_bn254_g2_dbl( r, p );
     292             :   // }
     293             : 
     294             :   /* H = U2-U1 */
     295         227 :   fd_bn254_fp2_sub( h, u2, u1 );
     296             :   /* HH = (2*H)^2 */
     297         227 :   fd_bn254_fp2_add( i, h, h );
     298         227 :   fd_bn254_fp2_sqr( i, i );
     299             :   /* J = H*I */
     300         227 :   fd_bn254_fp2_mul( j, h, i );
     301             :   /* r = 2*(S2-S1) */
     302         227 :   fd_bn254_fp2_sub( rr, s2, s1 );
     303         227 :   fd_bn254_fp2_add( rr, rr, rr );
     304             :   /* V = U1*I */
     305         227 :   fd_bn254_fp2_mul( v, u1, i );
     306             :   /* X3 = r^2-J-2*V */
     307         227 :   fd_bn254_fp2_sqr( &r->X, rr );
     308         227 :   fd_bn254_fp2_sub( &r->X, &r->X, j );
     309         227 :   fd_bn254_fp2_sub( &r->X, &r->X, v );
     310         227 :   fd_bn254_fp2_sub( &r->X, &r->X, v );
     311             :   /* Y3 = r*(V-X3)-2*S1*J
     312             :      note: i no longer used */
     313         227 :   fd_bn254_fp2_mul( i, s1, j ); /* i =   S1*J */
     314         227 :   fd_bn254_fp2_add( i, i, i );  /* i = 2*S1*J */
     315         227 :   fd_bn254_fp2_sub( &r->Y, v, &r->X );
     316         227 :   fd_bn254_fp2_mul( &r->Y, &r->Y, rr );
     317         227 :   fd_bn254_fp2_sub( &r->Y, &r->Y, i );
     318             :   /* Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2)*H */
     319         227 :   fd_bn254_fp2_add( &r->Z, &p->Z, &q->Z );
     320         227 :   fd_bn254_fp2_sqr( &r->Z, &r->Z );
     321         227 :   fd_bn254_fp2_sub( &r->Z, &r->Z, zz1 );
     322         227 :   fd_bn254_fp2_sub( &r->Z, &r->Z, zz2 );
     323         227 :   fd_bn254_fp2_mul( &r->Z, &r->Z, h );
     324         227 :   return r;
     325         227 : }
     326             : 
     327             : /* fd_bn254_g2_affine_add computes r = p + q.
     328             :    Both p, q are affine, i.e. Z==1. */
     329             : static fd_bn254_g2_t *
     330             : fd_bn254_g2_affine_add( fd_bn254_g2_t *       r,
     331             :                         fd_bn254_g2_t const * p,
     332         114 :                         fd_bn254_g2_t const * q ) {
     333             :   /* p==0, return q */
     334         114 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( p ) ) ) {
     335           0 :     return fd_bn254_g2_set( r, q );
     336           0 :   }
     337             :   /* q==0, return p */
     338         114 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( q ) ) ) {
     339           0 :     return fd_bn254_g2_set( r, p );
     340           0 :   }
     341             : 
     342         114 :   fd_bn254_fp2_t lambda[1], x[1], y[1];
     343             : 
     344             :   /* same X, either the points are equal or opposite */
     345         114 :   if( fd_bn254_fp2_eq( &p->X, &q->X ) ) {
     346           0 :     if( fd_bn254_fp2_eq( &p->Y, &q->Y ) ) {
     347             :       /* p==q => point double: lambda = 3 * x1^2 / (2 * y1) */
     348           0 :       fd_bn254_fp2_sqr( x, &p->X ); /* x =   x1^2 */
     349           0 :       fd_bn254_fp2_add( y, x, x );  /* y = 2 x1^2 */
     350           0 :       fd_bn254_fp2_add( x, x, y );  /* x = 3 x1^2 */
     351           0 :       fd_bn254_fp2_add( y, &p->Y, &p->Y );
     352           0 :       fd_bn254_fp2_inv( lambda, y );
     353           0 :       fd_bn254_fp2_mul( lambda, lambda, x );
     354           0 :     } else {
     355             :       /* p==-q => r=0 */
     356           0 :       return fd_bn254_g2_set_zero( r );
     357           0 :     }
     358         114 :   } else {
     359             :     /* point add: lambda = (y1 - y2) / (x1 - x2) */
     360         114 :     fd_bn254_fp2_sub( x, &p->X, &q->X );
     361         114 :     fd_bn254_fp2_sub( y, &p->Y, &q->Y );
     362         114 :     fd_bn254_fp2_inv( lambda, x );
     363         114 :     fd_bn254_fp2_mul( lambda, lambda, y );
     364         114 :   }
     365             : 
     366             :   /* x3 = lambda^2 - x1 - x2 */
     367         114 :   fd_bn254_fp2_sqr( x, lambda );
     368         114 :   fd_bn254_fp2_sub( x, x, &p->X );
     369         114 :   fd_bn254_fp2_sub( x, x, &q->X );
     370             : 
     371             :   /* y3 = lambda * (x1 - x3) - y1 */
     372         114 :   fd_bn254_fp2_sub( y, &p->X, x );
     373         114 :   fd_bn254_fp2_mul( y, y, lambda );
     374         114 :   fd_bn254_fp2_sub( y, y, &p->Y );
     375             : 
     376         114 :   fd_bn254_fp2_set( &r->X, x );
     377         114 :   fd_bn254_fp2_set( &r->Y, y );
     378         114 :   fd_bn254_fp2_set_one( &r->Z );
     379         114 :   return r;
     380         114 : }
     381             : 
     382             : /* fd_bn254_g2_scalar_mul computes r = [s]P.
     383             :    p must be in affine form (p->Z == 1).
     384             :    The result is in projective coordinates over Fp2. */
     385             : fd_bn254_g2_t *
     386             : fd_bn254_g2_scalar_mul( fd_bn254_g2_t *           r,
     387             :                         fd_bn254_g2_t const *     p,
     388         127 :                         fd_bn254_scalar_t const * s ) {
     389         127 :   if( FD_UNLIKELY( fd_uint256_is_zero( s ) || fd_bn254_g2_is_zero( p ) ) ) {
     390          13 :     return fd_bn254_g2_set_zero( r );
     391          13 :   }
     392             : 
     393         114 :   const ulong g1_const[ 3 ] = { 0x7a7bd9d4391eb18eUL, 0x4ccef014a773d2cfUL, 0x0000000000000002UL };
     394         114 :   ulong b1[ 3 ], b2[ 2 ];
     395         114 :   fd_bn254_glv_sxg3( b1, s, g1_const );
     396         114 :   fd_bn254_glv_sxg2( b2, s, g2_const );
     397             : 
     398             :   /* k1 = s - b1*N_C - b2*N_B (may be negative for G2) */
     399         114 :   fd_uint256_t k1_abs[1];
     400         114 :   int k1_neg = 0;
     401         114 :   {
     402         114 :     ulong p_nc[ 4 ];
     403             :     /* b2*nb will produce at most 3 limbs, so we want the 4th zeroed for the addition. */
     404         114 :     ulong p_nb[ 4 ] = {0};
     405         114 :     ulong    t[ 4 ];
     406         114 :     fd_bn254_glv_mul3x2( p_nc, b1, nc );
     407         114 :     fd_bn254_glv_mul2x1( p_nb, b2, nb );
     408         114 :     fd_bn254_glv_add4( t, p_nc, p_nb );
     409         114 :     ulong borrow = fd_bn254_glv_sub4( k1_abs->limbs, s->limbs, t );
     410         114 :     if( borrow ) {
     411           0 :       k1_neg = 1;
     412           0 :       fd_bn254_glv_negate4( k1_abs->limbs );
     413           0 :     }
     414         114 :   }
     415             : 
     416             :   /* k2 = b2*N_A - b1*N_B (usually negative for G2) */
     417         114 :   fd_uint256_t k2_abs[1];
     418         114 :   int k2_neg = 0;
     419         114 :   {
     420         114 :     ulong pos[ 4 ], neg[ 4 ];
     421         114 :     fd_bn254_glv_mul2x2( pos, b2, na );
     422         114 :     fd_bn254_glv_mul3x1( neg, b1, nb );
     423         114 :     ulong borrow = fd_bn254_glv_sub4( k2_abs->limbs, pos, neg );
     424         114 :     if( borrow ) {
     425           0 :       k2_neg = 1;
     426           0 :       fd_bn254_glv_negate4( k2_abs->limbs );
     427           0 :     }
     428         114 :   }
     429             : 
     430             :   /* pt1 = P, pt2 = phi(P) = (beta * P.x, P.y).
     431             :      If k1 < 0, negate pt1. If k2 < 0, negate pt2. */
     432         114 :   fd_bn254_g2_t pt1[1], pt2[1];
     433         114 :   fd_bn254_g2_set( pt1, p );
     434         114 :   fd_bn254_fp_mul( &pt2->X.el[0], &p->X.el[0], fd_bn254_const_beta_mont );
     435         114 :   fd_bn254_fp_mul( &pt2->X.el[1], &p->X.el[1], fd_bn254_const_beta_mont );
     436         114 :   fd_bn254_fp2_set( &pt2->Y, &p->Y );
     437         114 :   fd_bn254_fp2_set_one( &pt2->Z );
     438         114 :   if( k1_neg ) {
     439           0 :     fd_bn254_fp2_neg( &pt1->Y, &pt1->Y );
     440           0 :   }
     441         114 :   if( k2_neg ) {
     442           0 :     fd_bn254_fp2_neg( &pt2->Y, &pt2->Y );
     443           0 :   }
     444             : 
     445         114 :   fd_bn254_g2_t pt12[1];
     446         114 :   fd_bn254_g2_affine_add( pt12, pt1, pt2 );
     447             : 
     448             :   /* Shamir's trick: simultaneous double-and-add on k1, k2. */
     449         114 :   int i = 255;
     450       22116 :   for( ; i>=0; i-- ) {
     451       22116 :     int k1b = !!fd_uint256_bit( k1_abs, i );
     452       22116 :     int k2b = !!fd_uint256_bit( k2_abs, i );
     453       22116 :     if( k1b || k2b ) {
     454         114 :       fd_bn254_g2_set( r, ( k1b && k2b ) ? pt12 : ( k1b ? pt1 : pt2 ) );
     455         114 :       break;
     456         114 :     }
     457       22116 :   }
     458         114 :   if( FD_UNLIKELY( i<0 ) ) {
     459           0 :     return fd_bn254_g2_set_zero( r );
     460           0 :   }
     461        7120 :   for( i--; i >= 0; i-- ) {
     462        7006 :     fd_bn254_g2_dbl( r, r );
     463        7006 :     int k1b = !!fd_uint256_bit( k1_abs, i );
     464        7006 :     int k2b = !!fd_uint256_bit( k2_abs, i );
     465        7006 :     if( k1b && k2b ) {
     466           0 :       fd_bn254_g2_add_mixed( r, r, pt12 );
     467        7006 :     } else if( k1b ) {
     468        3058 :       fd_bn254_g2_add_mixed( r, r, pt1 );
     469        3948 :     } else if( k2b ) {
     470           0 :       fd_bn254_g2_add_mixed( r, r, pt2 );
     471           0 :     }
     472        7006 :   }
     473             : 
     474         114 :   return r;
     475         114 : }
     476             : 
     477             : /* fd_bn254_g2_frombytes_internal extracts (x, y) and performs basic checks.
     478             :    This is used by fd_bn254_g2_compress() and fd_bn254_g2_frombytes_check_subgroup(). */
     479             : static inline fd_bn254_g2_t *
     480             : fd_bn254_g2_frombytes_internal( fd_bn254_g2_t * p,
     481             :                                 uchar const     in[128],
     482         617 :                                 int             big_endian ) {
     483             :   /* Special case: all zeros => point at infinity */
     484         617 :   const uchar zero[128] = { 0 };
     485         617 :   if( FD_UNLIKELY( fd_memeq( in, zero, 128 ) ) ) {
     486           2 :     return fd_bn254_g2_set_zero( p );
     487           2 :   }
     488             : 
     489             :   /* Check x < p */
     490         615 :   if( FD_UNLIKELY( !fd_bn254_fp2_frombytes_nm( &p->X, &in[0], big_endian, NULL, NULL ) ) ) {
     491         294 :     return NULL;
     492         294 :   }
     493             : 
     494             :   /* Check flags and y < p */
     495         321 :   int is_inf, is_neg;
     496         321 :   if( FD_UNLIKELY( !fd_bn254_fp2_frombytes_nm( &p->Y, &in[64], big_endian, &is_inf, &is_neg ) ) ) {
     497         152 :     return NULL;
     498         152 :   }
     499             : 
     500         169 :   if( FD_UNLIKELY( is_inf ) ) {
     501          24 :     return fd_bn254_g2_set_zero( p );
     502          24 :   }
     503             : 
     504         145 :   fd_bn254_fp2_set_one( &p->Z );
     505         145 :   return p;
     506         169 : }
     507             : 
     508             : /* fd_bn254_g2_frombytes_check_eq_only performs frombytes, checks the curve
     509             :    equation, but does NOT check subgroup membership. */
     510             : static inline fd_bn254_g2_t *
     511             : fd_bn254_g2_frombytes_check_eq_only( fd_bn254_g2_t * p,
     512             :                                      uchar const     in[128],
     513         355 :                                      int             big_endian ) {
     514         355 :   if( FD_UNLIKELY( !fd_bn254_g2_frombytes_internal( p, in, big_endian ) ) ) {
     515         222 :     return NULL;
     516         222 :   }
     517         133 :   if( FD_UNLIKELY( fd_bn254_g2_is_zero( p ) ) ) {
     518          13 :     return p;
     519          13 :   }
     520             : 
     521         120 :   fd_bn254_fp2_to_mont( &p->X, &p->X );
     522         120 :   fd_bn254_fp2_to_mont( &p->Y, &p->Y );
     523         120 :   fd_bn254_fp2_set_one( &p->Z );
     524             : 
     525             :   /* Check that y^2 = x^3 + b */
     526         120 :   fd_bn254_fp2_t y2[1], x3b[1];
     527         120 :   fd_bn254_fp2_sqr( y2, &p->Y );
     528         120 :   fd_bn254_fp2_sqr( x3b, &p->X );
     529         120 :   fd_bn254_fp2_mul( x3b, x3b, &p->X );
     530         120 :   fd_bn254_fp2_add( x3b, x3b, fd_bn254_const_twist_b_mont );
     531         120 :   if( FD_UNLIKELY( !fd_bn254_fp2_eq( y2, x3b ) ) ) {
     532           8 :     return NULL;
     533           8 :   }
     534         112 :   return p;
     535         120 : }
     536             : 
     537             : /* fd_bn254_g2_frombytes_check_subgroup performs frombytes AND checks subgroup membership. */
     538             : static inline fd_bn254_g2_t *
     539             : fd_bn254_g2_frombytes_check_subgroup( fd_bn254_g2_t * p,
     540             :                                       uchar const     in[128],
     541         355 :                                       int             big_endian ) {
     542         355 :   if( FD_UNLIKELY( fd_bn254_g2_frombytes_check_eq_only( p, in, big_endian )==NULL ) ) {
     543         230 :     return NULL;
     544         230 :   }
     545             : 
     546             :   /* G2 does NOT have prime order, so we have to check group membership. */
     547             : 
     548             :   /* We use the fast subgroup membership check, that requires a single 64-bit scalar mul.
     549             :      https://eprint.iacr.org/2022/348, Sec 3.1.
     550             :      [r]P == 0 <==> [x+1]P + ψ([x]P) + ψ²([x]P) = ψ³([2x]P)
     551             :      See also: https://github.com/Consensys/gnark-crypto/blob/v0.12.1/ecc/bn254/g2.go#L404
     552             : 
     553             :      For reference, the followings also work:
     554             : 
     555             :      1) very slow: 256-bit scalar mul
     556             : 
     557             :      fd_bn254_g2_t r[1];
     558             :      fd_bn254_g2_scalar_mul( r, p, fd_bn254_const_r );
     559             :      if( !fd_bn254_g2_is_zero( r ) ) return NULL;
     560             : 
     561             :      2) slow: 128-bit scalar mul
     562             : 
     563             :      fd_bn254_g2_t a[1], b[1];
     564             :      const fd_bn254_scalar_t six_x_sqr[1] = {{{ 0xf83e9682e87cfd46, 0x6f4d8248eeb859fb, 0x0, 0x0, }}};
     565             :      fd_bn254_g2_scalar_mul( a, p, six_x_sqr );
     566             :      fd_bn254_g2_frob( b, p );
     567             :      if( !fd_bn254_g2_eq( a, b ) ) return NULL; */
     568             : 
     569         125 :   fd_bn254_g2_t xp[1], l[1], psi[1], r[1];
     570         125 :   fd_bn254_g2_scalar_mul( xp, p, fd_bn254_const_x ); /* 64-bit */
     571         125 :   fd_bn254_g2_add_mixed( l, xp, p );
     572             : 
     573         125 :   fd_bn254_g2_frob( psi, xp );
     574         125 :   fd_bn254_g2_add( l, l, psi );
     575             : 
     576         125 :   fd_bn254_g2_frob2( psi, xp ); /* faster than frob( psi, psi ) */
     577         125 :   fd_bn254_g2_add( l, l, psi );
     578             : 
     579         125 :   fd_bn254_g2_frob( psi, psi );
     580         125 :   fd_bn254_g2_dbl( r, psi );
     581         125 :   if( FD_UNLIKELY( !fd_bn254_g2_eq( l, r ) ) ) {
     582           8 :     return NULL;
     583           8 :   }
     584         117 :   return p;
     585         125 : }

Generated by: LCOV version 1.14