pub trait CryptoProvider:
Send
+ Sync
+ 'static {
// Required method
fn recover_signer_unchecked(
&self,
sig: &[u8; 65],
msg: &[u8; 32],
) -> Result<Address, RecoveryError>;
}crypto-backend only.Expand description
Trait for cryptographic providers that can perform signature recovery.
This trait allows pluggable cryptographic backends for Ethereum signature recovery. By default, alloy uses compile-time selected implementations (secp256k1 or k256), but applications can install a custom provider to override this behavior.
§Why is this needed?
The primary reason is performance - when targeting special execution environments that require custom cryptographic logic. For example, zkVMs (zero-knowledge virtual machines) may have special accelerators that would allow them to perform signature recovery faster.
§Usage
- Enable the
crypto-backendfeature in yourCargo.toml - Implement the
CryptoProvidertrait for your custom backend - Install it globally using
install_default_provider - All subsequent signature recovery operations will use your provider
Note: This trait currently only provides signature recovery functionality,
not signature creation. For signature creation, use the compile-time selected
implementations in the secp256k1 module.
use alloy_consensus::crypto::backend::{CryptoProvider, install_default_provider};
use alloy_primitives::Address;
use alloc::sync::Arc;
struct MyCustomProvider;
impl CryptoProvider for MyCustomProvider {
fn recover_signer_unchecked(
&self,
sig: &[u8; 65],
msg: &[u8; 32],
) -> Result<Address, RecoveryError> {
// Your custom implementation here
todo!()
}
}
// Install your provider globally
install_default_provider(Arc::new(MyCustomProvider)).unwrap();Required Methods§
Sourcefn recover_signer_unchecked(
&self,
sig: &[u8; 65],
msg: &[u8; 32],
) -> Result<Address, RecoveryError>
fn recover_signer_unchecked( &self, sig: &[u8; 65], msg: &[u8; 32], ) -> Result<Address, RecoveryError>
Recover signer from signature and message hash, without ensuring low S values.