1
1
use alloy:: {
2
- primitives:: { Address , B256 } ,
2
+ primitives:: { aliases :: B32 , Address , B256 } ,
3
3
rpc:: types:: beacon:: { constants:: BLS_DST_SIG , BlsPublicKey , BlsSignature } ,
4
4
} ;
5
5
use tree_hash:: TreeHash ;
@@ -17,49 +17,52 @@ pub fn sign_message(secret_key: &BlsSecretKey, msg: &[u8]) -> BlsSignature {
17
17
BlsSignature :: from_slice ( & signature)
18
18
}
19
19
20
- pub fn compute_signing_root < T : TreeHash > ( signing_data : & T ) -> [ u8 ; 32 ] {
21
- signing_data. tree_hash_root ( ) . 0
20
+ pub fn compute_tree_hash_root < T : TreeHash > ( signing_data : & T ) -> B256 {
21
+ signing_data. tree_hash_root ( )
22
22
}
23
23
24
24
pub fn compute_prop_commit_signing_root (
25
25
chain : Chain ,
26
- object_root : [ u8 ; 32 ] ,
27
- module_signing_id : Option < [ u8 ; 32 ] > ,
28
- domain_mask : [ u8 ; 4 ] ,
29
- ) -> [ u8 ; 32 ] {
26
+ object_root : & B256 ,
27
+ module_signing_id : Option < & B256 > ,
28
+ domain_mask : & B32 ,
29
+ ) -> B256 {
30
30
let domain = compute_domain ( chain, domain_mask) ;
31
31
match module_signing_id {
32
- Some ( id) => compute_signing_root ( & types:: SigningData {
33
- object_root : compute_signing_root ( & types:: PropCommitSigningInfo {
34
- data : object_root,
35
- module_signing_id : id,
32
+ Some ( id) => compute_tree_hash_root ( & types:: SigningData {
33
+ object_root : compute_tree_hash_root ( & types:: PropCommitSigningInfo {
34
+ data : * object_root,
35
+ module_signing_id : * id,
36
36
} ) ,
37
37
signing_domain : domain,
38
38
} ) ,
39
- None => compute_signing_root ( & types:: SigningData { object_root, signing_domain : domain } ) ,
39
+ None => compute_tree_hash_root ( & types:: SigningData {
40
+ object_root : * object_root,
41
+ signing_domain : domain,
42
+ } ) ,
40
43
}
41
44
}
42
45
43
46
// NOTE: this currently works only for builder domain signatures and
44
47
// verifications
45
48
// ref: https://github.com/ralexstokes/ethereum-consensus/blob/cf3c404043230559660810bc0c9d6d5a8498d819/ethereum-consensus/src/builder/mod.rs#L26-L29
46
- pub fn compute_domain ( chain : Chain , domain_mask : [ u8 ; 4 ] ) -> [ u8 ; 32 ] {
49
+ pub fn compute_domain ( chain : Chain , domain_mask : & B32 ) -> B256 {
47
50
#[ derive( Debug , TreeHash ) ]
48
51
struct ForkData {
49
52
fork_version : [ u8 ; 4 ] ,
50
53
genesis_validators_root : [ u8 ; 32 ] ,
51
54
}
52
55
53
56
let mut domain = [ 0u8 ; 32 ] ;
54
- domain[ ..4 ] . copy_from_slice ( & domain_mask) ;
57
+ domain[ ..4 ] . copy_from_slice ( & domain_mask. 0 ) ;
55
58
56
59
let fork_version = chain. genesis_fork_version ( ) ;
57
60
let fd = ForkData { fork_version, genesis_validators_root : GENESIS_VALIDATORS_ROOT } ;
58
61
let fork_data_root = fd. tree_hash_root ( ) ;
59
62
60
63
domain[ 4 ..] . copy_from_slice ( & fork_data_root[ ..28 ] ) ;
61
64
62
- domain
65
+ B256 :: from ( domain)
63
66
}
64
67
65
68
pub fn verify_signed_message < T : TreeHash > (
@@ -68,15 +71,15 @@ pub fn verify_signed_message<T: TreeHash>(
68
71
msg : & T ,
69
72
signature : & BlsSignature ,
70
73
module_signing_id : Option < & B256 > ,
71
- domain_mask : [ u8 ; 4 ] ,
74
+ domain_mask : & B32 ,
72
75
) -> Result < ( ) , BlstErrorWrapper > {
73
76
let signing_root = compute_prop_commit_signing_root (
74
77
chain,
75
- compute_signing_root ( msg) ,
76
- module_signing_id. map ( |id| id . 0 ) ,
78
+ & compute_tree_hash_root ( msg) ,
79
+ module_signing_id,
77
80
domain_mask,
78
81
) ;
79
- verify_bls_signature ( pubkey, & signing_root, signature)
82
+ verify_bls_signature ( pubkey, signing_root. as_slice ( ) , signature)
80
83
}
81
84
82
85
/// Signs a message with the Beacon builder domain.
@@ -85,36 +88,36 @@ pub fn sign_builder_message(
85
88
secret_key : & BlsSecretKey ,
86
89
msg : & impl TreeHash ,
87
90
) -> BlsSignature {
88
- sign_builder_root ( chain, secret_key, msg. tree_hash_root ( ) . 0 )
91
+ sign_builder_root ( chain, secret_key, & msg. tree_hash_root ( ) )
89
92
}
90
93
91
94
pub fn sign_builder_root (
92
95
chain : Chain ,
93
96
secret_key : & BlsSecretKey ,
94
- object_root : [ u8 ; 32 ] ,
97
+ object_root : & B256 ,
95
98
) -> BlsSignature {
96
99
let domain = chain. builder_domain ( ) ;
97
100
let signing_data = types:: SigningData {
98
- object_root : compute_signing_root ( & object_root) ,
101
+ object_root : compute_tree_hash_root ( object_root) ,
99
102
signing_domain : domain,
100
103
} ;
101
- let signing_root = compute_signing_root ( & signing_data) ;
102
- sign_message ( secret_key, & signing_root)
104
+ let signing_root = compute_tree_hash_root ( & signing_data) ;
105
+ sign_message ( secret_key, signing_root. as_slice ( ) )
103
106
}
104
107
105
108
pub fn sign_commit_boost_root (
106
109
chain : Chain ,
107
110
secret_key : & BlsSecretKey ,
108
- object_root : [ u8 ; 32 ] ,
109
- module_signing_id : Option < [ u8 ; 32 ] > ,
111
+ object_root : & B256 ,
112
+ module_signing_id : Option < & B256 > ,
110
113
) -> BlsSignature {
111
114
let signing_root = compute_prop_commit_signing_root (
112
115
chain,
113
116
object_root,
114
117
module_signing_id,
115
- COMMIT_BOOST_DOMAIN ,
118
+ & B32 :: from ( COMMIT_BOOST_DOMAIN ) ,
116
119
) ;
117
- sign_message ( secret_key, & signing_root)
120
+ sign_message ( secret_key, signing_root. as_slice ( ) )
118
121
}
119
122
120
123
// ==============================
@@ -128,18 +131,18 @@ pub fn verify_proposer_commitment_signature_bls(
128
131
pubkey : & BlsPublicKey ,
129
132
msg : & impl TreeHash ,
130
133
signature : & BlsSignature ,
131
- module_signing_id : B256 ,
134
+ module_signing_id : & B256 ,
132
135
) -> Result < ( ) , BlstErrorWrapper > {
133
- let object_root = msg. tree_hash_root ( ) . 0 ;
134
- let domain = compute_domain ( chain, COMMIT_BOOST_DOMAIN ) ;
135
- let signing_root = compute_signing_root ( & types:: SigningData {
136
- object_root : compute_signing_root ( & types:: PropCommitSigningInfo {
136
+ let object_root = msg. tree_hash_root ( ) ;
137
+ let domain = compute_domain ( chain, & B32 :: from ( COMMIT_BOOST_DOMAIN ) ) ;
138
+ let signing_root = compute_tree_hash_root ( & types:: SigningData {
139
+ object_root : compute_tree_hash_root ( & types:: PropCommitSigningInfo {
137
140
data : object_root,
138
141
module_signing_id : * module_signing_id,
139
142
} ) ,
140
143
signing_domain : domain,
141
144
} ) ;
142
- verify_bls_signature ( pubkey, & signing_root, signature)
145
+ verify_bls_signature ( pubkey, signing_root. as_slice ( ) , signature)
143
146
}
144
147
145
148
/// Verifies that a proposer commitment signature was generated by the given
@@ -149,12 +152,12 @@ pub fn verify_proposer_commitment_signature_ecdsa(
149
152
address : & Address ,
150
153
msg : & impl TreeHash ,
151
154
signature : & EcdsaSignature ,
152
- module_signing_id : B256 ,
155
+ module_signing_id : & B256 ,
153
156
) -> Result < ( ) , eyre:: Report > {
154
- let object_root = msg. tree_hash_root ( ) . 0 ;
155
- let domain = compute_domain ( chain, COMMIT_BOOST_DOMAIN ) ;
156
- let signing_root = compute_signing_root ( & types:: SigningData {
157
- object_root : compute_signing_root ( & types:: PropCommitSigningInfo {
157
+ let object_root = msg. tree_hash_root ( ) ;
158
+ let domain = compute_domain ( chain, & B32 :: from ( COMMIT_BOOST_DOMAIN ) ) ;
159
+ let signing_root = compute_tree_hash_root ( & types:: SigningData {
160
+ object_root : compute_tree_hash_root ( & types:: PropCommitSigningInfo {
158
161
data : object_root,
159
162
module_signing_id : * module_signing_id,
160
163
} ) ,
@@ -170,30 +173,18 @@ pub fn verify_proposer_commitment_signature_ecdsa(
170
173
#[ cfg( test) ]
171
174
mod tests {
172
175
176
+ use alloy:: primitives:: aliases:: B32 ;
177
+
173
178
use super :: compute_domain;
174
179
use crate :: { constants:: APPLICATION_BUILDER_DOMAIN , types:: Chain } ;
175
180
176
181
#[ test]
177
182
fn test_builder_domains ( ) {
178
- assert_eq ! (
179
- compute_domain( Chain :: Mainnet , APPLICATION_BUILDER_DOMAIN ) ,
180
- Chain :: Mainnet . builder_domain( )
181
- ) ;
182
- assert_eq ! (
183
- compute_domain( Chain :: Holesky , APPLICATION_BUILDER_DOMAIN ) ,
184
- Chain :: Holesky . builder_domain( )
185
- ) ;
186
- assert_eq ! (
187
- compute_domain( Chain :: Sepolia , APPLICATION_BUILDER_DOMAIN ) ,
188
- Chain :: Sepolia . builder_domain( )
189
- ) ;
190
- assert_eq ! (
191
- compute_domain( Chain :: Helder , APPLICATION_BUILDER_DOMAIN ) ,
192
- Chain :: Helder . builder_domain( )
193
- ) ;
194
- assert_eq ! (
195
- compute_domain( Chain :: Hoodi , APPLICATION_BUILDER_DOMAIN ) ,
196
- Chain :: Hoodi . builder_domain( )
197
- ) ;
183
+ let domain = & B32 :: from ( APPLICATION_BUILDER_DOMAIN ) ;
184
+ assert_eq ! ( compute_domain( Chain :: Mainnet , domain) , Chain :: Mainnet . builder_domain( ) ) ;
185
+ assert_eq ! ( compute_domain( Chain :: Holesky , domain) , Chain :: Holesky . builder_domain( ) ) ;
186
+ assert_eq ! ( compute_domain( Chain :: Sepolia , domain) , Chain :: Sepolia . builder_domain( ) ) ;
187
+ assert_eq ! ( compute_domain( Chain :: Helder , domain) , Chain :: Helder . builder_domain( ) ) ;
188
+ assert_eq ! ( compute_domain( Chain :: Hoodi , domain) , Chain :: Hoodi . builder_domain( ) ) ;
198
189
}
199
190
}
0 commit comments