Skip to content

Commit

Permalink
feat(precompile): Add secp256r1 precompile (#2859)
Browse files Browse the repository at this point in the history
* feat(precompile): Add secp256r1 precompile

* feat(precompile): move secp256r1 precompile code from frontier

* add tests

* Add weight handling for p256verify precompile

* add runtime-benchmark feature to mock code

* fix file format

* test(p256verify): validate used gas

* remove unused code

* remove test files

* test: fix errors

* apply remarks from review

* fix implementation to match the specification

* remove unused import

* remove unused variable

* double weight for `p256_verify` precompile
  • Loading branch information
RomarQ authored Jul 17, 2024
1 parent 41c2c8f commit 4557eaa
Show file tree
Hide file tree
Showing 23 changed files with 691 additions and 106 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ members = [
"precompiles/relay-data-verifier",
"precompiles/xcm-transactor",
"precompiles/xtokens",
"precompiles/p256verify",
"primitives/storage-proof",
"runtime/moonbase",
"runtime/moonbeam",
Expand Down Expand Up @@ -97,6 +98,7 @@ pallet-evm-precompile-relay-verifier = { path = "precompiles/relay-data-verifier
pallet-evm-precompile-xcm-transactor = { path = "precompiles/xcm-transactor", default-features = false }
pallet-evm-precompile-xcm-utils = { path = "precompiles/xcm-utils", default-features = false }
pallet-evm-precompile-xtokens = { path = "precompiles/xtokens", default-features = false }
pallet-evm-precompile-p256verify = { path = "precompiles/p256verify", default-features = false }
pallet-evm-precompileset-assets-erc20 = { path = "precompiles/assets-erc20", default-features = false }
pallet-moonbeam-orbiters = { path = "pallets/moonbeam-orbiters", default-features = false }
pallet-parachain-staking = { path = "pallets/parachain-staking", default-features = false }
Expand Down Expand Up @@ -368,6 +370,9 @@ slices = "0.2.0"
smallvec = "1.8.0"
strum = { version = "0.24", default-features = false, features = ["derive"] }
strum_macros = "0.24"
p256 = { version = "0.13.2", default-features = false, features = [
"ecdsa",
] }

# Other (client)

Expand Down
6 changes: 6 additions & 0 deletions benchmarking/frame-weight-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ impl<T: frame_system::Config> {{pallet}}::WeightInfo for WeightInfo<T> {
// 1 DB read that happen when filtering the proxy call transaction
.saturating_add(T::DbWeight::get().reads(1))
{{/if}}
{{#if (and (eq ../pallet "pallet_precompile_benchmarks") (eq benchmark.name "p256_verify"))}}
// TODO: Remove this multiplication once we are comfortable with the weight estimation
// Double the weight just to mitigate the possibility of having a signature that
// takes longer to verify
.saturating_mul(1u64)
{{/if}}
}
{{/each}}
}
10 changes: 10 additions & 0 deletions pallets/precompile-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.1.0"
[dependencies]
# Moonbeam
storage-proof-primitives = { workspace = true }
pallet-evm-precompile-p256verify = { workspace = true }

# substrate
cumulus-primitives-core = { workspace = true }
Expand All @@ -20,6 +21,11 @@ sp-std = { workspace = true }
# Moonkit
pallet-relay-storage-roots = { workspace = true }

# Frontier
precompile-utils = { workspace = true }
fp-evm = { workspace = true }
evm = { workspace = true }

# Benchmarks
frame-benchmarking = { workspace = true, optional = true }

Expand All @@ -31,10 +37,14 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-relay-storage-roots/std",
"pallet-evm-precompile-p256verify/std",
"precompile-utils/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-core/std",
"sp-std/std",
"fp-evm/std",
"evm/std",
]

runtime-benchmarks = [
Expand Down
47 changes: 43 additions & 4 deletions pallets/precompile-benchmarks/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
#![cfg(feature = "runtime-benchmarks")]
#![allow(clippy::no_effect)]

use crate::weights::WeightInfo;
use crate::{Config, Pallet};
use core::marker::PhantomData;
use cumulus_primitives_core::relay_chain::BlockNumber as RelayBlockNumber;
use fp_evm::{Context, Precompile, PrecompileResult};
use frame_benchmarking::benchmarks;
use frame_support::{traits::Get, BoundedVec};
use frame_support::{traits::Get, weights::Weight, BoundedVec};
use parity_scale_codec::{Decode, Encode};
use sp_core::H256;
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
use sp_std::{collections::btree_map::BTreeMap, vec, vec::Vec};

use crate::mock;

fn fill_relay_storage_roots<T: Config>() -> Vec<u32> {
// Initialize keys BoundedVec for RelayStorageRoots
Expand All @@ -45,6 +50,25 @@ fn get_latest_relay_block<T: Config>() -> RelayBlockNumber {
.expect("At least one relay block should be store")
}

fn p256verify<T: Config>(input: Vec<u8>) -> PrecompileResult {
let context: Context = Context {
address: Default::default(),
caller: Default::default(),
apparent_value: From::from(0),
};

let mut handle = mock::MockHandle::new(input, 4000, context);

struct P256VerifyWeight<T>(PhantomData<T>);
impl<T: Config> Get<Weight> for P256VerifyWeight<T> {
fn get() -> Weight {
<T as Config>::WeightInfo::p256_verify()
}
}

pallet_evm_precompile_p256verify::P256Verify::<P256VerifyWeight<T>>::execute(&mut handle)
}

benchmarks! {
verify_entry {
// x is the number of nodes in the proof
Expand Down Expand Up @@ -78,8 +102,23 @@ benchmarks! {
}
verify {
assert_eq!(
get_latest_relay_block::<T>(),
keys.last().cloned().expect("There should be at least one key")
get_latest_relay_block::<T>(), keys.last().cloned().expect("There should be at least one key")
);
}

p256_verify {
let input = vec![
181, 167, 126, 122, 144, 170, 20, 224, 191, 95, 51, 127, 6, 245, 151, 20, 134, 118, 66,
79, 174, 38, 225, 117, 198, 229, 98, 28, 52, 53, 25, 85, 40, 159, 49, 151, 137, 218,
66, 72, 69, 201, 234, 201, 53, 36, 95, 205, 221, 128, 89, 80, 226, 240, 37, 6, 208,
155, 231, 228, 17, 25, 149, 86, 210, 98, 20, 68, 117, 177, 250, 70, 173, 133, 37, 7,
40, 198, 0, 197, 61, 253, 16, 248, 179, 244, 173, 241, 64, 226, 114, 65, 174, 195, 194,
218, 58, 129, 4, 103, 3, 252, 207, 70, 139, 72, 177, 69, 249, 57, 239, 219, 185, 108,
55, 134, 219, 113, 43, 49, 19, 187, 36, 136, 239, 40, 108, 220, 239, 138, 254, 130,
210, 0, 165, 187, 54, 181, 70, 33, 102, 232, 206, 119, 242, 216, 49, 165, 46, 242, 19,
91, 42, 241, 136, 17, 11, 234, 239, 177
];
}:{
let _ = p256verify::<T>(input).expect("Should verify the signature without any errors.");
}
}
1 change: 1 addition & 0 deletions pallets/precompile-benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#[cfg(feature = "runtime-benchmarks")]
mod benchmarks;
mod mock;
pub mod weights;

pub use crate::weights::WeightInfo;
Expand Down
Loading

0 comments on commit 4557eaa

Please sign in to comment.