Skip to content

Commit

Permalink
runtime-sdk/modules/evm: Add Sr25519 to confidential precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Dec 6, 2024
1 parent dcb85ac commit 2af15ac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
1 change: 1 addition & 0 deletions runtime-sdk/modules/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ test = ["serde", "serde_json"]
[[bench]]
name = "criterion_benchmark"
harness = false
required-features = ["test"]

[[bin]]
name = "fuzz-precompile"
Expand Down
75 changes: 49 additions & 26 deletions runtime-sdk/modules/evm/src/precompile/confidential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static KEYPAIR_GENERATE_BASE_COST: Lazy<HashMap<SignatureType, u64>> = Lazy::new
(SignatureType::Secp256k1_PrehashedSha256, 1_500),
(SignatureType::Secp256r1_PrehashedSha256, 4_000),
(SignatureType::Secp384r1_PrehashedSha384, 18_000),
(SignatureType::Sr25519_Pure, 1_000),
])
});

Expand All @@ -58,6 +59,7 @@ static SIGN_MESSAGE_COST: Lazy<HashMap<SignatureType, (u64, u64)>> = Lazy::new(|
(SignatureType::Secp256k1_PrehashedSha256, (3_000, 0)),
(SignatureType::Secp256r1_PrehashedSha256, (9_000, 0)),
(SignatureType::Secp384r1_PrehashedSha384, (43_200, 0)),
(SignatureType::Sr25519_Pure, (1_500, 8)),
])
});

Expand All @@ -72,6 +74,7 @@ static VERIFY_MESSAGE_COST: Lazy<HashMap<SignatureType, (u64, u64)>> = Lazy::new
(SignatureType::Secp256k1_PrehashedSha256, (3_000, 0)),
(SignatureType::Secp256r1_PrehashedSha256, (7_900, 0)),
(SignatureType::Secp384r1_PrehashedSha384, (37_920, 0)),
(SignatureType::Sr25519_Pure, (2_000, 8)),
])
});

Expand Down Expand Up @@ -680,21 +683,6 @@ mod test {
.expect("call should return something")
.expect_err("call should fail");

// Unsupported method.
let params = ethabi::encode(&[
Token::Uint(6.into()), // sr25519 is not yet supported.
Token::Bytes(b"01234567890123456789012345678901".to_vec()),
]);
call_contract(
H160([
0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x05,
]),
&params,
10_000_000,
)
.expect("call should return something")
.expect_err("call should fail");

// Working test.
let params = ethabi::encode(&[
Token::Uint(SignatureType::Ed25519_Oasis.as_int().into()),
Expand Down Expand Up @@ -773,13 +761,18 @@ mod test {
bench_keypair_generate(b, SignatureType::Secp384r1_PrehashedSha384);
}

#[bench]
fn bench_keypair_generate_sr25519(b: &mut Bencher) {
bench_keypair_generate(b, SignatureType::Sr25519_Pure);
}

#[test]
fn test_basic_roundtrip() {
let seed = b"01234567890123456789012345678901";
let context = b"test context";
let message = b"test message";

for method in 0u8..6u8 {
for method in 0u8..=6u8 {
let sig_type: SignatureType = method.try_into().unwrap();
if sig_type.is_prehashed() {
// Tested in test_basic_roundtrip_prehashed below.
Expand Down Expand Up @@ -999,11 +992,6 @@ mod test {
.expect("call should return something")
.expect_err("call should fail");

// Unsupported method.
push_all_and_test(Some(6), None, None, None) // sr25519 is not yet supported.
.expect("call should return something")
.expect_err("call should fail");

// All ok, with context.
push_all_and_test(None, None, None, None)
.expect("call should return something")
Expand Down Expand Up @@ -1110,6 +1098,26 @@ mod test {
bench_signer(b, SignatureType::Secp384r1_PrehashedSha384, false, false);
}

#[bench]
fn bench_sign_sr25519_shortctx_shortmsg(b: &mut Bencher) {
bench_signer(b, SignatureType::Sr25519_Pure, false, false);
}

#[bench]
fn bench_sign_sr25519_shortctx_longmsg(b: &mut Bencher) {
bench_signer(b, SignatureType::Sr25519_Pure, false, true);
}

#[bench]
fn bench_sign_sr25519_longctx_shortmsg(b: &mut Bencher) {
bench_signer(b, SignatureType::Sr25519_Pure, true, false);
}

#[bench]
fn bench_sign_sr25519_longctx_longmsg(b: &mut Bencher) {
bench_signer(b, SignatureType::Sr25519_Pure, true, true);
}

#[test]
fn test_verification_params() {
fn push_all_and_test(
Expand Down Expand Up @@ -1156,11 +1164,6 @@ mod test {
.expect("call should return something")
.expect_err("call should fail");

// Unsupported method.
push_all_and_test(Some(6), None, None, None, None) // sr25519 is not yet supported.
.expect("call should return something")
.expect_err("call should fail");

// Invalid public key.
let zeroes: Vec<u8> = vec![0; 32];
let mut output = push_all_and_test(None, Some(&zeroes), None, None, None)
Expand Down Expand Up @@ -1307,4 +1310,24 @@ mod test {
fn bench_verify_secp384r1_prehashed_sha384(b: &mut Bencher) {
bench_verification(b, SignatureType::Secp384r1_PrehashedSha384, false, false);
}

#[bench]
fn bench_verify_sr25519_shortctx_shortmsg(b: &mut Bencher) {
bench_verification(b, SignatureType::Sr25519_Pure, false, false);
}

#[bench]
fn bench_verify_sr25519_shortctx_longmsg(b: &mut Bencher) {
bench_verification(b, SignatureType::Sr25519_Pure, false, true);
}

#[bench]
fn bench_verify_sr25519_longctx_shortmsg(b: &mut Bencher) {
bench_verification(b, SignatureType::Sr25519_Pure, true, false);
}

#[bench]
fn bench_verify_sr25519_longctx_longmsg(b: &mut Bencher) {
bench_verification(b, SignatureType::Sr25519_Pure, true, true);
}
}

0 comments on commit 2af15ac

Please sign in to comment.