change entropy to digest
This commit is contained in:
parent
76ed312e2c
commit
911dc38f30
|
@ -214,7 +214,7 @@ pub struct DatasetTargets<
|
||||||
pub dataset_root: HashOutTarget,
|
pub dataset_root: HashOutTarget,
|
||||||
|
|
||||||
pub cell_data: Vec<Vec<Target>>,
|
pub cell_data: Vec<Vec<Target>>,
|
||||||
pub entropy:Target,
|
pub entropy: HashOutTarget,
|
||||||
pub slot_index: Target,
|
pub slot_index: Target,
|
||||||
pub slot_root: HashOutTarget,
|
pub slot_root: HashOutTarget,
|
||||||
pub slot_proofs: Vec<MerkleProofTarget>,
|
pub slot_proofs: Vec<MerkleProofTarget>,
|
||||||
|
@ -238,6 +238,7 @@ impl<
|
||||||
)-> DatasetTargets<F,C,D,H>{
|
)-> DatasetTargets<F,C,D,H>{
|
||||||
|
|
||||||
// constants
|
// constants
|
||||||
|
let zero = builder.zero();
|
||||||
let one = builder.one();
|
let one = builder.one();
|
||||||
let two = builder.two();
|
let two = builder.two();
|
||||||
|
|
||||||
|
@ -289,7 +290,7 @@ impl<
|
||||||
|
|
||||||
let mut data_targets =vec![];
|
let mut data_targets =vec![];
|
||||||
let mut slot_sample_proofs = 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{
|
for i in 0..N_SAMPLES{
|
||||||
// cell data targets
|
// cell data targets
|
||||||
let mut data_i = (0..N_FIELD_ELEMS_PER_CELL).map(|_| builder.add_virtual_target()).collect::<Vec<_>>();
|
let mut data_i = (0..N_FIELD_ELEMS_PER_CELL).map(|_| builder.add_virtual_target()).collect::<Vec<_>>();
|
||||||
|
@ -298,7 +299,15 @@ impl<
|
||||||
perm_inputs.extend_from_slice(&data_i);
|
perm_inputs.extend_from_slice(&data_i);
|
||||||
let data_i_hash = builder.hash_n_to_hash_no_pad::<H>(perm_inputs);
|
let data_i_hash = builder.hash_n_to_hash_no_pad::<H>(perm_inputs);
|
||||||
// counter constant
|
// 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
|
// paths
|
||||||
let mut b_path_bits = Self::calculate_cell_index_bits(builder, &entropy_target, &d_targets.leaf, &ctr);
|
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);
|
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::<F, D>, p0: &Target, p1: &HashOutTarget, p2: &Target) -> Vec<BoolTarget> {
|
pub fn calculate_cell_index_bits(builder: &mut CircuitBuilder::<F, D>, entropy: &HashOutTarget, slot_root: &HashOutTarget, ctr: &HashOutTarget) -> Vec<BoolTarget> {
|
||||||
let mut perm_inputs:Vec<Target>= Vec::new();
|
let mut hash_inputs:Vec<Target>= Vec::new();
|
||||||
perm_inputs.extend_from_slice(&p1.elements);
|
hash_inputs.extend_from_slice(&entropy.elements);
|
||||||
perm_inputs.push(*p0);
|
hash_inputs.extend_from_slice(&slot_root.elements);
|
||||||
perm_inputs.push(*p2);
|
hash_inputs.extend_from_slice(&ctr.elements);
|
||||||
let data_i_hash = builder.hash_n_to_hash_no_pad::<H>(perm_inputs);
|
let hash_out = builder.hash_n_to_hash_no_pad::<H>(hash_inputs);
|
||||||
let p_bits = builder.low_bits(data_i_hash.elements[NUM_HASH_OUT_ELTS-1], MAX_DEPTH, 64);
|
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(
|
pub fn sample_slot_assign_witness(
|
||||||
|
@ -417,7 +426,14 @@ impl<
|
||||||
pw.set_hash_target(targets.slot_root, slot_root);
|
pw.set_hash_target(targets.slot_root, slot_root);
|
||||||
|
|
||||||
// assign entropy
|
// 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
|
// do the sample N times
|
||||||
for i in 0..N_SAMPLES {
|
for i in 0..N_SAMPLES {
|
||||||
|
|
|
@ -24,18 +24,22 @@ pub(crate) fn usize_to_bits_le_padded(index: usize, bit_length: usize) -> Vec<bo
|
||||||
}
|
}
|
||||||
/// calculate the sampled cell index from entropy, slot root, and counter
|
/// calculate the sampled cell index from entropy, slot root, and counter
|
||||||
pub(crate) fn calculate_cell_index_bits<F: RichField>(entropy: usize, slot_root: HashOut<F>, ctr: usize) -> Vec<bool> {
|
pub(crate) fn calculate_cell_index_bits<F: RichField>(entropy: usize, slot_root: HashOut<F>, ctr: usize) -> Vec<bool> {
|
||||||
let p0_field = F::from_canonical_u64(entropy as u64);
|
let entropy_field = F::from_canonical_u64(entropy as u64);
|
||||||
let p2_field = F::from_canonical_u64(ctr as u64);
|
let mut entropy_as_digest = HashOut::<F>::ZERO;
|
||||||
let mut inputs = Vec::new();
|
entropy_as_digest.elements[0] = entropy_field;
|
||||||
inputs.extend_from_slice(&slot_root.elements);
|
let ctr_field = F::from_canonical_u64(ctr as u64);
|
||||||
inputs.push(p0_field);
|
let mut ctr_as_digest = HashOut::<F>::ZERO;
|
||||||
inputs.push(p2_field);
|
ctr_as_digest.elements[0] = ctr_field;
|
||||||
let p_hash = HF::hash_no_pad(&inputs);
|
let mut hash_inputs = Vec::new();
|
||||||
let p_bytes = p_hash.elements[NUM_HASH_OUT_ELTS - 1].to_canonical_u64();
|
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 = take_n_bits_from_bytes(&p_bytes, MAX_DEPTH);
|
||||||
let p_bits = usize_to_bits_le_padded(p_bytes as usize, MAX_DEPTH);
|
let cell_index_bits = usize_to_bits_le_padded(cell_index_bytes as usize, MAX_DEPTH);
|
||||||
p_bits
|
cell_index_bits
|
||||||
}
|
}
|
||||||
pub(crate) fn take_n_bits_from_bytes(bytes: &[u8], n: usize) -> Vec<bool> {
|
pub(crate) fn take_n_bits_from_bytes(bytes: &[u8], n: usize) -> Vec<bool> {
|
||||||
bytes.iter()
|
bytes.iter()
|
||||||
|
|
Loading…
Reference in New Issue