Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
dab8913
wip
pgherveou Mar 29, 2023
d2da027
fix
pgherveou Mar 29, 2023
dd14d7f
wip
pgherveou Mar 29, 2023
d57ffcb
fix lint
pgherveou Mar 29, 2023
1a0f97d
rm fixture fix
pgherveou Mar 29, 2023
feba43d
missing comment
pgherveou Mar 29, 2023
d560208
fix lint
pgherveou Mar 29, 2023
c7e5430
add comment to the wsm file
pgherveou Mar 30, 2023
505919c
fix comment
pgherveou Mar 30, 2023
c5fba3a
Apply suggestions from code review
pgherveou Apr 3, 2023
a2ed618
wip
pgherveou Apr 4, 2023
1c305a4
wip weights
pgherveou Apr 4, 2023
5aa4bf1
wip weights
pgherveou Apr 4, 2023
57120be
Merge branch 'master' into pg/contracts-add-sr25519_recover
pgherveou Apr 4, 2023
2e7b44d
PR comment: test with return code
pgherveou Apr 4, 2023
f93e934
wip
pgherveou Apr 4, 2023
5084dbb
PR review add mock test
pgherveou Apr 4, 2023
90fd7ca
remove
pgherveou Apr 4, 2023
24fcbd4
lint
pgherveou Apr 4, 2023
66a9322
Update frame/contracts/fixtures/sr25519_verify.wat
pgherveou Apr 4, 2023
03ad784
fix comments
pgherveou Apr 5, 2023
dcd849d
Update frame/contracts/src/benchmarking/mod.rs
pgherveou Apr 5, 2023
b896c4c
Update frame/contracts/src/wasm/runtime.rs
pgherveou Apr 5, 2023
8a30502
Update frame/contracts/fixtures/sr25519_verify.wat
pgherveou Apr 5, 2023
d49c633
Update frame/contracts/src/benchmarking/mod.rs
pgherveou Apr 5, 2023
851111a
fix lint
pgherveou Apr 5, 2023
f7dda14
Merge branch 'master' of https://github.com/paritytech/substrate into…
Apr 5, 2023
a7ab12b
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Apr 5, 2023
d11b430
Update frame/contracts/src/wasm/runtime.rs
pgherveou Apr 5, 2023
9534b05
PR: review use unstable + remove arbitrary index 4
pgherveou Apr 5, 2023
c0a436a
Add benchmark for calculating overhead of calling sr25519_verify
pgherveou Apr 6, 2023
a39c784
fix message length encoding
pgherveou Apr 6, 2023
4f2b8e9
fix weights
pgherveou Apr 6, 2023
6fdb544
Merge branch 'master' of https://github.com/paritytech/substrate into…
Apr 6, 2023
3fbe20f
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Apr 6, 2023
44c0dca
Apply suggestions from code review
pgherveou Apr 7, 2023
5907075
Update frame/contracts/src/wasm/runtime.rs
pgherveou Apr 7, 2023
6c1bbdb
Update frame/contracts/src/wasm/runtime.rs
pgherveou Apr 7, 2023
4c1cee8
Update frame/contracts/src/benchmarking/mod.rs
pgherveou Apr 7, 2023
e2d01de
Update frame/contracts/src/benchmarking/mod.rs
pgherveou Apr 7, 2023
7d206cf
Update frame/contracts/src/schedule.rs
pgherveou Apr 12, 2023
5f1eb72
Update frame/contracts/src/schedule.rs
pgherveou Apr 12, 2023
9529c0a
Update frame/contracts/src/wasm/runtime.rs
pgherveou Apr 12, 2023
3b9b849
Update frame/contracts/src/wasm/runtime.rs
pgherveou Apr 12, 2023
e5c9723
PR review
pgherveou Apr 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
pgherveou committed Apr 4, 2023
commit a2ed6189c40e36fce5c2213843f14d3a01a7f12b
15 changes: 8 additions & 7 deletions frame/contracts/fixtures/sr25519_verify.wat
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@
(func (export "call")
;; define local variables
(local $signature_ptr i32)
(local $message_ptr i32)
(local $pub_key_ptr i32)
(local $message_len i32)
(local $message_ptr i32)
(local $result i32)

;; set the pointers to the memory locations
;; Memory layout during `call`
;; [10, 74) signature
;; [74, 86) message
;; [86, 118) public key
;; [74, 106) public key
;; [106, 118) message (12 bytes)
(local.set $signature_ptr (i32.const 10))
(local.set $message_ptr (i32.const 74))
(local.set $pub_key_ptr (i32.const 86))
(local.set $pub_key_ptr (i32.const 74))
(local.set $message_ptr (i32.const 106))

;; store the input into the memory, starting at the signature and
;; up to 108 bytes stored at offset 4
Expand All @@ -51,9 +52,9 @@
$result
(call $seal_sr25519_verify
(local.get $signature_ptr)
(local.get $message_ptr)
(i32.const 12)
(local.get $pub_key_ptr)
(i32.const 12)
(local.get $message_ptr)
)
)

