Skip to content

Commit

Permalink
test: Add and mod RLC tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xearty committed Oct 2, 2023
1 parent c049c93 commit f77d751
Showing 1 changed file with 83 additions and 7 deletions.
90 changes: 83 additions & 7 deletions plonky2x/src/frontend/eth/mpt/rlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub(crate) mod tests {
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

use crate::frontend::builder::DefaultBuilder;
use crate::prelude::{ByteVariable, CircuitVariable, Variable};
use crate::prelude::{ByteVariable, Variable};

impl Default for ByteVariable {
fn default() -> ByteVariable {
Expand All @@ -103,11 +103,87 @@ pub(crate) mod tests {
let mut b: [ByteVariable; MAX_LEN] = Default::default();

for i in 0..MAX_LEN {
a[i] = ByteVariable::constant(&mut builder, (i + 5) as u8);
a[i] = builder.constant::<ByteVariable>((i + 5) as u8);
}

for i in 0..MAX_LEN {
b[i] = ByteVariable::constant(&mut builder, i as u8);
b[i] = builder.constant::<ByteVariable>(i as u8);
}

let a_offset: Variable = builder.constant(F::ZERO);
let b_offset = builder.constant(F::from_canonical_usize(5));
let len: Variable = builder.constant(F::from_canonical_usize(5));
builder.assert_subarray_equal(&a, a_offset, &b, b_offset, len);

// Build your circuit.
let circuit = builder.build();

// Write to the circuit input.
let input = circuit.input();

// Generate a proof.
let (proof, output) = circuit.prove(&input);

// Verify proof.
circuit.verify(&proof, &input, &output)
}

#[test]
pub fn test_subarray_equal_diff_len_should_succeed() {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut builder = DefaultBuilder::new();

const LEN_A: usize = 15;
const LEN_B: usize = 10;
let mut a: [ByteVariable; LEN_A] = Default::default();
let mut b: [ByteVariable; LEN_B] = Default::default();

for i in 0..LEN_A {
a[i] = builder.constant::<ByteVariable>((i + 5) as u8);
}

for i in 0..LEN_B {
b[i] = builder.constant::<ByteVariable>(i as u8);
}

let a_offset: Variable = builder.constant(F::ZERO);
let b_offset = builder.constant(F::from_canonical_usize(5));
let len: Variable = builder.constant(F::from_canonical_usize(5));
builder.assert_subarray_equal(&a, a_offset, &b, b_offset, len);

// Build your circuit.
let circuit = builder.build();

// Write to the circuit input.
let input = circuit.input();

// Generate a proof.
let (proof, output) = circuit.prove(&input);

// Verify proof.
circuit.verify(&proof, &input, &output)
}

#[test]
pub fn test_subarray_equal_diff_len_inverse_should_succeed() {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut builder = DefaultBuilder::new();

const LEN_A: usize = 10;
const LEN_B: usize = 15;
let mut a: [ByteVariable; LEN_A] = Default::default();
let mut b: [ByteVariable; LEN_B] = Default::default();

for i in 0..LEN_A {
a[i] = builder.constant::<ByteVariable>((i + 5) as u8);
}

for i in 0..LEN_B {
b[i] = builder.constant::<ByteVariable>(i as u8);
}

let a_offset: Variable = builder.constant(F::ZERO);
Expand Down Expand Up @@ -141,15 +217,15 @@ pub(crate) mod tests {
let mut b: [ByteVariable; MAX_LEN] = Default::default();

for i in 0..MAX_LEN {
a[i] = ByteVariable::constant(&mut builder, (i + 5) as u8);
a[i] = builder.constant::<ByteVariable>((i + 5) as u8);
}

for i in 0..MAX_LEN {
b[i] = ByteVariable::constant(&mut builder, i as u8);
b[i] = builder.constant::<ByteVariable>(i as u8);
}

// Modify 1 byte here.
b[6] = ByteVariable::constant(&mut builder, 0);
b[6] = builder.constant::<ByteVariable>(0);

let a_offset = builder.constant(F::ZERO);
let b_offset = builder.constant(F::from_canonical_usize(5));
Expand Down

0 comments on commit f77d751

Please sign in to comment.