Skip to content

Commit

Permalink
debug position_mod_256
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayKostadinov21 committed Oct 18, 2023
1 parent 7879287 commit aa3b2c9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
1 change: 1 addition & 0 deletions casper-finality-proofs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions casper-finality-proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ serde_json = "1.0.103"
ethers = { version = "2.0" }
itertools = { version = "0.10.0", default-features = false }
hex = "0.4.3"
array-macro = "2.1.5"
85 changes: 62 additions & 23 deletions casper-finality-proofs/src/compute_shuffled_index.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use plonky2::field::types::Field;
use plonky2::iop::target::BoolTarget;
use plonky2x::prelude::{BoolVariable, Bytes32Variable, CircuitBuilder, PlonkParameters, BytesVariable, Variable};
use plonky2x::prelude::{BoolVariable, Bytes32Variable, CircuitBuilder, PlonkParameters, BytesVariable, Variable, ByteVariable};
use crate::utils::variable::{to_bits, to_byte_variable};
use crate::utils::universal::{assert_is_true, le_sum, div_rem, exp_from_bits};
use crate::utils::universal::{assert_is_true, div_rem};

fn compute_shuffled_index<L: PlonkParameters<D>, const D: usize>(
builder: &mut CircuitBuilder<L, D>,
Expand All @@ -11,32 +11,26 @@ fn compute_shuffled_index<L: PlonkParameters<D>, const D: usize>(
seed: Bytes32Variable,
) -> Variable {
let index_lte_index_count = builder.lte(index, index_count);
assert_is_true(builder, index_lte_index_count);
assert_is_true(builder, index_lte_index_count); // Check if that's true

let const_1: Variable = builder.constant(L::Field::from_canonical_u8(1));
let const_2: Variable = builder.constant(L::Field::from_canonical_u8(2));
let const_8: Variable = builder.constant(L::Field::from_canonical_u8(8));
let const_256: Variable = builder.constant(L::Field::from_canonical_u16(256));
let const_256: Variable = builder.constant(L::Field::from_canonical_usize(256));
let const_0_byte: ByteVariable = ByteVariable::constant(builder, 0);
const SHUFFLE_ROUND_COUNT: usize = 90;
const TEST: usize = 5;
for current_round in 0..SHUFFLE_ROUND_COUNT {
let current_round_variable: Variable =
builder.constant(L::Field::from_canonical_u8(current_round as u8));
let current_round_bytes = to_byte_variable(current_round_variable, builder);
const TEST: usize = 3;
for current_round in 0..TEST {
let current_round_bytes: ByteVariable =
ByteVariable::constant(builder, current_round as u8);

let mut seed_round_to_be_hashed: BytesVariable<33> = builder.init::<BytesVariable<33>>();
let mut seed_round_to_be_hashed: BytesVariable<33> = BytesVariable([const_0_byte; 33]);
for i in 0..32 {
seed_round_to_be_hashed.0[i] = seed.0 .0[i];
}
seed_round_to_be_hashed.0[32] = current_round_bytes;

// debug::debug(builder, "index_count checkpoint".to_string(), index_count);
let seed_current_round_hashed = builder.sha256(&seed_round_to_be_hashed.0);
// debug::debug(
// builder,
// "AFTER SHA256".to_string(),
// seed_current_round_hashed.0 .0[0].0[0].0,
// );

let mut seed_current_round_hashed_first_64_bits: Vec<BoolVariable> = Vec::new();
for i in 0..8 {
Expand All @@ -54,21 +48,67 @@ fn compute_shuffled_index<L: PlonkParameters<D>, const D: usize>(
builder.add(addend, seed_current_round_hash_to_variable);
power_of_2 = builder.mul(const_2, power_of_2);
}

let pivot = div_rem(builder, seed_current_round_hash_to_variable, index_count);
// debug::debug(builder, "pivot".to_string(), pivot);

let sum_pivot_index_count = builder.add(pivot, index_count);
let sum_pivot_index_count_sub_index = builder.sub(sum_pivot_index_count, index);
let flip = div_rem(builder, sum_pivot_index_count_sub_index, index_count);

let index_lte_flip = builder.lte(index, flip);

let position = builder.select(index_lte_flip, flip, index);

let position_div_256 = builder.div(position, const_256);
let position_div_256_bytes = to_byte_variable(position_div_256, builder);

let mut source_to_be_hashed: BytesVariable<34> = builder.init::<BytesVariable<34>>();
let mut position_div_256_temp = position_div_256;
let mut result_vec = Vec::new();
for _ in 0..4 {
let low_bits = builder.api.low_bits(position_div_256.0, 8, 8);
let bits: [BoolVariable; 8] = low_bits
.iter()
.map(|x| BoolVariable::from(Variable(x.target)))
.collect_vec()
.try_into()
.unwrap();
let byte_var = ByteVariable(bits);
result_vec.push(byte_var);

position_div_256_temp = builder.div(position_div_256_temp, const_256);
}

for i in 0..4 {
for j in 0..8 {
builder.watch(&result_vec[i].0[j].0, "result_vec");
}
}

// let position_div_256_bytes = ByteVariable::from_target(builder, position_div_256.0);
// debug::debug(
// builder,
// "position_div_256_bytes - in variable".to_string(),
// position_div_256_bytes.0[0].0,
// );

// let position_div_256_bytes = position_div_256_bytes.to_variable(builder);

// debug::debug(
// builder,
// "position_div_256_bytes - in variable second".to_string(),
// position_div_256_bytes,
// );

// let position_div_256_bits: [BoolVariable; 64] = position_div_256.to_bits(builder);

// for i in 0..64 {
// debug::debug(
// builder,
// "position_div_256_bits".to_string(),
// position_div_256_bits[i].0,
// );
// }

let position_div_256_bytes = to_byte_variable(position_div_256, builder);
let mut source_to_be_hashed: BytesVariable<34> = BytesVariable([const_0_byte; 34]);
for i in 0..32 {
source_to_be_hashed.0[i] = seed.0 .0[i];
}
Expand All @@ -82,7 +122,6 @@ fn compute_shuffled_index<L: PlonkParameters<D>, const D: usize>(

let byte = builder.select_array(&source.0 .0, position_mod_256_div_8);
let byte_to_variable = byte.to_variable(builder);

let position_mod_8 = div_rem(builder, position, const_8);
let position_mod_8_to_bits: [BoolVariable; 8] = to_bits(position_mod_8, builder);
let position_mod_8_to_iter = position_mod_8_to_bits
Expand Down Expand Up @@ -130,9 +169,9 @@ mod tests {

let mut seed_bytes_fixed_size = [0u8; 32];
seed_bytes_fixed_size[..32].copy_from_slice(&seed_bytes);
input.write::<ArrayVariable<ByteVariable, 32>>(seed_bytes_fixed_size.to_vec());
input.write::<Variable>(F::from_canonical_usize(10));
input.write::<Variable>(F::from_canonical_usize(5));
input.write::<Variable>(F::from_canonical_usize(10));
input.write::<ArrayVariable<ByteVariable, 32>>(seed_bytes_fixed_size.to_vec());

let (_witness, mut _output) = circuit.mock_prove(&input);
}
Expand Down
9 changes: 9 additions & 0 deletions casper-finality-proofs/src/utils/byte_variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use plonky2x::prelude::{PlonkParameters, ByteVariable, BoolVariable, CircuitBuilder, CircuitVariable};
use array_macro::array;

pub fn constant<L: PlonkParameters<D>, const D: usize>(
builder: &mut CircuitBuilder<L, D>,
value: ByteVariable<L::Field>,
) -> ByteVariable {
ByteVariable(array![i => BoolVariable::constant(builder, (value >> (7 - i)) & 1 == 1); 8])
}
1 change: 1 addition & 0 deletions casper-finality-proofs/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod variable;
pub mod universal;
pub mod byte_variable;
2 changes: 1 addition & 1 deletion casper-finality-proofs/src/utils/universal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn div_rem<L: PlonkParameters<D>, const D: usize>(
let quotient = builder.div(lhs, rhs);
let quotient_times_rhs = builder.mul(quotient, rhs);

builder.sub(rhs, quotient_times_rhs)
builder.sub(lhs, quotient_times_rhs)
}

pub fn exp_from_bits<L: PlonkParameters<D>, const D: usize>(
Expand Down

0 comments on commit aa3b2c9

Please sign in to comment.