Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo committed Mar 13, 2024
1 parent 114db25 commit 36f0dbd
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions recursion/compiler/tests/for_loops.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
// use p3_baby_bear::BabyBear;
// use p3_field::AbstractField;
// use sp1_core::stark::StarkGenericConfig;
// use sp1_core::utils::BabyBearPoseidon2;
// use sp1_recursion_compiler::prelude::*;
// use sp1_recursion_core::runtime::Runtime;

// #[test]
// fn test_compiler_for_loops() {
// let mut builder = AsmCompiler::<BabyBear>::new();

// let n_val = BabyBear::from_canonical_u32(10);
// let m_val = BabyBear::from_canonical_u32(5);

// let zero: Felt<_> = builder.constant(BabyBear::zero());
// let n: Felt<_> = builder.constant(n_val);
// let m: Felt<_> = builder.constant(m_val);

// let i_counter: Felt<_> = builder.constant(BabyBear::zero());
// let total_counter: Felt<_> = builder.constant(BabyBear::zero());
// builder.range(zero, n).for_each(|_, builder| {
// builder.assign(i_counter, i_counter + BabyBear::one());

// let j_counter: Felt<_> = builder.constant(BabyBear::zero());
// builder.range(zero, m).for_each(|_, builder| {
// builder.assign(total_counter, total_counter + BabyBear::one());
// builder.assign(j_counter, j_counter + BabyBear::one());
// });
// // Assert that the inner loop ran m times, in two different ways.
// builder.assert_eq(j_counter, m_val);
// builder.assert_eq(j_counter, m);
// });
// // Assert that the outer loop ran n times, in two different ways.
// builder.assert_eq(i_counter, n_val);
// builder.assert_eq(i_counter, n);
// // Assert that the total counter is equal to n * m, in two ways.
// builder.assert_eq(total_counter, n_val * m_val);
// builder.assert_eq(total_counter, n * m);

// let program = builder.compile();

// type SC = BabyBearPoseidon2;
// type F = <SC as StarkGenericConfig>::Val;

// let mut runtime = Runtime::<F>::new(&program);
// runtime.run();
// }
use p3_baby_bear::BabyBear;
use p3_field::AbstractField;
use sp1_core::stark::StarkGenericConfig;
use sp1_core::utils::BabyBearPoseidon2;
use sp1_recursion_compiler::asm::VmBuilder;
use sp1_recursion_compiler::prelude::*;
use sp1_recursion_core::runtime::Runtime;

#[test]
fn test_compiler_for_loops() {
let mut builder = VmBuilder::<BabyBear>::default();

let n_val = BabyBear::from_canonical_u32(10);
let m_val = BabyBear::from_canonical_u32(5);

let zero: Var<_> = builder.eval(BabyBear::zero());
let n: Var<_> = builder.eval(n_val);
let m: Var<_> = builder.eval(m_val);

let i_counter: Var<_> = builder.eval(BabyBear::zero());
let total_counter: Var<_> = builder.eval(BabyBear::zero());
builder.range(zero, n).for_each(|_, builder| {
builder.assign(i_counter, i_counter + BabyBear::one());

let j_counter: Var<_> = builder.eval(BabyBear::zero());
builder.range(zero, m).for_each(|_, builder| {
builder.assign(total_counter, total_counter + BabyBear::one());
builder.assign(j_counter, j_counter + BabyBear::one());
});
// Assert that the inner loop ran m times, in two different ways.
builder.assert_var_eq(j_counter, m_val);
builder.assert_var_eq(j_counter, m);
});
// Assert that the outer loop ran n times, in two different ways.
builder.assert_var_eq(i_counter, n_val);
builder.assert_var_eq(i_counter, n);
// Assert that the total counter is equal to n * m, in two ways.
builder.assert_var_eq(total_counter, n_val * m_val);
builder.assert_var_eq(total_counter, n * m);

let program = builder.compile();

type SC = BabyBearPoseidon2;
type F = <SC as StarkGenericConfig>::Val;

let mut runtime = Runtime::<F>::new(&program);
runtime.run();
}

0 comments on commit 36f0dbd

Please sign in to comment.