add ceiling_log2 and refactor

This commit is contained in:
M Alghazwi 2024-11-12 13:06:28 +01:00
parent 2b2aaab749
commit 01f13df605
6 changed files with 39 additions and 32 deletions

View File

@ -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 {

View File

@ -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<D> + Poseidon2,
const D: usize,
>(
builder: &mut CircuitBuilder::<F, D>,
inp: Target,
n: usize,
)-> (Vec<BoolTarget>, Vec<BoolTarget>){
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<BoolTarget> = vec![BoolTarget::new_unsafe(zero.clone()); n + 1];
aux[n] = BoolTarget::new_unsafe(one.clone());
let mut mask: Vec<BoolTarget> = 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<D> + Poseidon2,

View File

@ -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

View File

@ -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"

View File

@ -12,14 +12,3 @@ 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

View File

@ -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<F, D> = import_witness_from_json("input.json")?;
let witness: SampleCircuitInput<F, D> = 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::<F, D>::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);