Skip to content

Commit

Permalink
feat: v1 prove method + program table (#15)
Browse files Browse the repository at this point in the history
* v1 coordinator

* v1 coordinator

* resolve merge conflicts

* v1 program table
  • Loading branch information
jtguibas authored Dec 13, 2023
1 parent 80b819e commit 895cb48
Show file tree
Hide file tree
Showing 11 changed files with 584 additions and 455 deletions.
37 changes: 13 additions & 24 deletions core/src/alu/add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::lookup::Interaction;
use crate::utils::{pad_to_power_of_two, Chip};
use crate::Runtime;

use super::AluEvent;

pub const NUM_ADD_COLS: usize = size_of::<AddCols<u8>>();

/// The column layout for the chip.
Expand All @@ -35,15 +33,19 @@ pub struct AddCols<T> {
}

/// A chip that implements addition for the opcodes ADD and ADDI.
pub struct AddChip {
events: Vec<AluEvent>,
pub struct AddChip;

impl AddChip {
pub fn new() -> Self {
Self {}
}
}

impl<F: PrimeField> Chip<F> for AddChip {
fn generate_trace(&self, _: &mut Runtime) -> RowMajorMatrix<F> {
fn generate_trace(&self, runtime: &mut Runtime) -> RowMajorMatrix<F> {
// Generate the trace rows for each event.
let rows = self
.events
let rows = runtime
.add_events
.par_iter()
.map(|event| {
let mut row = [F::zero(); NUM_ADD_COLS];
Expand Down Expand Up @@ -165,14 +167,8 @@ mod tests {
fn generate_trace() {
let program = vec![];
let mut runtime = Runtime::new(program);
let events = vec![AluEvent {
clk: 0,
opcode: Opcode::ADD,
a: 14,
b: 8,
c: 6,
}];
let chip = AddChip { events };
runtime.add_events = vec![AluEvent::new(0, Opcode::ADD, 14, 8, 6)];
let chip = AddChip::new();
let trace: RowMajorMatrix<BabyBear> = chip.generate_trace(&mut runtime);
println!("{:?}", trace.values)
}
Expand Down Expand Up @@ -221,15 +217,8 @@ mod tests {

let program = vec![];
let mut runtime = Runtime::new(program);
let events = vec![AluEvent {
clk: 0,
opcode: Opcode::ADD,
a: 14,
b: 8,
c: 6,
}]
.repeat(1000);
let chip = AddChip { events };
runtime.add_events = vec![AluEvent::new(0, Opcode::ADD, 14, 8, 6)].repeat(1000);
let chip = AddChip::new();
let trace: RowMajorMatrix<BabyBear> = chip.generate_trace(&mut runtime);
let proof = prove::<MyConfig, _>(&config, &chip, &mut challenger, trace);

Expand Down
56 changes: 18 additions & 38 deletions core/src/alu/bitwise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use valida_derive::AlignedBorrow;

use crate::air::Word;
use crate::lookup::Interaction;
use crate::runtime::Opcode;
use crate::utils::{pad_to_power_of_two, Chip};

use super::AluEvent;
use crate::Runtime;

pub const NUM_BITWISE_COLS: usize = size_of::<BitwiseCols<u8>>();

Expand All @@ -41,15 +41,19 @@ pub struct BitwiseCols<T> {
}

/// A chip that implements bitwise operations for the opcodes XOR, XORI, OR, ORI, AND, and ANDI.
pub struct BitwiseChip {
events: Vec<AluEvent>,
pub struct BitwiseChip;

impl BitwiseChip {
pub fn new() -> Self {
Self {}
}
}

impl<F: PrimeField> Chip<F> for BitwiseChip {
fn generate_trace(&self, _: &mut crate::Runtime) -> RowMajorMatrix<F> {
fn generate_trace(&self, runtime: &mut Runtime) -> RowMajorMatrix<F> {
// Generate the trace rows for each event.
let rows = self
.events
let rows = runtime
.bitwise_events
.par_iter()
.map(|event| {
let mut row = [F::zero(); NUM_BITWISE_COLS];
Expand Down Expand Up @@ -195,14 +199,8 @@ mod tests {
fn generate_trace() {
let program = vec![];
let mut runtime = Runtime::new(program);
let events = vec![AluEvent {
clk: 0,
opcode: Opcode::ADD,
a: 14,
b: 8,
c: 6,
}];
let chip = BitwiseChip { events };
runtime.bitwise_events = vec![AluEvent::new(0, Opcode::XOR, 25, 10, 19)];
let chip = BitwiseChip::new();
let trace: RowMajorMatrix<BabyBear> = chip.generate_trace(&mut runtime);
println!("{:?}", trace.values)
}
Expand Down Expand Up @@ -251,31 +249,13 @@ mod tests {

let program = vec![];
let mut runtime = Runtime::new(program);
let events = vec![
AluEvent {
clk: 0,
opcode: Opcode::XOR,
a: 25,
b: 10,
c: 19,
},
AluEvent {
clk: 0,
opcode: Opcode::OR,
a: 27,
b: 10,
c: 19,
},
AluEvent {
clk: 0,
opcode: Opcode::AND,
a: 2,
b: 10,
c: 19,
},
runtime.bitwise_events = vec![
AluEvent::new(0, Opcode::XOR, 25, 10, 19),
AluEvent::new(0, Opcode::OR, 27, 10, 19),
AluEvent::new(0, Opcode::AND, 2, 10, 19),
]
.repeat(1000);
let chip = BitwiseChip { events };
let chip = BitwiseChip::new();
let trace: RowMajorMatrix<BabyBear> = chip.generate_trace(&mut runtime);
let proof = prove::<MyConfig, _>(&config, &chip, &mut challenger, trace);

Expand Down
25 changes: 17 additions & 8 deletions core/src/alu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;

use crate::{lookup::Interaction, runtime::Opcode, Runtime};
mod add;
mod bitwise;
mod shift;
mod sub;
use crate::runtime::Opcode;
pub mod add;
pub mod bitwise;
pub mod shift;
pub mod sub;

#[derive(Debug, Clone, Copy)]
pub struct AluEvent {
Expand All @@ -15,3 +12,15 @@ pub struct AluEvent {
pub b: u32,
pub c: u32,
}

impl AluEvent {
pub fn new(clk: u32, opcode: Opcode, a: u32, b: u32, c: u32) -> Self {
Self {
clk,
opcode,
a,
b,
c,
}
}
}
40 changes: 16 additions & 24 deletions core/src/alu/sub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use std::mem::transmute;
use valida_derive::AlignedBorrow;

use crate::air::Word;
use crate::lookup::Interaction;
use crate::utils::{pad_to_power_of_two, Chip};

use super::AluEvent;
use crate::utils::{pad_to_power_of_two, Chip};
use crate::Runtime;

pub const NUM_SUB_COLS: usize = size_of::<SubCols<u8>>();

Expand All @@ -34,15 +35,19 @@ pub struct SubCols<T> {
}

/// A chip that implements subtraction for the opcode SUB.
pub struct SubChip {
events: Vec<AluEvent>,
pub struct SubChip;

impl SubChip {
pub fn new() -> Self {
Self {}
}
}

impl<F: PrimeField> Chip<F> for SubChip {
fn generate_trace(&self, _: &mut crate::Runtime) -> RowMajorMatrix<F> {
fn generate_trace(&self, runtime: &mut Runtime) -> RowMajorMatrix<F> {
// Generate the trace rows for each event.
let rows = self
.events
let rows = runtime
.sub_events
.par_iter()
.map(|event| {
let mut row = [F::zero(); NUM_SUB_COLS];
Expand Down Expand Up @@ -169,14 +174,8 @@ mod tests {
fn generate_trace() {
let program = vec![];
let mut runtime = Runtime::new(program);
let events = vec![AluEvent {
clk: 0,
opcode: Opcode::ADD,
a: 2,
b: 8,
c: 6,
}];
let chip = SubChip { events };
runtime.sub_events = vec![AluEvent::new(0, Opcode::SUB, 14, 8, 6)];
let chip = SubChip {};
let trace: RowMajorMatrix<BabyBear> = chip.generate_trace(&mut runtime);
println!("{:?}", trace.values)
}
Expand Down Expand Up @@ -225,15 +224,8 @@ mod tests {

let program = vec![];
let mut runtime = Runtime::new(program);
let events = vec![AluEvent {
clk: 0,
opcode: Opcode::SUB,
a: 2,
b: 8,
c: 6,
}]
.repeat(1000);
let chip = SubChip { events };
runtime.sub_events = vec![AluEvent::new(0, Opcode::SUB, 14, 8, 6)].repeat(1000);
let chip = SubChip::new();
let trace: RowMajorMatrix<BabyBear> = chip.generate_trace(&mut runtime);
let proof = prove::<MyConfig, _>(&config, &chip, &mut challenger, trace);

Expand Down
Loading

0 comments on commit 895cb48

Please sign in to comment.