Skip to content

Commit 7003500

Browse files
committed
Simplify Phase 2: deduplicate signing logic and deser_pubkey_hex
1 parent dcb6d4d commit 7003500

File tree

2 files changed

+22
-68
lines changed

2 files changed

+22
-68
lines changed

bin/ethlambda/src/main.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use ethlambda_network_api::{InitBlockChain, InitP2P, ToBlockChainToP2PRef, ToP2P
2323
use ethlambda_p2p::{Bootnode, P2P, SwarmConfig, build_swarm, parse_enrs};
2424
use ethlambda_types::primitives::H256;
2525
use ethlambda_types::{
26-
genesis::GenesisConfig,
26+
genesis::{GenesisConfig, deser_pubkey_hex},
2727
signature::ValidatorSecretKey,
2828
state::{State, ValidatorPubkeyBytes},
2929
};
@@ -236,20 +236,6 @@ struct AnnotatedValidator {
236236
proposal_privkey_file: PathBuf,
237237
}
238238

239-
pub fn deser_pubkey_hex<'de, D>(d: D) -> Result<ValidatorPubkeyBytes, D::Error>
240-
where
241-
D: serde::Deserializer<'de>,
242-
{
243-
use serde::de::Error;
244-
245-
let value = String::deserialize(d)?;
246-
let pubkey: ValidatorPubkeyBytes = hex::decode(&value)
247-
.map_err(|_| D::Error::custom("ValidatorPubkey value is not valid hex"))?
248-
.try_into()
249-
.map_err(|_| D::Error::custom("ValidatorPubkey length != 52"))?;
250-
Ok(pubkey)
251-
}
252-
253239
fn read_validator_keys(
254240
validators_path: impl AsRef<Path>,
255241
validator_keys_dir: impl AsRef<Path>,

crates/blockchain/src/key_manager.rs

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,11 @@ pub enum KeyManagerError {
1919
SignatureConversionError(String),
2020
}
2121

22-
/// A validator's dual XMSS key pair for attestation and block proposal signing.
23-
///
24-
/// Each key is independent and advances its OTS preparation separately,
25-
/// allowing the validator to sign both an attestation and a block proposal
26-
/// within the same slot.
2722
pub struct ValidatorKeyPair {
2823
pub attestation_key: ValidatorSecretKey,
2924
pub proposal_key: ValidatorSecretKey,
3025
}
3126

32-
/// Manages validator secret keys for signing attestations and block proposals.
33-
///
34-
/// Each validator has two independent XMSS keys: one for attestation signing
35-
/// and one for block proposal signing.
3627
pub struct KeyManager {
3728
keys: HashMap<u64, ValidatorKeyPair>,
3829
}
@@ -42,78 +33,50 @@ impl KeyManager {
4233
Self { keys }
4334
}
4435

45-
/// Returns a list of all registered validator IDs.
4636
pub fn validator_ids(&self) -> Vec<u64> {
4737
self.keys.keys().copied().collect()
4838
}
4939

50-
/// Signs an attestation using the validator's attestation key.
5140
pub fn sign_attestation(
5241
&mut self,
5342
validator_id: u64,
5443
attestation_data: &AttestationData,
5544
) -> Result<XmssSignature, KeyManagerError> {
56-
let message_hash = attestation_data.tree_hash_root();
45+
let message = attestation_data.tree_hash_root();
5746
let slot = attestation_data.slot as u32;
58-
self.sign_with_attestation_key(validator_id, slot, &message_hash)
47+
let key_pair = self
48+
.keys
49+
.get_mut(&validator_id)
50+
.ok_or(KeyManagerError::ValidatorKeyNotFound(validator_id))?;
51+
let sig = Self::sign_with_key(&mut key_pair.attestation_key, slot, &message)?;
52+
metrics::inc_pq_sig_attestation_signatures();
53+
Ok(sig)
5954
}
6055

61-
/// Signs a block root using the validator's proposal key.
6256
pub fn sign_block_root(
6357
&mut self,
6458
validator_id: u64,
6559
slot: u32,
6660
block_root: &H256,
67-
) -> Result<XmssSignature, KeyManagerError> {
68-
self.sign_with_proposal_key(validator_id, slot, block_root)
69-
}
70-
71-
fn sign_with_attestation_key(
72-
&mut self,
73-
validator_id: u64,
74-
slot: u32,
75-
message: &H256,
7661
) -> Result<XmssSignature, KeyManagerError> {
7762
let key_pair = self
7863
.keys
7964
.get_mut(&validator_id)
8065
.ok_or(KeyManagerError::ValidatorKeyNotFound(validator_id))?;
81-
82-
let signature: ValidatorSignature = {
83-
let _timing = metrics::time_pq_sig_attestation_signing();
84-
key_pair
85-
.attestation_key
86-
.sign(slot, message)
87-
.map_err(|e| KeyManagerError::SigningError(e.to_string()))
88-
}?;
89-
metrics::inc_pq_sig_attestation_signatures();
90-
91-
let sig_bytes = signature.to_bytes();
92-
XmssSignature::try_from(sig_bytes)
93-
.map_err(|e| KeyManagerError::SignatureConversionError(e.to_string()))
66+
Self::sign_with_key(&mut key_pair.proposal_key, slot, block_root)
9467
}
9568

96-
fn sign_with_proposal_key(
97-
&mut self,
98-
validator_id: u64,
69+
fn sign_with_key(
70+
key: &mut ValidatorSecretKey,
9971
slot: u32,
10072
message: &H256,
10173
) -> Result<XmssSignature, KeyManagerError> {
102-
let key_pair = self
103-
.keys
104-
.get_mut(&validator_id)
105-
.ok_or(KeyManagerError::ValidatorKeyNotFound(validator_id))?;
106-
10774
let signature: ValidatorSignature = {
10875
let _timing = metrics::time_pq_sig_attestation_signing();
109-
key_pair
110-
.proposal_key
111-
.sign(slot, message)
76+
key.sign(slot, message)
11277
.map_err(|e| KeyManagerError::SigningError(e.to_string()))
11378
}?;
114-
115-
let sig_bytes = signature.to_bytes();
116-
XmssSignature::try_from(sig_bytes)
79+
XmssSignature::try_from(signature.to_bytes())
11780
.map_err(|e| KeyManagerError::SignatureConversionError(e.to_string()))
11881
}
11982
}
@@ -133,9 +96,14 @@ mod tests {
13396
fn test_sign_attestation_validator_not_found() {
13497
let keys = HashMap::new();
13598
let mut key_manager = KeyManager::new(keys);
136-
let message = H256::default();
137-
138-
let result = key_manager.sign_with_attestation_key(123, 0, &message);
99+
let data = AttestationData {
100+
slot: 0,
101+
head: Default::default(),
102+
target: Default::default(),
103+
source: Default::default(),
104+
};
105+
106+
let result = key_manager.sign_attestation(123, &data);
139107
assert!(matches!(
140108
result,
141109
Err(KeyManagerError::ValidatorKeyNotFound(123))

0 commit comments

Comments
 (0)