LCOV - code coverage report
Current view: top level - ballet/ed25519/avx512 - fd_f25519.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 48 93 51.6 %
Date: 2026-11-26 02:16:43 Functions: 53 560 9.5 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_ballet_ed25519_fd_f25519_h
       2             : #error "Do not include this directly; use fd_f25519.h"
       3             : #endif
       4             : 
       5             : #include "../../fd_ballet_base.h"
       6             : #include "fd_r43x6.h"
       7             : 
       8             : #define FD_F25519_ALIGN 64
       9             : 
      10             : /* A fd_f25519_t stores a curve25519 field element in six radix-2^43
      11             :    limbs of an AVX-512 vector, aligned to 64 bytes. */
      12             : struct fd_f25519 {
      13             :   fd_r43x6_t el __attribute__((aligned(FD_F25519_ALIGN)));
      14             : };
      15             : typedef struct fd_f25519 fd_f25519_t;
      16             : 
      17             : #include "../table/fd_f25519_table_avx512.c"
      18             : 
      19             : FD_PROTOTYPES_BEGIN
      20             : 
      21             : /*
      22             :  * Implementation of inline functions
      23             :  */
      24             : 
      25             : /* fd_f25519_mul computes r = a * b, and returns r. */
      26             : FD_25519_INLINE fd_f25519_t *
      27             : fd_f25519_mul( fd_f25519_t * r,
      28             :                fd_f25519_t const * a,
      29      984798 :                fd_f25519_t const * b ) {
      30      984798 :   FD_R43X6_MUL1_INL( r->el, a->el, b->el );
      31      984798 :   return r;
      32      984798 : }
      33             : 
      34             : /* fd_f25519_sqr computes r = a^2, and returns r. */
      35             : FD_25519_INLINE fd_f25519_t *
      36             : fd_f25519_sqr( fd_f25519_t * r,
      37     6340750 :                fd_f25519_t const * a ) {
      38     6340750 :   FD_R43X6_SQR1_INL( r->el, a->el );
      39     6340750 :   return r;
      40     6340750 : }
      41             : 
      42             : /* fd_f25519_add computes r = a + b, and returns r. */
      43             : FD_25519_INLINE fd_f25519_t *
      44             : fd_f25519_add( fd_f25519_t * r,
      45             :                fd_f25519_t const * a,
      46      158541 :                fd_f25519_t const * b ) {
      47      158541 :   (r->el) = fd_r43x6_add( (a->el), (b->el) );
      48      158541 :   return r;
      49      158541 : }
      50             : 
      51             : /* fd_f25519_add computes r = a - b, and returns r. */
      52             : FD_25519_INLINE fd_f25519_t *
      53             : fd_f25519_sub( fd_f25519_t * r,
      54             :                fd_f25519_t const * a,
      55      291165 :                fd_f25519_t const * b ) {
      56      291165 :   (r->el) = fd_r43x6_fold_signed( fd_r43x6_sub_fast( (a->el), (b->el) ) );
      57      291165 :   return r;
      58      291165 : }
      59             : 
      60             : /* fd_f25519_add computes r = a + b, and returns r.
      61             :    Note: this does NOT reduce the result mod p.
      62             :    It can be used before mul, sqr. */
      63             : FD_25519_INLINE fd_f25519_t *
      64             : fd_f25519_add_nr( fd_f25519_t * r,
      65             :                   fd_f25519_t const * a,
      66           0 :                   fd_f25519_t const * b ) {
      67           0 :   (r->el) = fd_r43x6_add_fast( (a->el), (b->el) );
      68           0 :   return r;
      69           0 : }
      70             : 
      71             : /* fd_f25519_sub computes r = a - b, and returns r.
      72             :    Note: this does NOT reduce the result mod p.
      73             :    It can be used before mul, sqr. */
      74             : FD_25519_INLINE fd_f25519_t *
      75             : fd_f25519_sub_nr( fd_f25519_t * r,
      76             :                   fd_f25519_t const * a,
      77           0 :                   fd_f25519_t const * b ) {
      78           0 :   (r->el) = fd_r43x6_fold_signed( fd_r43x6_sub_fast( (a->el), (b->el) ) );
      79           0 :   return r;
      80           0 : }
      81             : 
      82             : /* fd_f25519_add computes r = -a, and returns r. */
      83             : FD_25519_INLINE fd_f25519_t *
      84             : fd_f25519_neg( fd_f25519_t * r,
      85      113938 :                fd_f25519_t const * a ) {
      86      113938 :   (r->el) = fd_r43x6_neg( (a->el) );
      87      113938 :   return r;
      88      113938 : }
      89             : 
      90             : /* fd_f25519_add computes r = a * k, k=121666, and returns r. */
      91             : FD_25519_INLINE fd_f25519_t *
      92             : fd_f25519_mul_121666( fd_f25519_t * r,
      93           0 :                       FD_FN_UNUSED fd_f25519_t const * a ) {
      94           0 :   (r->el) = fd_r43x6_fold_unsigned( fd_r43x6_scale_fast( 121666L, (a->el) ) );
      95           0 :   return r;
      96           0 : }
      97             : 
      98             : /* fd_f25519_frombytes deserializes a 32-byte buffer buf into a
      99             :    fd_f25519_t element r, and returns r.
     100             :    buf is in little endian form, we accept non-canonical elements
     101             :    unlike RFC 8032. */
     102             : FD_25519_INLINE fd_f25519_t *
     103             : fd_f25519_frombytes( fd_f25519_t * r,
     104      159025 :                      uchar const   buf[ 32 ] ) {
     105      159025 :   ulong y0 = fd_ulong_load_8_fast( buf );                         /* Bits   0- 63 */
     106      159025 :   ulong y1 = fd_ulong_load_8_fast( buf+8 );                       /* Bits  64-127 */
     107      159025 :   ulong y2 = fd_ulong_load_8_fast( buf+16 );                      /* Bits 128-191 */
     108      159025 :   ulong y3 = fd_ulong_load_8_fast( buf+24 ) & 0x7fffffffffffffff; /* Bits 192-254 */
     109      159025 :   r->el = fd_r43x6_unpack( wv( y0, y1, y2, y3 ) );
     110      159025 :   return r;
     111      159025 : }
     112             : 
     113             : /* fd_f25519_tobytes serializes a fd_f25519_t element a into
     114             :    a 32-byte buffer out, and returns out.
     115             :    out is in little endian form, according to RFC 8032
     116             :    (we don't output non-canonical elements). */
     117             : FD_25519_INLINE uchar *
     118             : fd_f25519_tobytes( uchar               out[ 32 ],
     119      243048 :                    fd_f25519_t const * a ) {
     120      243048 :   wv_stu( out, fd_r43x6_pack( fd_r43x6_mod( a->el ) ) );
     121      243048 :   return out;
     122      243048 : }
     123             : 
     124             : /* fd_f25519_if sets r = a0 if cond, else r = a1, equivalent to:
     125             :    r = cond ? a0 : a1.
     126             :    Note: this is constant time. */
     127             : FD_25519_INLINE fd_f25519_t *
     128             : fd_f25519_if( fd_f25519_t *       r,
     129             :               int const           cond, /* 0, 1 */
     130             :               fd_f25519_t const * a0,
     131       85874 :               fd_f25519_t const * a1 ) {
     132       85874 :   r->el = fd_r43x6_if( -!!cond, a0->el, a1->el );
     133       85874 :   return r;
     134       85874 : }
     135             : 
     136             : /* fd_f25519_swap_if swaps r1, r2 if cond, else leave them as is.
     137             :    Note: this is constant time. */
     138             : FD_25519_INLINE void
     139             : fd_f25519_swap_if( fd_f25519_t * restrict r1,
     140             :                    fd_f25519_t * restrict r2,
     141           0 :                    int const              cond /* 0, 1 */ ) {
     142           0 :   wwl_t zero = wwl_zero();
     143           0 :   wwl_t m = wwl_xor(r1->el, r2->el);
     144           0 :   m  = wwl_if( -!!cond, m, zero );
     145           0 :   r1->el = wwl_xor( r1->el, m );
     146           0 :   r2->el = wwl_xor( r2->el, m );
     147           0 : }
     148             : 
     149             : /* fd_f25519_set copies r = a, and returns r. */
     150             : FD_25519_INLINE fd_f25519_t *
     151             : fd_f25519_set( fd_f25519_t * r,
     152       27564 :                fd_f25519_t const * a ) {
     153       27564 :   r->el = a->el;
     154       27564 :   return r;
     155       27564 : }
     156             : 
     157             : /* fd_f25519_is_zero returns 1 if a == 0, 0 otherwise. */
     158             : FD_25519_INLINE int
     159      146427 : fd_f25519_is_zero( fd_f25519_t const * a ) {
     160      146427 :   return ( ( wwl_eq( a->el, fd_r43x6_zero() ) & 0xFF ) == 0xFF )
     161      146427 :       || ( ( wwl_eq( a->el, fd_r43x6_p() )    & 0xFF ) == 0xFF );
     162      146427 : }
     163             : 
     164             : /*
     165             :  * Vectorized
     166             :  */
     167             : 
     168             : /* fd_f25519_muln computes r_i = a_i * b_i */
     169             : FD_25519_INLINE void
     170             : fd_f25519_mul2( fd_f25519_t * r1, fd_f25519_t const * a1, fd_f25519_t const * b1,
     171         232 :                 fd_f25519_t * r2, fd_f25519_t const * a2, fd_f25519_t const * b2 ) {
     172         232 :   FD_R43X6_MUL2_INL( r1->el, a1->el, b1->el,
     173         232 :                      r2->el, a2->el, b2->el );
     174         232 : }
     175             : 
     176             : FD_25519_INLINE void
     177             : fd_f25519_mul3( fd_f25519_t * r1, fd_f25519_t const * a1, fd_f25519_t const * b1,
     178             :                 fd_f25519_t * r2, fd_f25519_t const * a2, fd_f25519_t const * b2,
     179           0 :                 fd_f25519_t * r3, fd_f25519_t const * a3, fd_f25519_t const * b3 ) {
     180           0 :   FD_R43X6_MUL3_INL( r1->el, a1->el, b1->el,
     181           0 :                      r2->el, a2->el, b2->el,
     182           0 :                      r3->el, a3->el, b3->el );
     183           0 : }
     184             : 
     185             : FD_25519_INLINE void
     186             : fd_f25519_mul4( fd_f25519_t * r1, fd_f25519_t const * a1, fd_f25519_t const * b1,
     187             :                 fd_f25519_t * r2, fd_f25519_t const * a2, fd_f25519_t const * b2,
     188             :                 fd_f25519_t * r3, fd_f25519_t const * a3, fd_f25519_t const * b3,
     189           0 :                 fd_f25519_t * r4, fd_f25519_t const * a4, fd_f25519_t const * b4 ) {
     190           0 :   FD_R43X6_MUL4_INL( r1->el, a1->el, b1->el,
     191           0 :                      r2->el, a2->el, b2->el,
     192           0 :                      r3->el, a3->el, b3->el,
     193           0 :                      r4->el, a4->el, b4->el );
     194           0 : }
     195             : 
     196             : /* fd_f25519_sqrn computes r_i = a_i^2 */
     197             : FD_25519_INLINE void
     198             : fd_f25519_sqr2( fd_f25519_t * r1, fd_f25519_t const * a1,
     199           0 :                 fd_f25519_t * r2, fd_f25519_t const * a2 ) {
     200           0 :   FD_R43X6_SQR2_INL( r1->el, a1->el,
     201           0 :                      r2->el, a2->el );
     202           0 : }
     203             : 
     204             : FD_25519_INLINE void
     205             : fd_f25519_sqr3( fd_f25519_t * r1, fd_f25519_t const * a1,
     206             :                 fd_f25519_t * r2, fd_f25519_t const * a2,
     207           0 :                 fd_f25519_t * r3, fd_f25519_t const * a3 ) {
     208           0 :   FD_R43X6_SQR3_INL( r1->el, a1->el,
     209           0 :                      r2->el, a2->el,
     210           0 :                      r3->el, a3->el );
     211           0 : }
     212             : 
     213             : FD_25519_INLINE void
     214             : fd_f25519_sqr4( fd_f25519_t * r1, fd_f25519_t const * a1,
     215             :                 fd_f25519_t * r2, fd_f25519_t const * a2,
     216             :                 fd_f25519_t * r3, fd_f25519_t const * a3,
     217           0 :                 fd_f25519_t * r4, fd_f25519_t const * a4 ) {
     218           0 :   FD_R43X6_SQR4_INL( r1->el, a1->el,
     219           0 :                      r2->el, a2->el,
     220           0 :                      r3->el, a3->el,
     221           0 :                      r4->el, a4->el );
     222           0 : }
     223             : 
     224             : FD_PROTOTYPES_END

Generated by: LCOV version 1.14