Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test for interactions #9

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
stable
nightly-2024-05-01
2 changes: 1 addition & 1 deletion stark-middleware/src/verifier/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum VerificationError {
InvalidProofShape,
/// An error occurred while verifying the claimed openings.
Expand Down
20 changes: 16 additions & 4 deletions stark-middleware/tests/fib_selector_air/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ use std::borrow::Borrow;

use super::columns::FibonacciSelectorCols;
use crate::fib_air::columns::{FibonacciCols, NUM_FIBONACCI_COLS};
use afs_middleware::interaction::Chip;
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir, PairBuilder};
use afs_middleware::interaction::{Chip, Interaction};
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir, PairBuilder, VirtualPairCol};
use p3_field::{AbstractField, Field};
use p3_matrix::{dense::RowMajorMatrix, Matrix};

pub struct FibonacciSelectorAir {
pub sels: Vec<bool>,
pub enable_interactions: bool,
}

// No interactions
impl<F: Field> Chip<F> for FibonacciSelectorAir {}
impl<F: Field> Chip<F> for FibonacciSelectorAir {
fn receives(&self) -> Vec<Interaction<F>> {
if self.enable_interactions {
vec![Interaction::<F> {
fields: vec![VirtualPairCol::<F>::sum_main(vec![0, 1])],
count: VirtualPairCol::<F>::single_preprocessed(0),
argument_index: 0,
}]
} else {
vec![]
}
}
}

impl<F: Field> BaseAir<F> for FibonacciSelectorAir {
fn width(&self) -> usize {
Expand Down
25 changes: 21 additions & 4 deletions stark-middleware/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#![feature(trait_upcasting)]
#![allow(incomplete_features)]

use afs_middleware::{
prover::{trace::TraceCommitter, types::ProvenMultiMatrixAirTrace, PartitionProver},
prover::{
trace::TraceCommitter,
types::{ProvenMultiMatrixAirTrace, ProverRap},
PartitionProver,
},
setup::PartitionSetup,
verifier::PartitionVerifier,
verifier::{types::VerifierRap, PartitionVerifier},
};
use p3_air::BaseAir;
use p3_baby_bear::BabyBear;
Expand All @@ -18,6 +25,10 @@ use crate::config::poseidon2::StarkConfigPoseidon2;
mod config;
mod fib_air;
mod fib_selector_air;
mod interaction;

trait ProverVerifierRap<SC: StarkGenericConfig>: ProverRap<SC> + VerifierRap<SC> {}
impl<SC: StarkGenericConfig, RAP: ProverRap<SC> + VerifierRap<SC>> ProverVerifierRap<SC> for RAP {}

#[test]
fn test_single_fib_stark() {
Expand Down Expand Up @@ -99,7 +110,10 @@ fn test_single_fib_selector_stark() {
let sels: Vec<bool> = (0..n).map(|i| i % 2 == 0).collect();
let pis = [a, b, get_conditional_fib_number(&sels)].map(BabyBear::from_canonical_u32);

let air = FibonacciSelectorAir { sels };
let air = FibonacciSelectorAir {
sels,
enable_interactions: false,
};

let prep_trace = air.preprocessed_trace();
let setup = PartitionSetup::new(&config);
Expand Down Expand Up @@ -154,7 +168,10 @@ fn test_double_fib_starks() {
let pis = [a, b, get_fib_number(n)].map(BabyBear::from_canonical_u32);

let air1 = FibonacciAir {};
let air2 = FibonacciSelectorAir { sels };
let air2 = FibonacciSelectorAir {
sels,
enable_interactions: false,
};

let prep_trace1 = air1.preprocessed_trace();
let prep_trace2 = air2.preprocessed_trace();
Expand Down
73 changes: 73 additions & 0 deletions stark-middleware/tests/interaction/dummy_interaction_air.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use afs_middleware::interaction::{Chip, Interaction};
use afs_middleware_derive::AlignedBorrow;
use core::mem::size_of;
use p3_air::{Air, AirBuilderWithPublicValues, BaseAir, PairBuilder, VirtualPairCol};
use p3_field::Field;
use p3_matrix::dense::RowMajorMatrix;
use p3_util::indices_arr;
use std::mem::transmute;

#[repr(C)]
#[derive(AlignedBorrow)]
pub struct DummyInteractionCols<F> {
pub count: F,
pub val: F,
}

const NUM_DUMMY_INTERACTION_COLS: usize = size_of::<DummyInteractionCols<u8>>();
const DUMMY_INTERACTION_COL_MAP: DummyInteractionCols<usize> = make_col_map();

const fn make_col_map() -> DummyInteractionCols<usize> {
let indices_arr = indices_arr::<NUM_DUMMY_INTERACTION_COLS>();
unsafe {
transmute::<[usize; NUM_DUMMY_INTERACTION_COLS], DummyInteractionCols<usize>>(indices_arr)
}
}

pub struct DummyInteractionAir {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: this is general enough that we should move it into the sdk once it exists, as a general chip testing util

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep it here for now. Other tests can still use it.

// Send if true. Receive if false.
pub is_send: bool,
}

impl<F: Field> Chip<F> for DummyInteractionAir {
fn sends(&self) -> Vec<Interaction<F>> {
if self.is_send {
vec![Interaction::<F> {
fields: vec![VirtualPairCol::<F>::single_main(
DUMMY_INTERACTION_COL_MAP.val,
)],
count: VirtualPairCol::<F>::single_main(DUMMY_INTERACTION_COL_MAP.count),
argument_index: 0,
}]
} else {
vec![]
}
}
fn receives(&self) -> Vec<Interaction<F>> {
if !self.is_send {
vec![Interaction::<F> {
fields: vec![VirtualPairCol::<F>::single_main(
DUMMY_INTERACTION_COL_MAP.val,
)],
count: VirtualPairCol::<F>::single_main(DUMMY_INTERACTION_COL_MAP.count),
argument_index: 0,
}]
} else {
vec![]
}
}
}

impl<F: Field> BaseAir<F> for DummyInteractionAir {
fn width(&self) -> usize {
2
}

fn preprocessed_trace(&self) -> Option<RowMajorMatrix<F>> {
None
}
}

impl<AB: AirBuilderWithPublicValues + PairBuilder> Air<AB> for DummyInteractionAir {
fn eval(&self, _builder: &mut AB) {}
}
Loading
Loading