-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
25 deletions.
There are no files selected for viewing
26 changes: 19 additions & 7 deletions
26
noir-projects/noir-protocol-circuits/crates/parity-lib/src/base/base_parity_inputs.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
use crate::parity_public_inputs::ParityPublicInputs; | ||
use crate::{ | ||
parity_public_inputs::ParityPublicInputs, | ||
utils::sha256_merkle_tree::Sha256MerkleTree, | ||
}; | ||
use dep::types::{ | ||
constants::NUM_FIELDS_PER_SHA256, | ||
merkle_tree::MerkleTree, | ||
mocked::AggregationObject, | ||
}; | ||
|
||
global NUM_MSGS_PER_BASE_PARITY: Field = 4; | ||
global NUM_MSGS_PER_BASE_PARITY: u64 = 4; | ||
|
||
struct BaseParityInputs { | ||
msgs: [[Field; NUM_FIELDS_PER_SHA256]; NUM_MSGS_PER_BASE_PARITY], | ||
} | ||
|
||
impl BaseParityInputs { | ||
pub fn base_parity_circuit(self) -> ParityPublicInputs { | ||
// sha_root = MERKLE_TREE(inputs.msgs, SHA256); | ||
// converted_root = MERKLE_TREE(inputs.msgs, SNARK_FRIENDLY_HASH_FUNCTION); | ||
// return ParityPublicInputs(sha_root, converted_root) | ||
let sha_tree = Sha256MerkleTree::new(self.msgs); | ||
|
||
// TODO: nuke this flattening once we truncate sha256 to 1 field | ||
let mut flat_msgs = [0; NUM_FIELDS_PER_SHA256 * NUM_MSGS_PER_BASE_PARITY]; | ||
for i in 0..NUM_MSGS_PER_BASE_PARITY { | ||
for j in 0..NUM_FIELDS_PER_SHA256 { | ||
flat_msgs[i * NUM_FIELDS_PER_SHA256 + j] = self.msgs[i][j]; | ||
} | ||
} | ||
let pedersen_tree = MerkleTree::new(flat_msgs); | ||
|
||
ParityPublicInputs { | ||
aggregation_object: AggregationObject {}, | ||
sha_root: [0, 0], | ||
converted_root: 0, | ||
sha_root: sha_tree.get_root(), | ||
converted_root: pedersen_tree.get_root(), | ||
} | ||
} | ||
} |
5 changes: 1 addition & 4 deletions
5
noir-projects/noir-protocol-circuits/crates/parity-lib/src/lib.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 22 additions & 14 deletions
36
noir-projects/noir-protocol-circuits/crates/parity-lib/src/root/root_parity_inputs.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,38 @@ | ||
use dep::types::mocked::AggregationObject; | ||
use dep::types::{ | ||
constants::NUM_FIELDS_PER_SHA256, | ||
merkle_tree::MerkleTree, | ||
mocked::AggregationObject, | ||
}; | ||
use crate::{ | ||
parity_public_inputs::ParityPublicInputs, | ||
root::root_parity_input::RootParityInput, | ||
utils::sha256_merkle_tree::Sha256MerkleTree, | ||
}; | ||
|
||
global NUM_BASE_PARITY_PER_ROOT_PARITY: u64 = 4; | ||
|
||
struct RootParityInputs { | ||
children: [RootParityInput; 2], | ||
children: [RootParityInput; NUM_BASE_PARITY_PER_ROOT_PARITY], | ||
} | ||
|
||
impl RootParityInputs { | ||
pub fn root_parity_circuit(self) -> ParityPublicInputs { | ||
// for msg in inputs.children: | ||
// assert msg.proof.verify(msg.public_inputs); | ||
// TODO: verify proofs of inputs.children | ||
|
||
let mut sha_roots = [[0; NUM_FIELDS_PER_SHA256]; NUM_BASE_PARITY_PER_ROOT_PARITY]; | ||
let mut converted_roots = [0; NUM_BASE_PARITY_PER_ROOT_PARITY]; | ||
for i in 0..NUM_BASE_PARITY_PER_ROOT_PARITY { | ||
sha_roots[i] = self.children[i].public_inputs.sha_root; | ||
converted_roots[i] = self.children[i].public_inputs.converted_root; | ||
} | ||
|
||
let sha_tree = Sha256MerkleTree::new(sha_roots); | ||
let pedersen_tree = MerkleTree::new(converted_roots); | ||
|
||
// sha_root = MERKLE_TREE( | ||
// [msg.public_inputs.sha_root for msg in inputs.children], | ||
// SHA256 | ||
// ); | ||
// converted_root = MERKLE_TREE( | ||
// [msg.public_inputs.converted_root for msg in inputs.children], | ||
// SNARK_FRIENDLY_HASH_FUNCTION | ||
// ); | ||
ParityPublicInputs { | ||
aggregation_object: AggregationObject {}, | ||
sha_root: [0, 0], | ||
converted_root: 0, | ||
sha_root: sha_tree.get_root(), | ||
converted_root: pedersen_tree.get_root(), | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
noir-projects/noir-protocol-circuits/crates/parity-lib/src/utils.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod sha256_merkle_tree; |
51 changes: 51 additions & 0 deletions
51
noir-projects/noir-protocol-circuits/crates/parity-lib/src/utils/sha256_merkle_tree.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use dep::types::{ | ||
constants::NUM_FIELDS_PER_SHA256, | ||
hash::accumulate_sha256, | ||
}; | ||
|
||
// Note: Once we'll truncate sha256 to 1 Field we can nuke this and generalize the standard MerkleTree over different | ||
// hash functions. | ||
struct Sha256MerkleTree<N> { | ||
leaves: [[Field; NUM_FIELDS_PER_SHA256]; N], | ||
nodes: [[Field; NUM_FIELDS_PER_SHA256]; N], | ||
} | ||
|
||
impl<N> Sha256MerkleTree<N> { | ||
pub fn new(leaves: [[Field; NUM_FIELDS_PER_SHA256]; N]) -> Self { | ||
let mut nodes = [[0; NUM_FIELDS_PER_SHA256]; N]; | ||
|
||
// We need one less node than leaves, but we cannot have computed array lengths | ||
let total_nodes = N - 1; | ||
let half_size = N / 2; | ||
|
||
// hash base layer | ||
for i in 0..half_size { | ||
nodes[i] = accumulate_sha256( | ||
[ | ||
U128::from_integer(leaves[2*i][0]), | ||
U128::from_integer(leaves[2*i][1]), | ||
U128::from_integer(leaves[2*i+1][0]), | ||
U128::from_integer(leaves[2*i+1][1]) | ||
] | ||
); | ||
} | ||
|
||
// hash the other layers | ||
for i in 0..(total_nodes - half_size) { | ||
nodes[half_size+i] = accumulate_sha256( | ||
[ | ||
U128::from_integer(nodes[2*i][0]), | ||
U128::from_integer(nodes[2*i][1]), | ||
U128::from_integer(nodes[2*i+1][0]), | ||
U128::from_integer(nodes[2*i+1][1]) | ||
] | ||
); | ||
} | ||
|
||
Sha256MerkleTree { leaves, nodes } | ||
} | ||
|
||
fn get_root(self) -> [Field; NUM_FIELDS_PER_SHA256] { | ||
self.nodes[N - 2] | ||
} | ||
} |