LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_precompiles.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 255 0.0 %
Date: 2025-03-20 12:08:36 Functions: 0 7 0.0 %

          Line data    Source code
       1             : #include "fd_precompiles.h"
       2             : #include "../fd_executor_err.h"
       3             : #include "../../../ballet/keccak256/fd_keccak256.h"
       4             : #include "../../../ballet/ed25519/fd_ed25519.h"
       5             : #include "../../../ballet/secp256k1/fd_secp256k1.h"
       6             : #include "../../../ballet/secp256r1/fd_secp256r1.h"
       7             : 
       8             : /* Docs:
       9             :    https://docs.solana.com/developing/runtime-facilities/programs#ed25519-program
      10             :    https://docs.solana.com/developing/runtime-facilities/programs#secp256k1-program */
      11             : 
      12             : /* There are 3 precompiles and 2 ways to serialize data.
      13             :    The most recent one seems are ed25519 and secp256r1 with 2 bytes per instruction,
      14             :    that works better with JS sdk even though it consumes a few bytes. */
      15             : struct __attribute__((packed)) fd_precompile_sig_offsets {
      16             :   ushort sig_offset;
      17             :   ushort sig_instr_idx;
      18             :   ushort pubkey_offset;
      19             :   ushort pubkey_instr_idx;
      20             :   ushort msg_offset;
      21             :   ushort msg_data_sz;
      22             :   ushort msg_instr_idx;
      23             : };
      24             : typedef struct fd_precompile_sig_offsets fd_ed25519_signature_offsets_t;
      25             : typedef struct fd_precompile_sig_offsets fd_secp256r1_signature_offsets_t;
      26             : 
      27             : struct __attribute__((packed)) fd_precompile_one_byte_idx_sig_offsets {
      28             :   ushort sig_offset;
      29             :   uchar  sig_instr_idx;
      30             :   ushort pubkey_offset;
      31             :   uchar  pubkey_instr_idx;
      32             :   ushort msg_offset;
      33             :   ushort msg_data_sz;
      34             :   uchar  msg_instr_idx;
      35             : };
      36             : typedef struct fd_precompile_one_byte_idx_sig_offsets fd_secp256k1_signature_offsets_t;
      37             : 
      38             : /*
      39             :   Common
      40             : */
      41             : 
      42           0 : #define SIGNATURE_SERIALIZED_SIZE         (64UL)
      43           0 : #define SIGNATURE_OFFSETS_SERIALIZED_SIZE (14UL)
      44           0 : #define SIGNATURE_OFFSETS_START            (2UL)
      45             : #define DATA_START (SIGNATURE_OFFSETS_SERIALIZED_SIZE + SIGNATURE_OFFSETS_START)
      46             : 
      47             : /*
      48             :   Custom
      49             : */
      50             : 
      51           0 : #define ED25519_PUBKEY_SERIALIZED_SIZE              (32UL)
      52             : 
      53           0 : #define SECP256R1_PUBKEY_SERIALIZED_SIZE            (33UL)
      54             : 
      55           0 : #define SECP256K1_PUBKEY_SERIALIZED_SIZE            (20UL)
      56           0 : #define SECP256K1_SIGNATURE_OFFSETS_SERIALIZED_SIZE (11UL)
      57           0 : #define SECP256K1_SIGNATURE_OFFSETS_START            (1UL)
      58             : #define SECP256K1_DATA_START (SECP256K1_SIGNATURE_OFFSETS_SERIALIZED_SIZE + SECP256K1_SIGNATURE_OFFSETS_START)
      59             : 
      60             : FD_STATIC_ASSERT( sizeof( fd_ed25519_signature_offsets_t )==SIGNATURE_OFFSETS_SERIALIZED_SIZE, fd_ballet );
      61             : FD_STATIC_ASSERT( sizeof( fd_secp256k1_signature_offsets_t )==SECP256K1_SIGNATURE_OFFSETS_SERIALIZED_SIZE, fd_ballet );
      62             : 
      63             : /*
      64             :   Common code
      65             : */
      66             : 
      67             : /* fd_precompile_get_instr_data fetches data across instructions.
      68             :    In Agave, the 2 precompiles have slightly different behavior:
      69             :    1. Ed25519 has 16-bit instr index vs Secp256k1 has 8-bit
      70             :    2. Ed25519 accepts instr index==0xFFFF as a special value to indicate
      71             :       the current instruction, Secp256k1 doesn't have this feature
      72             :    3. Ed25519 always return InvalidDataOffsets, while Secp256k1 can
      73             :       return InvalidDataOffsets or InvalidSignature
      74             :    All these differences are completely useless, so we unify the logic.
      75             :    We handle the special case of index==0xFFFF as in Ed25519.
      76             :    We handle errors as in Secp256k1. */
      77             : static inline int
      78             : fd_precompile_get_instr_data( fd_exec_txn_ctx_t *     txn_ctx,
      79             :                               fd_txn_instr_t const *  cur_instr,
      80             :                               ushort                  index,
      81             :                               ushort                  offset,
      82             :                               ushort                  sz,
      83           0 :                               uchar const **          res ) {
      84           0 :   uchar const * data;
      85           0 :   ulong         data_sz;
      86             :   /* The special value index==USHORT_MAX means current instruction.
      87             :      This feature has been introduced for ed25519, but not for secp256k1 where
      88             :      index is 1-byte only.
      89             :      So, fortunately, we can use the same function.
      90             :      https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L161-L163
      91             :      https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L1018 */
      92           0 :   if( index==USHORT_MAX ) {
      93             : 
      94             :     /* Use current instruction data */
      95           0 :     data    = fd_txn_get_instr_data( cur_instr, txn_ctx->_txn_raw->raw );
      96           0 :     data_sz = cur_instr->data_sz;
      97             : 
      98           0 :   } else {
      99             : 
     100           0 :     fd_txn_t const * txn_descriptor = txn_ctx->txn_descriptor;
     101           0 :     if( FD_UNLIKELY( index >= txn_descriptor->instr_cnt ) )
     102           0 :       return FD_EXECUTOR_PRECOMPILE_ERR_DATA_OFFSET;
     103             : 
     104           0 :     fd_txn_instr_t const * instr = &txn_descriptor->instr[index];
     105           0 :     data    = fd_txn_get_instr_data( instr, txn_ctx->_txn_raw->raw );
     106           0 :     data_sz = instr->data_sz;
     107             : 
     108           0 :   }
     109             : 
     110           0 :   if( FD_UNLIKELY( (ulong)offset+(ulong)sz > data_sz ) )  /* (offset+sz) in [0,2^17) */
     111           0 :     return FD_EXECUTOR_PRECOMPILE_ERR_SIGNATURE;
     112             : 
     113           0 :   *res = data + offset;
     114           0 :   return 0;
     115           0 : }
     116             : 
     117             : /*
     118             :   Ed25519
     119             : */
     120             : 
     121             : /* Consider rewriting precompile verifiy functions to accept fd_instr_info_t
     122             :    instead of fd_txn_instr_t to simplify and remove the need for a wrapper execute function. */
     123             : 
     124             : int
     125           0 : fd_precompile_ed25519_execute( fd_exec_instr_ctx_t * ctx ) {
     126           0 :   if( FD_FEATURE_ACTIVE( ctx->txn_ctx->slot, ctx->txn_ctx->features, move_precompile_verification_to_svm ) ) {
     127           0 :     fd_txn_instr_t const * instr = &ctx->txn_ctx->txn_descriptor->instr[ ctx->txn_ctx->current_instr_idx ];
     128           0 :     return fd_precompile_ed25519_verify( ctx->txn_ctx, instr );
     129           0 :   } else {
     130           0 :     return FD_EXECUTOR_INSTR_SUCCESS;
     131           0 :   }
     132           0 : }
     133             : 
     134             : int
     135             : fd_precompile_ed25519_verify( fd_exec_txn_ctx_t *    txn_ctx,
     136           0 :                               fd_txn_instr_t const * instr ) {
     137             : 
     138           0 :   uchar const * data    = fd_txn_get_instr_data( instr, txn_ctx->_txn_raw->raw );
     139           0 :   ulong         data_sz = instr->data_sz;
     140             : 
     141             :   /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L90-L96
     142             :      note: this part is really silly and in fact in leaves out the edge case [0, 0].
     143             : 
     144             :      Our implementation does the following:
     145             :      1. assert that there's enough data to deser 1+ fd_ed25519_sig_offsets
     146             :         (in particular, data[0] is accessible)
     147             :         - in the unlikely case, check for the Agave edge case
     148             :      2. if data[0]==0 return
     149             :      3. compute and check expected size */
     150           0 :   if( FD_UNLIKELY( data_sz < DATA_START ) ) {
     151           0 :     if( FD_UNLIKELY( data_sz == 2 && data[0] == 0 ) ) {
     152           0 :       return FD_EXECUTOR_INSTR_SUCCESS;
     153           0 :     }
     154           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     155           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     156           0 :   }
     157             : 
     158           0 :   ulong sig_cnt = data[0];
     159           0 :   if( FD_UNLIKELY( sig_cnt==0 ) ) {
     160           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     161           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     162           0 :   }
     163             : 
     164             :   /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L97-L103 */
     165           0 :   ulong expected_data_size = sig_cnt * SIGNATURE_OFFSETS_SERIALIZED_SIZE + SIGNATURE_OFFSETS_START;
     166           0 :   if( FD_UNLIKELY( data_sz < expected_data_size ) ) {
     167           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     168           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     169           0 :   }
     170             : 
     171           0 :   ulong off = SIGNATURE_OFFSETS_START;
     172           0 :   for( ulong i = 0; i < sig_cnt; ++i ) {
     173           0 :     fd_ed25519_signature_offsets_t const * sigoffs = (const fd_ed25519_signature_offsets_t *) (data + off);
     174           0 :     off += SIGNATURE_OFFSETS_SERIALIZED_SIZE;
     175             : 
     176             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L110-L112 */
     177             :     // ???
     178             : 
     179             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L114-L121 */
     180           0 :     uchar const * sig = NULL;
     181           0 :     int err = fd_precompile_get_instr_data( txn_ctx,
     182           0 :                                             instr,
     183           0 :                                             sigoffs->sig_instr_idx,
     184           0 :                                             sigoffs->sig_offset,
     185           0 :                                             SIGNATURE_SERIALIZED_SIZE,
     186           0 :                                             &sig );
     187           0 :     if( FD_UNLIKELY( err ) ) {
     188           0 :       txn_ctx->custom_err = (uint)err;
     189           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     190           0 :     }
     191             : 
     192             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L123-L124
     193             :        Note: we parse the signature as part of fd_ed25519_verify.
     194             :        Because of this, the return error code might be different from Agave in some edge cases. */
     195             : 
     196             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L126-L133 */
     197           0 :     uchar const * pubkey = NULL;
     198           0 :     err = fd_precompile_get_instr_data( txn_ctx,
     199           0 :                                         instr,
     200           0 :                                         sigoffs->pubkey_instr_idx,
     201           0 :                                         sigoffs->pubkey_offset,
     202           0 :                                         ED25519_PUBKEY_SERIALIZED_SIZE,
     203           0 :                                         &pubkey );
     204           0 :     if( FD_UNLIKELY( err ) ) {
     205           0 :       txn_ctx->custom_err = (uint)err;
     206           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     207           0 :     }
     208             : 
     209             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L135-L136
     210             :        Note: we parse the public key as part of fd_ed25519_verify.
     211             :        Because of this, the return error code might be different from Agave in some edge cases. */
     212             : 
     213             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L138-L145 */
     214           0 :     uchar const * msg = NULL;
     215           0 :     ushort msg_sz = sigoffs->msg_data_sz;
     216           0 :     err = fd_precompile_get_instr_data( txn_ctx,
     217           0 :                                         instr,
     218           0 :                                         sigoffs->msg_instr_idx,
     219           0 :                                         sigoffs->msg_offset,
     220           0 :                                         msg_sz,
     221           0 :                                         &msg );
     222           0 :     if( FD_UNLIKELY( err ) ) {
     223           0 :       txn_ctx->custom_err = (uint)err;
     224           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     225           0 :     }
     226             : 
     227             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/ed25519_instruction.rs#L147-L149 */
     228           0 :     fd_sha512_t sha[1];
     229           0 :     if( FD_UNLIKELY( fd_ed25519_verify( msg, msg_sz, sig, pubkey, sha )!=FD_ED25519_SUCCESS ) ) {
     230           0 :       txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_SIGNATURE;
     231           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     232           0 :     }
     233           0 :   }
     234             : 
     235           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     236           0 : }
     237             : 
     238             : /*
     239             :   Secp256K1
     240             : */
     241             : 
     242             : int
     243           0 : fd_precompile_secp256k1_execute( fd_exec_instr_ctx_t * ctx ) {
     244           0 :   if( FD_FEATURE_ACTIVE( ctx->txn_ctx->slot, ctx->txn_ctx->features, move_precompile_verification_to_svm ) ) {
     245           0 :     fd_txn_instr_t const * instr = &ctx->txn_ctx->txn_descriptor->instr[ ctx->txn_ctx->current_instr_idx ];
     246           0 :     return fd_precompile_secp256k1_verify( ctx->txn_ctx, instr );
     247           0 :   } else {
     248           0 :     return FD_EXECUTOR_INSTR_SUCCESS;
     249           0 :   }
     250           0 : }
     251             : 
     252             : int
     253             : fd_precompile_secp256k1_verify( fd_exec_txn_ctx_t *    txn_ctx,
     254           0 :                                 fd_txn_instr_t const * instr ) {
     255             : 
     256           0 :   uchar const * data    = fd_txn_get_instr_data( instr, txn_ctx->_txn_raw->raw );
     257           0 :   ulong         data_sz = instr->data_sz;
     258             : 
     259             :   /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L934-L947
     260             :      see comment in ed25519, here the special case is [0] instead of [0, 0] */
     261           0 :   if( FD_UNLIKELY( data_sz < SECP256K1_DATA_START ) ) {
     262           0 :     if( FD_UNLIKELY( data_sz == 1 && data[0] == 0 ) ) {
     263           0 :       return FD_EXECUTOR_INSTR_SUCCESS;
     264           0 :     }
     265           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     266           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     267           0 :   }
     268             : 
     269             :   /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/sdk/src/secp256k1_instruction.rs#L938-L947 */
     270           0 :   ulong sig_cnt = data[0];
     271           0 :   if( FD_UNLIKELY( sig_cnt==0 && data_sz>1 ) ) {
     272           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     273           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     274           0 :   }
     275             : 
     276             :   /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L948-L953 */
     277           0 :   ulong expected_data_size = sig_cnt * SECP256K1_SIGNATURE_OFFSETS_SERIALIZED_SIZE + SECP256K1_SIGNATURE_OFFSETS_START;
     278           0 :   if( FD_UNLIKELY( data_sz < expected_data_size ) ) {
     279           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     280           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     281           0 :   }
     282             : 
     283           0 :   ulong off = SECP256K1_SIGNATURE_OFFSETS_START;
     284           0 :   for( ulong i = 0; i < sig_cnt; ++i ) {
     285           0 :     fd_secp256k1_signature_offsets_t const * sigoffs = (const fd_secp256k1_signature_offsets_t *) (data + off);
     286           0 :     off += SECP256K1_SIGNATURE_OFFSETS_SERIALIZED_SIZE;
     287             : 
     288             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L960-L961 */
     289             :     // ???
     290             : 
     291             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L963-L973
     292             :        Note: for whatever reason, Agave returns InvalidInstructionDataSize instead of InvalidDataOffsets.
     293             :        We just return the err as is. */
     294           0 :     uchar const * sig = NULL;
     295           0 :     int err = fd_precompile_get_instr_data( txn_ctx,
     296           0 :                                             instr,
     297           0 :                                             sigoffs->sig_instr_idx,
     298           0 :                                             sigoffs->sig_offset,
     299           0 :                                             SIGNATURE_SERIALIZED_SIZE + 1, /* extra byte is recovery id */
     300           0 :                                             &sig );
     301           0 :     if( FD_UNLIKELY( err ) ) {
     302           0 :       txn_ctx->custom_err = (uint)err;
     303           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     304           0 :     }
     305             : 
     306             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L975-L981
     307             :        Note: we parse the signature and recovery id as part of fd_secp256k1_recover.
     308             :        Because of this, the return error code might be different from Agave in some edge cases. */
     309           0 :     int recovery_id = (int)sig[SIGNATURE_SERIALIZED_SIZE]; /* extra byte is recovery id */
     310             : 
     311             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L983-L989 */
     312           0 :     uchar const * eth_address = NULL;
     313           0 :     err = fd_precompile_get_instr_data( txn_ctx,
     314           0 :                                         instr,
     315           0 :                                         sigoffs->pubkey_instr_idx,
     316           0 :                                         sigoffs->pubkey_offset,
     317           0 :                                         SECP256K1_PUBKEY_SERIALIZED_SIZE,
     318           0 :                                         &eth_address );
     319           0 :     if( FD_UNLIKELY( err ) ) {
     320           0 :       txn_ctx->custom_err = (uint)err;
     321           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     322           0 :     }
     323             : 
     324             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L991-L997 */
     325           0 :     uchar const * msg = NULL;
     326           0 :     ushort msg_sz = sigoffs->msg_data_sz;
     327           0 :     err = fd_precompile_get_instr_data( txn_ctx,
     328           0 :                                         instr,
     329           0 :                                         sigoffs->msg_instr_idx,
     330           0 :                                         sigoffs->msg_offset,
     331           0 :                                         msg_sz,
     332           0 :                                         &msg );
     333           0 :     if( FD_UNLIKELY( err ) ) {
     334           0 :       txn_ctx->custom_err = (uint)err;
     335           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     336           0 :     }
     337             : 
     338             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L999-L1001 */
     339           0 :     uchar msg_hash[ FD_KECCAK256_HASH_SZ ];
     340           0 :     fd_keccak256_hash( msg, msg_sz, msg_hash );
     341             : 
     342             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L1003-L1008 */
     343           0 :     uchar pubkey[64];
     344           0 :     if ( FD_UNLIKELY( fd_secp256k1_recover( pubkey, msg_hash, sig, recovery_id ) == NULL ) ) {
     345           0 :       txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_SIGNATURE;
     346           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     347           0 :     }
     348             : 
     349             :     /* https://github.com/anza-xyz/agave/blob/v1.18.12/sdk/src/secp256k1_instruction.rs#L1009-L1013 */
     350           0 :     uchar pubkey_hash[ FD_KECCAK256_HASH_SZ ];
     351           0 :     fd_keccak256_hash( pubkey, 64, pubkey_hash );
     352             : 
     353           0 :     if( FD_UNLIKELY( memcmp( eth_address, pubkey_hash+(FD_KECCAK256_HASH_SZ-SECP256K1_PUBKEY_SERIALIZED_SIZE), SECP256K1_PUBKEY_SERIALIZED_SIZE ) ) ) {
     354           0 :       txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_SIGNATURE;
     355           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     356           0 :     }
     357           0 :   }
     358             : 
     359           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     360           0 : }
     361             : 
     362             : /*
     363             :   Secp256r1
     364             : */
     365             : 
     366             : int
     367           0 : fd_precompile_secp256r1_execute( fd_exec_instr_ctx_t * ctx ) {
     368           0 :   if( FD_FEATURE_ACTIVE( ctx->txn_ctx->slot, ctx->txn_ctx->features, move_precompile_verification_to_svm ) ) {
     369           0 :     fd_txn_instr_t const * instr = &ctx->txn_ctx->txn_descriptor->instr[ ctx->txn_ctx->current_instr_idx ];
     370           0 :     return fd_precompile_secp256r1_verify( ctx->txn_ctx, instr );
     371           0 :   } else {
     372           0 :     return FD_EXECUTOR_INSTR_SUCCESS;
     373           0 :   }
     374           0 : }
     375             : 
     376             : #ifdef FD_HAS_S2NBIGNUM
     377             : int
     378             : fd_precompile_secp256r1_verify( fd_exec_txn_ctx_t *    txn_ctx,
     379           0 :                                 fd_txn_instr_t const * instr ) {
     380             : 
     381           0 :   uchar const * data    = fd_txn_get_instr_data( instr, txn_ctx->_txn_raw->raw );
     382           0 :   ulong         data_sz = instr->data_sz;
     383             : 
     384             :   /* ... */
     385           0 :   if( FD_UNLIKELY( data_sz < DATA_START ) ) {
     386           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     387           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     388           0 :   }
     389             : 
     390           0 :   ulong sig_cnt = data[0];
     391           0 :   if( FD_UNLIKELY( sig_cnt==0 ) ) {
     392           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     393           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     394           0 :   }
     395             : 
     396             :   /* ... */
     397           0 :   ulong expected_data_size = sig_cnt * SIGNATURE_OFFSETS_SERIALIZED_SIZE + SIGNATURE_OFFSETS_START;
     398           0 :   if( FD_UNLIKELY( data_sz < expected_data_size ) ) {
     399           0 :     txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_INSTR_DATA_SIZE;
     400           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     401           0 :   }
     402             : 
     403           0 :   ulong off = SIGNATURE_OFFSETS_START;
     404           0 :   for( ulong i = 0; i < sig_cnt; ++i ) {
     405           0 :     fd_secp256r1_signature_offsets_t const * sigoffs = (const fd_secp256r1_signature_offsets_t *) (data + off);
     406           0 :     off += SIGNATURE_OFFSETS_SERIALIZED_SIZE;
     407             : 
     408             :     /* ... */
     409           0 :     uchar const * sig = NULL;
     410           0 :     int err = fd_precompile_get_instr_data( txn_ctx,
     411           0 :                                             instr,
     412           0 :                                             sigoffs->sig_instr_idx,
     413           0 :                                             sigoffs->sig_offset,
     414           0 :                                             SIGNATURE_SERIALIZED_SIZE,
     415           0 :                                             &sig );
     416           0 :     if( FD_UNLIKELY( err ) ) {
     417           0 :       txn_ctx->custom_err = (uint)err;
     418           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     419           0 :     }
     420             : 
     421             :     /* ... */
     422           0 :     uchar const * pubkey = NULL;
     423           0 :     err = fd_precompile_get_instr_data( txn_ctx,
     424           0 :                                         instr,
     425           0 :                                         sigoffs->pubkey_instr_idx,
     426           0 :                                         sigoffs->pubkey_offset,
     427           0 :                                         SECP256R1_PUBKEY_SERIALIZED_SIZE,
     428           0 :                                         &pubkey );
     429           0 :     if( FD_UNLIKELY( err ) ) {
     430           0 :       txn_ctx->custom_err = (uint)err;
     431           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     432           0 :     }
     433             : 
     434             :     /* ... */
     435           0 :     uchar const * msg = NULL;
     436           0 :     ushort msg_sz = sigoffs->msg_data_sz;
     437           0 :     err = fd_precompile_get_instr_data( txn_ctx,
     438           0 :                                         instr,
     439           0 :                                         sigoffs->msg_instr_idx,
     440           0 :                                         sigoffs->msg_offset,
     441           0 :                                         msg_sz,
     442           0 :                                         &msg );
     443           0 :     if( FD_UNLIKELY( err ) ) {
     444           0 :       txn_ctx->custom_err = (uint)err;
     445           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     446           0 :     }
     447             : 
     448             :     /* ... */
     449           0 :     fd_sha256_t sha[1];
     450           0 :     if( FD_UNLIKELY( fd_secp256r1_verify( msg, msg_sz, sig, pubkey, sha )!=FD_SECP256R1_SUCCESS ) ) {
     451           0 :       txn_ctx->custom_err = FD_EXECUTOR_PRECOMPILE_ERR_SIGNATURE;
     452           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     453           0 :     }
     454           0 :   }
     455             : 
     456           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     457           0 : }
     458             : #else
     459             : int
     460             : fd_precompile_secp256r1_verify( FD_PARAM_UNUSED fd_exec_txn_ctx_t *    txn_ctx,
     461             :                                 FD_PARAM_UNUSED fd_txn_instr_t const * instr ) {
     462             :   return FD_EXECUTOR_INSTR_ERR_FATAL;
     463             : }
     464             : #endif

Generated by: LCOV version 1.14