Skip to content

Commit

Permalink
chore: recursion core cleanup (#355)
Browse files Browse the repository at this point in the history
Co-authored-by: John Guibas <[email protected]>
  • Loading branch information
jtguibas and John Guibas authored Mar 8, 2024
1 parent f2acc41 commit e56ab93
Show file tree
Hide file tree
Showing 14 changed files with 461 additions and 538 deletions.
7 changes: 3 additions & 4 deletions core/src/stark/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::marker::PhantomData;

use crate::air::MachineAir;
use crate::lookup::InteractionBuilder;
use crate::runtime::Program;
use crate::stark::record::MachineRecord;
use crate::stark::DebugConstraintBuilder;
use crate::stark::ProverConstraintFolder;
Expand Down Expand Up @@ -38,13 +37,13 @@ impl<SC: StarkGenericConfig, A> MachineStark<SC, A> {
#[derive(Debug, Clone)]
pub struct ProvingKey<SC: StarkGenericConfig> {
//TODO
pub marker: std::marker::PhantomData<SC>,
marker: std::marker::PhantomData<SC>,
}

#[derive(Debug, Clone)]
pub struct VerifyingKey<SC: StarkGenericConfig> {
// TODO:
pub marker: std::marker::PhantomData<SC>,
marker: std::marker::PhantomData<SC>,
}

impl<SC: StarkGenericConfig, A: MachineAir<SC::Val>> MachineStark<SC, A> {
Expand All @@ -67,7 +66,7 @@ impl<SC: StarkGenericConfig, A: MachineAir<SC::Val>> MachineStark<SC, A> {
///
/// Given a program, this function generates the proving and verifying keys. The keys correspond
/// to the program code and other preprocessed colunms such as lookup tables.
pub fn setup(&self, _program: &Program) -> (ProvingKey<SC>, VerifyingKey<SC>) {
pub fn setup<P>(&self, _program: &P) -> (ProvingKey<SC>, VerifyingKey<SC>) {
(
ProvingKey {
marker: PhantomData,
Expand Down
216 changes: 0 additions & 216 deletions recursion/core/src/air.rs

This file was deleted.

6 changes: 6 additions & 0 deletions recursion/core/src/air/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use core::mem::size_of;
use sp1_derive::AlignedBorrow;

#[derive(AlignedBorrow, Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct Word<T>(pub [T; 4]);
78 changes: 78 additions & 0 deletions recursion/core/src/cpu/air.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::cpu::CpuChip;
use core::mem::size_of;
use p3_air::Air;
use p3_air::BaseAir;
use p3_field::PrimeField32;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use sp1_core::stark::SP1AirBuilder;
use sp1_core::{air::MachineAir, utils::pad_to_power_of_two};
use std::borrow::Borrow;
use std::borrow::BorrowMut;

use super::columns::CpuCols;
use crate::runtime::ExecutionRecord;

pub const NUM_CPU_COLS: usize = size_of::<CpuCols<u8>>();

impl<F: PrimeField32> MachineAir<F> for CpuChip<F> {
type Record = ExecutionRecord<F>;

fn name(&self) -> String {
"CPU".to_string()
}

fn generate_trace(
&self,
input: &ExecutionRecord<F>,
_: &mut ExecutionRecord<F>,
) -> RowMajorMatrix<F> {
let rows = input
.cpu_events
.iter()
.map(|event| {
let mut row = [F::zero(); NUM_CPU_COLS];
let cols: &mut CpuCols<F> = row.as_mut_slice().borrow_mut();
cols.clk = event.clk;
cols.pc = event.pc;
cols.fp = event.fp;
cols.instruction.opcode = F::from_canonical_u32(event.instruction.opcode as u32);
cols.instruction.op_a = event.instruction.op_a;
cols.instruction.op_b = event.instruction.op_b;
cols.instruction.op_c = event.instruction.op_c;
cols.instruction.imm_b = F::from_canonical_u32(event.instruction.imm_b as u32);
cols.instruction.imm_c = F::from_canonical_u32(event.instruction.imm_c as u32);
row
})
.collect::<Vec<_>>();

let mut trace =
RowMajorMatrix::new(rows.into_iter().flatten().collect::<Vec<_>>(), NUM_CPU_COLS);

// Pad the trace to a power of two.
pad_to_power_of_two::<NUM_CPU_COLS, F>(&mut trace.values);

trace
}

fn included(&self, _: &Self::Record) -> bool {
true
}
}

impl<F: Send + Sync> BaseAir<F> for CpuChip<F> {
fn width(&self) -> usize {
NUM_CPU_COLS
}
}

impl<AB> Air<AB> for CpuChip<AB::F>
where
AB: SP1AirBuilder,
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let _: &CpuCols<AB::Var> = main.row_slice(0).borrow();
let _: &CpuCols<AB::Var> = main.row_slice(1).borrow();
}
}
Loading

0 comments on commit e56ab93

Please sign in to comment.