-
Notifications
You must be signed in to change notification settings - Fork 1.2k
pallet-revive: add secp256r1 (0x100) precompile #10267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6521ac7
pallet-revive: add secp256r1 (0x100) precompile
pgherveou 5f4d8e5
Update from github-actions[bot] running command 'prdoc --audience run…
github-actions[bot] 58d78e3
Update from github-actions[bot] running command 'bench --runtime dev …
github-actions[bot] d0a766b
Merge branch 'master' into pg/0x100
pgherveou ab5f143
fix
pgherveou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| title: 'pallet-revive: add secp256r1 (0x100) precompile' | ||
| doc: | ||
| - audience: Runtime Dev | ||
| description: Add secp256r1 precompile (0x100) | ||
| crates: | ||
| - name: pallet-revive | ||
| bump: patch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
substrate/frame/revive/src/precompiles/builtin/p256_verify.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! # RIP-7212 secp256r1 Precompile | ||
| //! | ||
| //! This module implements the [RIP-7212](https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md) precompile for | ||
| //! secp256r1 curve support. | ||
| //! | ||
| //! The main purpose of this precompile is to verify ECDSA signatures that use the secp256r1, or | ||
| //! P256 elliptic curve. | ||
|
|
||
| use crate::{ | ||
| precompiles::{BuiltinAddressMatcher, Error, Ext, PrimitivePrecompile}, | ||
| vm::RuntimeCosts, | ||
| Config, U256, | ||
| }; | ||
| use alloc::vec::Vec; | ||
| use core::{marker::PhantomData, num::NonZero}; | ||
|
|
||
| pub struct P256Verify<T>(PhantomData<T>); | ||
|
|
||
| impl<T: Config> PrimitivePrecompile for P256Verify<T> { | ||
| type T = T; | ||
| const MATCHER: BuiltinAddressMatcher = BuiltinAddressMatcher::Fixed(NonZero::new(100).unwrap()); | ||
| const HAS_CONTRACT_INFO: bool = false; | ||
|
|
||
| /// [RIP-7212](https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md#specification) secp256r1 precompile. | ||
| /// | ||
| /// The input is encoded as follows: | ||
| /// | ||
| /// | signed message hash | r | s | public key x | public key y | | ||
| /// | :-----------------: | :-: | :-: | :----------: | :----------: | | ||
| /// | 32 | 32 | 32 | 32 | 32 | | ||
| fn call( | ||
| _address: &[u8; 20], | ||
| input: Vec<u8>, | ||
| env: &mut impl Ext<T = Self::T>, | ||
| ) -> Result<Vec<u8>, Error> { | ||
| env.gas_meter_mut().charge(RuntimeCosts::P256Verify)?; | ||
|
|
||
| if revm::precompile::secp256r1::verify_impl(&input).is_some() { | ||
| Ok(U256::one().to_big_endian().to_vec()) | ||
| } else { | ||
| Ok(Default::default()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::{precompiles::tests::run_test_vectors, tests::Test}; | ||
|
|
||
| #[test] | ||
| fn test_p256_verify() { | ||
| // https://github.com/ethereum/go-ethereum/blob/master/core/vm/testdata/precompiles/p256Verify.json | ||
| run_test_vectors::<P256Verify<Test>>(include_str!("./testdata/256-p256_verify.json")); | ||
| } | ||
| } |
5,476 changes: 5,476 additions & 0 deletions
5,476
substrate/frame/revive/src/precompiles/builtin/testdata/256-p256_verify.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.