diff --git a/codex-plonky2-circuits/src/circuits/sample_cells.rs b/codex-plonky2-circuits/src/circuits/sample_cells.rs index 645c3a1..81e7743 100644 --- a/codex-plonky2-circuits/src/circuits/sample_cells.rs +++ b/codex-plonky2-circuits/src/circuits/sample_cells.rs @@ -18,7 +18,7 @@ use crate::circuits::params::{CircuitParams, HF}; use crate::circuits::merkle_circuit::{MerkleProofTarget, MerkleTreeCircuit, MerkleTreeTargets}; use crate::circuits::sponge::{hash_n_no_padding, hash_n_with_padding}; -use crate::circuits::utils::assign_hash_out_targets; +use crate::circuits::utils::{assign_hash_out_targets, ceiling_log2}; /// circuit for sampling a slot in a dataset merkle tree #[derive(Clone)] @@ -144,12 +144,9 @@ impl< // create virtual target for n_slots_per_dataset let n_slots_per_dataset = builder.add_virtual_target(); - // dataset last bits (binary decomposition of last_index = nleaves - 1) - let dataset_last_index = builder.sub(n_slots_per_dataset, one); - let d_last_bits = builder.split_le(dataset_last_index,max_log2_n_slots); - - // dataset mask bits - let mut d_mask_bits = builder.split_le(dataset_last_index,max_log2_n_slots+1); + // dataset last bits and mask bits + let (d_last_bits, d_mask_bits) = + ceiling_log2(builder, n_slots_per_dataset, max_log2_n_slots); // dataset Merkle path (sibling hashes from leaf to root) let d_merkle_path = MerkleProofTarget { diff --git a/codex-plonky2-circuits/src/circuits/utils.rs b/codex-plonky2-circuits/src/circuits/utils.rs index e9cc8bd..9448bd2 100644 --- a/codex-plonky2-circuits/src/circuits/utils.rs +++ b/codex-plonky2-circuits/src/circuits/utils.rs @@ -12,6 +12,33 @@ use plonky2::plonk::circuit_builder::CircuitBuilder; // --------- helper functions --------- +/// computes the `last_index` (the binary decomposition of `inp-1`) and the `mask_bits` +pub fn ceiling_log2< + F: RichField + Extendable + Poseidon2, + const D: usize, +>( + builder: &mut CircuitBuilder::, + inp: Target, + n: usize, +)-> (Vec, Vec){ + let one = builder.one(); + let zero = builder.zero(); + let last_index = builder.sub(inp, one.clone()); + let last_bits = builder.split_le(last_index,n); + + let mut aux: Vec = vec![BoolTarget::new_unsafe(zero.clone()); n + 1]; + aux[n] = BoolTarget::new_unsafe(one.clone()); + let mut mask: Vec = vec![BoolTarget::new_unsafe(zero.clone()); n + 1]; + for i in (0..n).rev(){ + let diff = (builder.sub(one.clone(), last_bits[i].target)); + let aux_i = builder.mul( aux[i+1].target, diff); + aux[i] = BoolTarget::new_unsafe(aux_i); + mask[i] = BoolTarget::new_unsafe(builder.sub(one.clone(), aux[i].target)); + } + + (last_bits, mask) +} + /// assign a vec of bool values to a vec of BoolTargets pub fn assign_bool_targets< F: RichField + Extendable + Poseidon2, diff --git a/proof-input/src/params.rs b/proof-input/src/params.rs index c2edee8..9d3ad03 100644 --- a/proof-input/src/params.rs +++ b/proof-input/src/params.rs @@ -24,7 +24,7 @@ const DEFAULT_N_SAMPLES: usize = 5; // number of samples to prove const DEFAULT_ENTROPY: usize = 1234567; // external randomness const DEFAULT_SEED: usize = 12345; // seed for creating fake data TODO: not used now -const DEFAULT_N_SLOTS: usize = 16; // number of slots in the dataset +const DEFAULT_N_SLOTS: usize = 11; // number of slots in the dataset const DEFAULT_SLOT_INDEX: usize = 3; // the index of the slot to be sampled const DEFAULT_N_CELLS: usize = 512; // number of cells in each slot diff --git a/workflow/input.json b/workflow/input.json index 3ddb7cb..903802a 100644 --- a/workflow/input.json +++ b/workflow/input.json @@ -1,15 +1,15 @@ { - "dataSetRoot": [ "14459953088494886308", "12400665201701660877", "8918969394875474575", "3734475392324688728" ] + "dataSetRoot": [ "6755155175595395828", "313507776630410965", "9227740409681413313", "2795138142659503669" ] , "entropy": [ "1234567", "0", "0", "0" ] , "nCellsPerSlot": 512 -, "nSlotsPerDataSet": 16 +, "nSlotsPerDataSet": 11 , "slotIndex": 3 , "slotRoot": [ "6216356142838248961", "7651361162368135479", "8250178335123580371", "3813462866599431579" ] , "slotProof": [ "1345604040032513712" , "7222769029677219453" , "4856886058017005512" , "17218820401481758629" , "6741690371018853470" , "10000950172891759230" , "1256624250298316158" , "14572953286928282395" , "11250861626949238654" , "2066450512590186880" , "4406339264013603126" , "6649535526486987988" -, "14920223145083393283" , "18017129979212138612" , "1235310154294028825" , "16382646529383194172" +, "13405718683906940252" , "10830552896276419282" , "17191223065200470009" , "6751178910350619564" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" , "0" diff --git a/workflow/params.sh b/workflow/params.sh index 69cb0ea..12db138 100644 --- a/workflow/params.sh +++ b/workflow/params.sh @@ -11,15 +11,4 @@ export SEED=12345 # seed for creating fake data export NSLOTS=11 # number of slots in the dataset export SLOTINDEX=3 # which slot we prove (0..NSLOTS-1) -export NCELLS=512 # number of cells in this slot - -#export MAXDEPTH=8 -#export MAXSLOTS=256 -#export CELLSIZE=2048 -#export BLOCKSIZE=65536 -#export NSAMPLES=5 -#export ENTROPY=1234567 -#export SEED=12345 -#export NSLOTS=8 -#export SLOTINDEX=2 -#export NCELLS=512 \ No newline at end of file +export NCELLS=512 # number of cells in this slot \ No newline at end of file diff --git a/workflow/src/main.rs b/workflow/src/main.rs index b94c422..75f76e4 100644 --- a/workflow/src/main.rs +++ b/workflow/src/main.rs @@ -5,7 +5,7 @@ use plonky2::plonk::circuit_builder::CircuitBuilder; use anyhow::Result; use std::time::Instant; -use proof_input::json::import_witness_from_json; +use proof_input::json::import_circ_input_from_json; use codex_plonky2_circuits::circuits::sample_cells::{SampleCircuit, SampleCircuitInput}; use codex_plonky2_circuits::circuits::params::CircuitParams; use proof_input::params::Params; @@ -16,20 +16,14 @@ fn main() -> Result<()> { let params = Params::from_env()?; // Read the witness from input.json - let witness: SampleCircuitInput = import_witness_from_json("input.json")?; + let witness: SampleCircuitInput = import_circ_input_from_json("input.json")?; println!("Witness imported from input.json"); // Create the circuit let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::::new(config); - let circuit_params = CircuitParams { - max_depth: params.max_depth, - max_log2_n_slots: params.dataset_depth(), - block_tree_depth: params.bot_depth(), - n_field_elems_per_cell: params.n_field_elems_per_cell(), - n_samples: params.n_samples, - }; + let circuit_params = params.circuit_params; let circ = SampleCircuit::new(circuit_params); let mut targets = circ.sample_slot_circuit(&mut builder);