Skip to content

Commit

Permalink
feat: Stark cleanup (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo authored Feb 10, 2024
1 parent 5b566b3 commit 4c93541
Show file tree
Hide file tree
Showing 58 changed files with 1,413 additions and 1,341 deletions.
36 changes: 2 additions & 34 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["core", "cli", "derive", "examples/*", "zkvm", "k12"]
members = ["core", "cli", "derive", "examples/*", "zkvm"]
exclude = ["examples/target"]
resolver = "2"

Expand Down
2 changes: 0 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ num = { version = "0.4.1" }
nohash-hasher = "0.2.0"
lazy_static = "1.4"

succinct-k12 = { path = "../k12" }

tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["std", "env-filter"] }
tracing-forest = { version = "0.1.6", features = ["ansi", "smallvec"] }
Expand Down
7 changes: 7 additions & 0 deletions core/src/air/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use p3_air::PermutationAirBuilder;
use p3_air::{AirBuilder, FilteredAirBuilder};
use p3_uni_stark::{ProverConstraintFolder, SymbolicAirBuilder, VerifierConstraintFolder};

Expand Down Expand Up @@ -461,6 +462,12 @@ pub trait ProgramAirBuilder: BaseAirBuilder {
}
}

pub trait MultiTableAirBuilder: PermutationAirBuilder {
type Sum: Into<Self::ExprEF>;

fn cumulative_sum(&self) -> Self::Sum;
}

/// A trait which contains all helper methods for building an AIR.
pub trait CurtaAirBuilder:
BaseAirBuilder
Expand Down
2 changes: 2 additions & 0 deletions core/src/air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ mod builder;
mod interaction;
mod polynomial;
mod sub_builder;
mod trace;
mod word;

pub use builder::*;
pub use interaction::*;
pub use polynomial::*;
pub use sub_builder::*;
pub use trace::*;
pub use word::*;
15 changes: 15 additions & 0 deletions core/src/air/trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use p3_air::BaseAir;
use p3_field::Field;
use p3_matrix::dense::RowMajorMatrix;

use crate::runtime::ExecutionRecord;

pub trait MachineAir<F: Field>: BaseAir<F> {
fn name(&self) -> String;

fn generate_trace(&self, record: &mut ExecutionRecord) -> RowMajorMatrix<F>;

fn shard(&self, input: &ExecutionRecord, outputs: &mut Vec<ExecutionRecord>);

fn include(&self, record: &ExecutionRecord) -> bool;
}
6 changes: 3 additions & 3 deletions core/src/alu/add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use valida_derive::AlignedBorrow;

use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::chip::Chip;
use crate::operations::AddOperation;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};
Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct AddCols<T> {
pub is_real: T,
}

