diff --git a/codex-plonky2-circuits/src/circuits/prove_single_cell.rs b/codex-plonky2-circuits/src/circuits/prove_single_cell.rs index e1a51ca..da9eba9 100644 --- a/codex-plonky2-circuits/src/circuits/prove_single_cell.rs +++ b/codex-plonky2-circuits/src/circuits/prove_single_cell.rs @@ -36,21 +36,17 @@ use crate::circuits::params::{MAX_DEPTH, BOT_DEPTH, N_FIELD_ELEMS_PER_CELL, N_CE #[derive(Clone)] pub struct SlotTreeCircuit< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, > { - pub tree: MerkleTreeCircuit, // slot tree - pub block_trees: Vec>, // vec of block trees + pub tree: MerkleTreeCircuit, // slot tree + pub block_trees: Vec>, // vec of block trees pub cell_data: Vec>, // cell data as field elements } impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, -> Default for SlotTreeCircuit{ +> Default for SlotTreeCircuit{ /// slot tree with fake data, for testing only fn default() -> Self { // generate fake cell data @@ -78,7 +74,7 @@ impl< let start = i * N_CELLS_IN_BLOCKS; let end = (i + 1) * N_CELLS_IN_BLOCKS; let b_tree = Self::get_block_tree(&leaves[start..end].to_vec()); // use helper function - MerkleTreeCircuit::{ tree:b_tree, _phantom:Default::default()} + MerkleTreeCircuit::{ tree:b_tree} }) .collect::>(); // get the roots or block trees @@ -88,10 +84,10 @@ impl< }) .collect::>(); // create slot tree - let slot_tree = MerkleTree::::new(&block_roots, zero).unwrap(); + let slot_tree = MerkleTree::::new(&block_roots, zero).unwrap(); Self{ - tree: MerkleTreeCircuit::{ tree:slot_tree, _phantom:Default::default()}, + tree: MerkleTreeCircuit::{ tree:slot_tree}, block_trees, cell_data, } @@ -100,10 +96,8 @@ impl< impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, -> SlotTreeCircuit { +> SlotTreeCircuit { /// Slot tree with fake data, for testing only pub fn new_for_testing() -> Self { @@ -133,9 +127,9 @@ impl< let b_tree = Self::get_block_tree(&leaves_block); // Create a block tree circuit - let block_tree_circuit = MerkleTreeCircuit:: { + let block_tree_circuit = MerkleTreeCircuit:: { tree: b_tree, - _phantom: Default::default(), + // _phantom: Default::default(), }; // Now replicate this block tree for all N_BLOCKS blocks @@ -148,16 +142,15 @@ impl< .collect::>(); // Create the slot tree from block roots - let slot_tree = MerkleTree::::new(&block_roots, zero).unwrap(); + let slot_tree = MerkleTree::::new(&block_roots, zero).unwrap(); // Create the full cell data and cell hash by repeating the block data let cell_data = vec![cell_data_block.clone(); N_BLOCKS].concat(); // Return the constructed Self Self { - tree: MerkleTreeCircuit:: { + tree: MerkleTreeCircuit:: { tree: slot_tree, - _phantom: Default::default(), }, block_trees, cell_data, @@ -181,7 +174,7 @@ impl< let start = i * N_CELLS_IN_BLOCKS; let end = (i + 1) * N_CELLS_IN_BLOCKS; let b_tree = Self::get_block_tree(&leaves[start..end].to_vec()); - MerkleTreeCircuit::{ tree:b_tree, _phantom:Default::default()} + MerkleTreeCircuit::{ tree:b_tree} }) .collect::>(); let block_roots = block_trees.iter() @@ -189,9 +182,9 @@ impl< t.tree.root().unwrap() }) .collect::>(); - let slot_tree = MerkleTree::::new(&block_roots, zero).unwrap(); + let slot_tree = MerkleTree::::new(&block_roots, zero).unwrap(); Self{ - tree: MerkleTreeCircuit::{ tree:slot_tree, _phantom:Default::default()}, + tree: MerkleTreeCircuit::{ tree:slot_tree}, block_trees, cell_data, } @@ -199,7 +192,7 @@ impl< /// generates a proof for given leaf index /// the path in the proof is a combined block and slot path to make up the full path - pub fn get_proof(&self, index: usize) -> MerkleProof { + pub fn get_proof(&self, index: usize) -> MerkleProof { let block_index = index/ N_CELLS_IN_BLOCKS; let leaf_index = index % N_CELLS_IN_BLOCKS; let block_proof = self.block_trees[block_index].tree.get_proof(leaf_index).unwrap(); @@ -209,18 +202,17 @@ impl< let mut combined_path = block_proof.path.clone(); combined_path.extend(slot_proof.path.clone()); - MerkleProof:: { + MerkleProof:: { index: index, path: combined_path, nleaves: self.cell_data.len(), zero: block_proof.zero.clone(), - phantom_data: Default::default(), } } /// verify the given proof for slot tree, checks equality with given root - pub fn verify_cell_proof(&self, proof: MerkleProof, root: HashOut) -> Result{ + pub fn verify_cell_proof(&self, proof: MerkleProof, root: HashOut) -> Result{ let mut block_path_bits = usize_to_bits_le_padded(proof.index, MAX_DEPTH); let last_index = N_CELLS - 1; let mut block_last_bits = usize_to_bits_le_padded(last_index, MAX_DEPTH); @@ -235,51 +227,42 @@ impl< let mut block_path = proof.path; let slot_path = block_path.split_off(split_point); - let block_res = MerkleProof::::reconstruct_root2(leaf_hash,block_path_bits.clone(),block_last_bits.clone(),block_path); - let reconstructed_root = MerkleProof::::reconstruct_root2(block_res.unwrap(),slot_path_bits,slot_last_bits,slot_path); + let block_res = MerkleProof::::reconstruct_root2(leaf_hash,block_path_bits.clone(),block_last_bits.clone(),block_path); + let reconstructed_root = MerkleProof::::reconstruct_root2(block_res.unwrap(),slot_path_bits,slot_last_bits,slot_path); Ok(reconstructed_root.unwrap() == root) } - fn get_block_tree(leaves: &Vec>) -> MerkleTree { + fn get_block_tree(leaves: &Vec>) -> MerkleTree { let zero = HashOut { elements: [F::ZERO; 4], }; // Build the Merkle tree - let block_tree = MerkleTree::::new(leaves, zero).unwrap(); - return block_tree; + let block_tree = MerkleTree::::new(leaves, zero).unwrap(); + block_tree } } //------- single cell struct ------ #[derive(Clone, Debug, Eq, PartialEq)] -pub struct SingleCellTargets< - F: RichField + Extendable + Poseidon2, - C: GenericConfig, - const D: usize, - H: Hasher + AlgebraicHasher, -> { +pub struct SingleCellTargets{ pub expected_slot_root_target: HashOutTarget, pub proof_target: MerkleProofTarget, pub leaf_target: Vec, pub path_bits: Vec, pub last_bits: Vec, - _phantom: PhantomData<(C,H)>, } //------- circuit impl -------- impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher + Hasher, -> SlotTreeCircuit { +> SlotTreeCircuit { pub fn prove_single_cell( - // &mut self, builder: &mut CircuitBuilder:: - ) -> SingleCellTargets { + ) -> SingleCellTargets { // Retrieve tree depth let depth = MAX_DEPTH; @@ -289,7 +272,7 @@ impl< let mut perm_inputs:Vec= Vec::new(); perm_inputs.extend_from_slice(&leaf); - let leaf_hash = builder.hash_n_to_hash_no_pad::(perm_inputs); + let leaf_hash = builder.hash_n_to_hash_no_pad::(perm_inputs); // path bits (binary decomposition of leaf_index) let mut block_path_bits = (0..BOT_DEPTH).map(|_| builder.add_virtual_bool_target_safe()).collect::>(); @@ -315,11 +298,10 @@ impl< path_bits:block_path_bits, last_bits: block_last_bits, merkle_path: block_merkle_path, - _phantom: PhantomData, }; // reconstruct block root - let block_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut block_targets); + let block_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut block_targets); // create MerkleTreeTargets struct let mut slot_targets = MerkleTreeTargets { @@ -327,11 +309,10 @@ impl< path_bits:slot_path_bits, last_bits:slot_last_bits, merkle_path:slot_merkle_path, - _phantom: PhantomData, }; // reconstruct slot root with block root as leaf - let slot_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut slot_targets); + let slot_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut slot_targets); // check equality with expected root for i in 0..NUM_HASH_OUT_ELTS { @@ -355,7 +336,6 @@ impl< leaf_target: leaf, path_bits, last_bits, - _phantom: Default::default(), }; // Return MerkleTreeTargets @@ -368,10 +348,10 @@ impl< pub fn single_cell_assign_witness( &self, pw: &mut PartialWitness, - targets: &mut SingleCellTargets, + targets: &mut SingleCellTargets, leaf_index: usize, leaf: &Vec, - proof: MerkleProof, + proof: MerkleProof, )-> Result<()> { // Assign the leaf to the leaf target @@ -415,7 +395,7 @@ impl< } fn hash_leaf(builder: &mut CircuitBuilder, leaf: &mut Vec){ - builder.hash_n_to_hash_no_pad::(leaf.to_owned()); + builder.hash_n_to_hash_no_pad::(leaf.to_owned()); } } @@ -435,7 +415,7 @@ mod tests { #[test] fn test_prove_single_cell(){ - let slot_t = SlotTreeCircuit::::default(); + let slot_t = SlotTreeCircuit::::default(); let index = 8; let proof = slot_t.get_proof(index); let res = slot_t.verify_cell_proof(proof,slot_t.tree.tree.root().unwrap()).unwrap(); @@ -445,7 +425,7 @@ mod tests { #[test] fn test_cell_build_circuit() -> Result<()> { - let slot_t = SlotTreeCircuit::::default(); + let slot_t = SlotTreeCircuit::::default(); // select leaf index to prove let leaf_index: usize = 8; @@ -460,7 +440,7 @@ mod tests { let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::::new(config); - let mut targets = SlotTreeCircuit::::prove_single_cell(&mut builder); + let mut targets = SlotTreeCircuit::::prove_single_cell(&mut builder); // create a PartialWitness and assign let mut pw = PartialWitness::new(); diff --git a/codex-plonky2-circuits/src/circuits/safe_tree_circuit.rs b/codex-plonky2-circuits/src/circuits/safe_tree_circuit.rs index f86dcfa..74f2f7c 100644 --- a/codex-plonky2-circuits/src/circuits/safe_tree_circuit.rs +++ b/codex-plonky2-circuits/src/circuits/safe_tree_circuit.rs @@ -18,6 +18,7 @@ use plonky2::plonk::proof::{Proof, ProofWithPublicInputs}; use std::marker::PhantomData; use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2; use serde::Serialize; +use crate::circuits::params::HF; use crate::circuits::utils::usize_to_bits_le_padded; use crate::merkle_tree::merkle_safe::{MerkleTree, MerkleProofTarget}; @@ -29,17 +30,11 @@ use crate::merkle_tree::merkle_safe::{KEY_NONE,KEY_BOTTOM_LAYER}; // uses the Plonk's permutation argument to check that two elements are equal. // TODO: double check the need for mask #[derive(Clone)] -pub struct MerkleTreeTargets< - F: RichField + Extendable + Poseidon2, - C: GenericConfig, - const D: usize, - H: Hasher + AlgebraicHasher, -> { +pub struct MerkleTreeTargets{ pub leaf: HashOutTarget, pub path_bits: Vec, pub last_bits: Vec, pub merkle_path: MerkleProofTarget, - pub _phantom: PhantomData<(C, H)>, } /// Merkle tree circuit contains the tree and functions for @@ -47,26 +42,21 @@ pub struct MerkleTreeTargets< #[derive(Clone)] pub struct MerkleTreeCircuit< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, > { - pub tree: MerkleTree, - pub _phantom: PhantomData, + pub tree: MerkleTree, } impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, -> MerkleTreeCircuit { +> MerkleTreeCircuit { /// defines the computations inside the circuit and returns the targets used pub fn build_circuit( &mut self, builder: &mut CircuitBuilder:: - ) -> (MerkleTreeTargets, HashOutTarget) { + ) -> (MerkleTreeTargets, HashOutTarget) { // Retrieve tree depth let depth = self.tree.depth(); @@ -85,12 +75,11 @@ impl< }; // create MerkleTreeTargets struct - let mut targets = MerkleTreeTargets { + let mut targets = MerkleTreeTargets{ leaf, path_bits, last_bits, merkle_path, - _phantom: PhantomData, }; // Add Merkle proof verification constraints to the circuit @@ -105,7 +94,7 @@ impl< pub fn assign_witness( &mut self, pw: &mut PartialWitness, - targets: &mut MerkleTreeTargets, + targets: &mut MerkleTreeTargets, leaf_index: usize, )-> Result<()> { // Get the total number of leaves and tree depth @@ -151,9 +140,8 @@ impl< /// takes the params from the targets struct /// outputs the reconstructed merkle root pub fn reconstruct_merkle_root_circuit( - // &self, builder: &mut CircuitBuilder, - targets: &mut MerkleTreeTargets, + targets: &mut MerkleTreeTargets, ) -> HashOutTarget { let max_depth = targets.path_bits.len(); let mut state: HashOutTarget = targets.leaf; @@ -200,7 +188,7 @@ impl< perm_inputs.extend_from_slice(&left); perm_inputs.extend_from_slice(&right); perm_inputs.push(key); - state = builder.hash_n_to_hash_no_pad::(perm_inputs); + state = builder.hash_n_to_hash_no_pad::(perm_inputs); i += 1; } @@ -245,7 +233,7 @@ mod tests { let zero_hash = HashOut { elements: [GoldilocksField::ZERO; 4], }; - let tree = MerkleTree::::new(&leaves, zero_hash)?; + let tree = MerkleTree::::new(&leaves, zero_hash)?; // select leaf index to prove let leaf_index: usize = 8; @@ -262,9 +250,9 @@ mod tests { // create the circuit let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::::new(config); - let mut circuit_instance = MerkleTreeCircuit:: { + let mut circuit_instance = MerkleTreeCircuit:: { tree: tree.clone(), - _phantom: PhantomData, + // _phantom: PhantomData, }; let (mut targets, expected_root_target) = circuit_instance.build_circuit(&mut builder); @@ -312,15 +300,14 @@ mod tests { let zero_hash = HashOut { elements: [GoldilocksField::ZERO; 4], }; - let tree = MerkleTree::::new(&leaves, zero_hash)?; + let tree = MerkleTree::::new(&leaves, zero_hash)?; let expected_root = tree.root()?; let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::::new(config); - let mut circuit_instance = MerkleTreeCircuit:: { + let mut circuit_instance = MerkleTreeCircuit:: { tree: tree.clone(), - _phantom: PhantomData, }; let (mut targets, expected_root_target) = circuit_instance.build_circuit(&mut builder); diff --git a/codex-plonky2-circuits/src/circuits/sample_cells.rs b/codex-plonky2-circuits/src/circuits/sample_cells.rs index 9231a41..fb675bf 100644 --- a/codex-plonky2-circuits/src/circuits/sample_cells.rs +++ b/codex-plonky2-circuits/src/circuits/sample_cells.rs @@ -26,7 +26,7 @@ use plonky2::plonk::config::PoseidonGoldilocksConfig; use plonky2::hash::hashing::PlonkyPermutation; use crate::circuits::prove_single_cell::{SingleCellTargets, SlotTreeCircuit}; -use crate::circuits::params::{BOT_DEPTH, DATASET_DEPTH, MAX_DEPTH, N_FIELD_ELEMS_PER_CELL, N_SAMPLES, TESTING_SLOT_INDEX}; +use crate::circuits::params::{BOT_DEPTH, DATASET_DEPTH, HF, MAX_DEPTH, N_FIELD_ELEMS_PER_CELL, N_SAMPLES, TESTING_SLOT_INDEX}; use crate::circuits::safe_tree_circuit::{MerkleTreeCircuit, MerkleTreeTargets}; use crate::circuits::utils::{bits_le_padded_to_usize, calculate_cell_index_bits}; @@ -36,35 +36,31 @@ use crate::circuits::utils::{bits_le_padded_to_usize, calculate_cell_index_bits} #[derive(Clone)] pub struct DatasetTreeCircuit< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, > { - pub tree: MerkleTreeCircuit, // dataset tree - pub slot_trees: Vec>, // vec of slot trees + pub tree: MerkleTreeCircuit, // dataset tree + pub slot_trees: Vec>, // vec of slot trees } /// Dataset Merkle proof struct, containing the dataset proof and N_SAMPLES proofs. #[derive(Clone)] -pub struct DatasetMerkleProof> { +pub struct DatasetMerkleProof { pub slot_index: usize, pub entropy: usize, - pub dataset_proof: MerkleProof, // proof for dataset level tree - pub slot_proofs: Vec>, // proofs for sampled slot, contains N_SAMPLES proofs + pub dataset_proof: MerkleProof, // proof for dataset level tree + pub slot_proofs: Vec>, // proofs for sampled slot, contains N_SAMPLES proofs } impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, -> Default for DatasetTreeCircuit { +> Default for DatasetTreeCircuit { /// dataset tree with fake data, for testing only fn default() -> Self { let mut slot_trees = vec![]; let n_slots = 1<::default()); + slot_trees.push(SlotTreeCircuit::::default()); } // get the roots or slot trees let slot_roots = slot_trees.iter() @@ -76,9 +72,9 @@ impl< let zero = HashOut { elements: [F::ZERO; 4], }; - let dataset_tree = MerkleTree::::new(&slot_roots, zero).unwrap(); + let dataset_tree = MerkleTree::::new(&slot_roots, zero).unwrap(); Self{ - tree: MerkleTreeCircuit::{ tree:dataset_tree, _phantom:Default::default()}, + tree: MerkleTreeCircuit::{ tree:dataset_tree}, slot_trees, } } @@ -87,10 +83,8 @@ impl< impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher, -> DatasetTreeCircuit { +> DatasetTreeCircuit { /// dataset tree with fake data, for testing only /// create data for only the TESTING_SLOT_INDEX in params file pub fn new_for_testing() -> Self { @@ -100,17 +94,16 @@ impl< let zero = HashOut { elements: [F::ZERO; 4], }; - let zero_slot = SlotTreeCircuit::{ + let zero_slot = SlotTreeCircuit::{ tree: MerkleTreeCircuit { - tree: MerkleTree::::new(&[zero.clone()], zero.clone()).unwrap(), - _phantom: Default::default(), + tree: MerkleTree::::new(&[zero.clone()], zero.clone()).unwrap(), }, block_trees: vec![], cell_data: vec![], }; for i in 0..n_slots { if(i == TESTING_SLOT_INDEX) { - slot_trees.push(SlotTreeCircuit::::new_for_testing()); + slot_trees.push(SlotTreeCircuit::::new_for_testing()); }else{ slot_trees.push(zero_slot.clone()); } @@ -122,15 +115,15 @@ impl< t.tree.tree.root().unwrap() }) .collect::>(); - let dataset_tree = MerkleTree::::new(&slot_roots, zero).unwrap(); + let dataset_tree = MerkleTree::::new(&slot_roots, zero).unwrap(); Self{ - tree: MerkleTreeCircuit::{ tree:dataset_tree, _phantom:Default::default()}, + tree: MerkleTreeCircuit::{ tree:dataset_tree}, slot_trees, } } /// same as default but with supplied slot trees - pub fn new(slot_trees: Vec>) -> Self{ + pub fn new(slot_trees: Vec>) -> Self{ // get the roots or slot trees let slot_roots = slot_trees.iter() .map(|t| { @@ -141,23 +134,23 @@ impl< let zero = HashOut { elements: [F::ZERO; 4], }; - let dataset_tree = MerkleTree::::new(&slot_roots, zero).unwrap(); + let dataset_tree = MerkleTree::::new(&slot_roots, zero).unwrap(); Self{ - tree: MerkleTreeCircuit::{ tree:dataset_tree, _phantom:Default::default()}, + tree: MerkleTreeCircuit::{ tree:dataset_tree}, slot_trees, } } /// generates a dataset level proof for given slot index /// just a regular merkle tree proof - pub fn get_proof(&self, index: usize) -> MerkleProof { + pub fn get_proof(&self, index: usize) -> MerkleProof { let dataset_proof = self.tree.tree.get_proof(index).unwrap(); dataset_proof } /// generates a proof for given slot index /// also takes entropy so it can use it sample the slot - pub fn sample_slot(&self, index: usize, entropy: usize) -> DatasetMerkleProof { + pub fn sample_slot(&self, index: usize, entropy: usize) -> DatasetMerkleProof { let dataset_proof = self.tree.tree.get_proof(index).unwrap(); let slot = &self.slot_trees[index]; let slot_root = slot.tree.tree.root().unwrap(); @@ -178,7 +171,7 @@ impl< } // verify the sampling - non-circuit version - pub fn verify_sampling(&self, proof: DatasetMerkleProof) -> Result{ + pub fn verify_sampling(&self, proof: DatasetMerkleProof) -> Result{ let slot = &self.slot_trees[proof.slot_index]; let slot_root = slot.tree.tree.root().unwrap(); // check dataset level proof @@ -204,12 +197,7 @@ impl< } #[derive(Clone)] -pub struct DatasetTargets< - F: RichField + Extendable + Poseidon2, - C: GenericConfig, - const D: usize, - H: Hasher + AlgebraicHasher, -> { +pub struct DatasetTargets{ pub dataset_proof: MerkleProofTarget, // proof that slot_root in dataset tree pub dataset_root: HashOutTarget, @@ -219,23 +207,20 @@ pub struct DatasetTargets< pub slot_root: HashOutTarget, pub slot_proofs: Vec, - _phantom: PhantomData<(C,H)>, } //------- circuit impl -------- impl< F: RichField + Extendable + Poseidon2, - C: GenericConfig, const D: usize, - H: Hasher + AlgebraicHasher + Hasher, -> DatasetTreeCircuit { +> DatasetTreeCircuit { // in-circuit sampling // TODO: make it more modular pub fn sample_slot_circuit( &mut self, builder: &mut CircuitBuilder::, - )-> DatasetTargets{ + )-> DatasetTargets { // constants let zero = builder.zero(); @@ -266,17 +251,16 @@ impl< }; // create MerkleTreeTargets struct - let mut d_targets = MerkleTreeTargets { + let mut d_targets = MerkleTreeTargets{ leaf: slot_root, path_bits: d_path_bits, last_bits: d_last_bits, merkle_path: d_merkle_path, - _phantom: PhantomData, }; // dataset reconstructed root let d_reconstructed_root = - MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut d_targets); + MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut d_targets); // expected Merkle root let d_expected_root = builder.add_virtual_hash(); @@ -309,7 +293,7 @@ impl< let mut perm_inputs:Vec= Vec::new(); perm_inputs.extend_from_slice(&data_i); - let data_i_hash = builder.hash_n_to_hash_no_pad::(perm_inputs); + let data_i_hash = builder.hash_n_to_hash_no_pad::(perm_inputs); // counter constant let ctr_target = builder.constant(F::from_canonical_u64((i+1) as u64)); let mut ctr = builder.add_virtual_hash(); @@ -337,22 +321,20 @@ impl< path_bits:b_path_bits, last_bits: b_last_bits.clone(), merkle_path: b_merkle_path, - _phantom: PhantomData, }; // reconstruct block root - let b_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut block_targets); + let b_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut block_targets); let mut slot_targets = MerkleTreeTargets { leaf: b_root, path_bits:s_path_bits, last_bits:s_last_bits.clone(), merkle_path:s_merkle_path, - _phantom: PhantomData, }; // reconstruct slot root with block root as leaf - let slot_reconstructed_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut slot_targets); + let slot_reconstructed_root = MerkleTreeCircuit::::reconstruct_merkle_root_circuit(builder, &mut slot_targets); // check equality with expected root for i in 0..NUM_HASH_OUT_ELTS { @@ -370,7 +352,7 @@ impl< } - DatasetTargets::{ + DatasetTargets{ dataset_proof: d_targets.merkle_path, dataset_root: d_expected_root, cell_data: data_targets, @@ -378,7 +360,6 @@ impl< slot_index, slot_root: d_targets.leaf, slot_proofs: slot_sample_proofs, - _phantom: Default::default(), } } @@ -387,7 +368,7 @@ impl< hash_inputs.extend_from_slice(&entropy.elements); hash_inputs.extend_from_slice(&slot_root.elements); hash_inputs.extend_from_slice(&ctr.elements); - let hash_out = builder.hash_n_to_hash_no_pad::(hash_inputs); + let hash_out = builder.hash_n_to_hash_no_pad::(hash_inputs); let cell_index_bits = builder.low_bits(hash_out.elements[0], MAX_DEPTH, 64); cell_index_bits @@ -396,7 +377,7 @@ impl< pub fn sample_slot_assign_witness( &mut self, pw: &mut PartialWitness, - targets: &mut DatasetTargets, + targets: &mut DatasetTargets, slot_index:usize, entropy:usize, ){ @@ -475,7 +456,7 @@ mod tests { #[test] fn test_sample_cells() { - let dataset_t = DatasetTreeCircuit::::default(); + let dataset_t = DatasetTreeCircuit::::default(); let slot_index = 2; let entropy = 123; let proof = dataset_t.sample_slot(slot_index,entropy); @@ -488,7 +469,7 @@ mod tests { #[test] fn test_sample_cells_circuit() -> Result<()> { - let mut dataset_t = DatasetTreeCircuit::::default(); + let mut dataset_t = DatasetTreeCircuit::::default(); let slot_index = 2; let entropy = 123; @@ -532,7 +513,7 @@ mod tests { #[test] fn test_sample_cells_circuit_from_selected_slot() -> Result<()> { - let mut dataset_t = DatasetTreeCircuit::::new_for_testing(); + let mut dataset_t = DatasetTreeCircuit::::new_for_testing(); let slot_index = TESTING_SLOT_INDEX; let entropy = 123; diff --git a/codex-plonky2-circuits/src/merkle_tree/merkle_safe.rs b/codex-plonky2-circuits/src/merkle_tree/merkle_safe.rs index 9c1af56..03d29f8 100644 --- a/codex-plonky2-circuits/src/merkle_tree/merkle_safe.rs +++ b/codex-plonky2-circuits/src/merkle_tree/merkle_safe.rs @@ -25,24 +25,21 @@ type HF = PoseidonHash; /// Merkle tree struct, containing the layers, compression function, and zero hash. #[derive(Clone)] -pub struct MerkleTree> { +pub struct MerkleTree { pub layers: Vec>>, pub zero: HashOut, - phantom_data: PhantomData } -impl> MerkleTree { +impl MerkleTree { /// Constructs a new Merkle tree from the given leaves. pub fn new( leaves: &[HashOut], zero: HashOut, ) -> Result { - let layers = merkle_tree_worker::(leaves, zero, true)?; + let layers = merkle_tree_worker::(leaves, zero, true)?; Ok(Self { layers, - // compress, zero, - phantom_data: Default::default(), }) } @@ -64,7 +61,7 @@ impl> MerkleTree { } /// Generates a Merkle proof for a given leaf index. - pub fn get_proof(&self, index: usize) -> Result> { + pub fn get_proof(&self, index: usize) -> Result> { let depth = self.depth(); let nleaves = self.leaves_count(); @@ -91,7 +88,6 @@ impl> MerkleTree { path, nleaves, zero: self.zero, - phantom_data: Default::default(), }) } } @@ -107,7 +103,7 @@ fn key_compress(x: HashOut, y: HashOut, key: u64) -> HashOut } /// Build the Merkle tree layers. -fn merkle_tree_worker>( +fn merkle_tree_worker( xs: &[HashOut], zero: HashOut, is_bottom_layer: bool, @@ -140,7 +136,7 @@ fn merkle_tree_worker>( } let mut layers = vec![xs.to_vec()]; - let mut upper_layers = merkle_tree_worker::(&ys, zero, false)?; + let mut upper_layers = merkle_tree_worker::(&ys, zero, false)?; layers.append(&mut upper_layers); Ok(layers) @@ -148,12 +144,11 @@ fn merkle_tree_worker>( /// Merkle proof struct, containing the index, path, and other necessary data. #[derive(Clone)] -pub struct MerkleProof> { +pub struct MerkleProof { pub index: usize, // Index of the leaf pub path: Vec>, // Sibling hashes from the leaf to the root pub nleaves: usize, // Total number of leaves pub zero: HashOut, - pub(crate) phantom_data: PhantomData } #[derive(Clone, Debug, Eq, PartialEq)] @@ -162,7 +157,7 @@ pub struct MerkleProofTarget { pub path: Vec, } -impl> MerkleProof { +impl MerkleProof { /// Reconstructs the root hash from the proof and the given leaf. pub fn reconstruct_root(&self, leaf: HashOut) -> Result> { let mut m = self.nleaves; @@ -271,7 +266,7 @@ mod tests { fn make_tree( data: &[F], zero: HashOut, - ) -> Result> { + ) -> Result> { // Hash the data to obtain leaf hashes let leaves: Vec> = data .iter() @@ -281,7 +276,7 @@ mod tests { }) .collect(); - MerkleTree::::new(&leaves, zero) + MerkleTree::::new(&leaves, zero) } #[test] @@ -304,7 +299,7 @@ mod tests { }; // Build the Merkle tree - let tree = MerkleTree::::new(&leaves, zero)?; + let tree = MerkleTree::::new(&leaves, zero)?; // Get the root let root = tree.root()?; @@ -366,7 +361,7 @@ mod tests { KEY_NONE, ), KEY_NONE, - ); + ); // Build the tree let tree = make_tree(&data, zero)?; @@ -427,7 +422,7 @@ mod tests { KEY_NONE, ), KEY_NONE, - ); + ); // Build the tree let tree = make_tree(&data, zero)?; @@ -536,7 +531,7 @@ mod tests { }; // Build the tree - let tree = MerkleTree::::new(&leaf_hashes, zero)?; + let tree = MerkleTree::::new(&leaf_hashes, zero)?; // Get the root let expected_root = tree.root()?;