LCOV - code coverage report
Current view: top level - ballet/ed25519 - fd_ed25519_user.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 126 136 92.6 %
Date: 2026-12-02 04:32:07 Functions: 7 8 87.5 %

          Line data    Source code
       1             : #include "fd_ed25519.h"
       2             : #include "fd_curve25519.h"
       3             : 
       4             : /* Backend choice: AVX512 (zen4+), then S2N, then REF (no s2n) */
       5             : #if defined(FD_HAS_S2NBIGNUM) && !FD_HAS_AVX512
       6             : #define FD_ED25519_S2N 1
       7             : #else
       8             : #define FD_ED25519_S2N 0
       9             : #endif
      10             : 
      11             : #if FD_ED25519_S2N
      12             : #include "fd_ed25519_s2n.c"
      13             : #else
      14             : 
      15             : /* The 2 functions below:
      16             :    - fd_ed25519_scalar_mul_base_tobytes (sensitive, const time)
      17             :    - fd_ed25519_verify_equation (var time)
      18             :    are a convenient interface to switch between our original
      19             :    implementations and s2n-bignum interface. */
      20             : 
      21             : /* fd_ed25519_scalar_mul_base_tobytes writes the RFC 8032 encoding of
      22             :    [n]B to out.  n can be a secret. */
      23             : static inline void FD_FN_SENSITIVE
      24             : fd_ed25519_scalar_mul_base_tobytes( uchar       out[ 32 ],
      25       79741 :                                     uchar const n[ 32 ] ) {
      26       79741 :   fd_ed25519_point_t nB[1];
      27       79741 :   fd_ed25519_scalar_mul_base_const_time( nB, n );
      28       79741 :   fd_ed25519_point_tobytes( out, nB );
      29       79741 : }
      30             : 
      31             : /* fd_ed25519_verify_equation returns 1 iff [S]B == R + [k]A,
      32             :    computed as [k](-A) + [S]B == R.  A and R must be affine (Z==1).
      33             :    A is clobbered.  k and S are 32-byte little-endian scalars, S < L. */
      34             : static inline int
      35             : fd_ed25519_verify_equation( uchar const                k[ 32 ],
      36             :                             fd_ed25519_point_t *       A,
      37             :                             uchar const                S[ 32 ],
      38      631588 :                             fd_ed25519_point_t const * R ) {
      39      631588 :   fd_ed25519_point_t Rcmp[1];
      40      631588 :   fd_ed25519_point_neg( A, A );
      41      631588 :   fd_ed25519_double_scalar_mul_base( Rcmp, k, A, S );
      42      631588 :   return fd_ed25519_point_eq_z1( Rcmp, R );
      43      631588 : }
      44             : 
      45             : #endif
      46             : 
      47             : uchar * FD_FN_SENSITIVE
      48             : fd_ed25519_public_from_private( uchar         public_key [ static 32 ],
      49             :                                 uchar const   private_key[ static 32 ],
      50       36378 :                                 fd_sha512_t * sha ) {
      51             : 
      52             :   //  RFC 8032 - Edwards-Curve Digital Signature Algorithm (EdDSA)
      53             :   //
      54             :   //  5.1.5.  Key Generation
      55             :   //
      56             :   //  The private key is 32 octets (256 bits, corresponding to b) of
      57             :   //  cryptographically secure random data.  See [RFC4086] for a discussion
      58             :   //  about randomness.
      59             :   //
      60             :   //  The 32-byte public key is generated by the following steps.
      61             :   //
      62             :   //  1.  Hash the 32-byte private key using SHA-512, storing the digest in
      63             :   //      a 64-octet large buffer, denoted h.  Only the lower 32 bytes are
      64             :   //      used for generating the public key.
      65             : 
      66       36378 :   uchar s[ FD_SHA512_HASH_SZ ];
      67       36378 :   fd_sha512_fini( fd_sha512_append( fd_sha512_init( sha ), private_key, 32UL ), s );
      68             : 
      69             :   //  2.  Prune the buffer: The lowest three bits of the first octet are
      70             :   //      cleared, the highest bit of the last octet is cleared, and the
      71             :   //      second highest bit of the last octet is set.
      72             : 
      73       36378 :   s[ 0] &= (uchar)0xF8;
      74       36378 :   s[31] &= (uchar)0x7F;
      75       36378 :   s[31] |= (uchar)0x40;
      76             : 
      77             :   //  3.  Interpret the buffer as the little-endian integer, forming a
      78             :   //      secret scalar s.  Perform a fixed-base scalar multiplication
      79             :   //      [s]B.
      80             : 
      81             :   //  4.  The public key A is the encoding of the point [s]B.  First,
      82             :   //      encode the y-coordinate (in the range 0 <= y < p) as a little-
      83             :   //      endian string of 32 octets.  The most significant bit of the
      84             :   //      final octet is always zero.  To form the encoding of the point
      85             :   //      [s]B, copy the least significant bit of the x coordinate to the
      86             :   //      most significant bit of the final octet.  The result is the
      87             :   //      public key.
      88             : 
      89       36378 :   fd_ed25519_scalar_mul_base_tobytes( public_key, s );
      90             : 
      91             :   /* Sanitize */
      92             : 
      93       36378 :   fd_memzero_explicit( s, FD_SHA512_HASH_SZ );
      94       36378 :   fd_sha512_clear( sha );
      95             : 
      96       36378 :   return public_key;
      97       36378 : }
      98             : 
      99             : uchar * FD_FN_SENSITIVE
     100             : fd_ed25519_sign( uchar         sig[ static 64 ],
     101             :                  uchar const   msg[], /* msg_sz */
     102             :                  ulong         msg_sz,
     103             :                  uchar const   public_key[ static 32 ],
     104             :                  uchar const   private_key[ static 32 ],
     105      202845 :                  fd_sha512_t * sha ) {
     106             : 
     107             :   //  RFC 8032 - Edwards-Curve Digital Signature Algorithm (EdDSA)
     108             :   //
     109             :   //  5.1.6.  Sign
     110             :   //
     111             :   //  The inputs to the signing procedure is the private key, a 32-octet
     112             :   //  string, and a message M of arbitrary size.  For Ed25519ctx and
     113             :   //  Ed25519ph, there is additionally a context C of at most 255 octets
     114             :   //  and a flag F, 0 for Ed25519ctx and 1 for Ed25519ph.
     115             :   //
     116             :   //  1.  Hash the private key, 32 octets, using SHA-512.  Let h denote the
     117             :   //      resulting digest.  Construct the secret scalar s from the first
     118             :   //      half of the digest, and the corresponding public key A, as
     119             :   //      described in the previous section.  Let prefix denote the second
     120             :   //      half of the hash digest, h[32],...,h[63].
     121             : 
     122      202845 :   uchar s[ FD_SHA512_HASH_SZ ];
     123      202845 :   fd_sha512_fini( fd_sha512_append( fd_sha512_init( sha ), private_key, 32UL ), s );
     124      202845 :   s[ 0] &= (uchar)0xF8;
     125      202845 :   s[31] &= (uchar)0x7F;
     126      202845 :   s[31] |= (uchar)0x40;
     127      202845 :   uchar * h = s + 32;
     128             : 
     129             :   /* public_key is an input */
     130             : 
     131             :   //  2.  Compute SHA-512(dom2(F, C) || prefix || PH(M)), where M is the
     132             :   //      message to be signed.  Interpret the 64-octet digest as a little-
     133             :   //      endian integer r.
     134             : 
     135      202845 :   uchar r[ FD_SHA512_HASH_SZ ];
     136      202845 :   fd_sha512_fini( fd_sha512_append( fd_sha512_append( fd_sha512_init( sha ), h, 32UL ), msg, msg_sz ), r );
     137             : 
     138             :   //  3.  Compute the point [r]B.  For efficiency, do this by first
     139             :   //      reducing r modulo L, the group order of B.  Let the string R be
     140             :   //      the encoding of this point.
     141             : 
     142      202845 :   fd_curve25519_scalar_reduce( r, r );           /* reduce r mod L */
     143      202845 :   fd_ed25519_scalar_mul_base_tobytes( sig, r );  /* R = [r]B */
     144             : 
     145             :   //  4.  Compute SHA512(dom2(F, C) || R || A || PH(M)), and interpret the
     146             :   //      64-octet digest as a little-endian integer k.
     147             : 
     148             :   /* note: all inputs to k are public values */
     149      202845 :   uchar k[ FD_SHA512_HASH_SZ ];
     150      202845 :   fd_sha512_fini( fd_sha512_append( fd_sha512_append( fd_sha512_append( fd_sha512_init( sha ),
     151      202845 :                   sig, 32UL ), public_key, 32UL ), msg, msg_sz ), k );
     152             : 
     153             :   //  5.  Compute S = (r + k * s) mod L.  For efficiency, again reduce k
     154             :   //      modulo L first.
     155             :   //
     156             :   //  6.  Form the signature of the concatenation of R (32 octets) and the
     157             :   //      little-endian encoding of S (32 octets; the three most
     158             :   //      significant bits of the final octet are always zero).
     159             : 
     160      202845 :   fd_curve25519_scalar_reduce( k, k );
     161      202845 :   fd_curve25519_scalar_muladd( ((uchar *)sig)+32, k, s, r );
     162             : 
     163             :   /* Sanitize */
     164             : 
     165             :   /* note: no need to sanitize k as all inputs to k are public values */
     166      202845 :   fd_memzero_explicit( s, FD_SHA512_HASH_SZ );
     167      202845 :   fd_memzero_explicit( r, FD_SHA512_HASH_SZ );
     168      202845 :   fd_sha512_clear( sha );
     169             : 
     170      202845 :   return sig;
     171      202845 : }
     172             : 
     173             : uchar * FD_FN_SENSITIVE
     174             : fd_ed25519_sign_batch8( uchar               sig[],        /* n*64 */
     175             :                         uchar const * const msg[],        /* n */
     176             :                         ulong const         msg_sz[],     /* n */
     177             :                         uchar const         public_key[ 32 ],
     178             :                         uchar const         private_key[ 32 ],
     179             :                         fd_sha512_t *       shas[],       /* n */
     180          48 :                         ulong               n ) {         /* in [1,8] */
     181             : 
     182          48 :   FD_TEST( 0UL<n && n<=8UL );
     183          48 :   if( FD_UNLIKELY( n==1UL ) ) {
     184           6 :     return fd_ed25519_sign( sig, msg[0], msg_sz[0], public_key, private_key, shas[0] );
     185           6 :   }
     186             : 
     187         252 :   for( ulong i=0UL; i<n; i++ ) FD_TEST( msg_sz[i]<=FD_ED25519_SIGN_BATCH_MSG_MAX );
     188             : 
     189          42 :   fd_sha512_batch_t batch[1];
     190          42 :   fd_sha512_batch_t * b;
     191             : 
     192             :   /* Expand the private key: s = SHA-512(private_key), clamped.  Lower
     193             :      32 bytes are the secret scalar, upper 32 the prefix. */
     194             : 
     195          42 :   uchar s[ FD_SHA512_HASH_SZ ] __attribute__((aligned(64)));
     196          42 :   fd_sha512_fini( fd_sha512_append( fd_sha512_init( shas[0] ), private_key, 32UL ), s );
     197          42 :   fd_sha512_clear( shas[0] );
     198          42 :   s[ 0] &= (uchar)0xF8;
     199          42 :   s[31] &= (uchar)0x7F;
     200          42 :   s[31] |= (uchar)0x40;
     201             : 
     202             :   /* r[i] = SHA-512(prefix || msg[i]).  The batch API hashes one
     203             :      contiguous region per message, so the inputs are staged in buf,
     204             :      laid out as R[i] || A[i] || msg[i]: the message is parked once at
     205             :      buf[i]+64 for both hashes, the r hash reads from buf[i]+32 with
     206             :      the secret prefix in the A slot, and the k hash below overwrites
     207             :      the prefix with the public A. */
     208             : 
     209          42 :   static FD_TL uchar buf[ 8UL ][ 64UL+FD_ED25519_SIGN_BATCH_MSG_MAX ] __attribute__((aligned(64)));
     210          42 :   uchar r[ 8UL ][ FD_SHA512_HASH_SZ ] __attribute__((aligned(64)));
     211          42 :   b = fd_sha512_batch_init( batch );
     212         252 :   for( ulong i=0UL; i<n; i++ ) {
     213         210 :     memcpy( buf[i]+32UL, s+32, 32UL );
     214         210 :     if( FD_LIKELY( msg_sz[i] ) ) memcpy( buf[i]+64UL, msg[i], msg_sz[i] );
     215         210 :     b = fd_sha512_batch_add( b, buf[i]+32UL, 32UL+msg_sz[i], r[i] );
     216         210 :   }
     217          42 :   fd_sha512_batch_fini( b );
     218             : 
     219             :   /* R[i] = [r[i] mod L]B */
     220             : 
     221          42 :   uchar R_bytes[ 8UL*32UL ];
     222          28 : #if FD_ED25519_S2N
     223             :   /* s2n serial is faster than non-s2n batch */
     224         168 :   for( ulong i=0UL; i<n; i++ ) {
     225         140 :     fd_curve25519_scalar_reduce( r[i], r[i] );
     226         140 :     fd_ed25519_scalar_mul_base_tobytes( R_bytes+32UL*i, r[i] );
     227         140 :   }
     228             : #else
     229             :   /* one field inversion shared across the n encodings */
     230          14 :   fd_ed25519_point_t R[ 8UL ];
     231          84 :   for( ulong i=0UL; i<n; i++ ) {
     232          70 :     fd_curve25519_scalar_reduce( r[i], r[i] );
     233          70 :     fd_ed25519_scalar_mul_base_const_time( &R[i], r[i] );
     234          70 :   }
     235          14 :   fd_ed25519_point_tobytes_batch8( R_bytes, R, n );
     236          14 : #endif
     237             : 
     238             :   /* k[i] = SHA-512(R[i] || A[i] || msg[i]); all inputs are public */
     239             : 
     240          42 :   uchar k[ 8UL ][ FD_SHA512_HASH_SZ ] __attribute__((aligned(64)));
     241          42 :   b = fd_sha512_batch_init( batch );
     242         252 :   for( ulong i=0UL; i<n; i++ ) {
     243         210 :     memcpy( sig+64UL*i,  R_bytes+32UL*i,    32UL );
     244         210 :     memcpy( buf[i],      R_bytes+32UL*i,    32UL );
     245         210 :     memcpy( buf[i]+32UL, public_key, 32UL );
     246         210 :     b = fd_sha512_batch_add( b, buf[i], 64UL+msg_sz[i], k[i] );
     247         210 :   }
     248          42 :   fd_sha512_batch_fini( b );
     249             : 
     250             :   /* S[i] = (r[i] + k[i]*s[i]) mod L */
     251             : 
     252         252 :   for( ulong i=0UL; i<n; i++ ) {
     253         210 :     fd_curve25519_scalar_reduce( k[i], k[i] );
     254         210 :     fd_curve25519_scalar_muladd( sig+64UL*i+32UL, k[i], s, r[i] );
     255         210 :   }
     256             : 
     257             :   /* Sanitize */
     258             : 
     259          42 :   fd_memzero_explicit( s, FD_SHA512_HASH_SZ );
     260          42 :   fd_memzero_explicit( r, n*FD_SHA512_HASH_SZ );
     261             : 
     262          42 :   return sig;
     263          42 : }
     264             : 
     265             : int
     266             : fd_ed25519_verify( uchar const   msg[], /* msg_sz */
     267             :                    ulong         msg_sz,
     268             :                    uchar const   sig[ static 64 ],
     269             :                    uchar const   public_key[ static 32 ],
     270     2860533 :                    fd_sha512_t * sha ) {
     271             : 
     272             :   //  RFC 8032 - Edwards-Curve Digital Signature Algorithm (EdDSA)
     273             :   //
     274             :   //  5.1.7.  Verify
     275             :   //
     276             :   //  1.  To verify a signature on a message M using public key A, with F
     277             :   //      being 0 for Ed25519ctx, 1 for Ed25519ph, and if Ed25519ctx or
     278             :   //      Ed25519ph is being used, C being the context, first split the
     279             :   //      signature into two 32-octet halves.  Decode the first half as a
     280             :   //      point R, and the second half as an integer S, in the range
     281             :   //      0 <= s < L.  Decode the public key A as point A'.  If any of the
     282             :   //      decodings fail (including S being out of range), the signature is
     283             :   //      invalid.
     284             : 
     285     2860533 :   uchar const * r = sig;
     286     2860533 :   uchar const * S = sig + 32;
     287             : 
     288             :   /* Check scalar s */
     289     2860533 :   if( FD_UNLIKELY( !fd_curve25519_scalar_validate( S ) )) {
     290       38453 :     return FD_ED25519_ERR_SIG;
     291       38453 :   }
     292             : 
     293             :   /* Decompress public_key and point r, concurrently */
     294     2822080 :   fd_ed25519_point_t Aprime[1], R[1];
     295     2822080 :   int res = fd_ed25519_point_frombytes_2x( Aprime, public_key,   R, r );
     296             : 
     297             :   /* Check public key and point r:
     298             :      1. both public key and point r decompress successfully (RFC)
     299             :      2. neither public key nor point r is small order (verify_strict)
     300             : 
     301             :      We do not enforce canonical encodings of the public key or r.
     302             : 
     303             :      The following code would additionally check that r is canonical.
     304             : 
     305             :         uchar compressed[ 32 ];
     306             :         fd_ed25519_affine_tobytes( compressed, R );
     307             :         if( FD_UNLIKELY( !fd_memeq( compressed, r, 32 ) ) ) {
     308             :           return FD_ED25519_ERR_SIG;
     309             :         }
     310             :     */
     311     2822080 :   if( FD_UNLIKELY( res ) ) {
     312       42510 :     return res == -1 ? FD_ED25519_ERR_PUBKEY : FD_ED25519_ERR_SIG;
     313       42510 :   }
     314     2779570 :   if( FD_UNLIKELY( fd_ed25519_affine_is_small_order(Aprime) ) ) {
     315      571989 :     return FD_ED25519_ERR_PUBKEY;
     316      571989 :   }
     317     2207581 :   if( FD_UNLIKELY( fd_ed25519_affine_is_small_order(R) ) ) {
     318      312921 :     return FD_ED25519_ERR_SIG;
     319      312921 :   }
     320             : 
     321             :   //  2.  Compute SHA512(dom2(F, C) || R || A || PH(M)), and interpret the
     322             :   //      64-octet digest as a little-endian integer k.
     323             : 
     324     1894660 :   uchar k[ 64 ];
     325     1894660 :   fd_sha512_fini( fd_sha512_append( fd_sha512_append( fd_sha512_append( fd_sha512_init( sha ),
     326     1894660 :                   r, 32UL ), public_key, 32UL ), msg, msg_sz ), k );
     327     1894660 :   fd_curve25519_scalar_reduce( k, k );
     328             : 
     329             :   //  3.  Check the group equation [8][S]B = [8]R + [8][k]A'.  It's
     330             :   //      sufficient, but not required, to instead check [S]B = R + [k]A'.
     331             : 
     332             :   /* Compute R = [k](-A') + [S]B, with B base point.
     333             :      Note: this is not the same as R = [-k]A' + [S]B, because the order
     334             :      of A' is 8l (computing -k mod 8l would work).
     335             : 
     336             :      Compare R (computed) and R from signature.
     337             :      Note: many implementations do this comparison by compressing Rcmd,
     338             :      and compare it against the r buf as it appears in the signature.
     339             :      This implicitly prevents non-canonical R.
     340             :      In our implementation we compare the points (see the comment
     341             :      above on "Check public key and point r" for details). */
     342     1894660 :   if( FD_LIKELY( fd_ed25519_verify_equation( k, Aprime, S, R ) ) ) {
     343     1746584 :     return FD_ED25519_SUCCESS;
     344     1746584 :   }
     345      148076 :   return FD_ED25519_ERR_MSG;
     346     1894660 : }
     347             : 
     348             : void
     349             : fd_ed25519_verify_batch8( uchar const * const msgs[],        /* n */
     350             :                           ulong const         msg_szs[],     /* n */
     351             :                           uchar const * const sigs[],        /* n */
     352             :                           uchar const * const public_keys[], /* n */
     353             :                           fd_sha512_t *       shas[],        /* n */
     354             :                           int                 results[],     /* n */
     355      132990 :                           ulong               n ) {
     356      132990 :   FD_TEST( 0UL<n && n<=8UL );
     357     1019337 :   for( ulong i=0UL; i<n; i++ ) results[i] = fd_ed25519_verify( msgs[i], msg_szs[i], sigs[i], public_keys[i], shas[i] );
     358      132990 : }
     359             : 
     360             : char const *
     361           0 : fd_ed25519_strerror( int err ) {
     362           0 :   switch( err ) {
     363           0 :   case FD_ED25519_SUCCESS:    return "success";
     364           0 :   case FD_ED25519_ERR_SIG:    return "bad signature";
     365           0 :   case FD_ED25519_ERR_PUBKEY: return "bad public key";
     366           0 :   case FD_ED25519_ERR_MSG:    return "bad message";
     367           0 :   default: break;
     368           0 :   }
     369           0 :   return "unknown";
     370           0 : }

Generated by: LCOV version 1.14