Expand Down
54 changes: 53 additions & 1 deletion frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,9 +2020,61 @@ benchmarks! {
let origin = RawOrigin::Signed(instance.caller.clone());
}: call(origin, instance.addr, 0u32.into(), Weight::MAX, None, vec![])

// `n`: Messaqe input to verify in bytes
#[pov_mode = Measured]
seal_sr25519_verify {
let n in 0 .. T::MaxCodeLen::get() - 255; // need some buffer so the code size does not
// exceed the max code size.

let message = (0..n).zip((32u8..127u8).cycle()).map(|(_, c)| c).collect::<Vec<_>>().encode();
let message_len = message.len() as i32;

let key_type = sp_core::crypto::KeyTypeId(*b"code");
let pub_key = sp_io::crypto::sr25519_generate(key_type, None);
let sig = sp_io::crypto::sr25519_sign(key_type, &pub_key, &message).expect("Generates signature");
let sig = AsRef::<[u8; 64]>::as_ref(&sig).to_vec();

let code = WasmModule::<T>::from(ModuleDefinition {
memory: Some(ImportedMemory::max::<T>()),
imported_functions: vec![ImportedFunction {
module: "seal0",
name: "seal_sr25519_verify",
params: vec![ValueType::I32, ValueType::I32, ValueType::I32, ValueType::I32],
return_type: Some(ValueType::I32),
}],
data_segments: vec![
DataSegment {
offset: 0,
value: sig,
},
DataSegment {
offset: 64,
value: pub_key.to_vec(),
},
DataSegment {
offset: 96,
value: message,
},
],
call_body: Some(body::plain(vec![
Instruction::I32Const(0), // signature_ptr
Instruction::I32Const(64), // pub_key_ptr
Instruction::I32Const(message_len), // message_len
Instruction::I32Const(96), // message_ptr
Instruction::Call(0),
Instruction::Drop,
Instruction::End,
])),
.. Default::default()
});

let instance = Contract::<T>::new(code, vec![])?;
let origin = RawOrigin::Signed(instance.caller.clone());
}: call(origin, instance.addr, 0u32.into(), Weight::MAX, None, vec![])

// Only calling the function itself with valid arguments.
// It generates different private keys and signatures for the message "Hello world".
// This is a slow call: We redeuce the number of runs.
// This is a slow call: We reduce the number of runs.
#[pov_mode = Measured]
seal_ecdsa_recover {
let r in 0 .. API_BENCHMARK_RUNS / 10;
Expand Down
11 changes: 6 additions & 5 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ use sp_core::{
ecdsa::Public as ECDSAPublic,
sr25519::{Public as SR25519Public, Signature as SR25519Signature},
};
use sp_io::{
crypto::{secp256k1_ecdsa_recover_compressed, sr25519_verify as crypto_sr25519_verify},
hashing::blake2_256,
};
use sp_io::{crypto::secp256k1_ecdsa_recover_compressed, hashing::blake2_256};
use sp_runtime::traits::{Convert, Hash};
use sp_std::{marker::PhantomData, mem, prelude::*, vec::Vec};

Expand Down Expand Up @@ -1357,7 +1354,11 @@ where
}

