From 911dc38f30e06bc739e1beaad919dc5c2a5cb8c5 Mon Sep 17 00:00:00 2001 From: M Alghazwi Date: Sun, 3 Nov 2024 11:39:59 +0100 Subject: [PATCH] change entropy to digest --- .../src/circuits/sample_cells.rs | 40 +++++++++++++------ codex-plonky2-circuits/src/circuits/utils.rs | 24 ++++++----- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/codex-plonky2-circuits/src/circuits/sample_cells.rs b/codex-plonky2-circuits/src/circuits/sample_cells.rs index 76eb800..6d574dd 100644 --- a/codex-plonky2-circuits/src/circuits/sample_cells.rs +++ b/codex-plonky2-circuits/src/circuits/sample_cells.rs @@ -214,7 +214,7 @@ pub struct DatasetTargets< pub dataset_root: HashOutTarget, pub cell_data: Vec>, - pub entropy:Target, + pub entropy: HashOutTarget, pub slot_index: Target, pub slot_root: HashOutTarget, pub slot_proofs: Vec, @@ -238,6 +238,7 @@ impl< )-> DatasetTargets{ // constants + let zero = builder.zero(); let one = builder.one(); let two = builder.two(); @@ -289,7 +290,7 @@ impl< let mut data_targets =vec![]; let mut slot_sample_proofs = vec![]; - let entropy_target = builder.add_virtual_target(); + let entropy_target = builder.add_virtual_hash(); for i in 0..N_SAMPLES{ // cell data targets let mut data_i = (0..N_FIELD_ELEMS_PER_CELL).map(|_| builder.add_virtual_target()).collect::>(); @@ -298,7 +299,15 @@ impl< perm_inputs.extend_from_slice(&data_i); let data_i_hash = builder.hash_n_to_hash_no_pad::(perm_inputs); // counter constant - let ctr = builder.constant(F::from_canonical_u64(i as u64)); + let ctr_target = builder.constant(F::from_canonical_u64(i as u64)); + let mut ctr = builder.add_virtual_hash(); + for i in 0..ctr.elements.len() { + if(i==0){ + ctr.elements[i] = ctr_target; + }else{ + ctr.elements[i] = zero.clone(); + } + } // paths let mut b_path_bits = Self::calculate_cell_index_bits(builder, &entropy_target, &d_targets.leaf, &ctr); let mut s_path_bits = b_path_bits.split_off(BOT_DEPTH); @@ -372,15 +381,15 @@ impl< } } - pub fn calculate_cell_index_bits(builder: &mut CircuitBuilder::, p0: &Target, p1: &HashOutTarget, p2: &Target) -> Vec { - let mut perm_inputs:Vec= Vec::new(); - perm_inputs.extend_from_slice(&p1.elements); - perm_inputs.push(*p0); - perm_inputs.push(*p2); - let data_i_hash = builder.hash_n_to_hash_no_pad::(perm_inputs); - let p_bits = builder.low_bits(data_i_hash.elements[NUM_HASH_OUT_ELTS-1], MAX_DEPTH, 64); + pub fn calculate_cell_index_bits(builder: &mut CircuitBuilder::, entropy: &HashOutTarget, slot_root: &HashOutTarget, ctr: &HashOutTarget) -> Vec { + let mut hash_inputs:Vec= Vec::new(); + 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 cell_index_bits = builder.low_bits(hash_out.elements[0], MAX_DEPTH, 64); - p_bits + cell_index_bits } pub fn sample_slot_assign_witness( @@ -417,7 +426,14 @@ impl< pw.set_hash_target(targets.slot_root, slot_root); // assign entropy - pw.set_target(targets.entropy, F::from_canonical_u64(entropy as u64)); + for (i, element) in targets.entropy.elements.iter().enumerate() { + if(i==0) { + pw.set_target(*element, F::from_canonical_u64(entropy as u64)); + }else { + pw.set_target(*element, F::from_canonical_u64(0)); + } + } + // pw.set_target(targets.entropy, F::from_canonical_u64(entropy as u64)); // do the sample N times for i in 0..N_SAMPLES { diff --git a/codex-plonky2-circuits/src/circuits/utils.rs b/codex-plonky2-circuits/src/circuits/utils.rs index 6436fc5..6721c98 100644 --- a/codex-plonky2-circuits/src/circuits/utils.rs +++ b/codex-plonky2-circuits/src/circuits/utils.rs @@ -24,18 +24,22 @@ pub(crate) fn usize_to_bits_le_padded(index: usize, bit_length: usize) -> Vec(entropy: usize, slot_root: HashOut, ctr: usize) -> Vec { - let p0_field = F::from_canonical_u64(entropy as u64); - let p2_field = F::from_canonical_u64(ctr as u64); - let mut inputs = Vec::new(); - inputs.extend_from_slice(&slot_root.elements); - inputs.push(p0_field); - inputs.push(p2_field); - let p_hash = HF::hash_no_pad(&inputs); - let p_bytes = p_hash.elements[NUM_HASH_OUT_ELTS - 1].to_canonical_u64(); + let entropy_field = F::from_canonical_u64(entropy as u64); + let mut entropy_as_digest = HashOut::::ZERO; + entropy_as_digest.elements[0] = entropy_field; + let ctr_field = F::from_canonical_u64(ctr as u64); + let mut ctr_as_digest = HashOut::::ZERO; + ctr_as_digest.elements[0] = ctr_field; + let mut hash_inputs = Vec::new(); + hash_inputs.extend_from_slice(&entropy_as_digest.elements); + hash_inputs.extend_from_slice(&slot_root.elements); + hash_inputs.extend_from_slice(&ctr_as_digest.elements); + let hash_output = HF::hash_no_pad(&hash_inputs); + let cell_index_bytes = hash_output.elements[0].to_canonical_u64(); // let p_bits = take_n_bits_from_bytes(&p_bytes, MAX_DEPTH); - let p_bits = usize_to_bits_le_padded(p_bytes as usize, MAX_DEPTH); - p_bits + let cell_index_bits = usize_to_bits_le_padded(cell_index_bytes as usize, MAX_DEPTH); + cell_index_bits } pub(crate) fn take_n_bits_from_bytes(bytes: &[u8], n: usize) -> Vec { bytes.iter()