Skip to content

Commit

Permalink
fib test
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo committed Mar 14, 2024
1 parent 0f722a6 commit 2c47dc0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 83 deletions.
196 changes: 119 additions & 77 deletions recursion/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,133 @@ pub mod program;
pub mod runtime;
pub mod stark;

// #[cfg(test)]
// pub mod tests {
// use crate::air::Block;
// use crate::runtime::{Instruction, Opcode, Program, Runtime};
// use crate::stark::RecursionAir;
#[cfg(test)]
pub mod tests {
use crate::air::Block;
use crate::runtime::{Instruction, Opcode, Program, Runtime};
use crate::stark::RecursionAir;

// use p3_baby_bear::BabyBear;
// use p3_field::extension::BinomialExtensionField;
// use p3_field::{AbstractField, PrimeField32};
// use sp1_core::lookup::{debug_interactions_with_all_chips, InteractionKind};
// use sp1_core::stark::{LocalProver, StarkGenericConfig};
// use sp1_core::utils::BabyBearPoseidon2;
// use sp1_core::utils::StarkUtils;
// use std::time::Instant;
use p3_baby_bear::BabyBear;
use p3_field::extension::BinomialExtensionField;
use p3_field::{AbstractField, PrimeField32};
use sp1_core::lookup::{debug_interactions_with_all_chips, InteractionKind};
use sp1_core::stark::{LocalProver, StarkGenericConfig};
use sp1_core::utils::BabyBearPoseidon2;
use sp1_core::utils::StarkUtils;
use std::time::Instant;

// type F = BabyBear;
// type EF = BinomialExtensionField<BabyBear, 4>;
type F = BabyBear;
type EF = BinomialExtensionField<BabyBear, 4>;

// pub fn fibonacci_program<F: PrimeField32>() -> Program<F> {
// // .main
// // imm 0(fp) 1 <-- a = 1
// // imm 1(fp) 1 <-- b = 1
// // imm 2(fp) 10 <-- iterations = 10
// // .body:
// // add 3(fp) 0(fp) 1(fp) <-- tmp = a + b
// // sw 0(fp) 1(fp) <-- a = b
// // sw 1(fp) 3(fp) <-- b = tmp
// // . subi 2(fp) 2(fp) 1 <-- iterations -= 1
// // bne 2(fp) 0 .body <-- if iterations != 0 goto .body
// Program::<F> {
// instructions: vec![
// // .main
// Instruction::new(Opcode::SW, 0, 1, 0, true, true),
// Instruction::new(Opcode::SW, 1, 1, 0, true, true),
// Instruction::new(Opcode::SW, 2, 10, 0, true, true),
// // .body:
// Instruction::new(Opcode::ADD, 3, 0, 1, false, true),
// Instruction::new(Opcode::SW, 0, 1, 0, false, true),
// Instruction::new(Opcode::SW, 1, 3, 0, false, true),
// Instruction::new(Opcode::SUB, 2, 2, 1, false, true),
// Instruction::new(Opcode::BNE, 2, 0, F::ORDER_U32 - 4, true, true),
// ],
// }
// }
pub fn fibonacci_program<F: PrimeField32>() -> Program<F> {
// .main
// imm 0(fp) 1 <-- a = 1
// imm 1(fp) 1 <-- b = 1
// imm 2(fp) 10 <-- iterations = 10
// .body:
// add 3(fp) 0(fp) 1(fp) <-- tmp = a + b
// sw 0(fp) 1(fp) <-- a = b
// sw 1(fp) 3(fp) <-- b = tmp
// . subi 2(fp) 2(fp) 1 <-- iterations -= 1
// bne 2(fp) 0 .body <-- if iterations != 0 goto .body
let zero = [F::zero(); 4];
let one = [F::one(), F::zero(), F::zero(), F::zero()];
Program::<F> {
instructions: vec![
// .main
Instruction::new(Opcode::SW, F::zero(), one, zero, true, true),
Instruction::new(Opcode::SW, F::from_canonical_u32(1), one, zero, true, true),
Instruction::new(
Opcode::SW,
F::from_canonical_u32(2),
[F::from_canonical_u32(10), F::zero(), F::zero(), F::zero()],
zero,
true,
true,
),
// .body:
Instruction::new(
Opcode::ADD,
F::from_canonical_u32(3),
zero,
one,
false,
true,
),
Instruction::new(Opcode::SW, F::from_canonical_u32(0), one, zero, false, true),
Instruction::new(
Opcode::SW,
F::from_canonical_u32(1),
[F::two() + F::one(), F::zero(), F::zero(), F::zero()],
zero,
false,
true,
),
Instruction::new(
Opcode::SUB,
F::from_canonical_u32(2),
[F::two(), F::zero(), F::zero(), F::zero()],
one,
false,
true,
),
Instruction::new(
Opcode::BNE,
F::from_canonical_u32(2),
zero,
[
F::from_canonical_u32(F::ORDER_U32 - 4),
F::zero(),
F::zero(),
F::zero(),
],
true,
true,
),
],
}
}

// #[test]
// fn test_fibonacci_execute() {
// let program = fibonacci_program::<F>();
// let mut runtime = Runtime::<F, EF>::new(&program);
// runtime.run();
// assert_eq!(
// runtime.memory[1024 + 1].value,
// Block::from(BabyBear::from_canonical_u32(144))
// );
// }
#[test]
fn test_fibonacci_execute() {
let program = fibonacci_program::<F>();
let mut runtime = Runtime::<F, EF>::new(&program);
runtime.run();
assert_eq!(
runtime.memory[1024 + 1].value,
Block::from(BabyBear::from_canonical_u32(144))
);
}

// #[test]
// fn test_fibonacci_prove() {
// std::env::set_var("RUST_LOG", "debug");
// sp1_core::utils::setup_logger();
#[test]
fn test_fibonacci_prove() {
std::env::set_var("RUST_LOG", "debug");
sp1_core::utils::setup_logger();

// type SC = BabyBearPoseidon2;
// type F = <SC as StarkGenericConfig>::Val;
// let program = fibonacci_program::<F>();
type SC = BabyBearPoseidon2;
type F = <SC as StarkGenericConfig>::Val;
let program = fibonacci_program::<F>();

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

// let config = SC::new();
// let machine = RecursionAir::machine(config);
// let (pk, vk) = machine.setup(&program);
// let mut challenger = machine.config().challenger();
let config = SC::new();
let machine = RecursionAir::machine(config);
let (pk, vk) = machine.setup(&program);
let mut challenger = machine.config().challenger();

// debug_interactions_with_all_chips::<BabyBearPoseidon2, RecursionAir<BabyBear>>(
// machine.chips(),
// &runtime.record,
// vec![InteractionKind::Memory],
// );
debug_interactions_with_all_chips::<BabyBearPoseidon2, RecursionAir<BabyBear>>(
machine.chips(),
&runtime.record,
vec![InteractionKind::Memory],
);

// let start = Instant::now();
// let proof = machine.prove::<LocalProver<_, _>>(&pk, runtime.record, &mut challenger);
// let duration = start.elapsed().as_secs();
let start = Instant::now();
let proof = machine.prove::<LocalProver<_, _>>(&pk, runtime.record, &mut challenger);
let duration = start.elapsed().as_secs();

// let mut challenger = machine.config().challenger();
// machine.verify(&vk, &proof, &mut challenger).unwrap();
// println!("proving duration = {}", duration);
// }
// }
let mut challenger = machine.config().challenger();
machine.verify(&vk, &proof, &mut challenger).unwrap();
println!("proving duration = {}", duration);
}
}
12 changes: 6 additions & 6 deletions recursion/core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ impl<F: PrimeField32, EF: ExtensionField<F>> Runtime<F, EF> {
let c_val = if !instruction.imm_c {
self.mr(self.fp + instruction.op_c[0], MemoryAccessPosition::C)
} else {
Block::from(instruction.op_c)
instruction.op_c
};
let b_val = if !instruction.imm_b {
self.mr(self.fp + instruction.op_b[0], MemoryAccessPosition::B)
} else {
Block::from(instruction.op_b)
instruction.op_b
};
(a_ptr, b_val, c_val)
}
Expand All @@ -151,7 +151,7 @@ impl<F: PrimeField32, EF: ExtensionField<F>> Runtime<F, EF> {
(a_ptr, b)
} else {
let a_ptr = self.fp + instruction.op_a;
let b = Block::from(instruction.op_b);
let b = instruction.op_b;
(a_ptr, b)
}
}
Expand All @@ -164,7 +164,7 @@ impl<F: PrimeField32, EF: ExtensionField<F>> Runtime<F, EF> {
(a_ptr, b)
} else {
let a_ptr = self.fp + instruction.op_a;
(a_ptr, Block::from(instruction.op_b))
(a_ptr, instruction.op_b)
}
}

Expand All @@ -174,7 +174,7 @@ impl<F: PrimeField32, EF: ExtensionField<F>> Runtime<F, EF> {
let b = if !instruction.imm_b {
self.mr(self.fp + instruction.op_b[0], MemoryAccessPosition::B)
} else {
Block::from(instruction.op_b)
instruction.op_b
};
let c = instruction.op_c[0];
(a, b, c)
Expand Down Expand Up @@ -287,7 +287,7 @@ impl<F: PrimeField32, EF: ExtensionField<F>> Runtime<F, EF> {
self.mw(a_ptr, a_val, MemoryAccessPosition::A);
next_pc = b_val.0[0];
self.fp = c_val[0];
(a, b, c) = (a_val, b_val, Block::from(c_val));
(a, b, c) = (a_val, b_val, c_val);
}
Opcode::TRAP => {
panic!("TRAP instruction encountered")
Expand Down

0 comments on commit 2c47dc0

Please sign in to comment.