Line data Source code
1 : #include "ag_vote_serde.h" 2 : 3 180 : #define FAIL( cond, err ) do { if( FD_UNLIKELY( cond ) ) return AG_VOTE_DE_ERR_##err; } while( 0 ) 4 : 5 : ulong 6 : ag_vote_ser( ag_vote_t const * self, 7 15 : uchar buf[ static AG_VOTE_SER_MAX ] ) { 8 15 : ag_vote_serde_t vote = { 9 15 : .version = (uchar)1, 10 15 : .tag = (uchar)( self->kind+AG_VOTE_SERDE_TAG_NOTAR ), 11 15 : .slot = ag_vote_slot( self ), 12 15 : .block_id = ag_vote_block_hash( self ), 13 15 : .shred_version = ag_vote_shred_version( self ), 14 15 : }; 15 : 16 15 : ulong off = 0UL; 17 15 : buf[ off ] = vote.version; off += sizeof(uchar); 18 15 : buf[ off ] = vote.tag; off += sizeof(uchar); 19 15 : FD_STORE( ulong, buf+off, vote.slot ); off += sizeof(ulong); 20 15 : if( FD_LIKELY( vote.block_id ) ) { memcpy( buf+off, vote.block_id, sizeof(ag_block_hash_t) ); off += sizeof(ag_block_hash_t); } 21 15 : blst_p2_affine sig_aff[1]; 22 15 : blst_p2_to_affine( sig_aff, ag_vote_sig( self ) ); 23 15 : blst_p2_affine_serialize( buf+off, sig_aff ); off += FD_BLS_SIG_SZ; 24 15 : FD_STORE( ushort, buf+off, vote.shred_version ); off += sizeof(ushort); 25 : 26 15 : return off; 27 15 : } 28 : 29 : int 30 : ag_vote_de( ag_vote_t * self, 31 : uchar const * buf, 32 45 : ulong buf_sz ) { 33 45 : FAIL( buf_sz<2 /* version + tag */, SZ ); 34 : 35 45 : ulong off = 0UL; 36 45 : ag_vote_serde_t vote; 37 45 : vote.version = buf[ off ]; off += sizeof(uchar); 38 45 : vote.tag = buf[ off ]; off += sizeof(uchar); 39 45 : FAIL( vote.version!=1, INVAL ); 40 : 41 45 : uint kind = (uint)vote.tag - AG_VOTE_SERDE_TAG_NOTAR; 42 : 43 45 : int has_block_id = kind==AG_VOTE_KIND_NOTAR || kind==AG_VOTE_KIND_NOTAR_FALLBACK; 44 45 : FAIL( buf_sz!=AG_VOTE_SER_SZ( has_block_id ), SZ ); /* too few, or trailing bytes */ 45 : 46 15 : vote.slot = FD_LOAD( ulong, buf+off ); off += sizeof(ulong); 47 15 : vote.block_id = NULL; 48 15 : if( FD_LIKELY( has_block_id ) ) { 49 6 : vote.block_id = buf+off; off += sizeof(ag_block_hash_t); 50 6 : } 51 15 : vote.signature = buf+off; off += FD_BLS_SIG_SZ; 52 : 53 15 : fd_bls_sig_t sig[1]; 54 15 : blst_p2_affine sig_aff[1]; 55 15 : FAIL( vote.signature[0]&0xA0U, INVAL ); 56 15 : FAIL( blst_p2_deserialize( sig_aff, vote.signature )!=BLST_SUCCESS, INVAL ); 57 15 : FAIL( !blst_p2_affine_in_g2( sig_aff ), INVAL ); 58 15 : blst_p2_from_affine( sig, sig_aff ); 59 15 : vote.shred_version = FD_LOAD( ushort, buf+off ); off += sizeof(ushort); 60 : 61 15 : fd_memset( self, 0, sizeof(ag_vote_t) ); 62 15 : self->kind = kind; 63 : 64 15 : switch( kind ) { 65 3 : case AG_VOTE_KIND_NOTAR: 66 3 : self->notar.slot = vote.slot; 67 3 : self->notar.shred_version = vote.shred_version; 68 3 : memcpy( self->notar.block_hash, vote.block_id, sizeof(ag_block_hash_t) ); 69 3 : self->notar.sig = *sig; 70 3 : break; 71 3 : case AG_VOTE_KIND_FINAL: 72 3 : self->final.slot = vote.slot; 73 3 : self->final.shred_version = vote.shred_version; 74 3 : self->final.sig = *sig; 75 3 : break; 76 3 : case AG_VOTE_KIND_SKIP: 77 3 : self->skip.slot = vote.slot; 78 3 : self->skip.shred_version = vote.shred_version; 79 3 : self->skip.sig = *sig; 80 3 : break; 81 3 : case AG_VOTE_KIND_NOTAR_FALLBACK: 82 3 : self->notar_fallback.slot = vote.slot; 83 3 : self->notar_fallback.shred_version = vote.shred_version; 84 3 : memcpy( self->notar_fallback.block_hash, vote.block_id, sizeof(ag_block_hash_t) ); 85 3 : self->notar_fallback.sig = *sig; 86 3 : break; 87 3 : case AG_VOTE_KIND_SKIP_FALLBACK: 88 3 : self->skip_fallback.slot = vote.slot; 89 3 : self->skip_fallback.shred_version = vote.shred_version; 90 3 : self->skip_fallback.sig = *sig; 91 3 : break; 92 0 : default: 93 0 : return AG_VOTE_DE_ERR_INVAL; 94 15 : } 95 : 96 15 : return AG_VOTE_DE_SUCCESS; 97 15 : } 98 : 99 : ulong 100 : ag_vote_signing_ser( uint kind, 101 : ulong slot, 102 : uchar const * block_hash, 103 : ushort shred_version, 104 10707 : uchar buf[ static AG_VOTE_SIGNING_SER_MAX ] ) { 105 10707 : ulong off = 0UL; 106 10707 : buf[ off ] = (uchar)( kind+AG_VOTE_SERDE_TAG_NOTAR ); off += sizeof(uchar); 107 10707 : FD_STORE( ulong, buf+off, slot ); off += sizeof(ulong); 108 10707 : if( FD_LIKELY( block_hash ) ) { memcpy( buf+off, block_hash, sizeof(ag_block_hash_t) ); off += sizeof(ag_block_hash_t); } 109 10707 : FD_STORE( ushort, buf+off, shred_version ); off += sizeof(ushort); 110 10707 : return off; 111 10707 : }