Corpus Core Docs

crypto.h

Helper functions for crypto operations.

address_t[ADDRESS_SIZE]

util/crypto.h

Ethereum address type (20 bytes)

typedef uint8_t address_t[ADDRESS_SIZE];

bytes32_t[BYTES32_SIZE]

util/crypto.h

32-byte hash or value type

typedef uint8_t bytes32_t[BYTES32_SIZE];

bls_pubkey_t[BLS_PUBKEY_SIZE]

util/crypto.h

BLS12-381 public key type (48 bytes compressed)

typedef uint8_t bls_pubkey_t[BLS_PUBKEY_SIZE];

bls_signature_t[BLS_SIGNATURE_SIZE]

util/crypto.h

BLS12-381 signature type (96 bytes)

typedef uint8_t bls_signature_t[BLS_SIGNATURE_SIZE];

keccak

util/crypto.h

Computes the Keccak-256 hash of the input data.

void keccak(bytes_t data, uint8_t* out) 

Parameters

  • data : The input data to hash
  • out : Pointer to 32-byte buffer to store the hash result (must not be NULL)

sha256

util/crypto.h

Computes the SHA-256 hash of the input data.

void sha256(bytes_t data, uint8_t* out) 

Parameters

  • data : The input data to hash
  • out : Pointer to 32-byte buffer to store the hash result (must not be NULL)

sha256_merkle

util/crypto.h

Computes the SHA-256 hash of two concatenated data buffers (merkle node hash). This is equivalent to sha256(data1 || data2) but more efficient.

void sha256_merkle(bytes_t data1, bytes_t data2, uint8_t* out) 

Parameters

  • data1 : The first data buffer
  • data2 : The second data buffer
  • out : Pointer to 32-byte buffer to store the hash result (must not be NULL)

blst_deserialize_p1_affine

util/crypto.h

Deserializes compressed BLS12-381 public keys into affine point representation. This is used as an optimization to avoid repeated deserialization during verification.

bytes_t blst_deserialize_p1_affine(uint8_t* compressed_pubkeys, int num_public_keys, uint8_t* out);

Parameters

  • compressed_pubkeys : Pointer to compressed public keys (48 bytes each, must not be NULL)
  • num_public_keys : The number of public keys to deserialize
  • out : Optional pre-allocated buffer for the result. If NULL, memory will be allocated.

Returns

A bytes_t containing the deserialized public keys, or NULL_BYTES on error

deserialized)

util/crypto.h

Verifies a BLS12-381 aggregate signature against a message and a set of public keys. This function aggregates the specified public keys and verifies the signature using pairing operations on the BLS12-381 curve.

Example:

bytes32_t msg_hash;
bls_signature_t sig;
uint8_t pubkeys[3 * 48];  // 3 public keys
uint8_t bitmask[1] = {0b00000101};  // Use keys 0 and 2
bool valid = blst_verify(msg_hash, sig, pubkeys, 3, bytes(bitmask, 1), false);
bool blst_verify(bytes32_t       message,
                 bls_signature_t signature,
                 uint8_t*        public_keys,
                 int             num_public_keys,
                 bytes_t         pubkey_bitmask,
                 bool            deserialized) 

Parameters

  • message : 32-byte hashed message (must not be NULL)
  • signature : 96-byte BLS signature (must not be NULL)
  • public_keys : Array of public keys, either 48 bytes each (compressed) or 96 bytes each (deserialized affine points) (must not be NULL)
  • num_public_keys : The total number of public keys in the array
  • pubkey_bitmask : Bitmask indicating which public keys to aggregate (length must be num_public_keys/8)
  • deserialized : If true, public_keys contains deserialized affine points (96 bytes each); if false, compressed keys (48 bytes each)

Returns

true if the signature is valid, false otherwise

secp256k1_recover

util/crypto.h

Recovers the public key from a secp256k1 ECDSA signature. Used primarily in Ethereum to derive the signer's address from a transaction signature.

bool secp256k1_recover(const bytes32_t digest, bytes_t signature, uint8_t* pubkey) 

Parameters

  • digest : The 32-byte message digest that was signed (must not be NULL)
  • signature : The signature bytes (must be exactly 65 bytes: r||s||v where v is the recovery id)
  • pubkey : Pointer to 64-byte buffer to store the recovered uncompressed public key (must not be NULL)

Returns

true if recovery was successful, false on error (invalid signature or recovery id)

secp256k1_sign

util/crypto.h

Signs a digest with a secp256k1 private key using ECDSA.

bool secp256k1_sign(const bytes32_t sk, const bytes32_t digest, uint8_t* signature) NONNULL;

Parameters

  • sk : The 32-byte secret key (must not be NULL)
  • digest : The 32-byte message digest to sign (must not be NULL)
  • signature : Pointer to 65-byte buffer to store the signature (r||s||v format, must not be NULL)

Returns

true if signing was successful, false on error (invalid private key)