LCOV - code coverage report
Current view: top level - ballet/ed25519 - fd_ed25519_cache.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 154 179 86.0 %
Date: 2026-12-02 04:32:07 Functions: 13 13 100.0 %

          Line data    Source code
       1             : #include "fd_ed25519.h"
       2             : #include "fd_curve25519.h"
       3             : 
       4         177 : #define FD_ED25519_CACHE_MAGIC (0xfd3d25519cac4e00UL) /* fd ed25519 cache ver 0 */
       5             : 
       6             : /* A set holds the keys of its WAY_CNT ways in 2 cache lines.  use[w]
       7             :    is the tick of the last use of way w (0 if way w is invalid). */
       8             : 
       9     2540461 : #define WAY_CNT (4UL)
      10             : 
      11             : struct __attribute__((aligned(128))) fd_ed25519_cache_set {
      12             :   uchar key[ WAY_CNT ][ 32 ];
      13             :   ulong use[ WAY_CNT ];
      14             : };
      15             : typedef struct fd_ed25519_cache_set fd_ed25519_cache_set_t;
      16             : 
      17             : struct __attribute__((aligned(FD_ED25519_CACHE_ALIGN))) fd_ed25519_cache {
      18             :   ulong magic;
      19             :   ulong set_cnt;    /* power of 2 */
      20             :   ulong filter_cnt; /* power of 2 */
      21             :   ulong seed;
      22             : 
      23             :   ulong credit; /* table builds are paid for with credits, see cache_admit */
      24             :   ulong tick;   /* incremented on every lookup */
      25             : 
      26             :   ulong hit_cnt;
      27             :   ulong miss_cnt;
      28             :   ulong insert_cnt;
      29             : 
      30             :   fd_ed25519_cache_set_t * set;    /* indexed [0,set_cnt) */
      31             :   ulong *                  filter; /* indexed [0,filter_cnt), hashes of recent successful misses */
      32             :   fd_ed25519_point_t *     tbl;    /* indexed [0,WAY_CNT*set_cnt*FD_ED25519_SPLIT_A_TBL_CNT), split tables of -A */
      33             : 
      34             :   fd_ed25519_point_t b_tbl[ FD_ED25519_SPLIT_B_TBL_CNT ];
      35             : };
      36             : 
      37             : /* The admission filter has 4 slots per entry */
      38             : 
      39         177 : #define FILTER_RATIO (4UL)
      40             : 
      41             : /* Each evaluated group equation earns a credit and a table build
      42             :    (~0.7 verify) costs BUILD_COST credits, bounding the build overhead
      43             :    to <10% even when every key is new.  Up to CREDIT_MAX credits bank
      44             :    up for bursts of new keys. */
      45             : 
      46      205306 : #define BUILD_COST (8UL)
      47      200216 : #define CREDIT_MAX (64UL*BUILD_COST)
      48             : 
      49             : FD_FN_CONST ulong
      50        3603 : fd_ed25519_cache_align( void ) {
      51        3603 :   return FD_ED25519_CACHE_ALIGN;
      52        3603 : }
      53             : 
      54             : FD_FN_CONST ulong
      55        1083 : fd_ed25519_cache_footprint( ulong ent_cnt ) {
      56        1083 :   if( FD_UNLIKELY( !fd_ulong_is_pow2( ent_cnt ) || ent_cnt<WAY_CNT || ent_cnt>(1UL<<20) ) ) return 0UL;
      57        1077 :   ulong set_cnt = ent_cnt/WAY_CNT;
      58        1077 :   ulong l = FD_LAYOUT_INIT;
      59        1077 :   l = FD_LAYOUT_APPEND( l, alignof(fd_ed25519_cache_t),     sizeof(fd_ed25519_cache_t)                              );
      60        1077 :   l = FD_LAYOUT_APPEND( l, alignof(fd_ed25519_cache_set_t), set_cnt*sizeof(fd_ed25519_cache_set_t)                  );
      61        1077 :   l = FD_LAYOUT_APPEND( l, alignof(ulong),                  FILTER_RATIO*ent_cnt*sizeof(ulong)                      );
      62        1077 :   l = FD_LAYOUT_APPEND( l, alignof(fd_ed25519_point_t),     ent_cnt*FD_ED25519_SPLIT_A_TBL_CNT*sizeof(fd_ed25519_point_t) );
      63        1077 :   return FD_LAYOUT_FINI( l, fd_ed25519_cache_align() );
      64        1083 : }
      65             : 
      66             : void *
      67             : fd_ed25519_cache_new( void * mem,
      68             :                       ulong  ent_cnt,
      69         177 :                       ulong  seed ) {
      70         177 :   if( FD_UNLIKELY( !mem ) ) {
      71           0 :     FD_LOG_WARNING(( "NULL mem" ));
      72           0 :     return NULL;
      73           0 :   }
      74         177 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, fd_ed25519_cache_align() ) ) ) {
      75           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      76           0 :     return NULL;
      77           0 :   }
      78         177 :   if( FD_UNLIKELY( !fd_ed25519_cache_footprint( ent_cnt ) ) ) {
      79           0 :     FD_LOG_WARNING(( "bad ent_cnt" ));
      80           0 :     return NULL;
      81           0 :   }
      82             : 
      83         177 :   ulong set_cnt    = ent_cnt/WAY_CNT;
      84         177 :   ulong filter_cnt = FILTER_RATIO*ent_cnt;
      85             : 
      86         177 :   FD_SCRATCH_ALLOC_INIT( l, mem );
      87         177 :   fd_ed25519_cache_t * cache = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_ed25519_cache_t),     sizeof(fd_ed25519_cache_t)                              );
      88         177 :   void *               set   = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_ed25519_cache_set_t), set_cnt*sizeof(fd_ed25519_cache_set_t)                  );
      89         177 :   ulong *              filt  = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong),                  filter_cnt*sizeof(ulong)                                );
      90         177 :   void *               tbl   = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_ed25519_point_t),     ent_cnt*FD_ED25519_SPLIT_A_TBL_CNT*sizeof(fd_ed25519_point_t) );
      91         177 :   FD_SCRATCH_ALLOC_FINI( l, fd_ed25519_cache_align() );
      92             : 
      93         177 :   cache->set_cnt    = set_cnt;
      94         177 :   cache->filter_cnt = filter_cnt;
      95         177 :   cache->seed       = seed;
      96         177 :   cache->credit     = CREDIT_MAX;
      97         177 :   cache->tick       = 0UL;
      98         177 :   cache->hit_cnt    = 0UL;
      99         177 :   cache->miss_cnt   = 0UL;
     100         177 :   cache->insert_cnt = 0UL;
     101         177 :   cache->set        = set;
     102         177 :   cache->filter     = filt;
     103         177 :   cache->tbl        = tbl;
     104             : 
     105         177 :   memset( set,  0, set_cnt*sizeof(fd_ed25519_cache_set_t) );
     106         177 :   memset( filt, 0, filter_cnt*sizeof(ulong)               );
     107             : 
     108         177 :   fd_ed25519_split_table_b( cache->b_tbl );
     109             : 
     110         177 :   FD_COMPILER_MFENCE();
     111         177 :   cache->magic = FD_ED25519_CACHE_MAGIC;
     112         177 :   FD_COMPILER_MFENCE();
     113             : 
     114         177 :   return mem;
     115         177 : }
     116             : 
     117             : fd_ed25519_cache_t *
     118         177 : fd_ed25519_cache_join( void * shcache ) {
     119         177 :   fd_ed25519_cache_t * cache = (fd_ed25519_cache_t *)shcache;
     120         177 :   if( FD_UNLIKELY( !cache ) ) {
     121           0 :     FD_LOG_WARNING(( "NULL shcache" ));
     122           0 :     return NULL;
     123           0 :   }
     124         177 :   if( FD_UNLIKELY( cache->magic!=FD_ED25519_CACHE_MAGIC ) ) {
     125           0 :     FD_LOG_WARNING(( "bad magic" ));
     126           0 :     return NULL;
     127           0 :   }
     128         177 :   return cache;
     129         177 : }
     130             : 
     131             : void *
     132          51 : fd_ed25519_cache_leave( fd_ed25519_cache_t * cache ) {
     133          51 :   return (void *)cache;
     134          51 : }
     135             : 
     136             : void *
     137          51 : fd_ed25519_cache_delete( void * shcache ) {
     138          51 :   fd_ed25519_cache_t * cache = (fd_ed25519_cache_t *)shcache;
     139          51 :   if( FD_UNLIKELY( !cache ) ) {
     140           0 :     FD_LOG_WARNING(( "NULL shcache" ));
     141           0 :     return NULL;
     142           0 :   }
     143          51 :   if( FD_UNLIKELY( cache->magic!=FD_ED25519_CACHE_MAGIC ) ) {
     144           0 :     FD_LOG_WARNING(( "bad magic" ));
     145           0 :     return NULL;
     146           0 :   }
     147          51 :   FD_COMPILER_MFENCE();
     148          51 :   cache->magic = 0UL;
     149          51 :   FD_COMPILER_MFENCE();
     150          51 :   return shcache;
     151          51 : }
     152             : 
     153       12039 : ulong fd_ed25519_cache_hit_cnt   ( fd_ed25519_cache_t const * cache ) { return cache->hit_cnt;    }
     154          27 : ulong fd_ed25519_cache_miss_cnt  ( fd_ed25519_cache_t const * cache ) { return cache->miss_cnt;   }
     155          42 : ulong fd_ed25519_cache_insert_cnt( fd_ed25519_cache_t const * cache ) { return cache->insert_cnt; }
     156             : 
     157             : /* cache_query returns the split table of -A for public_key, or NULL if
     158             :    public_key is not cached.  *_h is set to the hash of public_key. */
     159             : 
     160             : static fd_ed25519_point_t const *
     161             : cache_query( fd_ed25519_cache_t * cache,
     162             :              uchar const          public_key[ 32 ],
     163      642314 :              ulong *              _h ) {
     164      642314 :   ulong h   = fd_hash( cache->seed, public_key, 32UL );
     165      642314 :   ulong idx = h & (cache->set_cnt-1UL);
     166      642314 :   *_h = h;
     167             : 
     168      642314 :   fd_ed25519_cache_set_t * set = cache->set + idx;
     169      642314 :   ulong tick = ++cache->tick;
     170             : 
     171     2208528 :   for( ulong w=0UL; w<WAY_CNT; w++ ) {
     172     1871443 :     if( FD_LIKELY( set->use[ w ] && fd_memeq( set->key[ w ], public_key, 32UL ) ) ) {
     173      305229 :       set->use[ w ] = tick;
     174      305229 :       cache->hit_cnt++;
     175      305229 :       fd_ed25519_point_t const * tbl = cache->tbl + (WAY_CNT*idx+w)*FD_ED25519_SPLIT_A_TBL_CNT;
     176    26353741 :       for( ulong off=0UL; off<FD_ED25519_SPLIT_A_TBL_CNT*sizeof(fd_ed25519_point_t); off+=64UL ) {
     177    26048512 :         __builtin_prefetch( (uchar const *)tbl + off, 0, 3 );
     178    26048512 :       }
     179      305229 :       return tbl;
     180      305229 :     }
     181     1871443 :   }
     182      337085 :   cache->miss_cnt++;
     183      337085 :   return NULL;
     184      642314 : }
     185             : 
     186             : /* cache_admit is called after a signature by public_key (hash h, not
     187             :    cached) verified successfully, with neg_a the decoded point -A.  The
     188             :    key is inserted if it also verified successfully recently.  Only
     189             :    keys that decode to a valid, not small order point can verify
     190             :    successfully, and building a table requires a valid signature, so
     191             :    junk traffic can't force table builds. */
     192             : 
     193             : static void
     194             : cache_admit( fd_ed25519_cache_t *       cache,
     195             :              uchar const                public_key[ 32 ],
     196             :              ulong                      h,
     197       14786 :              fd_ed25519_point_t const * neg_a ) {
     198             : 
     199       14786 :   ulong * slot = cache->filter + (fd_ulong_hash( h ) & (cache->filter_cnt-1UL));
     200       14786 :   if( FD_LIKELY( *slot!=h ) ) {
     201        7635 :     *slot = h;
     202        7635 :     return;
     203        7635 :   }
     204        7151 :   if( FD_UNLIKELY( cache->credit<BUILD_COST ) ) return;
     205        5090 :   cache->credit -= BUILD_COST;
     206        5090 :   *slot = 0UL;
     207             : 
     208             :   /* Replace the least recently used way (invalid ways have use 0) */
     209             : 
     210        5090 :   ulong idx = h & (cache->set_cnt-1UL);
     211        5090 :   fd_ed25519_cache_set_t * set = cache->set + idx;
     212        5090 :   ulong w = 0UL;
     213       20360 :   for( ulong v=1UL; v<WAY_CNT; v++ ) w = fd_ulong_if( set->use[ v ]<set->use[ w ], v, w );
     214             : 
     215        5090 :   fd_ed25519_split_table_a( cache->tbl + (WAY_CNT*idx+w)*FD_ED25519_SPLIT_A_TBL_CNT, neg_a );
     216        5090 :   memcpy( set->key[ w ], public_key, 32UL );
     217        5090 :   set->use[ w ] = ++cache->tick;
     218        5090 :   cache->insert_cnt++;
     219        5090 : }
     220             : 
     221             : /* verify_one does the checks of fd_ed25519_verify on one signature, in
     222             :    the same order and with the same error codes, and returns the first
     223             :    failing check's error code.  If all checks pass, it returns
     224             :    FD_ED25519_SUCCESS and, if *_ok is non-zero on entry, sets *_ok to
     225             :    whether the group equation holds (if *_ok is zero on entry, the
     226             :    group equation is not evaluated). */
     227             : 
     228             : static int
     229             : verify_one( uchar const          msg[],
     230             :             ulong                msg_sz,
     231             :             uchar const          sig[ 64 ],
     232             :             uchar const          public_key[ 32 ],
     233             :             fd_sha512_t *        sha,
     234             :             fd_ed25519_cache_t * cache,
     235      662482 :             int *                _ok ) {
     236             : 
     237      662482 :   uchar const * r = sig;
     238      662482 :   uchar const * S = sig + 32;
     239             : 
     240      662482 :   if( FD_UNLIKELY( !fd_curve25519_scalar_validate( S ) ) ) return FD_ED25519_ERR_SIG;
     241             : 
     242      642314 :   ulong h;
     243      642314 :   fd_ed25519_point_t const * a_tbl = cache_query( cache, public_key, &h );
     244             : 
     245      642314 :   fd_ed25519_point_t Aprime[1], R[1];
     246      642314 :   if( FD_LIKELY( a_tbl ) ) {
     247             : 
     248             :     /* A is cached, so it decodes and is not small order: the checks of
     249             :        the uncached path reduce to the ones on R.  frombytes_1x gives
     250             :        the same point as frombytes_2x, so the small order checks
     251             :        agree.  When the equation is evaluated, R's decode is
     252             :        interleaved with the scalar mul and its checks run afterwards,
     253             :        in the same order. */
     254             : 
     255      305229 :     if( FD_UNLIKELY( !*_ok ) ) {
     256           0 :       if( FD_UNLIKELY( fd_ed25519_point_frombytes_1x( R, r ) ) ) return FD_ED25519_ERR_SIG;
     257           0 :       if( FD_UNLIKELY( fd_ed25519_affine_is_small_order( R ) ) ) return FD_ED25519_ERR_SIG;
     258           0 :       return FD_ED25519_SUCCESS;
     259           0 :     }
     260             : 
     261      305229 :     fd_ed25519_point_decode_t dec[1];
     262      305229 :     fd_ed25519_point_decode_init( dec, r );
     263             : 
     264      305229 :     uchar k[ 64 ];
     265      305229 :     fd_sha512_fini( fd_sha512_append( fd_sha512_append( fd_sha512_append( fd_sha512_init( sha ),
     266      305229 :                     r, 32UL ), public_key, 32UL ), msg, msg_sz ), k );
     267      305229 :     fd_curve25519_scalar_reduce( k, k );
     268             : 
     269             :     /* Rcmp = [k](-A') + [S]B */
     270             : 
     271      305229 :     fd_ed25519_point_t Rcmp[1];
     272      305229 :     fd_ed25519_double_scalar_mul_base_split_decode( Rcmp, k, a_tbl, S, cache->b_tbl, dec );
     273             : 
     274      305229 :     if( FD_UNLIKELY( fd_ed25519_point_decode_fini( R, dec ) ) ) return FD_ED25519_ERR_SIG;
     275      285864 :     if( FD_UNLIKELY( fd_ed25519_affine_is_small_order( R ) ) ) return FD_ED25519_ERR_SIG;
     276             : 
     277      141759 :     cache->credit = fd_ulong_min( cache->credit+1UL, CREDIT_MAX );
     278      141759 :     *_ok = fd_ed25519_point_eq_z1( Rcmp, R );
     279      141759 :     return FD_ED25519_SUCCESS;
     280      285864 :   }
     281             : 
     282      337085 :   int res = fd_ed25519_point_frombytes_2x( Aprime, public_key, R, r );
     283      337085 :   if( FD_UNLIKELY( res ) ) return res == -1 ? FD_ED25519_ERR_PUBKEY : FD_ED25519_ERR_SIG;
     284      318422 :   if( FD_UNLIKELY( fd_ed25519_affine_is_small_order( Aprime ) ) ) return FD_ED25519_ERR_PUBKEY;
     285       60839 :   if( FD_UNLIKELY( fd_ed25519_affine_is_small_order( R      ) ) ) return FD_ED25519_ERR_SIG;
     286             : 
     287       58280 :   if( FD_UNLIKELY( !*_ok ) ) return FD_ED25519_SUCCESS;
     288             : 
     289       58280 :   cache->credit = fd_ulong_min( cache->credit+1UL, CREDIT_MAX );
     290             : 
     291       58280 :   uchar k[ 64 ];
     292       58280 :   fd_sha512_fini( fd_sha512_append( fd_sha512_append( fd_sha512_append( fd_sha512_init( sha ),
     293       58280 :                   r, 32UL ), public_key, 32UL ), msg, msg_sz ), k );
     294       58280 :   fd_curve25519_scalar_reduce( k, k );
     295             : 
     296             :   /* Rcmp = [k](-A') + [S]B */
     297             : 
     298       58280 :   fd_ed25519_point_t Rcmp[1];
     299       58280 :   fd_ed25519_point_neg( Aprime, Aprime );
     300       58280 :   fd_ed25519_double_scalar_mul_base( Rcmp, k, Aprime, S );
     301       58280 :   *_ok = fd_ed25519_point_eq_z1( Rcmp, R );
     302       58280 :   if( FD_LIKELY( *_ok ) ) cache_admit( cache, public_key, h, Aprime );
     303       58280 :   return FD_ED25519_SUCCESS;
     304       58280 : }
     305             : 
     306             : int
     307             : fd_ed25519_verify_cached( uchar const          msg[], /* msg_sz */
     308             :                           ulong                msg_sz,
     309             :                           uchar const          sig[ 64 ],
     310             :                           uchar const          public_key[ 32 ],
     311             :                           fd_sha512_t *        sha,
     312      662482 :                           fd_ed25519_cache_t * cache ) {
     313      662482 :   int ok  = 1;
     314      662482 :   int err = verify_one( msg, msg_sz, sig, public_key, sha, cache, &ok );
     315      662482 :   if( FD_UNLIKELY( err ) ) return err;
     316      200039 :   return FD_LIKELY( ok ) ? FD_ED25519_SUCCESS : FD_ED25519_ERR_MSG;
     317      662482 : }

Generated by: LCOV version 1.14