From 9d57c4eb4d894becba3e9a94ae0215949010ef84 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 19 Jan 2025 00:16:50 -0500 Subject: [PATCH] Downscope dependencies in serai-processor-ethereum-primitives, const-hex decode bytecode in ethereum-schnorr-contract --- Cargo.lock | 3 ++- networks/ethereum/schnorr/Cargo.toml | 2 ++ networks/ethereum/schnorr/README.md | 3 ++- networks/ethereum/schnorr/src/lib.rs | 12 ++++++++++-- networks/ethereum/schnorr/src/tests/premise.rs | 12 ++++-------- processor/ethereum/primitives/Cargo.toml | 2 +- processor/ethereum/primitives/src/lib.rs | 6 +++--- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f28b9701..b247b181 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2209,6 +2209,7 @@ dependencies = [ "alloy-simple-request-transport", "alloy-sol-types", "build-solidity-contracts", + "const-hex", "group", "k256", "rand_core", @@ -6959,7 +6960,7 @@ name = "serai-processor-ethereum-primitives" version = "0.1.0" dependencies = [ "alloy-consensus", - "alloy-core", + "alloy-primitives", "group", "k256", ] diff --git a/networks/ethereum/schnorr/Cargo.toml b/networks/ethereum/schnorr/Cargo.toml index 42797bb7..5883a453 100644 --- a/networks/ethereum/schnorr/Cargo.toml +++ b/networks/ethereum/schnorr/Cargo.toml @@ -16,6 +16,8 @@ rustdoc-args = ["--cfg", "docsrs"] workspace = true [dependencies] +const-hex = { version = "1", default-features = false, features = ["std", "core-error"] } + subtle = { version = "2", default-features = false, features = ["std"] } sha3 = { version = "0.10", default-features = false, features = ["std"] } group = { version = "0.13", default-features = false, features = ["alloc"] } diff --git a/networks/ethereum/schnorr/README.md b/networks/ethereum/schnorr/README.md index 410cf520..896c92c1 100644 --- a/networks/ethereum/schnorr/README.md +++ b/networks/ethereum/schnorr/README.md @@ -2,4 +2,5 @@ An Ethereum contract to verify Schnorr signatures. -This crate will fail to build if `solc` is not installed and available. +This crate will fail to build if the expected version of `solc` is not +installed and available. diff --git a/networks/ethereum/schnorr/src/lib.rs b/networks/ethereum/schnorr/src/lib.rs index 3f67fbbf..ec6f6277 100644 --- a/networks/ethereum/schnorr/src/lib.rs +++ b/networks/ethereum/schnorr/src/lib.rs @@ -4,8 +4,16 @@ #![allow(non_snake_case)] /// The initialization bytecode of the Schnorr library. -pub const INIT_BYTECODE: &str = - include_str!(concat!(env!("OUT_DIR"), "/ethereum-schnorr-contract/Schnorr.bin")); +pub const BYTECODE: &[u8] = { + const BYTECODE_HEX: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/ethereum-schnorr-contract/Schnorr.bin")); + const BYTECODE: [u8; BYTECODE_HEX.len() / 2] = + match const_hex::const_decode_to_array::<{ BYTECODE_HEX.len() / 2 }>(BYTECODE_HEX) { + Ok(bytecode) => bytecode, + Err(_) => panic!("Schnorr.bin did not contain valid hex"), + }; + &BYTECODE +}; mod public_key; pub use public_key::PublicKey; diff --git a/networks/ethereum/schnorr/src/tests/premise.rs b/networks/ethereum/schnorr/src/tests/premise.rs index 28d9135d..dee78e44 100644 --- a/networks/ethereum/schnorr/src/tests/premise.rs +++ b/networks/ethereum/schnorr/src/tests/premise.rs @@ -18,14 +18,10 @@ use crate::{Signature, tests::test_key}; fn ecrecover(message: Scalar, odd_y: bool, r: Scalar, s: Scalar) -> Option<[u8; 20]> { let sig = ecdsa::Signature::from_scalars(r, s).ok()?; let message: [u8; 32] = message.to_repr().into(); - alloy_core::primitives::Signature::from_signature_and_parity( - sig, - alloy_core::primitives::Parity::Parity(odd_y), - ) - .ok()? - .recover_address_from_prehash(&alloy_core::primitives::B256::from(message)) - .ok() - .map(Into::into) + alloy_core::primitives::PrimitiveSignature::from_signature_and_parity(sig, odd_y) + .recover_address_from_prehash(&alloy_core::primitives::B256::from(message)) + .ok() + .map(Into::into) } // Test ecrecover behaves as expected diff --git a/processor/ethereum/primitives/Cargo.toml b/processor/ethereum/primitives/Cargo.toml index 180f5db7..05b23189 100644 --- a/processor/ethereum/primitives/Cargo.toml +++ b/processor/ethereum/primitives/Cargo.toml @@ -20,5 +20,5 @@ workspace = true group = { version = "0.13", default-features = false } k256 = { version = "^0.13.1", default-features = false, features = ["std", "arithmetic"] } -alloy-core = { version = "0.8", default-features = false } +alloy-primitives = { version = "0.8", default-features = false } alloy-consensus = { version = "0.9", default-features = false, features = ["k256"] } diff --git a/processor/ethereum/primitives/src/lib.rs b/processor/ethereum/primitives/src/lib.rs index eb7bd615..dadc5424 100644 --- a/processor/ethereum/primitives/src/lib.rs +++ b/processor/ethereum/primitives/src/lib.rs @@ -5,12 +5,12 @@ use group::ff::PrimeField; use k256::Scalar; -use alloy_core::primitives::PrimitiveSignature; +use alloy_primitives::PrimitiveSignature; use alloy_consensus::{SignableTransaction, Signed, TxLegacy}; /// The Keccak256 hash function. pub fn keccak256(data: impl AsRef<[u8]>) -> [u8; 32] { - alloy_core::primitives::keccak256(data.as_ref()).into() + alloy_primitives::keccak256(data.as_ref()).into() } /// Deterministically sign a transaction. @@ -67,7 +67,7 @@ fn test_deterministically_sign() { let signed = deterministically_sign(tx.clone()); assert!(signed.recover_signer().is_ok()); - let one = alloy_core::primitives::U256::from(1u64); + let one = alloy_primitives::U256::from(1u64); assert_eq!(signed.signature().r(), one); assert_eq!(signed.signature().s(), one);