LCOV - code coverage report
Current view: top level - ballet/ed25519 - fd_ed25519.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 6 6 100.0 %
Date: 2026-12-02 04:32:07 Functions: 0 0 -

          Line data    Source code
       1             : #ifndef HEADER_fd_src_ballet_ed25519_fd_ed25519_h
       2             : #define HEADER_fd_src_ballet_ed25519_fd_ed25519_h
       3             : 
       4             : /* fd_ed25519 provides APIs for ED25519 signature computations */
       5             : 
       6             : #include "../sha512/fd_sha512.h"
       7             : 
       8             : /* FD_ED25519_ERR_* gives a number of error codes used by fd_ed25519
       9             :    APIs. */
      10             : 
      11     2055945 : #define FD_ED25519_SUCCESS    ( 0) /* Operation was successful */
      12      573057 : #define FD_ED25519_ERR_SIG    (-1) /* Operation failed because the signature was obviously invalid */
      13      855259 : #define FD_ED25519_ERR_PUBKEY (-2) /* Operation failed because the public key was obviously invalid */
      14      257699 : #define FD_ED25519_ERR_MSG    (-3) /* Operation failed because the message didn't match the signature for the given key */
      15             : 
      16             : /* FD_ED25519_SIG_SZ: the size of an Ed25519 signature in bytes. */
      17       46961 : #define FD_ED25519_SIG_SZ (64UL)
      18             : 
      19             : /* An Ed25519 signature. */
      20             : typedef uchar fd_ed25519_sig_t[ FD_ED25519_SIG_SZ ];
      21             : 
      22             : FD_PROTOTYPES_BEGIN
      23             : 
      24             : /* fd_ed25519_public_from_private computes the public_key corresponding
      25             :    to the given private key.
      26             : 
      27             :    public_key is assumed to point to the first byte of a 32-byte memory
      28             :    region which will hold the public key on return.
      29             : 
      30             :    private_key assumed to point to first byte of a 32-byte memory region
      31             :    private key for which the public key is desired.
      32             : 
      33             :    sha is a handle of a local join to a sha512 calculator.
      34             : 
      35             :    Does no input argument checking.  The caller takes a write interest
      36             :    in public_key and sha and a read interest in public_key for the
      37             :    duration the call.  Sanitizes the sha and stack to minimize risk of
      38             :    leaking private key info before returning.  Returns public_key. */
      39             : 
      40             : uchar * FD_FN_SENSITIVE
      41             : fd_ed25519_public_from_private( uchar         public_key [ 32 ],
      42             :                                 uchar const   private_key[ 32 ],
      43             :                                 fd_sha512_t * sha );
      44             : 
      45             : /* fd_ed25519_sign signs a message according to the ED25519 standard.
      46             : 
      47             :    sig is assumed to point to the first byte of a 64-byte memory region
      48             :    which will hold the signature on return.
      49             : 
      50             :    msg is assumed to point to the first byte of a sz byte memory region
      51             :    which holds the message to sign (sz==0 fine, msg==NULL fine if
      52             :    sz==0).
      53             : 
      54             :    public_key is assumed to point to first byte of a 32-byte memory
      55             :    region that holds the public key to use to sign this message.
      56             : 
      57             :    private_key is assumed to point to first byte of a 32-byte memory
      58             :    region that holds the private key to use to sign this message.
      59             : 
      60             :    sha is a handle of a local join to a sha512 calculator.
      61             : 
      62             :    Does no input argument checking.  Sanitizes the sha and stack to
      63             :    minimize risk of leaking private key info after return.  The caller
      64             :    takes a write interest in sig and sha and a read interest in msg,
      65             :    public_key and private_key for the duration the call.  Returns sig. */
      66             : 
      67             : uchar * FD_FN_SENSITIVE
      68             : fd_ed25519_sign( uchar         sig[ 64 ],
      69             :                  uchar const   msg[], /* msg_sz */
      70             :                  ulong         msg_sz,
      71             :                  uchar const   public_key[ 32 ],
      72             :                  uchar const   private_key[ 32 ],
      73             :                  fd_sha512_t * sha );
      74             : 
      75             : /* FD_ED25519_SIGN_BATCH_MSG_MAX is the largest supported per-message
      76             :    size for the batched signing path below (covers the largest messages
      77             :    the validator signs; shred and transaction MTUs are ~1.2 KiB). */
      78             : 
      79             : #define FD_ED25519_SIGN_BATCH_MSG_MAX (2048UL)
      80             : 
      81             : /* fd_ed25519_sign_batch8 signs n independent messages, n in [1,8]
      82             :    (asserted).  Each signature is bit-identical to
      83             :    fd_ed25519_sign of the same (msg, key) pair, all signed with the
      84             :    single (public_key, private_key) identity.  For n>=2 the
      85             :    signatures share batched SHA-512 computations and a single field
      86             :    inversion for the point compressions, so per-signature cost is lower
      87             :    than fd_ed25519_sign.
      88             : 
      89             :    sig is assumed to point to the first byte of an n*64-byte memory
      90             :    region which will hold the n signatures on return (signature i at
      91             :    sig+64*i).
      92             : 
      93             :    msg[i] is assumed to point to the first byte of a msg_sz[i] byte
      94             :    memory region which holds message i (msg_sz[i]==0 fine, msg[i]==NULL
      95             :    fine if msg_sz[i]==0, msg_sz[i] at most
      96             :    FD_ED25519_SIGN_BATCH_MSG_MAX).
      97             : 
      98             :    public_key and private_key are each a single 32-byte key, shared by
      99             :    all n messages (this is a shared-identity batch signer).
     100             : 
     101             :    shas[i], i in [0,n), are handles of local joins to sha512
     102             :    calculators; shas[0] is scratch (sanitized on return), the others
     103             :    are left untouched.
     104             : 
     105             :    Sanitizes internal state to minimize risk of leaking private key
     106             :    info after return.  The caller takes a write interest in sig and
     107             :    shas[0] and a read interest in the messages and keys for the
     108             :    duration the call.  Returns sig. */
     109             : 
     110             : uchar * FD_FN_SENSITIVE
     111             : fd_ed25519_sign_batch8( uchar               sig[],        /* n*64 */
     112             :                         uchar const * const msg[],        /* n */
     113             :                         ulong const         msg_sz[],     /* n */
     114             :                         uchar const         public_key[ 32 ],
     115             :                         uchar const         private_key[ 32 ],
     116             :                         fd_sha512_t *       shas[],       /* n */
     117             :                         ulong               n );
     118             : 
     119             : /* fd_ed25519_verify verifies message according to the ED25519 standard.
     120             : 
     121             :    msg is assumed to point to the first byte of a sz byte memory region
     122             :    which holds the message to verify (sz==0 fine, msg==NULL fine if
     123             :    sz==0).
     124             : 
     125             :    sig is assumed to point to the first byte of a 64 byte memory region
     126             :    which holds the signature of the message.
     127             : 
     128             :    public_key is assumed to point to first byte of a 32-byte memory
     129             :    region that holds the public key to use to verify this message.
     130             : 
     131             :    sha is a handle of a local join to a sha512 calculator.
     132             : 
     133             :    Does no input argument checking.  This function takes a write
     134             :    interest in sha and a read interest in msg, sig and public_key for
     135             :    the duration of the call.  Returns
     136             :    FD_ED25519_SUCCESS (0) if the message verified successfully or a
     137             :    FD_ED25519_ERR_* code indicating the failure reason otherwise. */
     138             : 
     139             : int
     140             : fd_ed25519_verify( uchar const   msg[], /* msg_sz */
     141             :                    ulong         msg_sz,
     142             :                    uchar const   sig[ 64 ],
     143             :                    uchar const   public_key[ 32 ],
     144             :                    fd_sha512_t * sha );
     145             : 
     146             : /* fd_ed25519_verify_batch8 verifies n independent signatures, n in
     147             :    [1,8] (asserted).  results[i] is exactly what fd_ed25519_verify
     148             :    returns for (msgs[i], msg_szs[i], sigs[i], public_keys[i], shas[i]),
     149             :    error precedence included, for every input; nothing is aggregated
     150             :    across items.  shas[i] is scratch and its state on return is
     151             :    unspecified.  Grouping more items into calls of at most eight and
     152             :    deriving a transaction verdict from the codes are the caller's.
     153             : 
     154             :    Each sigs[i] points to 64 readable bytes, each public_keys[i] to 32
     155             :    and each msgs[i] to msg_szs[i] (NULL is fine if msg_szs[i] is 0).
     156             :    The caller takes a read interest in the inputs and a write interest
     157             :    in results[0,n) and the n distinct calculators shas[0,n) for the
     158             :    duration of the call; nothing else is written.  Inputs may alias
     159             :    each other (e.g. several items over one message or one key), but
     160             :    not results or the calculators. */
     161             : 
     162             : void
     163             : fd_ed25519_verify_batch8( uchar const * const msgs[],        /* n */
     164             :                           ulong const         msg_szs[],     /* n */
     165             :                           uchar const * const sigs[],        /* n */
     166             :                           uchar const * const public_keys[], /* n */
     167             :                           fd_sha512_t *       shas[],        /* n */
     168             :                           int                 results[],     /* n */
     169             :                           ulong               n );
     170             : 
     171             : /* fd_ed25519_cache_t is a bounded cache of per public key
     172             :    precomputation used to speed up verifying signatures of repeated
     173             :    public keys.  An entry holds a split table of odd multiples of -A
     174             :    (see fd_ed25519_double_scalar_mul_base_split), keyed by the full
     175             :    32-byte public key encoding.  The cache is 4-way set associative
     176             :    with LRU replacement.  A key is inserted when a signature by it
     177             :    verifies successfully for the second time within a window of recent
     178             :    misses (tracked by a small tag filter), so only valid, not small
     179             :    order keys are cached and one-time keys don't evict useful entries.
     180             :    Table builds are rate limited relative to the verify rate.
     181             : 
     182             :    The results (including error codes) of the cached verify APIs are
     183             :    identical to the uncached ones for all inputs and cache states; the
     184             :    cache only changes how long a verify takes.  A cache is not thread
     185             :    safe and is typically owned by a single tile.  The footprint is
     186             :    ~6.2 KiB per entry (AVX-512 build) plus ~170 KiB of fixed tables. */
     187             : 
     188             : struct fd_ed25519_cache;
     189             : typedef struct fd_ed25519_cache fd_ed25519_cache_t;
     190             : 
     191        3603 : #define FD_ED25519_CACHE_ALIGN (128UL)
     192             : 
     193             : /* fd_ed25519_cache_{align,footprint} give the required alignment and
     194             :    footprint of a memory region suitable for a cache with ent_cnt
     195             :    entries.  ent_cnt must be a power of 2 in [4,2^20].  footprint
     196             :    returns 0 for an invalid ent_cnt. */
     197             : 
     198             : FD_FN_CONST ulong
     199             : fd_ed25519_cache_align( void );
     200             : 
     201             : FD_FN_CONST ulong
     202             : fd_ed25519_cache_footprint( ulong ent_cnt );
     203             : 
     204             : /* fd_ed25519_cache_new formats mem as a cache with ent_cnt entries,
     205             :    seeded with seed (the seed randomizes set placement).  Returns mem on
     206             :    success and NULL on failure (logs details).  fd_ed25519_cache_join
     207             :    joins the caller to the cache.  fd_ed25519_cache_leave and
     208             :    fd_ed25519_cache_delete are the usual inverses. */
     209             : 
     210             : void *
     211             : fd_ed25519_cache_new( void * mem,
     212             :                       ulong  ent_cnt,
     213             :                       ulong  seed );
     214             : 
     215             : fd_ed25519_cache_t *
     216             : fd_ed25519_cache_join( void * shcache );
     217             : 
     218             : void *
     219             : fd_ed25519_cache_leave( fd_ed25519_cache_t * cache );
     220             : 
     221             : void *
     222             : fd_ed25519_cache_delete( void * shcache );
     223             : 
     224             : /* fd_ed25519_cache_{hit,miss,insert}_cnt return the number of public
     225             :    key lookups that hit, the number that missed and the number of
     226             :    entries inserted, since the cache was created. */
     227             : 
     228             : ulong fd_ed25519_cache_hit_cnt   ( fd_ed25519_cache_t const * cache );
     229             : ulong fd_ed25519_cache_miss_cnt  ( fd_ed25519_cache_t const * cache );
     230             : ulong fd_ed25519_cache_insert_cnt( fd_ed25519_cache_t const * cache );
     231             : 
     232             : /* fd_ed25519_verify_cached is fd_ed25519_verify using cache.  Returns
     233             :    exactly what fd_ed25519_verify returns for the same msg, sig and
     234             :    public_key. */
     235             : 
     236             : int
     237             : fd_ed25519_verify_cached( uchar const          msg[], /* msg_sz */
     238             :                           ulong                msg_sz,
     239             :                           uchar const          sig[ 64 ],
     240             :                           uchar const          public_key[ 32 ],
     241             :                           fd_sha512_t *        sha,
     242             :                           fd_ed25519_cache_t * cache );
     243             : 
     244             : /* fd_ed25519_strerror converts an FD_ED25519_SUCCESS / FD_ED25519_ERR_*
     245             :    code into a human readable cstr.  The lifetime of the returned
     246             :    pointer is infinite.  The returned pointer is always to a non-NULL
     247             :    cstr. */
     248             : 
     249             : FD_FN_CONST char const *
     250             : fd_ed25519_strerror( int err );
     251             : 
     252             : FD_PROTOTYPES_END
     253             : 
     254             : #endif /* HEADER_fd_src_ballet_ed25519_fd_ed25519_h */

Generated by: LCOV version 1.14