diff --git a/Node/Cargo.lock b/Node/Cargo.lock index 1aef33b..ddda898 100644 --- a/Node/Cargo.lock +++ b/Node/Cargo.lock @@ -143,11 +143,11 @@ dependencies = [ [[package]] name = "alloy-chains" -version = "0.1.51" +version = "0.1.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4e0f0136c085132939da6b753452ebed4efaa73fe523bb855b10c199c2ebfaf" +checksum = "56f15afc5993458b42739ab3b69bdb6b4c8112acd3997dbea9bc092c9517137c" dependencies = [ - "alloy-primitives 0.8.16", + "alloy-primitives 0.8.18", "num_enum", "strum 0.26.3", ] @@ -364,29 +364,17 @@ dependencies = [ [[package]] name = "alloy-primitives" -version = "0.8.16" +version = "0.8.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0540fd0355d400b59633c27bd4b42173e59943f28e9d3376b77a24771d432d04" +checksum = "788bb18e8f61d5d9340b52143f27771daf7e1dccbaf2741621d2493f9debf52e" dependencies = [ - "alloy-rlp", "bytes", "cfg-if", "const-hex", "derive_more 1.0.0", - "foldhash", - "hashbrown 0.15.2", - "hex-literal", - "indexmap 2.7.0", "itoa", - "k256 0.13.4", - "keccak-asm", "paste", - "proptest", - "rand 0.8.5", "ruint", - "rustc-hash 2.1.0", - "serde", - "sha3", "tiny-keccak", ] @@ -854,35 +842,6 @@ version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" -[[package]] -name = "ark-bls12-381" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c775f0d12169cba7aae4caeb547bb6a50781c7449a8aa53793827c9ec4abf488" -dependencies = [ - "ark-ec", - "ark-ff 0.4.2", - "ark-serialize 0.4.2", - "ark-std 0.4.0", -] - -[[package]] -name = "ark-ec" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "defd9a439d56ac24968cca0571f598a61bc8c55f71d50a89cda591cb750670ba" -dependencies = [ - "ark-ff 0.4.2", - "ark-poly", - "ark-serialize 0.4.2", - "ark-std 0.4.0", - "derivative", - "hashbrown 0.13.2", - "itertools 0.10.5", - "num-traits", - "zeroize", -] - [[package]] name = "ark-ff" version = "0.3.0" @@ -966,19 +925,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "ark-poly" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d320bfc44ee185d899ccbadfa8bc31aab923ce1558716e1997a1e74057fe86bf" -dependencies = [ - "ark-ff 0.4.2", - "ark-serialize 0.4.2", - "ark-std 0.4.0", - "derivative", - "hashbrown 0.13.2", -] - [[package]] name = "ark-serialize" version = "0.3.0" @@ -995,23 +941,11 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" dependencies = [ - "ark-serialize-derive", "ark-std 0.4.0", "digest 0.10.7", "num-bigint", ] -[[package]] -name = "ark-serialize-derive" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae3281bc6d0fd7e549af32b52511e1302185bd688fd3359fa36423346ff682ea" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "ark-std" version = "0.3.0" @@ -1602,23 +1536,6 @@ dependencies = [ "piper", ] -[[package]] -name = "bls_on_arkworks" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4107cadb31de0d0c1282dd57efe395706b1d81feb697799871f95c3324af7c44" -dependencies = [ - "ark-bls12-381", - "ark-ec", - "ark-ff 0.4.2", - "ark-serialize 0.4.2", - "ark-std 0.4.0", - "hkdf", - "hmac 0.12.1", - "libm", - "sha2 0.10.8", -] - [[package]] name = "blst" version = "0.3.13" @@ -2701,29 +2618,6 @@ dependencies = [ "sha2 0.10.8", ] -[[package]] -name = "ethereum_ssz" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e999563461faea0ab9bc0024e5e66adcee35881f3d5062f52f31a4070fe1522" -dependencies = [ - "alloy-primitives 0.8.16", - "itertools 0.13.0", - "smallvec", -] - -[[package]] -name = "ethereum_ssz_derive" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3deae99c8e74829a00ba7a92d49055732b3c1f093f2ccfa3cbc621679b6fa91" -dependencies = [ - "darling 0.20.10", - "proc-macro2", - "quote", - "syn 2.0.94", -] - [[package]] name = "ethers-core" version = "2.0.14" @@ -3224,15 +3118,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "hashbrown" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" -dependencies = [ - "ahash", -] - [[package]] name = "hashbrown" version = "0.14.5" @@ -7754,15 +7639,13 @@ dependencies = [ "anyhow", "beacon-api-client", "bincode", - "bls_on_arkworks", + "blst", "chrono", "clap", "dotenv", "ecdsa 0.16.9", "elliptic-curve 0.13.8", "ethereum-consensus", - "ethereum_ssz", - "ethereum_ssz_derive", "futures-util", "hex", "jsonrpsee", @@ -7771,7 +7654,6 @@ dependencies = [ "mockall", "mockall_double", "mockito", - "num-bigint", "p2p-network", "rand 0.8.5", "rand_core 0.6.4", @@ -8810,9 +8692,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.21" +version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6f5bb5257f2407a5425c6e749bfd9692192a73e70a6060516ac04f889087d68" +checksum = "39281189af81c07ec09db316b302a3e67bf9bd7cbf6c820b50e35fee9c2fa980" dependencies = [ "memchr", ] diff --git a/Node/Cargo.toml b/Node/Cargo.toml index ae73e25..762942d 100644 --- a/Node/Cargo.toml +++ b/Node/Cargo.toml @@ -34,14 +34,11 @@ bincode = "1.3" serde_bytes = "0.11" clap = "4.5" futures-util = "0.3" -ethereum_ssz = "0.7" -ethereum_ssz_derive = "0.7" -bls_on_arkworks = "0.3.0" -num-bigint = "0.4.6" rand = "0.8" tree_hash = "0.6.0" tree_hash_derive = "0.6.0" reth-primitives = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" } +blst = "0.3" [dev-dependencies] mockall_double = "0.3" diff --git a/Node/src/bls/mod.rs b/Node/src/bls/mod.rs index 79a97f4..79b0963 100644 --- a/Node/src/bls/mod.rs +++ b/Node/src/bls/mod.rs @@ -1,10 +1,7 @@ use alloy::primitives::U256; use anyhow::Error; -use bls::types::{G1AffinePoint, G2AffinePoint, PublicKey, SecretKey, Signature}; -use bls_on_arkworks as bls; -use ethereum_consensus::crypto::{PublicKey as EthereumPublicKey, SecretKey as EthereumSecretKey}; -use ethereum_consensus::primitives::BlsSignature; -use num_bigint::BigUint; + +use blst::min_pk::{PublicKey, SecretKey, Signature}; #[cfg(test)] #[cfg(not(feature = "use_mock"))] use rand_core::{OsRng, RngCore}; @@ -12,87 +9,66 @@ use rand_core::{OsRng, RngCore}; pub struct BLSService { pk: PublicKey, sk: SecretKey, - eth_secret_key: EthereumSecretKey, - eth_public_key: EthereumPublicKey, } impl BLSService { pub fn new(private_key: &str) -> Result { let pk_bytes = alloy::hex::decode(private_key) - .map_err(|e| anyhow::anyhow!("BLSService: failed to decode private key: {}", e))?; - let sk = bls::os2ip(&pk_bytes); - let public_key = bls::sk_to_pk(sk); - - let eth_secret_key = EthereumSecretKey::try_from(private_key.to_string()) - .map_err(|e| anyhow::anyhow!("Invalid secret key: {:?}", e))?; - let eth_public_key = eth_secret_key.public_key(); - - tracing::info!( - "BLSService: public key: {}", - hex::encode(public_key.clone()) - ); - - Ok(Self { - pk: public_key, - sk, - eth_public_key, - eth_secret_key, - }) + .map_err(|e| anyhow::anyhow!("BLSService: failed to decode secret key: {}", e))?; + let sk = SecretKey::from_bytes(&pk_bytes).map_err(|e| { + anyhow::anyhow!( + "BLSService: failed to create secret key from bytes: {:?}", + e + ) + })?; + let pk = sk.sk_to_pk(); + + Ok(Self { pk, sk }) } #[cfg(test)] #[cfg(not(feature = "use_mock"))] - pub fn generate_key() -> Self { + pub fn generate_key() -> Result { let mut ikm = [0u8; 64]; OsRng.fill_bytes(&mut ikm); - let sk = bls::keygen(&ikm.to_vec()); - let pk = bls::sk_to_pk(sk); - - let eth_secret_key = EthereumSecretKey::random(&mut OsRng).unwrap(); - let eth_public_key = eth_secret_key.public_key(); - - Self { - pk, - sk, - eth_public_key, - eth_secret_key, - } - } + let sk = SecretKey::key_gen(&ikm.to_vec(), &[]) + .map_err(|e| anyhow::anyhow!("BLSService: failed to generate secret key: {:?}", e))?; + let pk = sk.sk_to_pk(); - pub fn sign(&self, message: &Vec, dst: &Vec) -> Signature { - bls::sign(self.sk, message, dst).unwrap() + Ok(Self { pk, sk }) } - pub fn sign_as_point(&self, message: &Vec, dst: &Vec) -> G2AffinePoint { - let sign = self.sign(message, dst); - bls::signature_to_point(&sign).unwrap() + pub fn sign(&self, message: &[u8], dst: &[u8]) -> Signature { + self.sk.sign(message, dst, &[]) } - pub fn biguint_to_u256_array(biguint: BigUint) -> [U256; 2] { - let s = format!("{:0>96x}", biguint); - let res1 = U256::from_str_radix(&s[0..32], 16).unwrap(); - let res2 = U256::from_str_radix(&s[32..96], 16).unwrap(); - + fn to_contract_layout(value: &[u8; 48]) -> [U256; 2] { + let mut buffer = [0u8; 32]; + buffer[16..32].copy_from_slice(&value[0..16]); + let res1: alloy::primitives::Uint<256, 4> = U256::from_be_bytes::<32>(buffer); + let res2: alloy::primitives::Uint<256, 4> = + U256::from_be_bytes::<32>(value[16..48].try_into().unwrap()); [res1, res2] } - #[cfg(test)] - #[cfg(not(feature = "use_mock"))] - pub fn get_public_key_compressed(&self) -> PublicKey { - self.pk.clone() - } - - pub fn get_public_key(&self) -> G1AffinePoint { - bls::pubkey_to_point(&self.pk).unwrap() + pub fn pubkey_to_g1_point(&self) -> [[U256; 2]; 2] { + let pk = self.get_public_key().serialize(); + let x = Self::to_contract_layout(pk[0..48].try_into().unwrap()); + let y = Self::to_contract_layout(pk[48..96].try_into().unwrap()); + [x, y] } - pub fn get_ethereum_public_key(&self) -> EthereumPublicKey { - self.eth_public_key.clone() + pub fn signature_to_g2_point(&self, signature: &Signature) -> [[U256; 2]; 4] { + let signature = signature.serialize(); + let x = Self::to_contract_layout(signature[0..48].try_into().unwrap()); + let x_i = Self::to_contract_layout(signature[48..96].try_into().unwrap()); + let y = Self::to_contract_layout(signature[96..144].try_into().unwrap()); + let y_i = Self::to_contract_layout(signature[144..192].try_into().unwrap()); + [x, x_i, y, y_i] } - pub fn sign_with_ethereum_secret_key(&self, message: &[u8]) -> Result { - let signature = self.eth_secret_key.sign(message); - Ok(signature) + pub fn get_public_key(&self) -> PublicKey { + self.pk } } diff --git a/Node/src/ethereum_l1/execution_layer.rs b/Node/src/ethereum_l1/execution_layer.rs index 371ca75..aff0965 100644 --- a/Node/src/ethereum_l1/execution_layer.rs +++ b/Node/src/ethereum_l1/execution_layer.rs @@ -28,7 +28,6 @@ use futures_util::StreamExt; use k256::Secp256k1; #[cfg(test)] use mockall::automock; -use num_bigint::BigUint; use rand_core::{OsRng, RngCore}; use std::str::FromStr; use std::sync::Arc; @@ -622,20 +621,21 @@ impl ExecutionLayer { let message = data.abi_encode_packed(); // Convert bls public key to G1Point - let pk_point = self.bls_service.get_public_key(); + let pk_point = self.bls_service.pubkey_to_g1_point(); let pubkey = PreconfRegistry::G1Point { - x: BLSService::biguint_to_u256_array(BigUint::from(pk_point.x)), - y: BLSService::biguint_to_u256_array(BigUint::from(pk_point.y)), + x: pk_point[0], + y: pk_point[1], }; + let signature = self.bls_service.sign(&message, &[]); // Sign message and convert to G2Point - let signature_point = self.bls_service.sign_as_point(&message, &vec![]); + let signature_point = self.bls_service.signature_to_g2_point(&signature); let signature = PreconfRegistry::G2Point { - x: BLSService::biguint_to_u256_array(BigUint::from(signature_point.x.c0)), - x_I: BLSService::biguint_to_u256_array(BigUint::from(signature_point.x.c1)), - y: BLSService::biguint_to_u256_array(BigUint::from(signature_point.y.c0)), - y_I: BLSService::biguint_to_u256_array(BigUint::from(signature_point.y.c1)), + x: signature_point[0], + x_I: signature_point[1], + y: signature_point[2], + y_I: signature_point[3], }; // Call contract @@ -680,20 +680,21 @@ impl ExecutionLayer { let message = data.abi_encode_packed(); // Convert bls public key to G1Point - let pk_point = self.bls_service.get_public_key(); + let pk_point = self.bls_service.pubkey_to_g1_point(); let pubkey = PreconfRegistry::G1Point { - x: BLSService::biguint_to_u256_array(BigUint::from(pk_point.x)), - y: BLSService::biguint_to_u256_array(BigUint::from(pk_point.y)), + x: pk_point[0], + y: pk_point[1], }; + let signature = self.bls_service.sign(&message, &[]); // Sign message and convert to G2Point - let signature_point = self.bls_service.sign_as_point(&message, &vec![]); + let signature_point = self.bls_service.signature_to_g2_point(&signature); let signature = PreconfRegistry::G2Point { - x: BLSService::biguint_to_u256_array(BigUint::from(signature_point.x.c0)), - x_I: BLSService::biguint_to_u256_array(BigUint::from(signature_point.x.c1)), - y: BLSService::biguint_to_u256_array(BigUint::from(signature_point.y.c0)), - y_I: BLSService::biguint_to_u256_array(BigUint::from(signature_point.y.c1)), + x: signature_point[0], + x_I: signature_point[1], + y: signature_point[2], + y_I: signature_point[3], }; // Call contract diff --git a/Node/src/mev_boost/constraints.rs b/Node/src/mev_boost/constraints.rs index 86e2b15..0811af6 100644 --- a/Node/src/mev_boost/constraints.rs +++ b/Node/src/mev_boost/constraints.rs @@ -1,10 +1,10 @@ use crate::bls::BLSService; use alloy::signers::k256::sha2::{Digest, Sha256}; use anyhow::Error; -use ethereum_consensus::crypto::PublicKey; -use ethereum_consensus::primitives::BlsSignature; +use blst::min_pk::{PublicKey, Signature}; use ethereum_consensus::state_transition::Context; use reth_primitives::PooledTransactionsElement; +use serde::ser::Serializer; use serde::Serialize; use std::sync::Arc; use tree_hash::TreeHash; @@ -12,14 +12,23 @@ use tree_hash_derive::TreeHash; pub const GENESIS_VALIDATORS_ROOT: [u8; 32] = [0; 32]; pub const COMMIT_BOOST_DOMAIN: [u8; 4] = [109, 109, 111, 67]; +const BLS_DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; #[derive(Debug, Clone, Serialize, Eq, PartialEq)] pub struct ConstraintsMessage { + #[serde(serialize_with = "serialize_publickey")] pub pubkey: PublicKey, pub slot: u64, pub top: bool, pub transactions: Vec>, } +fn serialize_publickey(data: &PublicKey, serializer: S) -> Result +where + S: Serializer, +{ + let hex_string = format!("0x{}", alloy::hex::encode(data.compress())); + serializer.serialize_str(&hex_string) +} impl ConstraintsMessage { pub fn new(pubkey: PublicKey, slot: u64, transactions: Vec>) -> Self { @@ -33,7 +42,7 @@ impl ConstraintsMessage { fn digest(&self) -> Result<[u8; 32], Error> { let mut hasher = Sha256::new(); - hasher.update(self.pubkey.to_vec()); + hasher.update(self.pubkey.compress()); hasher.update(self.slot.to_le_bytes()); hasher.update((self.top as u8).to_le_bytes()); for tx in self.transactions.iter() { @@ -50,7 +59,16 @@ impl ConstraintsMessage { #[derive(Debug, Clone, Serialize)] pub struct SignedConstraints { pub message: ConstraintsMessage, - pub signature: BlsSignature, + #[serde(serialize_with = "serialize_signature")] + pub signature: Signature, +} + +fn serialize_signature(data: &Signature, serializer: S) -> Result +where + S: Serializer, +{ + let hex_string = format!("0x{}", alloy::hex::encode(data.compress())); + serializer.serialize_str(&hex_string) } impl SignedConstraints { @@ -108,10 +126,110 @@ impl SignedConstraints { let signing_root = Self::compute_signing_root_custom(digest.tree_hash_root().0, domain); // Sign message - let signature = bls - .sign_with_ethereum_secret_key(&signing_root) - .map_err(|e| anyhow::anyhow!("Sign_with_domain error: {}", e))?; + let signature = bls.sign(&signing_root, BLS_DST); Ok(Self { message, signature }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_constraints_message_digest() { + let constraints = vec![vec![ + 2, 249, 3, 213, 131, 48, 24, 36, 6, 132, 59, 154, 202, 0, 133, 4, 168, 23, 200, 0, 131, + 15, 66, 64, 148, 96, 100, 247, 86, 247, 243, 220, 130, 128, 193, 207, 160, 28, 228, 26, + 55, 181, 241, 109, 241, 128, 185, 3, 100, 242, 118, 107, 125, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 96, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 3, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 69, 97, + 210, 209, 67, 98, 30, 18, 110, 135, 131, 26, 239, 40, 118, 120, 180, 66, 184, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 188, 101, 74, 119, 65, 102, 81, 67, 67, 47, 47, 104, 55, 117, 72, + 107, 67, 43, 72, 97, 68, 65, 111, 120, 89, 65, 89, 82, 51, 78, 90, 81, 65, 104, 81, 98, + 56, 73, 54, 119, 65, 103, 119, 77, 48, 85, 74, 82, 83, 107, 97, 85, 53, 70, 48, 101, + 70, 43, 116, 121, 84, 55, 47, 54, 99, 110, 79, 116, 54, 86, 72, 71, 97, 53, 73, 99, 86, + 85, 80, 102, 99, 112, 119, 65, 65, 103, 77, 65, 66, 111, 75, 114, 75, 88, 82, 69, 106, + 104, 98, 106, 115, 115, 104, 112, 50, 119, 104, 83, 102, 54, 47, 117, 79, 101, 70, 110, + 69, 82, 56, 113, 57, 52, 99, 80, 119, 103, 120, 116, 108, 51, 110, 79, 78, 111, 67, 82, + 115, 53, 98, 70, 52, 108, 87, 85, 113, 101, 118, 122, 121, 109, 106, 50, 75, 81, 71, + 74, 106, 57, 117, 87, 54, 107, 110, 74, 101, 85, 86, 68, 112, 47, 81, 43, 83, 65, 49, + 78, 74, 65, 81, 65, 65, 47, 47, 56, 97, 57, 84, 57, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 1, + 160, 175, 154, 114, 57, 217, 247, 104, 167, 114, 56, 70, 166, 250, 175, 140, 155, 4, + 255, 254, 185, 177, 119, 184, 80, 186, 72, 127, 32, 38, 28, 186, 156, 160, 63, 77, 214, + 119, 203, 52, 47, 233, 31, 158, 227, 102, 147, 186, 40, 117, 153, 98, 44, 15, 194, 109, + 222, 72, 181, 48, 135, 170, 91, 85, 51, 123, + ]]; + let slot_id = 17; + let pubkey = PublicKey::from_bytes(&alloy::hex::decode("908d6f98b5eaf6ac1b632c6b80b304612d48afd9c104874f9025960accdae128028119608b0d95a7e141390101fba669").unwrap()).unwrap(); + + let message = ConstraintsMessage::new(pubkey, slot_id, constraints); + + assert_eq!( + message.digest().unwrap(), + [ + 9, 87, 0, 71, 187, 129, 0, 133, 126, 114, 244, 187, 129, 105, 194, 105, 195, 115, + 27, 220, 144, 157, 88, 34, 184, 108, 130, 34, 84, 248, 88, 125 + ] + ); + } + + #[test] + fn test_signed_constraints_compute_domain_custom() { + let mut context = Context::for_minimal(); + context.genesis_fork_version = [16, 0, 0, 56]; + + let domain = SignedConstraints::compute_domain_custom(&context, COMMIT_BOOST_DOMAIN); + + assert_eq!( + domain, + [ + 109, 109, 111, 67, 11, 65, 190, 76, 219, 52, 209, 131, 221, 220, 165, 57, 131, 55, + 98, 109, 205, 207, 175, 23, 32, 193, 32, 45, 59, 149, 248, 78 + ] + ); + } + + #[test] + fn test_signed_constraints_compute_signing_root_custom() { + let digest = [ + 9, 87, 0, 71, 187, 129, 0, 133, 126, 114, 244, 187, 129, 105, 194, 105, 195, 115, 27, + 220, 144, 157, 88, 34, 184, 108, 130, 34, 84, 248, 88, 125, + ]; + let domain = [ + 109, 109, 111, 67, 11, 65, 190, 76, 219, 52, 209, 131, 221, 220, 165, 57, 131, 55, 98, + 109, 205, 207, 175, 23, 32, 193, 32, 45, 59, 149, 248, 78, + ]; + + let signing_root = + SignedConstraints::compute_signing_root_custom(digest.tree_hash_root().0, domain); + + assert_eq!( + signing_root, + [ + 46, 115, 119, 45, 23, 162, 89, 198, 203, 58, 165, 97, 28, 21, 0, 117, 149, 27, 106, + 219, 120, 115, 115, 227, 114, 157, 221, 247, 183, 14, 65, 54 + ] + ); + } +} diff --git a/Node/src/mev_boost/mod.rs b/Node/src/mev_boost/mod.rs index a7a2647..bcce988 100644 --- a/Node/src/mev_boost/mod.rs +++ b/Node/src/mev_boost/mod.rs @@ -48,7 +48,7 @@ impl MevBoost { bls_service: Arc, ) -> Result<(), Error> { // Prepare the message - let pubkey = bls_service.get_ethereum_public_key(); + let pubkey = bls_service.get_public_key(); let message = ConstraintsMessage::new(pubkey, slot_id, constraints); let signed = SignedConstraints::new(message, bls_service, self.genesis_fork_version)?; @@ -61,3 +61,69 @@ impl MevBoost { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mev_boost() { + let constraints = vec![vec![ + 2, 249, 3, 213, 131, 48, 24, 36, 6, 132, 59, 154, 202, 0, 133, 4, 168, 23, 200, 0, 131, + 15, 66, 64, 148, 96, 100, 247, 86, 247, 243, 220, 130, 128, 193, 207, 160, 28, 228, 26, + 55, 181, 241, 109, 241, 128, 185, 3, 100, 242, 118, 107, 125, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 96, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 3, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 69, 97, + 210, 209, 67, 98, 30,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 188, 101, 74, 119, 65, 102, 81, 67, 67, 47, 47, 104, 55, 117, 72, + 107, 67, 43, 72, 97, 68, 65, 111, 120, 89, 65, 89, 82, 51, 78, 90, 81, 65, 104, 81, 98, + 56, 73, 54, 119, 65, 103, 119, 77, 48, 85, 74, 82, 83, 107, 97, 85, 53, 70, 48, 101, + 70, 43, 116, 121, 84, 55, 47, 54, 99, 110, 79, 116, 54, 86, 72, 71, 97, 53, 73, 99, 86, + 85, 80, 102, 99, 112, 119, 65, 65, 103, 77, 65, 66, 111, 75, 114, 75, 88, 82, 69, 106, + 104, 98, 106, 115, 115, 104, 112, 50, 119, 104, 83, 102, 54, 47, 117, 79, 101, 70, 110, + 69, 82, 56, 113, 57, 52, 99, 80, 119, 103, 120, 116, 108, 51, 110, 79, 78, 111, 67, 82, + 115, 53, 98, 70, 52, 108, 87, 85, 113, 101, 118, 122, 121, 109, 106, 50, 75, 81, 71, + 74, 106, 57, 117, 87, 54, 107, 110, 74, 101, 85, 86, 68, 112, 47, 81, 43, 83, 65, 49, + 78, 74, 65, 81, 65, 65, 47, 47, 56, 97, 57, 84, 57, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 1, + 160, 175, 154, 114, 57, 217, 247, 104, 167, 114, 56, 70, 166, 250, 175, 140, 155, 4, + 255, 254, 185, 177, 119, 184, 80, 186, 72, 127, 32, 38, 28, 186, 156, 160, 63, 77, 214, + 119, 203, 52, 47, 233, 31, 158, 227, 102, 147, 186, 40, 117, 153, 98, 44, 15, 194, 109, + 222, 72, 181, 48, 135, 170, 91, 85, 51, 123, + ]]; + let slot_id = 11; + let genesis_fork_version = [16, 0, 0, 56]; + let bls_service = Arc::new( + BLSService::new("108e47e28c6c6027eac478d742cbb5ef675e20fdb6cbfcbf2bf0cd725b649813") + .unwrap(), + ); + let pubkey = bls_service.get_public_key(); + let message = ConstraintsMessage::new(pubkey, slot_id, constraints); + let signed = SignedConstraints::new(message, bls_service, genesis_fork_version).unwrap(); + + let json_data = serde_json::to_value([&signed]).unwrap(); + + assert_eq!(json_data[0]["message"]["pubkey"], "0x908d6f98b5eaf6ac1b632c6b80b304612d48afd9c104874f9025960accdae128028119608b0d95a7e141390101fba669"); + assert_eq!(json_data[0]["message"]["slot"], 11); + assert_eq!(json_data[0]["signature"],"0x952d6076a21d43069b883b84e015fb3f56d4333b069bda1a120243e020d560b5f0b4af297d32e2cba69e9c67bb4a320a0e980df34b3934c4554a6fb32e3490d568d885217b4d5657fc58cefb30f30209f40c0223480c897600313a650fa29ab2"); + } +} diff --git a/Node/src/registration/tests.rs b/Node/src/registration/tests.rs index ec683ee..ef773ad 100644 --- a/Node/src/registration/tests.rs +++ b/Node/src/registration/tests.rs @@ -156,7 +156,7 @@ mod tests { let preconf_task_manager = get_contract_address(&output, "Preconf Task Manager"); // Create a new BLSService with a random private key - let bls_service = Arc::new(BLSService::generate_key()); + let bls_service = Arc::new(BLSService::generate_key().unwrap()); // Create AVS contract addresses let avs_contracts = AvsContractAddresses { @@ -221,7 +221,7 @@ mod tests { } // Copy logic form smart contract to get public key hash - let pk_compressed = bls_service.get_public_key_compressed(); + let pk_compressed = bls_service.get_public_key().compress(); let mut res_arr: [u8; 32] = [0; 32]; res_arr[16..32].copy_from_slice(&pk_compressed[0..16]); let res1 = U256::from_be_bytes(res_arr);