impl<F: PrimeField> Chip<F> for AddChip {
impl<F: PrimeField> MachineAir<F> for AddChip {
fn name(&self) -> String {
"Add".to_string()
}
Expand Down Expand Up @@ -125,7 +125,7 @@ mod tests {
use p3_matrix::dense::RowMajorMatrix;

use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use rand::{thread_rng, Rng};
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/bitwise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use valida_derive::AlignedBorrow;

use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::bytes::{ByteLookupEvent, ByteOpcode};
use crate::chip::Chip;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};

Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct BitwiseCols<T> {
pub is_and: T,
}

impl<F: PrimeField> Chip<F> for BitwiseChip {
impl<F: PrimeField> MachineAir<F> for BitwiseChip {
fn name(&self) -> String {
"Bitwise".to_string()
}
Expand Down Expand Up @@ -160,7 +160,7 @@ mod tests {
use p3_baby_bear::BabyBear;
use p3_matrix::dense::RowMajorMatrix;

use crate::chip::Chip;
use crate::air::MachineAir;
use crate::utils::{uni_stark_prove as prove, uni_stark_verify as verify};

use super::BitwiseChip;
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/divrem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ use p3_matrix::MatrixRowSlices;
use valida_derive::AlignedBorrow;

use self::utils::eval_abs_value;
use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::alu::divrem::utils::{get_msb, get_quotient_and_remainder, is_signed_operation};
use crate::alu::AluEvent;
use crate::bytes::{ByteLookupEvent, ByteOpcode};
use crate::chip::Chip;
use crate::disassembler::WORD_SIZE;
use crate::operations::{IsEqualWordOperation, IsZeroWordOperation};
use crate::runtime::{ExecutionRecord, Opcode};
Expand Down Expand Up @@ -180,7 +180,7 @@ pub struct DivRemCols<T> {
pub is_real: T,
}

impl<F: PrimeField> Chip<F> for DivRemChip {
impl<F: PrimeField> MachineAir<F> for DivRemChip {
fn name(&self) -> String {
"DivRem".to_string()
}
Expand Down Expand Up @@ -755,7 +755,7 @@ where
mod tests {

use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use p3_baby_bear::BabyBear;
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/lt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use valida_derive::AlignedBorrow;

use crate::air::{CurtaAirBuilder, Word};

use crate::chip::Chip;
use crate::air::MachineAir;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};

Expand Down Expand Up @@ -72,7 +72,7 @@ impl LtCols<u32> {
}
}

impl<F: PrimeField> Chip<F> for LtChip {
impl<F: PrimeField> MachineAir<F> for LtChip {
fn name(&self) -> String {
"Lt".to_string()
}
Expand Down Expand Up @@ -302,7 +302,7 @@ where
mod tests {

use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use p3_baby_bear::BabyBear;
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use valida_derive::AlignedBorrow;

use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::alu::mul::utils::get_msb;
use crate::bytes::{ByteLookupEvent, ByteOpcode};
use crate::chip::Chip;
use crate::disassembler::WORD_SIZE;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};
Expand Down Expand Up @@ -106,7 +106,7 @@ pub struct MulCols<T> {
pub is_real: T,
}

impl<F: PrimeField> Chip<F> for MulChip {
impl<F: PrimeField> MachineAir<F> for MulChip {
fn name(&self) -> String {
"Mul".to_string()
}
Expand Down Expand Up @@ -397,7 +397,7 @@ where
mod tests {

use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use p3_baby_bear::BabyBear;
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/sll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use valida_derive::AlignedBorrow;

use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::chip::Chip;
use crate::disassembler::WORD_SIZE;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};
Expand Down Expand Up @@ -89,7 +89,7 @@ pub struct ShiftLeftCols<T> {
pub is_real: T,
}

impl<F: PrimeField> Chip<F> for ShiftLeft {
impl<F: PrimeField> MachineAir<F> for ShiftLeft {
fn name(&self) -> String {
"ShiftLeft".to_string()
}
Expand Down Expand Up @@ -346,7 +346,7 @@ where
mod tests {

use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use p3_baby_bear::BabyBear;
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/sr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use valida_derive::AlignedBorrow;

use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::alu::sr::utils::{nb_bits_to_shift, nb_bytes_to_shift};
use crate::bytes::utils::shr_carry;
use crate::bytes::{ByteLookupEvent, ByteOpcode};
use crate::chip::Chip;
use crate::disassembler::WORD_SIZE;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};
Expand Down Expand Up @@ -121,7 +121,7 @@ pub struct ShiftRightCols<T> {
pub is_real: T,
}

impl<F: PrimeField> Chip<F> for ShiftRightChip {
impl<F: PrimeField> MachineAir<F> for ShiftRightChip {
fn name(&self) -> String {
"ShiftRight".to_string()
}
Expand Down Expand Up @@ -473,7 +473,7 @@ where
#[cfg(test)]
mod tests {
use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use p3_baby_bear::BabyBear;
Expand Down
6 changes: 3 additions & 3 deletions core/src/alu/sub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use p3_maybe_rayon::prelude::*;

use valida_derive::AlignedBorrow;

use crate::air::MachineAir;
use crate::air::{CurtaAirBuilder, Word};
use crate::chip::Chip;
use crate::runtime::{ExecutionRecord, Opcode};
use crate::utils::{env, pad_to_power_of_two};

Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct SubCols<T> {
pub is_real: T,
}

impl<F: PrimeField> Chip<F> for SubChip {
impl<F: PrimeField> MachineAir<F> for SubChip {
fn name(&self) -> String {
"Sub".to_string()
}
Expand Down Expand Up @@ -171,7 +171,7 @@ where
mod tests {

use crate::{
chip::Chip,
air::MachineAir,
utils::{uni_stark_prove as prove, uni_stark_verify as verify},
};
use p3_baby_bear::BabyBear;
Expand Down
Loading

0 comments on commit 4c93541

Please sign in to comment.