-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathutils.rs
91 lines (80 loc) · 2.56 KB
/
utils.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
use crate::{types::Bytes32, utils::bytes32_to_node};
use anyhow::Result;
use ethportal_api::consensus::{header::BeaconBlockHeader, signature::BlsSignature};
use milagro_bls::{AggregateSignature, PublicKey};
use ssz_rs::prelude::*;
use tree_hash::TreeHash;
pub fn calc_sync_period(slot: u64) -> u64 {
let epoch = slot / 32; // 32 slots per epoch
epoch / 256 // 256 epochs per sync committee
}
pub fn is_aggregate_valid(sig_bytes: &BlsSignature, msg: &[u8], pks: &[&PublicKey]) -> bool {
let sig_res = AggregateSignature::from_bytes(&sig_bytes.signature);
match sig_res {
Ok(sig) => sig.fast_aggregate_verify(msg, pks),
Err(_) => false,
}
}
pub fn is_proof_valid<L: TreeHash>(
attested_header: &BeaconBlockHeader,
leaf_object: &mut L,
branch: &[Bytes32],
depth: usize,
index: usize,
) -> bool {
let res: Result<bool> = (move || {
let leaf_hash = Node::from_bytes(<[u8; 32]>::from(leaf_object.tree_hash_root()));
let state_root = bytes32_to_node(
&Bytes32::try_from(attested_header.state_root.0.to_vec())
.expect("Unable to convert state root to bytes"),
)?;
let branch = branch_to_nodes(branch.to_vec())?;
let is_valid = is_valid_merkle_branch(&leaf_hash, branch.iter(), depth, index, &state_root);
Ok(is_valid)
})();
res.unwrap_or_default()
}
#[derive(SimpleSerialize, Default, Debug)]
struct SigningData {
object_root: Bytes32,
domain: Bytes32,
}
#[derive(SimpleSerialize, Default, Debug)]
struct ForkData {
current_version: Vector<u8, 4>,
genesis_validator_root: Bytes32,
}
pub fn compute_signing_root(object_root: Bytes32, domain: Bytes32) -> Result<Node> {
let mut data = SigningData {
object_root,
domain,
};
Ok(data.hash_tree_root()?)
}
pub fn compute_domain(
domain_type: &[u8],
fork_version: Vector<u8, 4>,
genesis_root: Bytes32,
) -> Result<Bytes32> {
let fork_data_root = compute_fork_data_root(fork_version, genesis_root)?;
let start = domain_type;
let end = &fork_data_root.as_bytes()[..28];
let d = [start, end].concat();
Ok(d.to_vec().try_into()?)
}
fn compute_fork_data_root(
current_version: Vector<u8, 4>,
genesis_validator_root: Bytes32,
) -> Result<Node> {
let mut fork_data = ForkData {
current_version,
genesis_validator_root,
};
Ok(fork_data.hash_tree_root()?)
}
pub fn branch_to_nodes(branch: Vec<Bytes32>) -> Result<Vec<Node>> {
branch
.iter()
.map(bytes32_to_node)
.collect::<Result<Vec<Node>>>()
}