Skip to main content

Crypto

Trait Crypto 

Source
pub trait Crypto:
    Send
    + Sync
    + Debug {
Show 21 methods // Provided methods fn secp256k1_ecrecover( &self, sig: &[u8; 64], recid: u8, msg: &[u8; 32], ) -> Result<[u8; 32], CryptoError> { ... } fn recover_signer( &self, sig: &[u8; 65], msg: &[u8; 32], ) -> Result<Address, CryptoError> { ... } fn keccak256(&self, input: &[u8]) -> [u8; 32] { ... } fn sha256(&self, input: &[u8]) -> [u8; 32] { ... } fn ripemd160(&self, input: &[u8]) -> [u8; 32] { ... } fn bn254_g1_add( &self, p1: &[u8], p2: &[u8], ) -> Result<[u8; 64], CryptoError> { ... } fn bn254_g1_mul( &self, point: &[u8], scalar: &[u8], ) -> Result<[u8; 64], CryptoError> { ... } fn bn254_pairing_check( &self, pairs: &[(&[u8], &[u8])], ) -> Result<bool, CryptoError> { ... } fn modexp( &self, base: &[u8], exp: &[u8], modulus: &[u8], ) -> Result<Vec<u8>, CryptoError> { ... } fn mulmod256(&self, a: &[u8; 32], b: &[u8; 32], m: &[u8; 32]) -> [u8; 32] { ... } fn blake2_compress( &self, rounds: u32, h: &mut [u64; 8], m: [u64; 16], t: [u64; 2], f: bool, ) { ... } fn secp256r1_verify( &self, msg: &[u8; 32], sig: &[u8; 64], pk: &[u8; 64], ) -> bool { ... } fn verify_kzg_proof( &self, z: &[u8; 32], y: &[u8; 32], commitment: &[u8; 48], proof: &[u8; 48], ) -> Result<(), CryptoError> { ... } fn verify_blob_kzg_proof( &self, blob: &[u8], commitment: &[u8; 48], proof: &[u8; 48], ) -> Result<bool, CryptoError> { ... } fn bls12_381_g1_add( &self, a: ([u8; 48], [u8; 48]), b: ([u8; 48], [u8; 48]), ) -> Result<[u8; 96], CryptoError> { ... } fn bls12_381_g1_msm( &self, pairs: &[(([u8; 48], [u8; 48]), [u8; 32])], ) -> Result<[u8; 96], CryptoError> { ... } fn bls12_381_g2_add( &self, a: ([u8; 48], [u8; 48], [u8; 48], [u8; 48]), b: ([u8; 48], [u8; 48], [u8; 48], [u8; 48]), ) -> Result<[u8; 192], CryptoError> { ... } fn bls12_381_g2_msm( &self, pairs: &[(([u8; 48], [u8; 48], [u8; 48], [u8; 48]), [u8; 32])], ) -> Result<[u8; 192], CryptoError> { ... } fn bls12_381_pairing_check( &self, pairs: &[(([u8; 48], [u8; 48]), ([u8; 48], [u8; 48], [u8; 48], [u8; 48]))], ) -> Result<bool, CryptoError> { ... } fn bls12_381_fp_to_g1(&self, fp: &[u8; 48]) -> Result<[u8; 96], CryptoError> { ... } fn bls12_381_fp2_to_g2( &self, fp2: ([u8; 48], [u8; 48]), ) -> Result<[u8; 192], CryptoError> { ... }
}
Expand description

All cryptographic operations the EVM needs.

Implementors provide the actual crypto — native libraries, zkVM circuits, or anything else. ethrex’s EVM code depends only on this trait.

Default implementations use native system libraries. Implementors only override methods where they need different behavior (e.g. zkVM-accelerated ECDSA or pairing checks).

Methods take &self to support &dyn Crypto (dynamic dispatch). Implementations are typically zero-sized structs.

§zkVM implementors

The following methods must be overridden for zkVM targets, as their default implementations use native C libraries (secp256k1, ark-bn254, etc.) that cannot run inside a zkVM guest:

Non-overridden methods will silently use the native default, which will fail to compile or panic at runtime inside a zkVM guest.

Provided Methods§

Source

fn secp256k1_ecrecover( &self, sig: &[u8; 64], recid: u8, msg: &[u8; 32], ) -> Result<[u8; 32], CryptoError>

Recover the Ethereum address from a 64-byte signature + recovery id + 32-byte message hash. Used by the ECRECOVER precompile (0x01). Returns the 32-byte keccak hash of the uncompressed public key (address is last 20 bytes).

Source

fn recover_signer( &self, sig: &[u8; 65], msg: &[u8; 32], ) -> Result<Address, CryptoError>

