Skip to content

feat: Add dynamic crypto provider#2706

Closed
kevaundray wants to merge 57 commits intobluealloy:mainfrom
kevaundray:kw/precompile-dyn-crypto-provider
Closed

feat: Add dynamic crypto provider#2706
kevaundray wants to merge 57 commits intobluealloy:mainfrom
kevaundray:kw/precompile-dyn-crypto-provider

Conversation

@kevaundray
Copy link
Contributor

@kevaundray kevaundray commented Jul 14, 2025

Based off of #2705

This PR includes all of the changes including code restructuring now, to show what the end goal would be. If this looks okay, then I can split this into smaller PRs.

Changes:

  • All raw cryptography functions now have byte API
  • CryptoProvider trait now defines all of the raw cryptography functions that we need
  • DefaultCryptoProvider implements all of the methods on the trait and behaves similar to the previous code. The difference is that now we call get_crypto_provider to return DefaultCryptoProvider
  • cryptography functions have been moved to crypto_provider directory. It should now be possible to pull out this directory.

Comment on lines 66 to 68
pub fn install_provider<P: CryptoProvider>(provider: P) -> bool {
PROVIDER.set(Box::new(Box::new(provider))).is_ok()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this global installation of the crypto backend by having the run method for precompiles take a CryptoProvider parameter and it can be installed like:

new_evm().with_crypto_provider(Box::new(CustomCryptoProvider))?

And if with_crypto_provider is missing then it uses the default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this way initially so it doesn't change the API

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 14, 2025

CodSpeed Performance Report

Merging #2706 will not alter performance

Comparing kevaundray:kw/precompile-dyn-crypto-provider (f4a0c99) with main (066fc30)

Summary

✅ 161 untouched benchmarks

Comment on lines +3 to +5
use crate::crypto_provider::bn128::{
G1_LENGTH as G1_LEN, G2_LENGTH as G2_LEN, SCALAR_LENGTH as SCALAR_LEN,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this type alias to reduce the diff

kevaundray and others added 9 commits July 19, 2025 19:58
Co-authored-by: rakita <rakita@users.noreply.github.com>
Co-authored-by: rakita <rakita@users.noreply.github.com>
Co-authored-by: rakita <rakita@users.noreply.github.com>
Co-authored-by: rakita <rakita@users.noreply.github.com>
@kevaundray kevaundray changed the title chore(experiment): Add dynamic crypto provider feat: Add dynamic crypto provider Jul 19, 2025
Comment on lines +219 to +228
/// * `pk` - The uncompressed public key (secp256r1::PUBKEY_LENGTH bytes: 0x04 || x || y)
///
/// # Returns
/// `true` if the signature is valid, `false` otherwise.
fn secp256r1_verify(
&self,
msg: &[u8; secp256r1::MESSAGE_HASH_LENGTH],
sig: &[u8; secp256r1::SIGNATURE_LENGTH],
pk: &[u8; secp256r1::PUBKEY_LENGTH],
) -> bool;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would change with #2736

&self,
fp2_x: &[u8; bls12_381::FP_LENGTH],
fp2_y: &[u8; bls12_381::FP_LENGTH],
) -> Result<[u8; bls12_381::G2_LENGTH], PrecompileError>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically speaking, putting PrecompileError here and in the APIs for the cryptography code is leaking an abstraction, but I think it can be cleaned up after

@kevaundray
Copy link
Contributor Author

Superceded by #2786

@kevaundray kevaundray closed this Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant