Skip to content

Commit

Permalink
finished parity circuits
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 8, 2024
1 parent c863c97 commit 1896b74
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
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(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Base parity
mod base;

// Root parity
mod root;

mod parity_public_inputs;
mod utils;

use crate::base::base_parity_inputs::BaseParityInputs;
use crate::root::root_parity_input::RootParityInput;
Expand Down
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(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod sha256_merkle_tree;
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]
}
}

0 comments on commit 1896b74

Please sign in to comment.