fn sr25519_verify(&self, signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> bool {
crypto_sr25519_verify(&SR25519Signature(*signature), message, &SR25519Public(*pub_key))
sp_io::crypto::sr25519_verify(
&SR25519Signature(*signature),
message,
&SR25519Public(*pub_key),
)
}

fn ecdsa_to_eth_address(&self, pk: &[u8; 33]) -> Result<[u8; 20], ()> {
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2947,8 +2947,8 @@ fn sr25519_verify() {

let mut params = vec![];
params.extend_from_slice(&signature);
params.extend_from_slice(&message.to_vec().encode());
params.extend_from_slice(&public_key);
params.extend_from_slice(&message.to_vec().encode());

<Pallet<Test>>::bare_call(
ALICE,
Expand Down
20 changes: 12 additions & 8 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub enum RuntimeCosts {
/// Weight of calling `seal_ecdsa_recover`.
EcdsaRecovery,
/// Weight of calling `seal_sr25519_verify`.
Sr25519Verify,
Sr25519Verify(u32),
/// Weight charged by a chain extension through `seal_call_chain_extension`.
ChainExtension(Weight),
/// Weight charged for calling into the runtime.
Expand All @@ -276,6 +276,7 @@ impl RuntimeCosts {
let weight = match *self {
MeteringBlock(amount) => s.gas.saturating_add(Weight::from_parts(amount, 0)),
CopyFromContract(len) => s.return_per_byte.saturating_mul(len.into()),
Sr25519Verify(len) => s.sr25519_verify.saturating_mul(len.into()),
CopyToContract(len) => s.input_per_byte.saturating_mul(len.into()),
Caller => s.caller,
IsContract => s.is_contract,
Expand Down Expand Up @@ -339,7 +340,6 @@ impl RuntimeCosts {
.hash_blake2_128
.saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())),
EcdsaRecovery => s.ecdsa_recover,
Sr25519Verify => s.sr25519_verify,
ChainExtension(weight) => weight,
CallRuntime(weight) => weight,
SetCodeHash => s.set_code_hash,
Expand Down Expand Up @@ -2476,11 +2476,11 @@ pub mod env {
///
/// - `signature_ptr`: the pointer into the linear memory where the signature is placed. Should
/// be decodable as a 64 bytes. Traps otherwise.
/// - `message_ptr`: the pointer into the linear memory where the message is placed.
/// - `message_len`: the length of the message payload. Should be decodable as a Scale encoded
/// Vec<u8>. Traps otherwise.
/// - `pub_key_ptr`: the pointer into the linear memory where the public key is placed. Should
/// be decodable as a 32 bytes. Traps otherwise.
/// - `message_len`: the length of the message payload.
/// - `message_ptr`: the pointer into the linear memory where the message is placed. Should be
/// decodable as a Scale encoded Vec<u8>. Traps otherwise
/// # Errors
///
/// - `ReturnCode::Sr25519VerifyFailed
Expand All @@ -2490,17 +2490,21 @@ pub mod env {
ctx: _,
memory: _,
signature_ptr: u32,
message_ptr: u32,
message_len: u32,
pub_key_ptr: u32,
message_len: u32,
message_ptr: u32,
) -> Result<ReturnCode, TrapReason> {
ctx.charge_gas(RuntimeCosts::Sr25519Verify)?;
ctx.charge_gas(RuntimeCosts::Sr25519Verify(message_len))?;

let mut signature: [u8; 64] = [0; 64];
ctx.read_sandbox_memory_into_buf(memory, signature_ptr, &mut signature)?;

let mut pub_key: [u8; 32] = [0; 32];
ctx.read_sandbox_memory_into_buf(memory, pub_key_ptr, &mut pub_key)?;

let message: Vec<u8> =
ctx.read_sandbox_memory_as_unbounded(memory, message_ptr, message_len)?;

if ctx.ext.sr25519_verify(&signature, &message, &pub_key) {
Ok(ReturnCode::Success)
} else {
Expand Down
Loading