Skip to content

Commit

Permalink
feat: update to latest p3 (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas authored Apr 12, 2024
1 parent 20076e6 commit 69a3681
Show file tree
Hide file tree
Showing 65 changed files with 2,002 additions and 757 deletions.
175 changes: 91 additions & 84 deletions Cargo.lock

Large diffs are not rendered by default.

52 changes: 15 additions & 37 deletions core/src/air/sub_builder.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::ops::Range;

use p3_air::{AirBuilder, BaseAir};
use p3_matrix::{Matrix, MatrixRowSlices, MatrixRows};
use p3_matrix::Matrix;

/// A submatrix of a matrix. The matrix will contain a subset of the columns of `self.inner`.
pub struct SubMatrixRowSlices<M: MatrixRowSlices<T>, T> {
pub struct SubMatrixRowSlices<M: Matrix<T>, T: Send + Sync> {
inner: M,
column_range: Range<usize>,
_phantom: std::marker::PhantomData<T>,
}

impl<M: MatrixRowSlices<T>, T> SubMatrixRowSlices<M, T> {
impl<M: Matrix<T>, T: Send + Sync> SubMatrixRowSlices<M, T> {
pub fn new(inner: M, column_range: Range<usize>) -> Self {
Self {
inner,
Expand All @@ -21,50 +21,28 @@ impl<M: MatrixRowSlices<T>, T> SubMatrixRowSlices<M, T> {
}

/// Implement `Matrix` for `SubMatrixRowSlices`.
impl<M: MatrixRowSlices<T>, T> Matrix<T> for SubMatrixRowSlices<M, T> {
fn width(&self) -> usize {
self.column_range.end - self.column_range.start
}

fn height(&self) -> usize {
self.inner.height()
}
}

/// Implement `MatrixRows` for `SubMatrixRowSlices`.
impl<M: MatrixRowSlices<T>, T> MatrixRows<T> for SubMatrixRowSlices<M, T> {
impl<M: Matrix<T>, T: Send + Sync> Matrix<T> for SubMatrixRowSlices<M, T> {
type Row<'a> = M::Row<'a> where Self: 'a;

fn row(&self, r: usize) -> Self::Row<'_> {
self.inner.row(r)
}

fn row_vec(&self, r: usize) -> Vec<T> {
self.inner.row_vec(r)
fn row_slice(&self, r: usize) -> impl std::ops::Deref<Target = [T]> {
self.inner
.row(r)
.enumerate()
.filter(|(i, _)| self.column_range.contains(i))
.map(|(_, el)| el)
.collect::<Vec<_>>()
}

fn first_row(&self) -> Self::Row<'_> {
self.inner.first_row()
}

fn last_row(&self) -> Self::Row<'_> {
self.inner.last_row()
}

fn to_row_major_matrix(self) -> p3_matrix::dense::RowMajorMatrix<T>
where
Self: Sized,
T: Clone,
{
self.inner.to_row_major_matrix()
fn width(&self) -> usize {
self.column_range.end - self.column_range.start
}
}

/// Implement `MatrixRowSlices` for `SubMatrixRowSlices`.
impl<M: MatrixRowSlices<T>, T> MatrixRowSlices<T> for SubMatrixRowSlices<M, T> {
fn row_slice(&self, r: usize) -> &[T] {
let entry = self.inner.row_slice(r);
&entry[self.column_range.start..self.column_range.end]
fn height(&self) -> usize {
self.inner.height()
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/add_sub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::mem::size_of;
use p3_air::{Air, BaseAir};
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use p3_maybe_rayon::prelude::ParallelIterator;
use p3_maybe_rayon::prelude::ParallelSlice;
use sp1_derive::AlignedBorrow;
Expand Down Expand Up @@ -140,7 +140,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &AddSubCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &AddSubCols<AB::Var> = (*local).borrow();

builder.assert_bool(local.is_add);
builder.assert_bool(local.is_sub);
Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/bitwise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::mem::size_of;
use p3_air::{Air, BaseAir};
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use sp1_derive::AlignedBorrow;
use tracing::instrument;

Expand Down Expand Up @@ -126,7 +126,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &BitwiseCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &BitwiseCols<AB::Var> = (*local).borrow();

// Get the opcode for the operation.
let opcode = local.is_xor * ByteOpcode::XOR.as_field::<AB::F>()
Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/divrem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::AbstractField;
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use sp1_derive::AlignedBorrow;
use tracing::instrument;

Expand Down Expand Up @@ -427,7 +427,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &DivRemCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &DivRemCols<AB::Var> = (*local).borrow();
let base = AB::F::from_canonical_u32(1 << 8);
let one: AB::Expr = AB::F::one().into();
let zero: AB::Expr = AB::F::zero().into();
Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/lt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::PrimeField;
use p3_field::{AbstractField, PrimeField32};
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use p3_maybe_rayon::prelude::*;
use sp1_derive::AlignedBorrow;
use tracing::instrument;
Expand Down Expand Up @@ -195,7 +195,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &LtCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &LtCols<AB::Var> = (*local).borrow();

let one = AB::Expr::one();

Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::AbstractField;
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use p3_maybe_rayon::prelude::ParallelIterator;
use p3_maybe_rayon::prelude::ParallelSlice;
use sp1_derive::AlignedBorrow;
Expand Down Expand Up @@ -281,7 +281,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &MulCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &MulCols<AB::Var> = (*local).borrow();
let base = AB::F::from_canonical_u32(1 << 8);

let zero: AB::Expr = AB::F::zero().into();
Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/sll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::AbstractField;
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use sp1_derive::AlignedBorrow;
use tracing::instrument;

Expand Down Expand Up @@ -215,7 +215,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &ShiftLeftCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &ShiftLeftCols<AB::Var> = (*local).borrow();

let zero: AB::Expr = AB::F::zero().into();
let one: AB::Expr = AB::F::one().into();
Expand Down
5 changes: 3 additions & 2 deletions core/src/alu/sr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use p3_air::{Air, AirBuilder, BaseAir};
use p3_field::AbstractField;
use p3_field::PrimeField;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use sp1_derive::AlignedBorrow;
use tracing::instrument;

Expand Down Expand Up @@ -294,7 +294,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &ShiftRightCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &ShiftRightCols<AB::Var> = (*local).borrow();
let zero: AB::Expr = AB::F::zero().into();
let one: AB::Expr = AB::F::one().into();

Expand Down
8 changes: 5 additions & 3 deletions core/src/bytes/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use p3_air::PairBuilder;
use p3_air::{Air, BaseAir};
use p3_field::AbstractField;
use p3_field::Field;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;

use super::columns::{ByteMultCols, BytePreprocessedCols, NUM_BYTE_MULT_COLS};
use super::{ByteChip, ByteOpcode};
Expand All @@ -24,10 +24,12 @@ impl<F: Field> BaseAir<F> for ByteChip<F> {
impl<AB: SP1AirBuilder + PairBuilder> Air<AB> for ByteChip<AB::F> {
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local_mult: &ByteMultCols<AB::Var> = main.row_slice(0).borrow();
let local_mult = main.row_slice(0);
let local_mult: &ByteMultCols<AB::Var> = (*local_mult).borrow();

let prep = builder.preprocessed();
let local: &BytePreprocessedCols<AB::Var> = prep.row_slice(0).borrow();
let prep = prep.row_slice(0);
let local: &BytePreprocessedCols<AB::Var> = (*prep).borrow();

// Send all the lookups for each operation.
for (i, opcode) in ByteOpcode::all().iter().enumerate() {
Expand Down
7 changes: 4 additions & 3 deletions core/src/cpu/air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use p3_air::Air;
use p3_air::AirBuilder;
use p3_air::BaseAir;
use p3_field::AbstractField;
use p3_matrix::MatrixRowSlices;

use crate::air::BaseAirBuilder;
use crate::air::PublicValues;
Expand All @@ -22,6 +21,7 @@ use crate::memory::MemoryCols;
use crate::operations::IsZeroOperation;
use crate::runtime::SyscallCode;
use crate::runtime::{MemoryAccessPosition, Opcode};
use p3_matrix::Matrix;

impl<AB> Air<AB> for CpuChip
where
Expand All @@ -30,8 +30,9 @@ where
#[inline(never)]
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &CpuCols<AB::Var> = main.row_slice(0).borrow();
let next: &CpuCols<AB::Var> = main.row_slice(1).borrow();
let (local, next) = (main.row_slice(0), main.row_slice(1));
let local: &CpuCols<AB::Var> = (*local).borrow();
let next: &CpuCols<AB::Var> = (*next).borrow();

let public_values = PublicValues::<Word<AB::Expr>, AB::Expr>::from_vec(
builder
Expand Down
9 changes: 7 additions & 2 deletions core/src/lookup/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ fn eval_symbolic_to_virtual_pair<F: Field>(
(vec![(PairCol::Preprocessed(v.index), F::one())], F::zero())
}
Entry::Main { offset: 0 } => (vec![(PairCol::Main(v.index), F::one())], F::zero()),
_ => panic!("Not an affine expression in current row elements"),
_ => panic!(
"Not an affine expression in current row elements {:?}",
v.entry
),
},
SymbolicExpression::Add { x, y, .. } => {
let (v_l, c_l) = eval_symbolic_to_virtual_pair(x);
Expand Down Expand Up @@ -196,7 +199,8 @@ mod tests {
use p3_air::{Air, BaseAir};
use p3_baby_bear::BabyBear;
use p3_field::AbstractField;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use std::borrow::Borrow;

use super::*;
use crate::{air::SP1AirBuilder, lookup::InteractionKind};
Expand Down Expand Up @@ -238,6 +242,7 @@ mod tests {
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local = main.row_slice(0);
let local: &[AB::Var] = (*local).borrow();

let x = local[0];
let y = local[1];
Expand Down
5 changes: 3 additions & 2 deletions core/src/memory/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::mem::{size_of, transmute};
use p3_air::BaseAir;
use p3_air::{Air, AirBuilder};
use p3_field::AbstractField;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use p3_util::indices_arr;
use sp1_derive::AlignedBorrow;

Expand Down Expand Up @@ -127,7 +127,8 @@ where
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &MemoryInitCols<AB::Var> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &MemoryInitCols<AB::Var> = (*local).borrow();

// Dummy constraint of degree 3.
builder.assert_eq(
Expand Down
5 changes: 3 additions & 2 deletions core/src/operations/field/field_den.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod tests {
use p3_air::Air;
use p3_baby_bear::BabyBear;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use rand::thread_rng;
use sp1_derive::AlignedBorrow;

Expand Down Expand Up @@ -243,7 +243,8 @@ mod tests {
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &TestCols<AB::Var, P> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &TestCols<AB::Var, P> = (*local).borrow();
local
.a_den_b
.eval::<AB>(builder, &local.a, &local.b, self.sign);
Expand Down
5 changes: 3 additions & 2 deletions core/src/operations/field/field_inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod tests {
use p3_air::Air;
use p3_baby_bear::BabyBear;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use rand::thread_rng;
use sp1_derive::AlignedBorrow;

Expand Down Expand Up @@ -236,7 +236,8 @@ mod tests {
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &TestCols<AB::Var, P> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &TestCols<AB::Var, P> = (*local).borrow();
local.a_ip_b.eval::<AB>(builder, &local.a, &local.b);

// A dummy constraint to keep the degree 3.
Expand Down
5 changes: 3 additions & 2 deletions core/src/operations/field/field_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
use p3_air::Air;
use p3_baby_bear::BabyBear;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use rand::thread_rng;
use sp1_derive::AlignedBorrow;
use std::mem::size_of;
Expand Down Expand Up @@ -289,7 +289,8 @@ mod tests {
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &TestCols<AB::Var, P> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &TestCols<AB::Var, P> = (*local).borrow();
local
.a_op_b
.eval::<AB, _, _>(builder, &local.a, &local.b, self.operation);
Expand Down
5 changes: 3 additions & 2 deletions core/src/operations/field/field_sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod tests {
use p3_air::Air;
use p3_baby_bear::BabyBear;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::MatrixRowSlices;
use p3_matrix::Matrix;
use rand::thread_rng;
use sp1_derive::AlignedBorrow;

Expand Down Expand Up @@ -184,7 +184,8 @@ mod tests {
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local: &TestCols<AB::Var, P> = main.row_slice(0).borrow();
let local = main.row_slice(0);
let local: &TestCols<AB::Var, P> = (*local).borrow();

// eval verifies that local.sqrt.result is indeed the square root of local.a.
local.sqrt.eval::<AB>(builder, &local.a);
Expand Down
Loading

0 comments on commit 69a3681

Please sign in to comment.