-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathlib.rs
More file actions
51 lines (45 loc) · 1.63 KB
/
lib.rs
File metadata and controls
51 lines (45 loc) · 1.63 KB
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
use lambdaworks_crypto::merkle_tree::traits::IsMerkleTreeBackend;
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};
#[derive(Serialize, Deserialize)]
pub struct SP1VkAndPubInputs {
pub vk: [u32; 8],
pub public_inputs: Vec<u8>,
}
impl SP1VkAndPubInputs {
pub fn hash(&self) -> [u8; 32] {
let mut hasher = Keccak256::new();
for &word in &self.vk {
hasher.update(word.to_be_bytes());
}
hasher.update(&self.public_inputs);
hasher.finalize().into()
}
}
// Note: this MerkleTreeBackend is defined in three locations
// - aggregation_mode/src/aggregators/mod.rs
// - aggregation_mode/src/aggregators/risc0_aggregator.rs and
// - aggregation_mode/src/aggregators/sp1_aggregator.rs
// All 3 implementations should match
// The definition on aggregator/mod.rs supports taking proofs from both Risc0 and SP1,
// Additionally, a version that takes the leaves as already hashed data is defined on:
// - batcher/aligned-sdk/src/sdk/aggregation.rs
// This one is used in the SDK since
// the user may not have access to the proofs that they didn't submit
impl IsMerkleTreeBackend for SP1VkAndPubInputs {
type Data = SP1VkAndPubInputs;
type Node = [u8; 32];
fn hash_data(leaf: &Self::Data) -> Self::Node {
leaf.hash()
}
fn hash_new_parent(child_1: &Self::Node, child_2: &Self::Node) -> Self::Node {
let mut hasher = Keccak256::new();
hasher.update(child_1);
hasher.update(child_2);
hasher.finalize().into()
}
}
#[derive(Serialize, Deserialize)]
pub struct Input {
pub proofs_vk_and_pub_inputs: Vec<SP1VkAndPubInputs>,
}