LCOV - code coverage report
Current view: top level - choreo/rotor - fd_rotor_serde.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 152 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 14 0.0 %

          Line data    Source code
       1             : #include "fd_rotor_serde.h"
       2             : #include "../../ballet/bmtree/fd_bmtree.h"
       3             : #include "../../ballet/sha256/fd_sha256.h"
       4             : #include "../../disco/shred/fd_fec_set.h"
       5             : 
       6             : ulong
       7             : fd_rotor_pong_ser( fd_rotor_pong_t const * self,
       8             :                    fd_ed25519_sig_t const  signature,
       9             :                    fd_pubkey_t const *     from,
      10           0 :                    uchar                   buf[ static FD_ROTOR_PONG_SER_SZ ] ) {
      11           0 :   ulong off = 0UL;
      12           0 :   FD_STORE( uint, buf+off, FD_ROTOR_SERDE_TAG_PONG );    off += sizeof(uint);
      13           0 :   memcpy( buf+off, from->uc,      sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
      14           0 :   memcpy( buf+off, self->hash.uc, sizeof(fd_hash_t)   ); off += sizeof(fd_hash_t);
      15           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   ); off += FD_ED25519_SIG_SZ;
      16           0 :   return off;
      17           0 : }
      18             : 
      19             : ulong
      20             : fd_rotor_req_sig_ser( uchar const * buf,
      21             :                       ulong         buf_sz,
      22           0 :                       uchar         out[ static FD_ROTOR_SIG_SER_MAX ] ) {
      23           0 :   memcpy( out,              buf,                                sizeof(uint)                          );
      24           0 :   memcpy( out+sizeof(uint), buf+sizeof(uint)+FD_ED25519_SIG_SZ, buf_sz-sizeof(uint)-FD_ED25519_SIG_SZ );
      25           0 :   return buf_sz-FD_ED25519_SIG_SZ;
      26           0 : }
      27             : 
      28             : ulong
      29             : fd_rotor_req_window_index_ser( fd_rotor_shred_t const * self,
      30             :                                fd_ed25519_sig_t const   signature,
      31             :                                fd_pubkey_t const *      sender,
      32             :                                fd_pubkey_t const *      recipient,
      33             :                                ulong                    timestamp,
      34             :                                uint                     nonce,
      35           0 :                                uchar                    buf[ static FD_ROTOR_WINDOW_INDEX_SER_SZ ] ) {
      36           0 :   ulong off = 0UL;
      37           0 :   FD_STORE( uint,  buf+off, FD_ROTOR_SERDE_TAG_WINDOW_INDEX ); off += sizeof(uint);
      38           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   );       off += FD_ED25519_SIG_SZ;
      39           0 :   memcpy( buf+off, sender->uc,    sizeof(fd_pubkey_t) );       off += sizeof(fd_pubkey_t);
      40           0 :   memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) );       off += sizeof(fd_pubkey_t);
      41           0 :   FD_STORE( ulong, buf+off, timestamp );                       off += sizeof(ulong);
      42           0 :   FD_STORE( uint,  buf+off, nonce     );                       off += sizeof(uint);
      43           0 :   FD_STORE( ulong, buf+off, self->slot );                      off += sizeof(ulong);
      44           0 :   FD_STORE( ulong, buf+off, self->shred_idx );                 off += sizeof(ulong);
      45           0 :   return off;
      46           0 : }
      47             : 
      48             : ulong
      49             : fd_rotor_req_highest_window_index_ser( fd_rotor_highest_shred_t const * self,
      50             :                                        fd_ed25519_sig_t const           signature,
      51             :                                        fd_pubkey_t const *              sender,
      52             :                                        fd_pubkey_t const *              recipient,
      53             :                                        ulong                            timestamp,
      54             :                                        uint                             nonce,
      55           0 :                                        uchar                            buf[ static FD_ROTOR_HIGHEST_WINDOW_INDEX_SER_SZ ] ) {
      56           0 :   ulong off = 0UL;
      57           0 :   FD_STORE( uint,  buf+off, FD_ROTOR_SERDE_TAG_HIGHEST_WINDOW_INDEX ); off += sizeof(uint);
      58           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   );               off += FD_ED25519_SIG_SZ;
      59           0 :   memcpy( buf+off, sender->uc,    sizeof(fd_pubkey_t) );               off += sizeof(fd_pubkey_t);
      60           0 :   memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) );               off += sizeof(fd_pubkey_t);
      61           0 :   FD_STORE( ulong, buf+off, timestamp );                               off += sizeof(ulong);
      62           0 :   FD_STORE( uint,  buf+off, nonce     );                               off += sizeof(uint);
      63           0 :   FD_STORE( ulong, buf+off, self->slot );                              off += sizeof(ulong);
      64           0 :   FD_STORE( ulong, buf+off, self->shred_idx );                         off += sizeof(ulong);
      65           0 :   return off;
      66           0 : }
      67             : 
      68             : ulong
      69             : fd_rotor_req_orphan_ser( fd_rotor_orphan_t const * self,
      70             :                          fd_ed25519_sig_t const    signature,
      71             :                          fd_pubkey_t const *       sender,
      72             :                          fd_pubkey_t const *       recipient,
      73             :                          ulong                     timestamp,
      74             :                          uint                      nonce,
      75           0 :                          uchar                     buf[ static FD_ROTOR_ORPHAN_SER_SZ ] ) {
      76           0 :   ulong off = 0UL;
      77           0 :   FD_STORE( uint,  buf+off, FD_ROTOR_SERDE_TAG_ORPHAN ); off += sizeof(uint);
      78           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   ); off += FD_ED25519_SIG_SZ;
      79           0 :   memcpy( buf+off, sender->uc,    sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
      80           0 :   memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
      81           0 :   FD_STORE( ulong, buf+off, timestamp );                 off += sizeof(ulong);
      82           0 :   FD_STORE( uint,  buf+off, nonce     );                 off += sizeof(uint);
      83           0 :   FD_STORE( ulong, buf+off, self->slot );                off += sizeof(ulong);
      84           0 :   return off;
      85           0 : }
      86             : 
      87             : ulong
      88             : fd_rotor_req_parent_and_fec_set_count_ser( fd_rotor_parent_fec_set_count_t const * self,
      89             :                                            fd_ed25519_sig_t const                  signature,
      90             :                                            fd_pubkey_t const *                     sender,
      91             :                                            fd_pubkey_t const *                     recipient,
      92             :                                            ulong                                   timestamp,
      93             :                                            uint                                    nonce,
      94           0 :                                            uchar                                   buf[ static FD_ROTOR_PARENT_AND_FEC_SET_COUNT_SER_SZ ] ) {
      95           0 :   ulong off = 0UL;
      96           0 :   FD_STORE( uint,  buf+off, FD_ROTOR_SERDE_TAG_PARENT_AND_FEC_SET_COUNT ); off += sizeof(uint);
      97           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   );                   off += FD_ED25519_SIG_SZ;
      98           0 :   memcpy( buf+off, sender->uc,    sizeof(fd_pubkey_t) );                   off += sizeof(fd_pubkey_t);
      99           0 :   memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) );                   off += sizeof(fd_pubkey_t);
     100           0 :   FD_STORE( ulong, buf+off, timestamp );                                   off += sizeof(ulong);
     101           0 :   FD_STORE( uint,  buf+off, nonce     );                                   off += sizeof(uint);
     102           0 :   FD_STORE( ulong, buf+off, self->slot );                                  off += sizeof(ulong);
     103           0 :   memcpy( buf+off, self->block_id.uc, sizeof(fd_hash_t) );                 off += sizeof(fd_hash_t);
     104           0 :   return off;
     105           0 : }
     106             : 
     107             : ulong
     108             : fd_rotor_req_fec_set_root_ser( fd_rotor_fec_set_root_t const * self,
     109             :                                fd_ed25519_sig_t const          signature,
     110             :                                fd_pubkey_t const *             sender,
     111             :                                fd_pubkey_t const *             recipient,
     112             :                                ulong                           timestamp,
     113             :                                uint                            nonce,
     114           0 :                                uchar                           buf[ static FD_ROTOR_FEC_SET_ROOT_SER_SZ ] ) {
     115           0 :   ulong off = 0UL;
     116           0 :   FD_STORE( uint,  buf+off, FD_ROTOR_SERDE_TAG_FEC_SET_ROOT ); off += sizeof(uint);
     117           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   );       off += FD_ED25519_SIG_SZ;
     118           0 :   memcpy( buf+off, sender->uc,    sizeof(fd_pubkey_t) );       off += sizeof(fd_pubkey_t);
     119           0 :   memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) );       off += sizeof(fd_pubkey_t);
     120           0 :   FD_STORE( ulong, buf+off, timestamp );                       off += sizeof(ulong);
     121           0 :   FD_STORE( uint,  buf+off, nonce     );                       off += sizeof(uint);
     122           0 :   FD_STORE( ulong, buf+off, self->slot );                      off += sizeof(ulong);
     123           0 :   memcpy( buf+off, self->block_id.uc, sizeof(fd_hash_t) );     off += sizeof(fd_hash_t);
     124           0 :   FD_STORE( uint,  buf+off, self->fec_set_idx );               off += sizeof(uint);
     125           0 :   return off;
     126           0 : }
     127             : 
     128             : ulong
     129             : fd_rotor_req_window_index_for_block_id_ser( fd_rotor_shred_for_block_id_t const * self,
     130             :                                             fd_ed25519_sig_t const                signature,
     131             :                                             fd_pubkey_t const *                   sender,
     132             :                                             fd_pubkey_t const *                   recipient,
     133             :                                             ulong                                 timestamp,
     134             :                                             uint                                  nonce,
     135           0 :                                             uchar                                 buf[ static FD_ROTOR_WINDOW_INDEX_FOR_BLOCK_ID_SER_SZ ] ) {
     136           0 :   ulong off = 0UL;
     137           0 :   FD_STORE( uint,  buf+off, FD_ROTOR_SERDE_TAG_WINDOW_INDEX_FOR_BLOCK_ID ); off += sizeof(uint);
     138           0 :   memcpy( buf+off, signature,     FD_ED25519_SIG_SZ   );                    off += FD_ED25519_SIG_SZ;
     139           0 :   memcpy( buf+off, sender->uc,    sizeof(fd_pubkey_t) );                    off += sizeof(fd_pubkey_t);
     140           0 :   memcpy( buf+off, recipient->uc, sizeof(fd_pubkey_t) );                    off += sizeof(fd_pubkey_t);
     141           0 :   FD_STORE( ulong, buf+off, timestamp );                                    off += sizeof(ulong);
     142           0 :   FD_STORE( uint,  buf+off, nonce     );                                    off += sizeof(uint);
     143           0 :   FD_STORE( ulong, buf+off, self->slot );                                   off += sizeof(ulong);
     144           0 :   FD_STORE( uint,  buf+off, self->shred_idx );                              off += sizeof(uint);
     145           0 :   memcpy( buf+off, self->block_id.uc, sizeof(fd_hash_t) );                  off += sizeof(fd_hash_t);
     146           0 :   return off;
     147           0 : }
     148             : 
     149             : ulong
     150             : fd_rotor_ping_de( uchar const *    buf,
     151             :                   ulong            buf_sz,
     152             :                   fd_pubkey_t *    from,
     153             :                   fd_hash_t *      token,
     154           0 :                   fd_ed25519_sig_t signature ) {
     155           0 :   if( FD_UNLIKELY( buf_sz<FD_ROTOR_PING_DE_SZ || FD_LOAD( uint, buf )!=FD_ROTOR_SERDE_TAG_PING ) ) return 0UL;
     156           0 :   ulong off = sizeof(uint);
     157           0 :   memcpy( from->uc,  buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
     158           0 :   memcpy( token->uc, buf+off, sizeof(fd_hash_t)   ); off += sizeof(fd_hash_t);
     159           0 :   memcpy( signature, buf+off, FD_ED25519_SIG_SZ   ); off += FD_ED25519_SIG_SZ;
     160           0 :   return off;
     161           0 : }
     162             : 
     163             : ulong
     164             : fd_rotor_block_id_ping_de( uchar const *    buf,
     165             :                            ulong            buf_sz,
     166             :                            fd_pubkey_t *    from,
     167             :                            fd_hash_t *      token,
     168           0 :                            fd_ed25519_sig_t signature ) {
     169           0 :   if( FD_UNLIKELY( buf_sz<FD_ROTOR_PING_DE_SZ || FD_LOAD( uint, buf )!=FD_ROTOR_SERDE_TAG_BLOCK_ID_PING ) ) return 0UL;
     170           0 :   ulong off = sizeof(uint);
     171           0 :   memcpy( from->uc,  buf+off, sizeof(fd_pubkey_t) ); off += sizeof(fd_pubkey_t);
     172           0 :   memcpy( token->uc, buf+off, sizeof(fd_hash_t)   ); off += sizeof(fd_hash_t);
     173           0 :   memcpy( signature, buf+off, FD_ED25519_SIG_SZ   ); off += FD_ED25519_SIG_SZ;
     174           0 :   return off;
     175           0 : }
     176             : 
     177             : ulong
     178             : fd_rotor_res_parent_fec_set_count_de( uchar const * buf,
     179             :                                       ulong         buf_sz,
     180             :                                       uint *        fec_set_cnt,
     181             :                                       ulong *       parent_slot,
     182             :                                       fd_hash_t *   parent_block_id,
     183             :                                       uchar         proof[ static FD_ROTOR_PROOF_MAX ],
     184             :                                       ulong *       proof_sz,
     185           0 :                                       uint *        nonce ) {
     186           0 :   if( FD_UNLIKELY( buf_sz<sizeof(uint)+sizeof(uint)+sizeof(ulong)+sizeof(fd_hash_t)+sizeof(ulong) ) ) return 0UL;
     187           0 :   if( FD_UNLIKELY( FD_LOAD( uint, buf )!=FD_ROTOR_SERDE_TAG_PARENT_FEC_SET_COUNT_RES              ) ) return 0UL;
     188           0 :   ulong off = sizeof(uint);
     189           0 :   *fec_set_cnt = FD_LOAD( uint,  buf+off );                  off += sizeof(uint);
     190           0 :   *parent_slot = FD_LOAD( ulong, buf+off );                  off += sizeof(ulong);
     191           0 :   memcpy( parent_block_id->uc, buf+off, sizeof(fd_hash_t) ); off += sizeof(fd_hash_t);
     192           0 :   *proof_sz    = FD_LOAD( ulong, buf+off );                  off += sizeof(ulong);
     193           0 :   if( FD_UNLIKELY( *proof_sz>FD_ROTOR_PROOF_MAX || *proof_sz+sizeof(uint)>buf_sz-off ) ) return 0UL;
     194           0 :   memcpy( proof, buf+off, *proof_sz );                       off += *proof_sz;
     195           0 :   *nonce       = FD_LOAD( uint, buf+off );                   off += sizeof(uint);
     196           0 :   return off;
     197           0 : }
     198             : 
     199             : ulong
     200             : fd_rotor_res_fec_set_root_de( uchar const * buf,
     201             :                               ulong         buf_sz,
     202             :                               uchar         fec_set_root[ static FD_SHRED_MERKLE_NODE_SZ ],
     203             :                               uchar         proof[ static FD_ROTOR_PROOF_MAX ],
     204             :                               ulong *       proof_sz,
     205           0 :                               uint *        nonce ) {
     206           0 :   if( FD_UNLIKELY( buf_sz<sizeof(uint)+FD_SHRED_MERKLE_NODE_SZ+sizeof(ulong) ) ) return 0UL;
     207           0 :   if( FD_UNLIKELY( FD_LOAD( uint, buf )!=FD_ROTOR_SERDE_TAG_FEC_SET_ROOT_RES   ) ) return 0UL;
     208           0 :   ulong off = sizeof(uint);
     209           0 :   memcpy( fec_set_root, buf+off, FD_SHRED_MERKLE_NODE_SZ ); off += FD_SHRED_MERKLE_NODE_SZ;
     210           0 :   *proof_sz = FD_LOAD( ulong, buf+off );                     off += sizeof(ulong);
     211           0 :   if( FD_UNLIKELY( *proof_sz>FD_ROTOR_PROOF_MAX || *proof_sz+sizeof(uint)>buf_sz-off ) ) return 0UL;
     212           0 :   memcpy( proof, buf+off, *proof_sz );                       off += *proof_sz;
     213           0 :   *nonce    = FD_LOAD( uint, buf+off );                      off += sizeof(uint);
     214           0 :   return off;
     215           0 : }
     216             : 
     217             : int
     218             : fd_rotor_res_parent_fec_set_count_verify( uint              fec_set_cnt,
     219             :                                           ulong             parent_slot,
     220             :                                           fd_hash_t const * parent_block_id,
     221             :                                           uchar const *     proof,
     222             :                                           ulong             proof_sz,
     223           0 :                                           fd_hash_t const * block_id ) {
     224           0 :   if( FD_UNLIKELY( !fec_set_cnt || fec_set_cnt>FD_FEC_BLK_MAX                                     ) ) return -1;
     225           0 :   if( FD_UNLIKELY( proof_sz!=( fd_bmtree_depth( fec_set_cnt+1UL )-1UL )*FD_SHRED_MERKLE_NODE_SZ ) ) return -1;
     226             : 
     227           0 :   fd_bmtree_node_t leaf[1];
     228           0 :   fd_sha256_t      sha[1];
     229           0 :   fd_sha256_init  ( sha );
     230           0 :   fd_sha256_append( sha, &parent_slot,        sizeof(ulong)     );
     231           0 :   fd_sha256_append( sha, parent_block_id->uc, sizeof(fd_hash_t) );
     232           0 :   fd_sha256_append( sha, &fec_set_cnt,        sizeof(uint)      );
     233           0 :   fd_sha256_fini  ( sha, leaf->hash );
     234             : 
     235           0 :   fd_bmtree_node_t root[1];
     236           0 :   if( FD_UNLIKELY( !fd_bmtree_from_proof( leaf, fec_set_cnt, root, proof, proof_sz/FD_SHRED_MERKLE_NODE_SZ, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ ) ) ) return -1;
     237           0 :   if( FD_UNLIKELY( memcmp( root->hash, block_id->uc, sizeof(fd_hash_t) )                                                                                   ) ) return -1;
     238           0 :   return 0;
     239           0 : }
     240             : 
     241             : int
     242             : fd_rotor_res_fec_set_root_verify( uchar const       fec_set_root[ static FD_SHRED_MERKLE_NODE_SZ ],
     243             :                                   uchar const *     proof,
     244             :                                   ulong             proof_sz,
     245             :                                   fd_hash_t const * block_id,
     246             :                                   uint              fec_set_idx,
     247           0 :                                   uint              fec_set_cnt ) {
     248           0 :   if( FD_UNLIKELY( !fec_set_cnt || fec_set_cnt>FD_FEC_BLK_MAX                                     ) ) return -1;
     249           0 :   if( FD_UNLIKELY( fec_set_idx%FD_FEC_SHRED_CNT || fec_set_idx/FD_FEC_SHRED_CNT>=fec_set_cnt      ) ) return -1;
     250           0 :   if( FD_UNLIKELY( proof_sz!=( fd_bmtree_depth( fec_set_cnt+1UL )-1UL )*FD_SHRED_MERKLE_NODE_SZ ) ) return -1;
     251             : 
     252           0 :   fd_bmtree_node_t leaf[1] = {0};
     253           0 :   memcpy( leaf->hash, fec_set_root, FD_SHRED_MERKLE_NODE_SZ );
     254             : 
     255           0 :   fd_bmtree_node_t root[1];
     256           0 :   if( FD_UNLIKELY( !fd_bmtree_from_proof( leaf, fec_set_idx/FD_FEC_SHRED_CNT, root, proof, proof_sz/FD_SHRED_MERKLE_NODE_SZ, FD_SHRED_MERKLE_NODE_SZ, FD_BMTREE_LONG_PREFIX_SZ ) ) ) return -1;
     257           0 :   if( FD_UNLIKELY( memcmp( root->hash, block_id->uc, sizeof(fd_hash_t) )                                                                                                       ) ) return -1;
     258           0 :   return 0;
     259           0 : }

Generated by: LCOV version 1.14