-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathstake_table.rs
384 lines (354 loc) · 10.7 KB
/
stake_table.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use std::str::FromStr;
use ark_ec::{
short_weierstrass,
twisted_edwards::{self, Affine, TECurveConfig},
AffineRepr,
};
use ark_ed_on_bn254::EdwardsConfig;
use ark_ff::{BigInteger, PrimeField};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::rand::{Rng, RngCore};
use contract_bindings_alloy::permissionedstaketable::{
EdOnBN254::EdOnBN254Point as EdOnBN254PointAlloy,
PermissionedStakeTable::NodeInfo as NodeInfoAlloy, BN254::G2Point as G2PointAlloy,
};
use contract_bindings_ethers::permissioned_stake_table::{self, EdOnBN254Point, NodeInfo};
use diff_test_bn254::ParsedG2Point;
use ethers::{
abi::AbiDecode,
prelude::{AbiError, EthAbiCodec, EthAbiType},
types::U256,
};
use ethers_conv::{ToAlloy, ToEthers};
use hotshot_types::{
light_client::{StateKeyPair, StateVerKey},
network::PeerConfigKeys,
signature_key::BLSPubKey,
stake_table::StakeTableEntry,
traits::{node_implementation::NodeType, signature_key::SignatureKey as _},
PeerConfig,
};
use serde::{Deserialize, Serialize};
use crate::jellyfish::u256_to_field;
// TODO: (alex) maybe move these commonly shared util to a crate
/// convert a field element to U256, panic if field size is larger than 256 bit
pub fn field_to_u256<F: PrimeField>(f: F) -> U256 {
if F::MODULUS_BIT_SIZE > 256 {
panic!("Shouldn't convert a >256-bit field to U256");
}
U256::from_little_endian(&f.into_bigint().to_bytes_le())
}
/// an intermediate representation of `EdOnBN254Point` in solidity.
#[derive(Clone, PartialEq, Eq, Debug, EthAbiType, EthAbiCodec)]
pub struct ParsedEdOnBN254Point {
/// x coordinate of affine repr
pub x: U256,
/// y coordinate of affine repr
pub y: U256,
}
// this is convention from BN256 precompile
impl Default for ParsedEdOnBN254Point {
fn default() -> Self {
Self {
x: U256::from(0),
y: U256::from(0),
}
}
}
impl From<ParsedEdOnBN254Point> for EdOnBN254Point {
fn from(value: ParsedEdOnBN254Point) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}
impl From<ParsedEdOnBN254Point> for EdOnBN254PointAlloy {
fn from(value: ParsedEdOnBN254Point) -> Self {
Self {
x: value.x.to_alloy(),
y: value.y.to_alloy(),
}
}
}
impl From<EdOnBN254Point> for ParsedEdOnBN254Point {
fn from(value: EdOnBN254Point) -> Self {
let EdOnBN254Point { x, y } = value;
Self { x, y }
}
}
impl From<EdOnBN254PointAlloy> for ParsedEdOnBN254Point {
fn from(value: EdOnBN254PointAlloy) -> Self {
let EdOnBN254PointAlloy { x, y } = value;
Self {
x: x.to_ethers(),
y: y.to_ethers(),
}
}
}
impl FromStr for ParsedEdOnBN254Point {
type Err = AbiError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parsed: (Self,) = AbiDecode::decode_hex(s)?;
Ok(parsed.0)
}
}
impl<P: TECurveConfig> From<Affine<P>> for ParsedEdOnBN254Point
where
P::BaseField: PrimeField,
{
fn from(p: Affine<P>) -> Self {
if p.is_zero() {
// this convention is from the BN precompile
Self {
x: U256::from(0),
y: U256::from(0),
}
} else {
Self {
x: field_to_u256::<P::BaseField>(*p.x().unwrap()),
y: field_to_u256::<P::BaseField>(*p.y().unwrap()),
}
}
}
}
impl<P: TECurveConfig> From<ParsedEdOnBN254Point> for Affine<P>
where
P::BaseField: PrimeField,
{
fn from(p: ParsedEdOnBN254Point) -> Self {
if p == ParsedEdOnBN254Point::default() {
Self::default()
} else {
Self::new_unchecked(
u256_to_field::<P::BaseField>(p.x),
u256_to_field::<P::BaseField>(p.y),
)
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct NodeInfoJf {
pub stake_table_key: BLSPubKey,
pub state_ver_key: StateVerKey,
pub da: bool,
}
impl NodeInfoJf {
pub fn random(rng: &mut impl RngCore) -> Self {
let mut seed = [0u8; 32];
rng.fill_bytes(&mut seed);
let (stake_table_key, _) = BLSPubKey::generated_from_seed_indexed(seed, 0);
let state_key_pair = StateKeyPair::generate_from_seed_indexed(seed, 0);
Self {
stake_table_key,
state_ver_key: state_key_pair.ver_key(),
da: rng.gen(),
}
}
pub fn stake_table_key_alloy(&self) -> G2PointAlloy {
bls_jf_to_alloy(self.stake_table_key)
}
}
impl From<NodeInfoJf> for NodeInfo {
fn from(value: NodeInfoJf) -> Self {
let NodeInfoJf {
stake_table_key,
state_ver_key,
da,
} = value;
let ParsedG2Point { x0, x1, y0, y1 } = stake_table_key.to_affine().into();
let schnorr: ParsedEdOnBN254Point = state_ver_key.to_affine().into();
Self {
bls_vk: permissioned_stake_table::G2Point {
x_0: x0,
x_1: x1,
y_0: y0,
y_1: y1,
},
schnorr_vk: schnorr.into(),
is_da: da,
}
}
}
impl From<NodeInfoJf> for NodeInfoAlloy {
fn from(value: NodeInfoJf) -> Self {
let NodeInfoJf {
stake_table_key,
state_ver_key,
da,
} = value;
let ParsedG2Point { x0, x1, y0, y1 } = stake_table_key.to_affine().into();
let schnorr: ParsedEdOnBN254Point = state_ver_key.to_affine().into();
Self {
blsVK: G2PointAlloy {
x0: x0.to_alloy(),
x1: x1.to_alloy(),
y0: y0.to_alloy(),
y1: y1.to_alloy(),
},
schnorrVK: schnorr.into(),
isDA: da,
}
}
}
impl From<NodeInfoJf> for StakeTableEntry<BLSPubKey> {
fn from(value: NodeInfoJf) -> Self {
StakeTableEntry {
stake_key: value.stake_table_key,
stake_amount: U256::from(1), // dummy stake amount
}
}
}
impl<TYPES> From<NodeInfoJf> for PeerConfig<TYPES>
where
TYPES: NodeType<SignatureKey = BLSPubKey, StateSignatureKey = StateVerKey>,
{
fn from(value: NodeInfoJf) -> Self {
Self {
stake_table_entry: StakeTableEntry {
stake_key: value.stake_table_key,
stake_amount: U256::from(1), // dummy stake amount
},
state_ver_key: value.state_ver_key,
}
}
}
impl From<NodeInfo> for NodeInfoJf {
fn from(value: NodeInfo) -> Self {
let NodeInfo {
bls_vk,
schnorr_vk,
is_da,
} = value;
let stake_table_key = bls_sol_to_jf(bls_vk);
let state_ver_key = {
let g1_point: ParsedEdOnBN254Point = schnorr_vk.into();
let state_sk_affine = twisted_edwards::Affine::<EdwardsConfig>::from(g1_point);
StateVerKey::from(state_sk_affine)
};
Self {
stake_table_key,
state_ver_key,
da: is_da,
}
}
}
impl From<NodeInfoAlloy> for NodeInfoJf {
fn from(value: NodeInfoAlloy) -> Self {
let NodeInfoAlloy {
blsVK,
schnorrVK,
isDA,
} = value;
let stake_table_key = {
let g2 = diff_test_bn254::ParsedG2Point {
x0: blsVK.x0.to_ethers(),
x1: blsVK.x1.to_ethers(),
y0: blsVK.y0.to_ethers(),
y1: blsVK.y1.to_ethers(),
};
let g2_affine = short_weierstrass::Affine::<ark_bn254::g2::Config>::from(g2);
let mut bytes = vec![];
// TODO: remove serde round-trip once jellyfin provides a way to
// convert from Affine representation to VerKey.
//
// Serialization and de-serialization shouldn't fail.
g2_affine
.into_group()
.serialize_compressed(&mut bytes)
.unwrap();
BLSPubKey::deserialize_compressed(&bytes[..]).unwrap()
};
let state_ver_key = {
let g1_point: ParsedEdOnBN254Point = schnorrVK.into();
let state_sk_affine = twisted_edwards::Affine::<EdwardsConfig>::from(g1_point);
StateVerKey::from(state_sk_affine)
};
Self {
stake_table_key,
state_ver_key,
da: isDA,
}
}
}
impl<TYPES> From<PeerConfigKeys<TYPES>> for NodeInfoJf
where
TYPES: NodeType<SignatureKey = BLSPubKey, StateSignatureKey = StateVerKey>,
{
fn from(value: PeerConfigKeys<TYPES>) -> Self {
let PeerConfigKeys {
stake_table_key,
state_ver_key,
da,
..
} = value;
Self {
stake_table_key,
state_ver_key,
da,
}
}
}
pub fn bls_jf_to_sol(bls_vk: BLSPubKey) -> permissioned_stake_table::G2Point {
let ParsedG2Point { x0, x1, y0, y1 } = bls_vk.to_affine().into();
permissioned_stake_table::G2Point {
x_0: x0,
x_1: x1,
y_0: y0,
y_1: y1,
}
}
fn bls_conv_helper(g2: diff_test_bn254::ParsedG2Point) -> BLSPubKey {
let g2_affine = short_weierstrass::Affine::<ark_bn254::g2::Config>::from(g2);
let mut bytes = vec![];
// TODO: remove serde round-trip once jellyfin provides a way to
// convert from Affine representation to VerKey.
//
// Serialization and de-serialization shouldn't fail.
g2_affine
.into_group()
.serialize_compressed(&mut bytes)
.unwrap();
BLSPubKey::deserialize_compressed(&bytes[..]).unwrap()
}
pub fn bls_sol_to_jf(bls_vk: permissioned_stake_table::G2Point) -> BLSPubKey {
let g2 = diff_test_bn254::ParsedG2Point {
x0: bls_vk.x_0,
x1: bls_vk.x_1,
y0: bls_vk.y_0,
y1: bls_vk.y_1,
};
bls_conv_helper(g2)
}
pub fn bls_alloy_to_jf(bls_vk: G2PointAlloy) -> BLSPubKey {
let g2 = diff_test_bn254::ParsedG2Point {
x0: bls_vk.x0.to_ethers(),
x1: bls_vk.x1.to_ethers(),
y0: bls_vk.y0.to_ethers(),
y1: bls_vk.y1.to_ethers(),
};
bls_conv_helper(g2)
}
pub fn bls_jf_to_alloy(bls_vk: BLSPubKey) -> G2PointAlloy {
let ParsedG2Point { x0, x1, y0, y1 } = bls_vk.to_affine().into();
G2PointAlloy {
x0: x0.to_alloy(),
x1: x1.to_alloy(),
y0: y0.to_alloy(),
y1: y1.to_alloy(),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_node_info_round_trip() {
let mut rng = rand::thread_rng();
for _ in 0..20 {
let jf = NodeInfoJf::random(&mut rng);
let sol: NodeInfo = jf.clone().into();
let jf2: NodeInfoJf = sol.into();
assert_eq!(jf2, jf);
}
}
}