Recover the signer address from a 65-byte signature (r||s||v) + 32-byte message hash. Used by transaction validation (tx.sender()) and EIP-7702 authority recovery.

Source

fn keccak256(&self, input: &[u8]) -> [u8; 32]

Keccak-256 hash. Used by the KECCAK256 opcode (0x20) and address derivation.

Source

fn sha256(&self, input: &[u8]) -> [u8; 32]

SHA-256 hash. Used by SHA2-256 precompile (0x02) and KZG point evaluation.

Source

fn ripemd160(&self, input: &[u8]) -> [u8; 32]

RIPEMD-160 hash (zero-padded to 32 bytes). Used by RIPEMD-160 precompile (0x03).

Source

fn bn254_g1_add(&self, p1: &[u8], p2: &[u8]) -> Result<[u8; 64], CryptoError>

G1 point addition. Used by ECADD precompile (0x06). Input: two uncompressed G1 points (64 bytes each as big-endian x||y). Output: uncompressed G1 point (64 bytes).

Source

fn bn254_g1_mul( &self, point: &[u8], scalar: &[u8], ) -> Result<[u8; 64], CryptoError>

G1 scalar multiplication. Used by ECMUL precompile (0x07). Input: uncompressed G1 point (64 bytes) + scalar (32 bytes big-endian). Output: uncompressed G1 point (64 bytes).

Source

fn bn254_pairing_check( &self, pairs: &[(&[u8], &[u8])], ) -> Result<bool, CryptoError>

Pairing check. Used by ECPAIRING precompile (0x08). Input: pairs of (G1 64 bytes, G2 128 bytes) as raw byte slices. Returns true if the pairing equation holds.

Source

fn modexp( &self, base: &[u8], exp: &[u8], modulus: &[u8], ) -> Result<Vec<u8>, CryptoError>

Modular exponentiation (arbitrary precision). Used by MODEXP precompile (0x05).

Source

fn mulmod256(&self, a: &[u8; 32], b: &[u8; 32], m: &[u8; 32]) -> [u8; 32]

256-bit modular multiplication. Used by the MULMOD opcode. Default impl uses standard bigint arithmetic. ZisK overrides with a native circuit instruction.

Source

fn blake2_compress( &self, rounds: u32, h: &mut [u64; 8], m: [u64; 16], t: [u64; 2], f: bool, )

Blake2b compression function F. Used by BLAKE2F precompile (0x09).

Source

fn secp256r1_verify( &self, msg: &[u8; 32], sig: &[u8; 64], pk: &[u8; 64], ) -> bool

P-256 signature verification. Used by P256VERIFY precompile (0x0100, Osaka).

Source

fn verify_kzg_proof( &self, z: &[u8; 32], y: &[u8; 32], commitment: &[u8; 48], proof: &[u8; 48], ) -> Result<(), CryptoError>

Source

fn verify_blob_kzg_proof( &self, blob: &[u8], commitment: &[u8; 48], proof: &[u8; 48], ) -> Result<bool, CryptoError>

Source

fn bls12_381_g1_add( &self, a: ([u8; 48], [u8; 48]), b: ([u8; 48], [u8; 48]), ) -> Result<[u8; 96], CryptoError>

G1 addition. Returns 96-byte unpadded G1 point.

Source

fn bls12_381_g1_msm( &self, pairs: &[(([u8; 48], [u8; 48]), [u8; 32])], ) -> Result<[u8; 96], CryptoError>

G1 multi-scalar multiplication. Returns 96-byte unpadded G1 point.

Source

fn bls12_381_g2_add( &self, a: ([u8; 48], [u8; 48], [u8; 48], [u8; 48]), b: ([u8; 48], [u8; 48], [u8; 48], [u8; 48]), ) -> Result<[u8; 192], CryptoError>

G2 addition. Returns 192-byte unpadded G2 point.

Source

fn bls12_381_g2_msm( &self, pairs: &[(([u8; 48], [u8; 48], [u8; 48], [u8; 48]), [u8; 32])], ) -> Result<[u8; 192], CryptoError>

G2 multi-scalar multiplication. Returns 192-byte unpadded G2 point.

Source

fn bls12_381_pairing_check( &self, pairs: &[(([u8; 48], [u8; 48]), ([u8; 48], [u8; 48], [u8; 48], [u8; 48]))], ) -> Result<bool, CryptoError>

BLS12-381 pairing check.

Source

fn bls12_381_fp_to_g1(&self, fp: &[u8; 48]) -> Result<[u8; 96], CryptoError>

Map field element to G1 point.

Source

fn bls12_381_fp2_to_g2( &self, fp2: ([u8; 48], [u8; 48]), ) -> Result<[u8; 192], CryptoError>

Map field element pair to G2 point.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Crypto for NativeCrypto

Available on crate feature aws-lc-rs only.