diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae931f72..a7a140b8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,10 +11,15 @@ - Removed `TraceSegmentId::index()` and replaced segment indexing with `TraceShape`/`FullTraceShape` across ACE codegen, MIR-to-AIR pass, and constraints (#442). - Allow computed indices (#444). - Fix regressions on MIR and list_comprehensions (#449). +- Add Plonky3 codegen backend (#461). - Fixed a vector unrolling issue in nested match evaluations (#491). - Fix evaluator argument vector slice expansion (#495). -- Fixed MIR's constant propagation to fold 0^0 to 1 (#509) +- Support importing hierarchical modules (#507). +- In Plonky3 codegen, use AirScriptAir and AirScriptBuilder traits, and generate aux constraints (#508). +- Fixed MIR's constant propagation to fold 0^0 to 1 (#509). - Support importing hierarchical modules (#507, #513, #514). +- In Plonky3 codegen, use MidenAir and MidenAirBuilder from 0xMiden's Plonky3 fork instead of AirScriptAir and AirScriptBuilder (#515). +- In Plonky3 codegen, use prove/verify workflow for tests (#523). - Fix MIR inlining loop on deeply nested calls (#524). ## 0.4.0 (2025-06-20) diff --git a/Cargo.toml b/Cargo.toml index c1bc061b8..36de3cedc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "air", "codegen/winterfell", "codegen/ace", + "codegen/plonky3", ] resolver = "2" diff --git a/air-script/Cargo.toml b/air-script/Cargo.toml index 8d5d5e5ed..ed1709f76 100644 --- a/air-script/Cargo.toml +++ b/air-script/Cargo.toml @@ -18,6 +18,7 @@ path = "src/main.rs" [dependencies] air-codegen-winter = { package = "air-codegen-winter", path = "../codegen/winterfell", version = "0.5" } +air-codegen-plonky3 = { package = "air-codegen-plonky3", path = "../codegen/plonky3", version = "0.5" } air-ir = { package = "air-ir", path = "../air", version = "0.5" } air-parser = { package = "air-parser", path = "../parser", version = "0.5" } air-pass = { package = "air-pass", path = "../pass", version = "0.5" } @@ -27,8 +28,30 @@ log = { version = "0.4", default-features = false } miden-diagnostics = { workspace = true } mir = { package = "air-mir", path = "../mir", version = "0.5" } +# 0xMiden Plonky3 Fork +p3-matrix = { package = "p3-matrix", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-field = { package = "p3-field", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-miden-air = { package = "miden-air", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } + +# MassaLabs fork +miden-processor = { package = "miden-processor", git="https://github.com/massalabs/miden-vm", rev = "bc553af69a2543a0789830e8508b019694528181", default-features = false } +miden-air = { package = "miden-air", git="https://github.com/massalabs/miden-vm", rev = "bc553af69a2543a0789830e8508b019694528181", default-features = false } + [dev-dependencies] expect-test = "1.4" + +# 0xMiden Plonky3 Fork +p3-air = { package = "p3-air", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-challenger = { package = "p3-challenger", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-commit = { package = "p3-commit", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-dft = { package = "p3-dft", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-fri = { package = "p3-fri", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-merkle-tree = { package = "p3-merkle-tree", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-miden-prover = { package = "miden-prover", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-goldilocks = { package = "p3-goldilocks", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-sha256 = { package = "p3-sha256", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } +p3-symmetric = { package = "p3-symmetric", git="https://github.com/0xMiden/Plonky3", rev = "632fb26a9141cc70f903e5378035ca8d5855c490", default-features = false } + winter-air = { package = "winter-air", version = "0.12", default-features = false } winter-math = { package = "winter-math", version = "0.12", default-features = false } winter-utils = { package = "winter-utils", version = "0.12", default-features = false } diff --git a/air-script/src/cli/transpile.rs b/air-script/src/cli/transpile.rs index 2ecea6545..3da78b676 100644 --- a/air-script/src/cli/transpile.rs +++ b/air-script/src/cli/transpile.rs @@ -9,11 +9,13 @@ use miden_diagnostics::{ #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum Target { Winterfell, + Plonky3, } impl Target { pub fn extension(&self) -> &'static str { match self { Self::Winterfell => "rs", + Self::Plonky3 => "rs", } } } @@ -55,6 +57,7 @@ impl Transpile { let target = self.target.unwrap_or(Target::Winterfell); let backend: Box> = match target { Target::Winterfell => Box::new(air_codegen_winter::CodeGenerator), + Target::Plonky3 => Box::new(air_codegen_plonky3::CodeGenerator), }; // write transpiled output to the output path @@ -62,6 +65,13 @@ impl Transpile { Some(path) => path.clone(), None => { let mut path = input_path.clone(); + if target == Target::Plonky3 { + path.set_file_name(format!( + "{}_plonky3", + path.file_stem().unwrap().display() + )); + path.set_extension("air"); + } path.set_extension(target.extension()); path }, diff --git a/air-script/src/lib.rs b/air-script/src/lib.rs index 5d477c7d0..d75247b94 100644 --- a/air-script/src/lib.rs +++ b/air-script/src/lib.rs @@ -1,3 +1,11 @@ pub use air_codegen_winter::CodeGenerator as WinterfellCodeGenerator; pub use air_ir::{Air, CompileError, compile}; pub use air_parser::{parse, parse_file, transforms}; + +/// Miden VM auxiliary trace generator +pub mod miden_vm_aux_trace_generator; + +#[cfg(test)] +pub mod test_utils; +#[cfg(test)] +mod tests; diff --git a/air-script/src/miden_vm_aux_trace_generator.rs b/air-script/src/miden_vm_aux_trace_generator.rs new file mode 100644 index 000000000..e233a970a --- /dev/null +++ b/air-script/src/miden_vm_aux_trace_generator.rs @@ -0,0 +1,117 @@ +use std::collections::BTreeMap; + +use miden_air::{Felt, FieldElement, trace::main_trace::MainTrace}; +use miden_processor::{ + ColMatrix, Kernel, PrecompileTranscriptState, QuadExtension, + chiplets::{AceHints, AuxTraceBuilder as ChipletsAuxTraceBuilder}, + decoder::AuxTraceBuilder as DecoderAuxTraceBuilder, + range::AuxTraceBuilder as RangeAuxTraceBuilder, + stack::AuxTraceBuilder as StackAuxTraceBuilder, +}; +use p3_field::{ExtensionField, Field, PrimeField64}; +use p3_matrix::Matrix; +use p3_miden_air::RowMajorMatrix; + +pub enum MidenModule { + Chiplets, + Decoder, + Stack, + Range, +} + +impl MidenModule { + fn build_aux_columns>( + &self, + main_trace: &MainTrace, + rand_elements: &[EF], + ) -> Vec> { + match self { + MidenModule::Chiplets => { + let kernel = Kernel::new(&[]).unwrap(); + let ace_hints = AceHints::new(0, vec![]); + let final_transcript_state = PrecompileTranscriptState::default(); + let aux_trace_builder = + ChipletsAuxTraceBuilder::new(kernel, ace_hints, final_transcript_state); + aux_trace_builder.build_aux_columns(main_trace, rand_elements).to_vec() + }, + MidenModule::Decoder => { + let aux_trace_builder = DecoderAuxTraceBuilder {}; + aux_trace_builder.build_aux_columns(main_trace, rand_elements) + }, + MidenModule::Stack => { + let aux_trace_builder = StackAuxTraceBuilder {}; + aux_trace_builder.build_aux_columns(main_trace, rand_elements) + }, + MidenModule::Range => { + let lookup_values = vec![]; + let cycle_lookups = BTreeMap::new(); + let values_start = 0; + let aux_trace_builder = + RangeAuxTraceBuilder::new(lookup_values, cycle_lookups, values_start); + aux_trace_builder.build_aux_columns(main_trace, rand_elements) + }, + } + } +} + +/// Builds the Miden VM auxiliary trace using the provided main trace and challenges. +pub fn build_aux_trace_with_miden_vm( + main: &RowMajorMatrix, + challenges: &[EF], + module: MidenModule, +) -> RowMajorMatrix +where + F: Field + PrimeField64, + EF: ExtensionField, +{ + // Convert main trace to Miden format: transposed to column-major and use `BaseElement` instead + // of `F`` + let main_transposed = main.transpose(); + let mut felt_columns_vec = Vec::new(); + for row in main_transposed.rows() { + let col_felt = row.map(|x| Felt::new(x.as_canonical_u64())).collect::>(); + felt_columns_vec.push(col_felt); + } + let col_matrix = ColMatrix::new(felt_columns_vec); + let last_program_row = main.height().into(); + let main_trace = MainTrace::new(col_matrix, last_program_row); + + // Convert challenges to Miden format + let mut rand_elements = Vec::new(); + for r in challenges { + let coeffs: Vec = r + .as_basis_coefficients_slice() + .iter() + .map(|x| Felt::new(x.as_canonical_u64())) + .collect(); + let r_fe: &[QuadExtension] = FieldElement::slice_from_base_elements(&coeffs); + rand_elements.push(r_fe[0]); + } + + // Build aux trace using Miden VM AuxTraceBuilder + let aux_trace_miden = module.build_aux_columns(&main_trace, &rand_elements); + let aux_width = aux_trace_miden.len(); + + // Convert aux trace back to RowMajorMatrix + let num_rows = main.height(); + let trace_length = num_rows * aux_width; + let long_trace = EF::zero_vec(trace_length); + let mut aux_trace = RowMajorMatrix::new(long_trace, aux_width); + + for j in 0..aux_width { + let col = aux_trace_miden.get(j).unwrap(); + for i in 0..num_rows { + let value_felt = col[i]; + let coeffs_f: Vec = value_felt + .to_base_elements() + .iter() + .map(|x| F::from_canonical_checked(x.as_int()).unwrap()) + .collect(); + let value_ef = EF::from_basis_coefficients_iter(coeffs_f.iter().cloned()).unwrap(); + aux_trace.row_mut(i)[j] = value_ef; + } + } + + let aux_trace_f = aux_trace.flatten_to_base(); + aux_trace_f +} diff --git a/air-script/src/test_utils/air_tester_macros.rs b/air-script/src/test_utils/air_tester_macros.rs new file mode 100644 index 000000000..7c5e305ee --- /dev/null +++ b/air-script/src/test_utils/air_tester_macros.rs @@ -0,0 +1,96 @@ +// Helper macros for Winterfell test generation + +/// Generates a Winterfell AIR test function with the standard boilerplate +/// +/// # Arguments +/// * `test_name` - The identifier for the test function (e.g., `test_binary_air`) +/// * `air_name` - The identifier for the AIR struct (e.g., `BinaryAir`) +/// * `tester_name` - The identifier for the `AirTester` struct (e.g., `BinaryAirTester`) +/// * `trace_length` - The length of the trace for the test (e.g., `32` or `1024`) +#[macro_export] +macro_rules! generate_air_winterfell_test { + ($test_name:ident, $air_name:path, $tester_name:ident, $trace_length:expr) => { + #[test] + fn $test_name() { + use winter_math::fields::f64::BaseElement as Felt; + let air_tester = Box::new($tester_name {}); + let length = $trace_length; + + let main_trace = air_tester.build_main_trace(length); + let aux_trace = air_tester.build_aux_trace(length); + let pub_inputs = air_tester.public_inputs(); + let trace_info = air_tester.build_trace_info(length); + let options = air_tester.build_proof_options(); + + let air = <$air_name>::new(trace_info, pub_inputs, options); + main_trace.validate::<$air_name, Felt>(&air, aux_trace.as_ref()); + } + }; +} + +// Helper macros for Plonky3 test generation + +/// Generates a Plonky3 AIR test function with the standard boilerplate +/// +/// # Arguments +/// * `test_name` - The identifier for the test function (e.g., `test_binary_air`) +/// * `air_name` - The identifier for the AIR struct (e.g., `BinaryAir`) +#[macro_export] +macro_rules! generate_air_plonky3_test_with_airscript_traits { + ($test_name:ident, $air_name:ident) => { + #[test] + fn $test_name() { + type Val = p3_goldilocks::Goldilocks; + type Challenge = p3_field::extension::BinomialExtensionField; + type ByteHash = p3_sha256::Sha256; + type FieldHash = p3_symmetric::SerializingHasher; + type MyCompress = p3_symmetric::CompressionFunctionFromHasher; + type ValMmcs = p3_merkle_tree::MerkleTreeMmcs; + type ChallengeMmcs = p3_commit::ExtensionMmcs; + type Challenger = p3_challenger::SerializingChallenger64< + Val, + p3_challenger::HashChallenger, + >; + type Dft = p3_dft::Radix2DitParallel; + type Pcs = p3_fri::TwoAdicFriPcs; + type MyConfig = p3_miden_prover::StarkConfig; + + let byte_hash = ByteHash {}; + let field_hash = FieldHash::new(p3_sha256::Sha256); + let compress = MyCompress::new(byte_hash); + let val_mmcs = ValMmcs::new(field_hash, compress); + let challenge_mmcs = ChallengeMmcs::new(val_mmcs.clone()); + let challenger = Challenger::from_hasher(vec![], byte_hash); + let dft = Dft::default(); + let mut fri_params = p3_fri::create_recursive_miden_fri_params(challenge_mmcs); + let pcs = Pcs::new(dft, val_mmcs, fri_params); + let config = MyConfig::new(pcs, challenger); + + // Generate public inputs and convert them to Goldilocks field elements + let inputs = generate_inputs(); + let inputs_goldilocks = crate::test_utils::pub_inputs_conversion_utils::convert_pub_inputs_to_goldilocks(&inputs); + + // Generate variable-length public inputs as a `Vec>>`. + // The outer `Vec` represents multiple tables (one for each bus in the AIR) + // The middle `Vec` represents the rows of each table + // The innermost `Vec` represents the tuple values contained by the bus, that will be combined with randomness into a single field element. + let var_len_pub_inputs = generate_var_len_pub_inputs(); + // Convert variable-length public inputs to Goldilocks field elements + let var_len_pub_inputs_goldilocks_vec = crate::test_utils::pub_inputs_conversion_utils::convert_var_len_pub_inputs_to_goldilocks(var_len_pub_inputs); + let var_len_pub_inputs_goldilocks_vec_slice = crate::test_utils::pub_inputs_conversion_utils::convert_inner_vec_to_slice(&var_len_pub_inputs_goldilocks_vec); + let var_len_pub_inputs_goldilocks = crate::test_utils::pub_inputs_conversion_utils::convert_mid_vec_to_slice(&var_len_pub_inputs_goldilocks_vec_slice); + + let trace = generate_trace_rows::(inputs); + + let proof = p3_miden_prover::prove(&config, &$air_name {}, &trace, &inputs_goldilocks); + p3_miden_prover::verify( + &config, + &$air_name {}, + &proof, + &inputs_goldilocks, + &var_len_pub_inputs_goldilocks, + ) + .expect("Verification failed"); + } + }; +} diff --git a/air-script/tests/codegen/helpers.rs b/air-script/src/test_utils/codegen.rs similarity index 92% rename from air-script/tests/codegen/helpers.rs rename to air-script/src/test_utils/codegen.rs index 3fc9c8aa6..9e68ca753 100644 --- a/air-script/tests/codegen/helpers.rs +++ b/air-script/src/test_utils/codegen.rs @@ -1,13 +1,15 @@ use std::sync::Arc; use air_ir::{CodeGenerator, CompileError}; -use air_script::compile; use miden_diagnostics::{ CodeMap, DefaultEmitter, DiagnosticsHandler, term::termcolor::ColorChoice, }; +use crate::compile; + pub enum Target { Winterfell, + Plonky3, } pub struct Test { @@ -30,6 +32,7 @@ impl Test { let backend: Box> = match target { Target::Winterfell => Box::new(air_codegen_winter::CodeGenerator), + Target::Plonky3 => Box::new(air_codegen_plonky3::CodeGenerator), }; // generate Rust code targeting Winterfell diff --git a/air-script/src/test_utils/mod.rs b/air-script/src/test_utils/mod.rs new file mode 100644 index 000000000..03ce65907 --- /dev/null +++ b/air-script/src/test_utils/mod.rs @@ -0,0 +1,8 @@ +/// Macros to generate Air tester structs and tests for both Plonky3 and Winterfell backends. +pub mod air_tester_macros; +/// Code generation for tests/**/*.air files. +pub mod codegen; +/// Conversion utilities for test inputs (both public inputs and variable-length public inputs). +pub mod pub_inputs_conversion_utils; +/// Winterfell-specific traits +pub mod winterfell_traits; diff --git a/air-script/src/test_utils/pub_inputs_conversion_utils.rs b/air-script/src/test_utils/pub_inputs_conversion_utils.rs new file mode 100644 index 000000000..894ba75d6 --- /dev/null +++ b/air-script/src/test_utils/pub_inputs_conversion_utils.rs @@ -0,0 +1,46 @@ +type Val = p3_goldilocks::Goldilocks; + +/// Converts public inputs from u64 to Goldilocks field elements. +pub(crate) fn convert_pub_inputs_to_goldilocks(pub_inputs: &[u64]) -> Vec { + pub_inputs + .iter() + .map(|&x| ::from_u64(x)) + .collect() +} + +/// Converts variable-length public inputs from u64 to Goldilocks field elements. +/// The input should be a Vec of tables (Vec>) in a RowMajor format. +pub(crate) fn convert_var_len_pub_inputs_to_goldilocks( + var_len_pub_inputs: Vec>>, +) -> Vec>> { + let mut var_len_pub_inputs_goldilocks_vec: Vec>> = vec![]; + for arr in var_len_pub_inputs.iter() { + let mut goldilocks_arr: Vec> = vec![]; + for slice in arr.iter() { + let goldilocks_slice: Vec = slice + .iter() + .map(|&x| ::from_u64(x)) + .collect(); + goldilocks_arr.push(goldilocks_slice); + } + var_len_pub_inputs_goldilocks_vec.push(goldilocks_arr); + } + var_len_pub_inputs_goldilocks_vec +} + +/// Converts the innermost vectors of variable-length public inputs to slices. +pub(crate) fn convert_inner_vec_to_slice<'a>( + var_len_pub_inputs: &'a Vec>>, +) -> Vec> { + var_len_pub_inputs + .iter() + .map(|outer| outer.iter().map(|inner| inner.as_slice()).collect()) + .collect() +} + +/// Converts the middle vectors of variable-length public inputs to slices. +pub(crate) fn convert_mid_vec_to_slice<'a>( + var_len_pub_inputs: &'a Vec>, +) -> Vec<&'a [&'a [Val]]> { + var_len_pub_inputs.iter().map(|v| v.as_slice()).collect() +} diff --git a/air-script/tests/helpers/mod.rs b/air-script/src/test_utils/winterfell_traits.rs similarity index 99% rename from air-script/tests/helpers/mod.rs rename to air-script/src/test_utils/winterfell_traits.rs index dbabc0709..5f91493d8 100644 --- a/air-script/tests/helpers/mod.rs +++ b/air-script/src/test_utils/winterfell_traits.rs @@ -2,8 +2,6 @@ use winter_air::{BatchingMethod, EvaluationFrame, FieldExtension, ProofOptions, use winter_math::fields::f64::BaseElement as Felt; use winterfell::{AuxTraceWithMetadata, Trace, TraceTable, matrix::ColMatrix}; -pub mod macros; - /// We need to encapsulate the trace table in a struct to manually implement the `aux_trace_width` /// method of the `Table` trait. Otherwise, using only a TraceTable will return an /// `aux_trace_width` of 0 even if we provide a non-empty aux trace in `Trace::validate`, diff --git a/air-script/tests/binary/binary.air b/air-script/src/tests/binary/binary.air similarity index 84% rename from air-script/tests/binary/binary.air rename to air-script/src/tests/binary/binary.air index 50e218052..e609a69b3 100644 --- a/air-script/tests/binary/binary.air +++ b/air-script/src/tests/binary/binary.air @@ -9,7 +9,7 @@ public_inputs { } boundary_constraints { - enf a.first = 0; + enf a.first = stack_inputs[0]; } integrity_constraints { diff --git a/air-script/tests/binary/binary.rs b/air-script/src/tests/binary/binary.rs similarity index 97% rename from air-script/tests/binary/binary.rs rename to air-script/src/tests/binary/binary.rs index 8b7185bbd..b398843dc 100644 --- a/air-script/tests/binary/binary.rs +++ b/air-script/src/tests/binary/binary.rs @@ -70,7 +70,7 @@ impl Air for BinaryAir { fn get_assertions(&self) -> Vec> { let mut result = Vec::new(); - result.push(Assertion::single(0, 0, Felt::ZERO)); + result.push(Assertion::single(0, 0, self.stack_inputs[0])); result } diff --git a/air-script/src/tests/binary/binary_plonky3.rs b/air-script/src/tests/binary/binary_plonky3.rs new file mode 100644 index 000000000..9f64d933c --- /dev/null +++ b/air-script/src/tests/binary/binary_plonky3.rs @@ -0,0 +1,43 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 2; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct BinaryAir; + +impl MidenAir for BinaryAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into() - public_values[0].into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() * main_current[0].clone().into() - main_current[0].clone().into()); + builder.assert_zero(main_current[1].clone().into() * main_current[1].clone().into() - main_current[1].clone().into()); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/binary/mod.rs b/air-script/src/tests/binary/mod.rs new file mode 100644 index 000000000..08cbf3225 --- /dev/null +++ b/air-script/src/tests/binary/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod binary; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod binary_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/binary/test_air_plonky3.rs b/air-script/src/tests/binary/test_air_plonky3.rs new file mode 100644 index 000000000..ba508f868 --- /dev/null +++ b/air-script/src/tests/binary/test_air_plonky3.rs @@ -0,0 +1,47 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::binary::binary_plonky3::{BinaryAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::from_canonical_checked(inputs[0]).unwrap(); + rows[0][1] = F::ONE; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let a_prev = rows[i - 1][0]; + let b_prev = rows[i - 1][1]; + + // Update current row based on previous values + rows[i][0] = F::ONE - a_prev; + rows[i][1] = F::ONE - b_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, BinaryAir); diff --git a/air-script/tests/binary/test_air.rs b/air-script/src/tests/binary/test_air_winterfell.rs similarity index 76% rename from air-script/tests/binary/test_air.rs rename to air-script/src/tests/binary/test_air_winterfell.rs index 4f40af405..2135c11b6 100644 --- a/air-script/tests/binary/test_air.rs +++ b/air-script/src/tests/binary/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - binary::binary::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::binary::binary::PublicInputs, }; #[derive(Clone)] @@ -39,4 +39,9 @@ impl AirTester for BinaryAirTester { } } -generate_air_test!(test_binary_air, crate::binary::binary::BinaryAir, BinaryAirTester, 1024); +generate_air_winterfell_test!( + test_binary_air, + crate::tests::binary::binary::BinaryAir, + BinaryAirTester, + 1024 +); diff --git a/air-script/tests/bitwise/bitwise.air b/air-script/src/tests/bitwise/bitwise.air similarity index 100% rename from air-script/tests/bitwise/bitwise.air rename to air-script/src/tests/bitwise/bitwise.air diff --git a/air-script/tests/bitwise/bitwise.rs b/air-script/src/tests/bitwise/bitwise.rs similarity index 100% rename from air-script/tests/bitwise/bitwise.rs rename to air-script/src/tests/bitwise/bitwise.rs diff --git a/air-script/src/tests/bitwise/bitwise_plonky3.rs b/air-script/src/tests/bitwise/bitwise_plonky3.rs new file mode 100644 index 000000000..9811db8cf --- /dev/null +++ b/air-script/src/tests/bitwise/bitwise_plonky3.rs @@ -0,0 +1,72 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 14; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 2; +pub const PERIOD: usize = 8; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct BitwiseAir; + +impl MidenAir for BitwiseAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_public_values(&self) -> usize { + NUM_PUBLIC_VALUES + } + + fn periodic_table(&self) -> Vec> { + vec![ + vec![F::from_u64(1), F::from_u64(0), F::from_u64(0), F::from_u64(0), F::from_u64(0), F::from_u64(0), F::from_u64(0), F::from_u64(0)], + vec![F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(0)], + ] + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[13].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() * main_current[0].clone().into() - main_current[0].clone().into()); + builder.when_transition().assert_zero_ext(AB::ExprEF::from(periodic_values[1].clone().into()) * (AB::ExprEF::from(main_next[0].clone().into()) - AB::ExprEF::from(main_current[0].clone().into()))); + builder.assert_zero(main_current[3].clone().into() * main_current[3].clone().into() - main_current[3].clone().into()); + builder.assert_zero(main_current[4].clone().into() * main_current[4].clone().into() - main_current[4].clone().into()); + builder.assert_zero(main_current[5].clone().into() * main_current[5].clone().into() - main_current[5].clone().into()); + builder.assert_zero(main_current[6].clone().into() * main_current[6].clone().into() - main_current[6].clone().into()); + builder.assert_zero(main_current[7].clone().into() * main_current[7].clone().into() - main_current[7].clone().into()); + builder.assert_zero(main_current[8].clone().into() * main_current[8].clone().into() - main_current[8].clone().into()); + builder.assert_zero(main_current[9].clone().into() * main_current[9].clone().into() - main_current[9].clone().into()); + builder.assert_zero(main_current[10].clone().into() * main_current[10].clone().into() - main_current[10].clone().into()); + builder.assert_zero_ext(AB::ExprEF::from(periodic_values[0].clone().into()) * (AB::ExprEF::from(main_current[1].clone().into()) - (AB::ExprEF::from(main_current[3].clone().into()) + AB::ExprEF::from(main_current[4].clone().into()).double() + AB::ExprEF::from_u64(4) * AB::ExprEF::from(main_current[5].clone().into()) + AB::ExprEF::from_u64(8) * AB::ExprEF::from(main_current[6].clone().into())))); + builder.assert_zero_ext(AB::ExprEF::from(periodic_values[0].clone().into()) * (AB::ExprEF::from(main_current[2].clone().into()) - (AB::ExprEF::from(main_current[7].clone().into()) + AB::ExprEF::from(main_current[8].clone().into()).double() + AB::ExprEF::from_u64(4) * AB::ExprEF::from(main_current[9].clone().into()) + AB::ExprEF::from_u64(8) * AB::ExprEF::from(main_current[10].clone().into())))); + builder.when_transition().assert_zero_ext(AB::ExprEF::from(periodic_values[1].clone().into()) * (AB::ExprEF::from(main_next[1].clone().into()) - (AB::ExprEF::from(main_current[1].clone().into()) * AB::ExprEF::from_u64(16) + AB::ExprEF::from(main_current[3].clone().into()) + AB::ExprEF::from(main_current[4].clone().into()).double() + AB::ExprEF::from_u64(4) * AB::ExprEF::from(main_current[5].clone().into()) + AB::ExprEF::from_u64(8) * AB::ExprEF::from(main_current[6].clone().into())))); + builder.when_transition().assert_zero_ext(AB::ExprEF::from(periodic_values[1].clone().into()) * (AB::ExprEF::from(main_next[2].clone().into()) - (AB::ExprEF::from(main_current[2].clone().into()) * AB::ExprEF::from_u64(16) + AB::ExprEF::from(main_current[7].clone().into()) + AB::ExprEF::from(main_current[8].clone().into()).double() + AB::ExprEF::from_u64(4) * AB::ExprEF::from(main_current[9].clone().into()) + AB::ExprEF::from_u64(8) * AB::ExprEF::from(main_current[10].clone().into())))); + builder.assert_zero_ext(AB::ExprEF::from(periodic_values[0].clone().into()) * AB::ExprEF::from(main_current[11].clone().into())); + builder.when_transition().assert_zero_ext(AB::ExprEF::from(periodic_values[1].clone().into()) * (AB::ExprEF::from(main_current[12].clone().into()) - AB::ExprEF::from(main_next[11].clone().into()))); + builder.assert_zero((AB::Expr::ONE - main_current[0].clone().into()) * (main_current[12].clone().into() - (main_current[11].clone().into() * AB::Expr::from_u64(16) + main_current[3].clone().into() * main_current[7].clone().into() + main_current[4].clone().into().double() * main_current[8].clone().into() + AB::Expr::from_u64(4) * main_current[5].clone().into() * main_current[9].clone().into() + AB::Expr::from_u64(8) * main_current[6].clone().into() * main_current[10].clone().into())) + main_current[0].clone().into() * (main_current[12].clone().into() - (main_current[11].clone().into() * AB::Expr::from_u64(16) + main_current[3].clone().into() + main_current[7].clone().into() - main_current[3].clone().into().double() * main_current[7].clone().into() + (main_current[4].clone().into() + main_current[8].clone().into() - main_current[4].clone().into().double() * main_current[8].clone().into()).double() + AB::Expr::from_u64(4) * (main_current[5].clone().into() + main_current[9].clone().into() - main_current[5].clone().into().double() * main_current[9].clone().into()) + AB::Expr::from_u64(8) * (main_current[6].clone().into() + main_current[10].clone().into() - main_current[6].clone().into().double() * main_current[10].clone().into())))); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/bitwise/mod.rs b/air-script/src/tests/bitwise/mod.rs new file mode 100644 index 000000000..54b732efb --- /dev/null +++ b/air-script/src/tests/bitwise/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod bitwise; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod bitwise_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/bitwise/test_air_plonky3.rs b/air-script/src/tests/bitwise/test_air_plonky3.rs new file mode 100644 index 000000000..91b2c0637 --- /dev/null +++ b/air-script/src/tests/bitwise/test_air_plonky3.rs @@ -0,0 +1,85 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::bitwise::bitwise_plonky3::{BitwiseAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + let first = F::ZERO; + + // Initialize first row + rows[0][0] = first; + rows[0][1] = first; + rows[0][2] = first; + rows[0][3] = first; + rows[0][4] = first; + rows[0][5] = first; + rows[0][6] = first; + rows[0][7] = first; + rows[0][8] = first; + rows[0][9] = first; + rows[0][10] = first; + rows[0][11] = first; + rows[0][12] = first; + rows[0][13] = first; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + let col_8_prev = rows[i - 1][8]; + let col_9_prev = rows[i - 1][9]; + let col_10_prev = rows[i - 1][10]; + let col_11_prev = rows[i - 1][11]; + let col_12_prev = rows[i - 1][12]; + let col_13_prev = rows[i - 1][13]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + rows[i][7] = col_7_prev; + rows[i][8] = col_8_prev; + rows[i][9] = col_9_prev; + rows[i][10] = col_10_prev; + rows[i][11] = col_11_prev; + rows[i][12] = col_12_prev; + rows[i][13] = col_13_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, BitwiseAir); diff --git a/air-script/tests/bitwise/test_air.rs b/air-script/src/tests/bitwise/test_air_winterfell.rs similarity index 82% rename from air-script/tests/bitwise/test_air.rs rename to air-script/src/tests/bitwise/test_air_winterfell.rs index 16daca900..e563782ef 100644 --- a/air-script/tests/bitwise/test_air.rs +++ b/air-script/src/tests/bitwise/test_air_winterfell.rs @@ -5,9 +5,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{AuxTraceWithMetadata, Trace, TraceTable, matrix::ColMatrix}; use crate::{ - bitwise::bitwise::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::bitwise::bitwise::PublicInputs, }; #[derive(Clone)] @@ -50,4 +50,9 @@ impl AirTester for BitwiseAirTester { } } -generate_air_test!(test_bitwise_air, crate::bitwise::bitwise::BitwiseAir, BitwiseAirTester, 1024); +generate_air_winterfell_test!( + test_bitwise_air, + crate::tests::bitwise::bitwise::BitwiseAir, + BitwiseAirTester, + 1024 +); diff --git a/air-script/tests/buses/buses_complex.air b/air-script/src/tests/buses/buses_complex.air similarity index 80% rename from air-script/tests/buses/buses_complex.air rename to air-script/src/tests/buses/buses_complex.air index 401ca8b67..f27a2745c 100644 --- a/air-script/tests/buses/buses_complex.air +++ b/air-script/src/tests/buses/buses_complex.air @@ -1,7 +1,7 @@ def BusesAir trace_columns { - main: [a, b, s1, s2, d], + main: [a, b, s1, s2, s3, s4, d], } buses { @@ -34,7 +34,7 @@ integrity_constraints { p.insert(2, b) when 1 - s1; p.remove(2, a) when 1 - s2; - q.insert(3, a) when s1; - q.insert(3, a) when s1; - q.remove(4, b) with d; + q.insert(3, a) when s3; + q.insert(3, a) when s4; + q.remove(3, b) with d; } diff --git a/air-script/tests/buses/buses_complex.rs b/air-script/src/tests/buses/buses_complex.rs similarity index 93% rename from air-script/tests/buses/buses_complex.rs rename to air-script/src/tests/buses/buses_complex.rs index d15eeeb8f..33271a2ab 100644 --- a/air-script/tests/buses/buses_complex.rs +++ b/air-script/src/tests/buses/buses_complex.rs @@ -99,6 +99,6 @@ impl Air for BusesAir { let aux_current = aux_frame.current(); let aux_next = aux_frame.next(); result[0] = ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + (E::from(Felt::new(3_u64)) + E::from(main_current[1])) * aux_rand_elements.rand_elements()[2] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[3]) * E::from(main_current[2]) + E::ONE - E::from(main_current[2])) * ((aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * (E::ONE - E::from(main_current[2])) + E::from(main_current[2])) * aux_current[0] - ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + (E::from(Felt::new(3_u64)) + E::from(main_current[1])) * aux_rand_elements.rand_elements()[2] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[3]) * E::from(main_current[3]) + E::ONE - E::from(main_current[3])) * ((aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (E::ONE - E::from(main_current[3])) + E::from(main_current[3])) * aux_next[0]; - result[1] = (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(4_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * aux_current[1] + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(4_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[2]) + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(4_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[2]) - ((aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(4_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * aux_next[1] + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[4])); + result[1] = (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * aux_current[1] + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[4]) + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[5]) - ((aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[1]) * aux_rand_elements.rand_elements()[2]) * aux_next[1] + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(3_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[6])); } } \ No newline at end of file diff --git a/air-script/src/tests/buses/buses_complex_plonky3.rs b/air-script/src/tests/buses/buses_complex_plonky3.rs new file mode 100644 index 000000000..2d038524d --- /dev/null +++ b/air-script/src/tests/buses/buses_complex_plonky3.rs @@ -0,0 +1,141 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 7; +pub const AUX_WIDTH: usize = 2; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 2; +pub const MAX_BETA_CHALLENGE_POWER: usize = 3; + +pub struct BusesAir; + +impl MidenAir for BusesAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_randomness(&self) -> usize { + 1 + MAX_BETA_CHALLENGE_POWER + } + + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn bus_types(&self) -> Vec { + vec![ + BusType::Multiset, + BusType::Logup, + ] + } + + fn build_aux_trace(&self, _main: &RowMajorMatrix, _challenges: &[EF]) -> Option> { + // Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders. + + let num_rows = _main.height(); + let trace_length = num_rows * AUX_WIDTH; + let mut long_trace = EF::zero_vec(trace_length); + let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH); + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + // Initialize first row + let initial_values = Self::buses_initial_values::(); + for j in 0..AUX_WIDTH { + rows[0][j] = initial_values[j]; + } + // Fill subsequent rows using direct access to the rows array + for i in 0..num_rows-1 { + let i_next = (i + 1) % num_rows; + let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail. + let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail. + let main = VerticalPair::new( + RowMajorMatrixView::new_row(&*main_local), + RowMajorMatrixView::new_row(&*main_next), + ); + let periodic_values: [_; NUM_PERIODIC_VALUES] = >::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect("Wrong number of periodic values"); + let prev_row = &rows[i]; + let next_row = Self::buses_transitions::( + &main, + _challenges, + &periodic_values, + prev_row, + ); + for j in 0..AUX_WIDTH { + rows[i+1][j] = next_row[j]; + } + } + let trace_f = trace.flatten_to_base(); + Some(trace_f) + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = ( + aux.row_slice(0).unwrap(), + aux.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[2].clone().into() * main_current[2].clone().into() - main_current[2].clone().into()); + builder.assert_zero(main_current[3].clone().into() * main_current[3].clone().into() - main_current[3].clone().into()); + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext(((alpha.into() + beta_challenges[0].into() + (AB::ExprEF::from_u64(3) + AB::ExprEF::from(main_current[1].clone().into())) * beta_challenges[1].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[2].into()) * AB::ExprEF::from(main_current[2].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[2].clone().into())) * ((alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[1].clone().into()) * beta_challenges[1].into()) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[2].clone().into())) + AB::ExprEF::from(main_current[2].clone().into())) * AB::ExprEF::from(aux_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into() + (AB::ExprEF::from_u64(3) + AB::ExprEF::from(main_current[1].clone().into())) * beta_challenges[1].into() + AB::ExprEF::from(main_current[1].clone().into()) * beta_challenges[2].into()) * AB::ExprEF::from(main_current[3].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[3].clone().into())) * ((alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[3].clone().into())) + AB::ExprEF::from(main_current[3].clone().into())) * AB::ExprEF::from(aux_next[0].clone().into())); + builder.when_transition().assert_zero_ext((alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[1].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(aux_current[1].clone().into()) + (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[1].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(main_current[4].clone().into()) + (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[1].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(main_current[5].clone().into()) - ((alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[1].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(aux_next[1].clone().into()) + (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(main_current[6].clone().into()))); + } +} + +impl BusesAir { + fn buses_initial_values() -> Vec + where F: Field, + EF: ExtensionField, + { + vec![ + EF::ONE, + EF::ZERO, + ] + } + + fn buses_transitions(main: &VerticalPair, RowMajorMatrixView>, challenges: &[EF], periodic_evals: &[F], aux_current: &[EF]) -> Vec + where F: Field, + EF: ExtensionField, + { + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = challenges.split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect("Wrong number of periodic values"); + vec![ + (((alpha + beta_challenges[0] + (EF::from_u64(3) + EF::from(main_current[1].clone())) * beta_challenges[1] + EF::from(main_current[0].clone()) * beta_challenges[2]) * EF::from(main_current[2].clone()) + EF::ONE - EF::from(main_current[2].clone())) * ((alpha + beta_challenges[0].double() + EF::from(main_current[1].clone()) * beta_challenges[1]) * (EF::ONE - EF::from(main_current[2].clone())) + EF::from(main_current[2].clone())) * EF::from(aux_current[0].clone())) * (((alpha + beta_challenges[0] + (EF::from_u64(3) + EF::from(main_current[1].clone())) * beta_challenges[1] + EF::from(main_current[1].clone()) * beta_challenges[2]) * EF::from(main_current[3].clone()) + EF::ONE - EF::from(main_current[3].clone())) * ((alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (EF::ONE - EF::from(main_current[3].clone())) + EF::from(main_current[3].clone()))).inverse(), + ((alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[1].clone()) * beta_challenges[1]) * EF::from(aux_current[1].clone()) + (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[1].clone()) * beta_challenges[1]) * EF::from(main_current[4].clone()) + (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[1].clone()) * beta_challenges[1]) * EF::from(main_current[5].clone()) - (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * EF::from(main_current[6].clone())) * ((alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + EF::from_u64(3) * beta_challenges[0] + EF::from(main_current[1].clone()) * beta_challenges[1])).inverse(), + ] + } +} \ No newline at end of file diff --git a/air-script/tests/buses/buses_simple.air b/air-script/src/tests/buses/buses_simple.air similarity index 100% rename from air-script/tests/buses/buses_simple.air rename to air-script/src/tests/buses/buses_simple.air diff --git a/air-script/tests/buses/buses_simple.rs b/air-script/src/tests/buses/buses_simple.rs similarity index 100% rename from air-script/tests/buses/buses_simple.rs rename to air-script/src/tests/buses/buses_simple.rs diff --git a/air-script/src/tests/buses/buses_simple_plonky3.rs b/air-script/src/tests/buses/buses_simple_plonky3.rs new file mode 100644 index 000000000..65efcf8fb --- /dev/null +++ b/air-script/src/tests/buses/buses_simple_plonky3.rs @@ -0,0 +1,137 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 1; +pub const AUX_WIDTH: usize = 2; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 2; +pub const MAX_BETA_CHALLENGE_POWER: usize = 2; + +pub struct BusesAir; + +impl MidenAir for BusesAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_randomness(&self) -> usize { + 1 + MAX_BETA_CHALLENGE_POWER + } + + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn bus_types(&self) -> Vec { + vec![ + BusType::Multiset, + BusType::Logup, + ] + } + + fn build_aux_trace(&self, _main: &RowMajorMatrix, _challenges: &[EF]) -> Option> { + // Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders. + + let num_rows = _main.height(); + let trace_length = num_rows * AUX_WIDTH; + let mut long_trace = EF::zero_vec(trace_length); + let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH); + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + // Initialize first row + let initial_values = Self::buses_initial_values::(); + for j in 0..AUX_WIDTH { + rows[0][j] = initial_values[j]; + } + // Fill subsequent rows using direct access to the rows array + for i in 0..num_rows-1 { + let i_next = (i + 1) % num_rows; + let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail. + let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail. + let main = VerticalPair::new( + RowMajorMatrixView::new_row(&*main_local), + RowMajorMatrixView::new_row(&*main_next), + ); + let periodic_values: [_; NUM_PERIODIC_VALUES] = >::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect("Wrong number of periodic values"); + let prev_row = &rows[i]; + let next_row = Self::buses_transitions::( + &main, + _challenges, + &periodic_values, + prev_row, + ); + for j in 0..AUX_WIDTH { + rows[i+1][j] = next_row[j]; + } + } + let trace_f = trace.flatten_to_base(); + Some(trace_f) + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = ( + aux.row_slice(0).unwrap(), + aux.row_slice(1).unwrap(), + ); + + // Main boundary constraints + + // Main integrity/transition constraints + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext(((alpha.into() + beta_challenges[0].into()) * AB::ExprEF::from(main_current[0].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * AB::ExprEF::from(aux_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into()) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) + AB::ExprEF::from(main_current[0].clone().into())) * AB::ExprEF::from(aux_next[0].clone().into())); + builder.when_transition().assert_zero_ext((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(aux_current[1].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(main_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(aux_next[1].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()).double())); + } +} + +impl BusesAir { + fn buses_initial_values() -> Vec + where F: Field, + EF: ExtensionField, + { + vec![ + EF::ZERO, + ] + } + + fn buses_transitions(main: &VerticalPair, RowMajorMatrixView>, challenges: &[EF], periodic_evals: &[F], aux_current: &[EF]) -> Vec + where F: Field, + EF: ExtensionField, + { + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = challenges.split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect("Wrong number of periodic values"); + vec![ + (((alpha + beta_challenges[0]) * EF::from(main_current[0].clone()) + EF::ONE - EF::from(main_current[0].clone())) * EF::from(aux_current[0].clone())) * ((alpha + beta_challenges[0]) * (EF::ONE - EF::from(main_current[0].clone())) + EF::from(main_current[0].clone())).inverse(), + ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(aux_current[1].clone()) + (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(main_current[0].clone()) - (alpha + beta_challenges[0] + beta_challenges[1].double()).double()) * ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double())).inverse(), + ] + } +} \ No newline at end of file diff --git a/air-script/tests/buses/buses_simple_with_evaluators.air b/air-script/src/tests/buses/buses_simple_with_evaluators.air similarity index 100% rename from air-script/tests/buses/buses_simple_with_evaluators.air rename to air-script/src/tests/buses/buses_simple_with_evaluators.air diff --git a/air-script/src/tests/buses/buses_simple_with_evaluators.rs b/air-script/src/tests/buses/buses_simple_with_evaluators.rs new file mode 100644 index 000000000..b0012a15a --- /dev/null +++ b/air-script/src/tests/buses/buses_simple_with_evaluators.rs @@ -0,0 +1,100 @@ +use winter_air::{Air, AirContext, Assertion, AuxRandElements, EvaluationFrame, ProofOptions as WinterProofOptions, TransitionConstraintDegree, TraceInfo}; +use winter_math::fields::f64::BaseElement as Felt; +use winter_math::{ExtensionOf, FieldElement, ToElements}; +use winter_utils::{ByteWriter, Serializable}; + +pub struct PublicInputs { + inputs: [Felt; 2], +} + +impl PublicInputs { + pub fn new(inputs: [Felt; 2]) -> Self { + Self { inputs } + } +} + +impl Serializable for PublicInputs { + fn write_into(&self, target: &mut W) { + self.inputs.write_into(target); + } +} + +impl ToElements for PublicInputs { + fn to_elements(&self) -> Vec { + let mut elements = Vec::new(); + elements.extend_from_slice(&self.inputs); + elements + } +} + +pub struct BusesAir { + context: AirContext, + inputs: [Felt; 2], +} + +impl BusesAir { + pub fn last_step(&self) -> usize { + self.trace_length() - self.context().num_transition_exemptions() + } +} + +impl Air for BusesAir { + type BaseField = Felt; + type PublicInputs = PublicInputs; + + fn context(&self) -> &AirContext { + &self.context + } + + fn new(trace_info: TraceInfo, public_inputs: PublicInputs, options: WinterProofOptions) -> Self { + let main_degrees = vec![]; + let aux_degrees = vec![TransitionConstraintDegree::new(2), TransitionConstraintDegree::new(1)]; + let num_main_assertions = 0; + let num_aux_assertions = 3; + + let context = AirContext::new_multi_segment( + trace_info, + main_degrees, + aux_degrees, + num_main_assertions, + num_aux_assertions, + options, + ) + .set_num_transition_exemptions(2); + Self { context, inputs: public_inputs.inputs } + } + + fn get_periodic_column_values(&self) -> Vec> { + vec![] + } + + fn get_assertions(&self) -> Vec> { + let mut result = Vec::new(); + result + } + + fn get_aux_assertions>(&self, aux_rand_elements: &AuxRandElements) -> Vec> { + let mut result = Vec::new(); + result.push(Assertion::single(0, self.last_step(), E::ONE)); + result.push(Assertion::single(1, 0, E::ZERO)); + result.push(Assertion::single(1, self.last_step(), E::ZERO)); + result + } + + fn evaluate_transition>(&self, frame: &EvaluationFrame, periodic_values: &[E], result: &mut [E]) { + let main_current = frame.current(); + let main_next = frame.next(); + } + + fn evaluate_aux_transition(&self, main_frame: &EvaluationFrame, aux_frame: &EvaluationFrame, _periodic_values: &[F], aux_rand_elements: &AuxRandElements, result: &mut [E]) + where F: FieldElement, + E: FieldElement + ExtensionOf, + { + let main_current = main_frame.current(); + let main_next = main_frame.next(); + let aux_current = aux_frame.current(); + let aux_next = aux_frame.next(); + result[0] = ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1]) * E::from(main_current[0]) + E::ONE - E::from(main_current[0])) * aux_current[0] - ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1]) * (E::ONE - E::from(main_current[0])) + E::from(main_current[0])) * aux_next[0]; + result[1] = (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * aux_current[1] + (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[0]) - ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * aux_next[1] + (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * E::from(Felt::new(2_u64))); + } +} \ No newline at end of file diff --git a/air-script/src/tests/buses/buses_simple_with_evaluators_plonky3.rs b/air-script/src/tests/buses/buses_simple_with_evaluators_plonky3.rs new file mode 100644 index 000000000..7ad2d398f --- /dev/null +++ b/air-script/src/tests/buses/buses_simple_with_evaluators_plonky3.rs @@ -0,0 +1,108 @@ +use crate::test_utils::plonky3_traits::{AirScriptAir, AirScriptBuilder}; +use p3_air::{Air, AirBuilder, BaseAir, BaseAirWithPublicValues, ExtensionBuilder}; +use p3_field::{Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; + +pub const MAIN_WIDTH: usize = 1; +pub const AUX_WIDTH: usize = 2; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 2; +pub const MAX_BETA_CHALLENGE_POWER: usize = 2; + +pub struct BusesAir; + +impl BaseAir for BusesAir { + fn width(&self) -> usize { + MAIN_WIDTH + } +} + +impl BaseAirWithPublicValues for BusesAir { + fn num_public_values(&self) -> usize { + NUM_PUBLIC_VALUES + } +} + +impl> AirScriptAir for BusesAir { + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn max_beta_challenge_powers(&self) -> usize { + MAX_BETA_CHALLENGE_POWER + } + + fn periodic_table(&self) -> Vec> { + vec![] + } + + fn eval(&self, builder: &mut AB) { + let public_values: [_; NUM_PUBLIC_VALUES] = + builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = + builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + let main = builder.main(); + let (main_current, main_next) = (main.row_slice(0).unwrap(), main.row_slice(1).unwrap()); + let alpha = builder.alpha(); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = + builder.beta_powers().try_into().expect("Wrong number of beta challenges"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder + .aux_bus_boundary_values() + .try_into() + .expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = (aux.row_slice(0).unwrap(), aux.row_slice(1).unwrap()); + + // Main boundary constraints + + // Main integrity/transition constraints + + // Aux boundary constraints + builder + .when_last_row() + .assert_zero_ext(AB::ExprEF::from(aux_current[0].clone().into()) - AB::ExprEF::ONE); + builder + .when_first_row() + .assert_zero_ext(AB::ExprEF::from(aux_current[1].clone().into())); + builder + .when_last_row() + .assert_zero_ext(AB::ExprEF::from(aux_current[1].clone().into())); + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext( + ((alpha.into() + beta_challenges[0].into()) + * AB::ExprEF::from(main_current[0].clone().into()) + + AB::ExprEF::ONE + - AB::ExprEF::from(main_current[0].clone().into())) + * AB::ExprEF::from(aux_current[0].clone().into()) + - ((alpha.into() + beta_challenges[0].into()) + * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) + + AB::ExprEF::from(main_current[0].clone().into())) + * AB::ExprEF::from(aux_next[0].clone().into()), + ); + builder.when_transition().assert_zero_ext( + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) + * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) + * AB::ExprEF::from(aux_current[1].clone().into()) + + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) + * AB::ExprEF::from(main_current[0].clone().into()) + - ((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) + * (alpha.into() + + beta_challenges[0].into() + + beta_challenges[1].into().double()) + * AB::ExprEF::from(aux_next[1].clone().into()) + + (alpha.into() + + beta_challenges[0].into() + + beta_challenges[1].into().double()) + .double()), + ); + } +} + +impl Air for BusesAir { + fn eval(&self, builder: &mut AB) { + >::eval(self, builder); + } +} + diff --git a/air-script/tests/buses/buses_varlen_boundary_both.air b/air-script/src/tests/buses/buses_varlen_boundary_both.air similarity index 100% rename from air-script/tests/buses/buses_varlen_boundary_both.air rename to air-script/src/tests/buses/buses_varlen_boundary_both.air diff --git a/air-script/tests/buses/buses_varlen_boundary_both.rs b/air-script/src/tests/buses/buses_varlen_boundary_both.rs similarity index 100% rename from air-script/tests/buses/buses_varlen_boundary_both.rs rename to air-script/src/tests/buses/buses_varlen_boundary_both.rs diff --git a/air-script/src/tests/buses/buses_varlen_boundary_both_plonky3.rs b/air-script/src/tests/buses/buses_varlen_boundary_both_plonky3.rs new file mode 100644 index 000000000..b32d738e1 --- /dev/null +++ b/air-script/src/tests/buses/buses_varlen_boundary_both_plonky3.rs @@ -0,0 +1,138 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 1; +pub const AUX_WIDTH: usize = 2; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 0; +pub const MAX_BETA_CHALLENGE_POWER: usize = 2; + +pub struct BusesAir; + +impl MidenAir for BusesAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_randomness(&self) -> usize { + 1 + MAX_BETA_CHALLENGE_POWER + } + + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn bus_types(&self) -> Vec { + vec![ + BusType::Multiset, + BusType::Logup, + ] + } + + fn build_aux_trace(&self, _main: &RowMajorMatrix, _challenges: &[EF]) -> Option> { + // Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders. + + let num_rows = _main.height(); + let trace_length = num_rows * AUX_WIDTH; + let mut long_trace = EF::zero_vec(trace_length); + let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH); + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + // Initialize first row + let initial_values = Self::buses_initial_values::(); + for j in 0..AUX_WIDTH { + rows[0][j] = initial_values[j]; + } + // Fill subsequent rows using direct access to the rows array + for i in 0..num_rows-1 { + let i_next = (i + 1) % num_rows; + let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail. + let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail. + let main = VerticalPair::new( + RowMajorMatrixView::new_row(&*main_local), + RowMajorMatrixView::new_row(&*main_next), + ); + let periodic_values: [_; NUM_PERIODIC_VALUES] = >::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect("Wrong number of periodic values"); + let prev_row = &rows[i]; + let next_row = Self::buses_transitions::( + &main, + _challenges, + &periodic_values, + prev_row, + ); + for j in 0..AUX_WIDTH { + rows[i+1][j] = next_row[j]; + } + } + let trace_f = trace.flatten_to_base(); + Some(trace_f) + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = ( + aux.row_slice(0).unwrap(), + aux.row_slice(1).unwrap(), + ); + + // Main boundary constraints + + // Main integrity/transition constraints + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext(((alpha.into() + beta_challenges[0].into()) * AB::ExprEF::from(main_current[0].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * AB::ExprEF::from(aux_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into()) * (AB::ExprEF::from(main_current[0].clone().into()) - AB::ExprEF::ONE) + AB::ExprEF::ONE - (AB::ExprEF::from(main_current[0].clone().into()) - AB::ExprEF::ONE)) * AB::ExprEF::from(aux_next[0].clone().into())); + builder.when_transition().assert_zero_ext((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(aux_current[1].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(main_current[0].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(main_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(aux_next[1].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()).double())); + } +} + +impl BusesAir { + fn buses_initial_values() -> Vec + where F: Field, + EF: ExtensionField, + { + vec![ + EF::ZERO, + EF::ZERO, + ] + } + + fn buses_transitions(main: &VerticalPair, RowMajorMatrixView>, challenges: &[EF], periodic_evals: &[F], aux_current: &[EF]) -> Vec + where F: Field, + EF: ExtensionField, + { + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = challenges.split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect("Wrong number of periodic values"); + vec![ + (((alpha + beta_challenges[0]) * EF::from(main_current[0].clone()) + EF::ONE - EF::from(main_current[0].clone())) * EF::from(aux_current[0].clone())) * ((alpha + beta_challenges[0]) * (EF::from(main_current[0].clone()) - EF::ONE) + EF::ONE - (EF::from(main_current[0].clone()) - EF::ONE)).inverse(), + ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(aux_current[1].clone()) + (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(main_current[0].clone()) + (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(main_current[0].clone()) - (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()).double()) * ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double())).inverse(), + ] + } +} \ No newline at end of file diff --git a/air-script/tests/buses/buses_varlen_boundary_first.air b/air-script/src/tests/buses/buses_varlen_boundary_first.air similarity index 100% rename from air-script/tests/buses/buses_varlen_boundary_first.air rename to air-script/src/tests/buses/buses_varlen_boundary_first.air diff --git a/air-script/tests/buses/buses_varlen_boundary_first.rs b/air-script/src/tests/buses/buses_varlen_boundary_first.rs similarity index 100% rename from air-script/tests/buses/buses_varlen_boundary_first.rs rename to air-script/src/tests/buses/buses_varlen_boundary_first.rs diff --git a/air-script/src/tests/buses/buses_varlen_boundary_first_plonky3.rs b/air-script/src/tests/buses/buses_varlen_boundary_first_plonky3.rs new file mode 100644 index 000000000..b32d738e1 --- /dev/null +++ b/air-script/src/tests/buses/buses_varlen_boundary_first_plonky3.rs @@ -0,0 +1,138 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 1; +pub const AUX_WIDTH: usize = 2; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 0; +pub const MAX_BETA_CHALLENGE_POWER: usize = 2; + +pub struct BusesAir; + +impl MidenAir for BusesAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_randomness(&self) -> usize { + 1 + MAX_BETA_CHALLENGE_POWER + } + + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn bus_types(&self) -> Vec { + vec![ + BusType::Multiset, + BusType::Logup, + ] + } + + fn build_aux_trace(&self, _main: &RowMajorMatrix, _challenges: &[EF]) -> Option> { + // Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders. + + let num_rows = _main.height(); + let trace_length = num_rows * AUX_WIDTH; + let mut long_trace = EF::zero_vec(trace_length); + let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH); + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + // Initialize first row + let initial_values = Self::buses_initial_values::(); + for j in 0..AUX_WIDTH { + rows[0][j] = initial_values[j]; + } + // Fill subsequent rows using direct access to the rows array + for i in 0..num_rows-1 { + let i_next = (i + 1) % num_rows; + let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail. + let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail. + let main = VerticalPair::new( + RowMajorMatrixView::new_row(&*main_local), + RowMajorMatrixView::new_row(&*main_next), + ); + let periodic_values: [_; NUM_PERIODIC_VALUES] = >::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect("Wrong number of periodic values"); + let prev_row = &rows[i]; + let next_row = Self::buses_transitions::( + &main, + _challenges, + &periodic_values, + prev_row, + ); + for j in 0..AUX_WIDTH { + rows[i+1][j] = next_row[j]; + } + } + let trace_f = trace.flatten_to_base(); + Some(trace_f) + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = ( + aux.row_slice(0).unwrap(), + aux.row_slice(1).unwrap(), + ); + + // Main boundary constraints + + // Main integrity/transition constraints + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext(((alpha.into() + beta_challenges[0].into()) * AB::ExprEF::from(main_current[0].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * AB::ExprEF::from(aux_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into()) * (AB::ExprEF::from(main_current[0].clone().into()) - AB::ExprEF::ONE) + AB::ExprEF::ONE - (AB::ExprEF::from(main_current[0].clone().into()) - AB::ExprEF::ONE)) * AB::ExprEF::from(aux_next[0].clone().into())); + builder.when_transition().assert_zero_ext((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(aux_current[1].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(main_current[0].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(main_current[0].clone().into()) - ((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(aux_next[1].clone().into()) + (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()).double())); + } +} + +impl BusesAir { + fn buses_initial_values() -> Vec + where F: Field, + EF: ExtensionField, + { + vec![ + EF::ZERO, + EF::ZERO, + ] + } + + fn buses_transitions(main: &VerticalPair, RowMajorMatrixView>, challenges: &[EF], periodic_evals: &[F], aux_current: &[EF]) -> Vec + where F: Field, + EF: ExtensionField, + { + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = challenges.split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect("Wrong number of periodic values"); + vec![ + (((alpha + beta_challenges[0]) * EF::from(main_current[0].clone()) + EF::ONE - EF::from(main_current[0].clone())) * EF::from(aux_current[0].clone())) * ((alpha + beta_challenges[0]) * (EF::from(main_current[0].clone()) - EF::ONE) + EF::ONE - (EF::from(main_current[0].clone()) - EF::ONE)).inverse(), + ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(aux_current[1].clone()) + (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(main_current[0].clone()) + (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(main_current[0].clone()) - (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()).double()) * ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double()) * (alpha + beta_challenges[0] + beta_challenges[1].double())).inverse(), + ] + } +} \ No newline at end of file diff --git a/air-script/src/tests/buses/buses_varlen_boundary_last.air b/air-script/src/tests/buses/buses_varlen_boundary_last.air new file mode 100644 index 000000000..39e48144a --- /dev/null +++ b/air-script/src/tests/buses/buses_varlen_boundary_last.air @@ -0,0 +1,30 @@ +def BusesAir + +trace_columns { + main: [a, sp_insert, sp_remove, sq_insert_twice, sq_remove], +} + +buses { + multiset p, + logup q, +} + +public_inputs { + outputs_p: [[2]], + outputs_q: [[2]], +} + +boundary_constraints { + enf p.first = null; + enf q.first = null; + enf p.last = outputs_p; + enf q.last = outputs_q; +} + +integrity_constraints { + p.insert(a) when sp_insert; + p.remove(a) when sp_remove; + q.insert(2, a) when sq_insert_twice; + q.insert(2, a) when sq_insert_twice; + q.remove(2, a) with sq_remove; +} diff --git a/air-script/tests/buses/buses_varlen_boundary_last.rs b/air-script/src/tests/buses/buses_varlen_boundary_last.rs similarity index 52% rename from air-script/tests/buses/buses_varlen_boundary_last.rs rename to air-script/src/tests/buses/buses_varlen_boundary_last.rs index 686604e08..65dbbda92 100644 --- a/air-script/tests/buses/buses_varlen_boundary_last.rs +++ b/air-script/src/tests/buses/buses_varlen_boundary_last.rs @@ -4,32 +4,36 @@ use winter_math::{ExtensionOf, FieldElement, ToElements}; use winter_utils::{ByteWriter, Serializable}; pub struct PublicInputs { - outputs: Vec<[Felt; 2]>, + outputs_p: Vec<[Felt; 2]>, + outputs_q: Vec<[Felt; 2]>, } impl PublicInputs { - pub fn new(outputs: Vec<[Felt; 2]>) -> Self { - Self { outputs } + pub fn new(outputs_p: Vec<[Felt; 2]>, outputs_q: Vec<[Felt; 2]>) -> Self { + Self { outputs_p, outputs_q } } } impl Serializable for PublicInputs { fn write_into(&self, target: &mut W) { - self.outputs.write_into(target); + self.outputs_p.write_into(target); + self.outputs_q.write_into(target); } } impl ToElements for PublicInputs { fn to_elements(&self) -> Vec { let mut elements = Vec::new(); - self.outputs.iter().for_each(|row| elements.extend_from_slice(row)); + self.outputs_p.iter().for_each(|row| elements.extend_from_slice(row)); + self.outputs_q.iter().for_each(|row| elements.extend_from_slice(row)); elements } } pub struct BusesAir { context: AirContext, - outputs: Vec<[Felt; 2]>, + outputs_p: Vec<[Felt; 2]>, + outputs_q: Vec<[Felt; 2]>, } impl BusesAir { @@ -75,7 +79,7 @@ impl Air for BusesAir { fn new(trace_info: TraceInfo, public_inputs: PublicInputs, options: WinterProofOptions) -> Self { let main_degrees = vec![]; - let aux_degrees = vec![TransitionConstraintDegree::new(2), TransitionConstraintDegree::new(1)]; + let aux_degrees = vec![TransitionConstraintDegree::new(3), TransitionConstraintDegree::new(4)]; let num_main_assertions = 0; let num_aux_assertions = 4; @@ -88,7 +92,7 @@ impl Air for BusesAir { options, ) .set_num_transition_exemptions(2); - Self { context, outputs: public_inputs.outputs } + Self { context, outputs_p: public_inputs.outputs_p, outputs_q: public_inputs.outputs_q } } fn get_periodic_column_values(&self) -> Vec> { @@ -102,12 +106,12 @@ impl Air for BusesAir { fn get_aux_assertions>(&self, aux_rand_elements: &AuxRandElements) -> Vec> { let mut result = Vec::new(); - let reduced_outputs_multiset = Self::bus_multiset_boundary_varlen(aux_rand_elements, &self.outputs); - let reduced_outputs_logup = Self::bus_logup_boundary_varlen(aux_rand_elements, &self.outputs); + let reduced_outputs_p_multiset = Self::bus_multiset_boundary_varlen(aux_rand_elements, &self.outputs_p); + let reduced_outputs_q_logup = Self::bus_logup_boundary_varlen(aux_rand_elements, &self.outputs_q); result.push(Assertion::single(0, 0, E::ONE)); - result.push(Assertion::single(0, self.last_step(), reduced_outputs_multiset)); + result.push(Assertion::single(0, self.last_step(), reduced_outputs_p_multiset)); result.push(Assertion::single(1, 0, E::ZERO)); - result.push(Assertion::single(1, self.last_step(), reduced_outputs_logup)); + result.push(Assertion::single(1, self.last_step(), reduced_outputs_q_logup)); result } @@ -124,7 +128,7 @@ impl Air for BusesAir { let main_next = main_frame.next(); let aux_current = aux_frame.current(); let aux_next = aux_frame.next(); - result[0] = ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1]) * E::from(main_current[0]) + E::ONE - E::from(main_current[0])) * aux_current[0] - ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1]) * (E::from(main_current[0]) - E::ONE) + E::ONE - (E::from(main_current[0]) - E::ONE)) * aux_next[0]; - result[1] = (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * aux_current[1] + (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[0]) + (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[0]) - ((aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * aux_next[1] + (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + aux_rand_elements.rand_elements()[1] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[2]) * E::from(Felt::new(2_u64))); + result[0] = ((aux_rand_elements.rand_elements()[0] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[1]) * E::from(main_current[1]) + E::ONE - E::from(main_current[1])) * aux_current[0] - ((aux_rand_elements.rand_elements()[0] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[1]) * E::from(main_current[2]) + E::ONE - E::from(main_current[2])) * aux_next[0]; + result[1] = (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * aux_current[1] + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[3]) + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[3]) - ((aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * aux_next[1] + (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * (aux_rand_elements.rand_elements()[0] + E::from(Felt::new(2_u64)) * aux_rand_elements.rand_elements()[1] + E::from(main_current[0]) * aux_rand_elements.rand_elements()[2]) * E::from(main_current[4])); } } \ No newline at end of file diff --git a/air-script/src/tests/buses/buses_varlen_boundary_last_plonky3.rs b/air-script/src/tests/buses/buses_varlen_boundary_last_plonky3.rs new file mode 100644 index 000000000..dba927c11 --- /dev/null +++ b/air-script/src/tests/buses/buses_varlen_boundary_last_plonky3.rs @@ -0,0 +1,138 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 5; +pub const AUX_WIDTH: usize = 2; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 0; +pub const MAX_BETA_CHALLENGE_POWER: usize = 2; + +pub struct BusesAir; + +impl MidenAir for BusesAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_randomness(&self) -> usize { + 1 + MAX_BETA_CHALLENGE_POWER + } + + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn bus_types(&self) -> Vec { + vec![ + BusType::Multiset, + BusType::Logup, + ] + } + + fn build_aux_trace(&self, _main: &RowMajorMatrix, _challenges: &[EF]) -> Option> { + // Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders. + + let num_rows = _main.height(); + let trace_length = num_rows * AUX_WIDTH; + let mut long_trace = EF::zero_vec(trace_length); + let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH); + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + // Initialize first row + let initial_values = Self::buses_initial_values::(); + for j in 0..AUX_WIDTH { + rows[0][j] = initial_values[j]; + } + // Fill subsequent rows using direct access to the rows array + for i in 0..num_rows-1 { + let i_next = (i + 1) % num_rows; + let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail. + let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail. + let main = VerticalPair::new( + RowMajorMatrixView::new_row(&*main_local), + RowMajorMatrixView::new_row(&*main_next), + ); + let periodic_values: [_; NUM_PERIODIC_VALUES] = >::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect("Wrong number of periodic values"); + let prev_row = &rows[i]; + let next_row = Self::buses_transitions::( + &main, + _challenges, + &periodic_values, + prev_row, + ); + for j in 0..AUX_WIDTH { + rows[i+1][j] = next_row[j]; + } + } + let trace_f = trace.flatten_to_base(); + Some(trace_f) + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = ( + aux.row_slice(0).unwrap(), + aux.row_slice(1).unwrap(), + ); + + // Main boundary constraints + + // Main integrity/transition constraints + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext(((alpha.into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[0].into()) * AB::ExprEF::from(main_current[1].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[1].clone().into())) * AB::ExprEF::from(aux_current[0].clone().into()) - ((alpha.into() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[0].into()) * AB::ExprEF::from(main_current[2].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[2].clone().into())) * AB::ExprEF::from(aux_next[0].clone().into())); + builder.when_transition().assert_zero_ext((alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(aux_current[1].clone().into()) + (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(main_current[3].clone().into()) + (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(main_current[3].clone().into()) - ((alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(aux_next[1].clone().into()) + (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * (alpha.into() + beta_challenges[0].into().double() + AB::ExprEF::from(main_current[0].clone().into()) * beta_challenges[1].into()) * AB::ExprEF::from(main_current[4].clone().into()))); + } +} + +impl BusesAir { + fn buses_initial_values() -> Vec + where F: Field, + EF: ExtensionField, + { + vec![ + EF::ONE, + EF::ZERO, + ] + } + + fn buses_transitions(main: &VerticalPair, RowMajorMatrixView>, challenges: &[EF], periodic_evals: &[F], aux_current: &[EF]) -> Vec + where F: Field, + EF: ExtensionField, + { + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = challenges.split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect("Wrong number of periodic values"); + vec![ + (((alpha + EF::from(main_current[0].clone()) * beta_challenges[0]) * EF::from(main_current[1].clone()) + EF::ONE - EF::from(main_current[1].clone())) * EF::from(aux_current[0].clone())) * ((alpha + EF::from(main_current[0].clone()) * beta_challenges[0]) * EF::from(main_current[2].clone()) + EF::ONE - EF::from(main_current[2].clone())).inverse(), + ((alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * EF::from(aux_current[1].clone()) + (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * EF::from(main_current[3].clone()) + (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * EF::from(main_current[3].clone()) - (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * EF::from(main_current[4].clone())) * ((alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1]) * (alpha + beta_challenges[0].double() + EF::from(main_current[0].clone()) * beta_challenges[1])).inverse(), + ] + } +} \ No newline at end of file diff --git a/air-script/src/tests/buses/mod.rs b/air-script/src/tests/buses/mod.rs new file mode 100644 index 000000000..5c0ca958c --- /dev/null +++ b/air-script/src/tests/buses/mod.rs @@ -0,0 +1,40 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod buses_complex; +#[rustfmt::skip] +#[allow(clippy::all)] +mod buses_simple; +#[rustfmt::skip] +#[allow(clippy::all)] +mod buses_varlen_boundary_both; +#[rustfmt::skip] +#[allow(clippy::all)] +mod buses_varlen_boundary_first; +#[rustfmt::skip] +#[allow(clippy::all)] +mod buses_varlen_boundary_last; + +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod buses_complex_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod buses_simple_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod buses_varlen_boundary_both_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod buses_varlen_boundary_first_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod buses_varlen_boundary_last_plonky3; + +mod test_air_plonky3; +mod test_air_plonky3_varlen_boundary_last; +mod test_air_winterfell; diff --git a/air-script/src/tests/buses/test_air_plonky3.rs b/air-script/src/tests/buses/test_air_plonky3.rs new file mode 100644 index 000000000..676d87701 --- /dev/null +++ b/air-script/src/tests/buses/test_air_plonky3.rs @@ -0,0 +1,62 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::buses::buses_complex_plonky3::{BusesAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let a_prev = rows[i - 1][0]; + let b_prev = rows[i - 1][1]; + let c_prev = rows[i - 1][2]; + let d_prev = rows[i - 1][3]; + let e_prev = rows[i - 1][4]; + let f_prev = rows[i - 1][5]; + let g_prev = rows[i - 1][6]; + + // Update current row based on previous values + rows[i][0] = F::ZERO; + rows[i][1] = F::ZERO; + rows[i][2] = if i > 3 && i < 8 { F::ONE } else { F::ZERO }; // s1 is true 4 times + rows[i][3] = if i > 5 && i < 10 { F::ONE } else { F::ZERO }; // s2 is true 4 times + rows[i][4] = if i > 4 && i < 10 { F::ONE } else { F::ZERO }; // s3 is true 5 times + rows[i][5] = if i > 5 && i < 13 { F::ONE } else { F::ZERO }; // s4 is true 7 times + rows[i][6] = if i > 15 && i < 20 { F::from_u64(3) } else { F::ZERO }; // d is set to 3 four times + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 2] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![vec![], vec![], vec![]] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, BusesAir); diff --git a/air-script/src/tests/buses/test_air_plonky3_varlen_boundary_last.rs b/air-script/src/tests/buses/test_air_plonky3_varlen_boundary_last.rs new file mode 100644 index 000000000..6415f669e --- /dev/null +++ b/air-script/src/tests/buses/test_air_plonky3_varlen_boundary_last.rs @@ -0,0 +1,64 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::buses::buses_varlen_boundary_last_plonky3::{BusesAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ONE; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let a_prev = rows[i - 1][0]; + let b_prev = rows[i - 1][1]; + let c_prev = rows[i - 1][2]; + let d_prev = rows[i - 1][3]; + let e_prev = rows[i - 1][4]; + + // Update current row based on previous values + rows[i][0] = F::ONE; + rows[i][1] = if i > 3 && i < 8 { F::ONE } else { F::ZERO }; // sp_insert is true 4 times + rows[i][2] = if i > 3 && i < 7 { F::ONE } else { F::ZERO }; // sp_remove is true 3 times + rows[i][3] = if i > 4 && i < 10 { F::ONE } else { F::ZERO }; // sq_insert_twice is true 5 times + rows[i][4] = if i > 5 && i < 10 { + F::from_canonical_checked(2).unwrap() + } else { + F::ZERO + }; // sq_remove has value "2" 4 times + } + + trace +} + +fn generate_inputs() -> Vec { + vec![] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + // At the end, the bus p will have the tuple (a) (that equals (1)) inserted once + let var_len_p = vec![vec![1]]; + // At the end, the bus q will have the tuple (2, a) (that equals (2, 1)) inserted twice + let var_len_q = vec![vec![2, 1], vec![2, 1]]; + vec![var_len_p, var_len_q] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, BusesAir); diff --git a/air-script/tests/buses/test_air.rs b/air-script/src/tests/buses/test_air_winterfell.rs similarity index 78% rename from air-script/tests/buses/test_air.rs rename to air-script/src/tests/buses/test_air_winterfell.rs index 4c27304a2..150617152 100644 --- a/air-script/tests/buses/test_air.rs +++ b/air-script/src/tests/buses/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{AuxTraceWithMetadata, Trace, TraceTable, matrix::ColMatrix}; use crate::{ - buses::buses_complex::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::buses::buses_complex::PublicInputs, }; #[derive(Clone)] @@ -15,7 +15,7 @@ impl AirTester for BusesAirTester { type PubInputs = PublicInputs; fn build_main_trace(&self, length: usize) -> MyTraceTable { - let trace_width = 5; + let trace_width = 7; let start = Felt::new(0); let mut trace = TraceTable::new(trace_width, length); @@ -26,6 +26,8 @@ impl AirTester for BusesAirTester { state[2] = start; state[3] = start; state[4] = start; + state[5] = start; + state[6] = start; }, |_, state| { state[0] = Felt::new(1) - state[0]; @@ -33,6 +35,8 @@ impl AirTester for BusesAirTester { state[2] = Felt::new(1) - state[2]; state[3] = Felt::new(1) - state[3]; state[4] = Felt::new(1) - state[4]; + state[5] = Felt::new(1) - state[4]; + state[6] = Felt::new(1) - state[4]; }, ); @@ -57,4 +61,9 @@ impl AirTester for BusesAirTester { } } -generate_air_test!(test_buses_air, crate::buses::buses_complex::BusesAir, BusesAirTester, 1024); +generate_air_winterfell_test!( + test_buses_air, + crate::tests::buses::buses_complex::BusesAir, + BusesAirTester, + 1024 +); diff --git a/air-script/tests/computed_indices/computed_indices_complex.air b/air-script/src/tests/computed_indices/computed_indices_complex.air similarity index 100% rename from air-script/tests/computed_indices/computed_indices_complex.air rename to air-script/src/tests/computed_indices/computed_indices_complex.air diff --git a/air-script/tests/computed_indices/computed_indices_complex.rs b/air-script/src/tests/computed_indices/computed_indices_complex.rs similarity index 100% rename from air-script/tests/computed_indices/computed_indices_complex.rs rename to air-script/src/tests/computed_indices/computed_indices_complex.rs diff --git a/air-script/src/tests/computed_indices/computed_indices_complex_plonky3.rs b/air-script/src/tests/computed_indices/computed_indices_complex_plonky3.rs new file mode 100644 index 000000000..6b87e196b --- /dev/null +++ b/air-script/src/tests/computed_indices/computed_indices_complex_plonky3.rs @@ -0,0 +1,42 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 1; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ComputedIndicesAir; + +impl MidenAir for ComputedIndicesAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[2].clone().into() * AB::Expr::from_u64(3) + main_current[3].clone().into() * AB::Expr::from_u64(4)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/computed_indices/computed_indices_simple.air b/air-script/src/tests/computed_indices/computed_indices_simple.air similarity index 100% rename from air-script/tests/computed_indices/computed_indices_simple.air rename to air-script/src/tests/computed_indices/computed_indices_simple.air diff --git a/air-script/tests/computed_indices/computed_indices_simple.rs b/air-script/src/tests/computed_indices/computed_indices_simple.rs similarity index 100% rename from air-script/tests/computed_indices/computed_indices_simple.rs rename to air-script/src/tests/computed_indices/computed_indices_simple.rs diff --git a/air-script/src/tests/computed_indices/computed_indices_simple_plonky3.rs b/air-script/src/tests/computed_indices/computed_indices_simple_plonky3.rs new file mode 100644 index 000000000..1ab566b59 --- /dev/null +++ b/air-script/src/tests/computed_indices/computed_indices_simple_plonky3.rs @@ -0,0 +1,49 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 8; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ComputedIndicesAir; + +impl MidenAir for ComputedIndicesAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into()); + builder.assert_zero(main_current[1].clone().into() - AB::Expr::from_u64(2)); + builder.assert_zero(main_current[2].clone().into() - AB::Expr::from_u64(4)); + builder.assert_zero(main_current[3].clone().into() - AB::Expr::from_u64(6)); + builder.when_transition().assert_zero(main_next[4].clone().into()); + builder.when_transition().assert_zero(main_next[5].clone().into() - main_current[5].clone().into().double()); + builder.when_transition().assert_zero(main_next[6].clone().into() - AB::Expr::from_u64(6) * main_current[6].clone().into()); + builder.when_transition().assert_zero(main_next[7].clone().into() - AB::Expr::from_u64(12) * main_current[7].clone().into()); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/computed_indices/mod.rs b/air-script/src/tests/computed_indices/mod.rs new file mode 100644 index 000000000..948afee49 --- /dev/null +++ b/air-script/src/tests/computed_indices/mod.rs @@ -0,0 +1,18 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod computed_indices_complex; +#[rustfmt::skip] +#[allow(clippy::all)] +mod computed_indices_simple; + +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod computed_indices_complex_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod computed_indices_simple_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/computed_indices/test_air_plonky3.rs b/air-script/src/tests/computed_indices/test_air_plonky3.rs new file mode 100644 index 000000000..0ebed55a3 --- /dev/null +++ b/air-script/src/tests/computed_indices/test_air_plonky3.rs @@ -0,0 +1,65 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::computed_indices::computed_indices_simple_plonky3::{ComputedIndicesAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::from_canonical_checked(2).unwrap(); + rows[0][2] = F::from_canonical_checked(4).unwrap(); + rows[0][3] = F::from_canonical_checked(6).unwrap(); + rows[0][4] = F::ZERO; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + rows[0][7] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + rows[i][4] = col_4_prev * F::ZERO; + rows[i][5] = col_5_prev * F::from_canonical_checked(2).unwrap(); + rows[i][6] = col_6_prev * F::from_canonical_checked(6).unwrap(); + rows[i][7] = col_7_prev * F::from_canonical_checked(12).unwrap(); + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, ComputedIndicesAir); diff --git a/air-script/tests/computed_indices/test_air.rs b/air-script/src/tests/computed_indices/test_air_winterfell.rs similarity index 63% rename from air-script/tests/computed_indices/test_air.rs rename to air-script/src/tests/computed_indices/test_air_winterfell.rs index 5a953f6e5..1f7e35ed3 100644 --- a/air-script/tests/computed_indices/test_air.rs +++ b/air-script/src/tests/computed_indices/test_air_winterfell.rs @@ -3,8 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - computed_indices::computed_indices_simple::{ComputedIndicesAir, PublicInputs}, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::computed_indices::computed_indices_simple::PublicInputs, }; #[derive(Clone)] @@ -45,17 +46,9 @@ impl AirTester for ComputedIndicesAirTester { } } -#[test] -fn test_computed_indices_air() { - let air_tester = Box::new(ComputedIndicesAirTester {}); - let length = 1024; - - let main_trace = air_tester.build_main_trace(length); - let aux_trace = air_tester.build_aux_trace(length); - let pub_inputs = air_tester.public_inputs(); - let trace_info = air_tester.build_trace_info(length); - let options = air_tester.build_proof_options(); - - let air = ComputedIndicesAir::new(trace_info, pub_inputs, options); - main_trace.validate::(&air, aux_trace.as_ref()); -} +generate_air_winterfell_test!( + test_computed_indices_air, + crate::tests::computed_indices::computed_indices_simple::ComputedIndicesAir, + ComputedIndicesAirTester, + 1024 +); diff --git a/air-script/tests/constant_in_range/constant_in_range.air b/air-script/src/tests/constant_in_range/constant_in_range.air similarity index 100% rename from air-script/tests/constant_in_range/constant_in_range.air rename to air-script/src/tests/constant_in_range/constant_in_range.air diff --git a/air-script/tests/constant_in_range/constant_in_range.rs b/air-script/src/tests/constant_in_range/constant_in_range.rs similarity index 100% rename from air-script/tests/constant_in_range/constant_in_range.rs rename to air-script/src/tests/constant_in_range/constant_in_range.rs diff --git a/air-script/tests/constant_in_range/constant_in_range_module.air b/air-script/src/tests/constant_in_range/constant_in_range_module.air similarity index 100% rename from air-script/tests/constant_in_range/constant_in_range_module.air rename to air-script/src/tests/constant_in_range/constant_in_range_module.air diff --git a/air-script/src/tests/constant_in_range/constant_in_range_plonky3.rs b/air-script/src/tests/constant_in_range/constant_in_range_plonky3.rs new file mode 100644 index 000000000..134d141eb --- /dev/null +++ b/air-script/src/tests/constant_in_range/constant_in_range_plonky3.rs @@ -0,0 +1,42 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 12; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ConstantInRangeAir; + +impl MidenAir for ConstantInRangeAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[6].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() - (main_current[1].clone().into() - main_current[4].clone().into() - main_current[8].clone().into() + AB::Expr::ONE + main_current[2].clone().into() - main_current[5].clone().into() - main_current[9].clone().into() + AB::Expr::from_u64(2) + main_current[3].clone().into() - main_current[6].clone().into() - main_current[10].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/constant_in_range/mod.rs b/air-script/src/tests/constant_in_range/mod.rs new file mode 100644 index 000000000..68a0d2cbd --- /dev/null +++ b/air-script/src/tests/constant_in_range/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod constant_in_range; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod constant_in_range_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/constant_in_range/test_air_plonky3.rs b/air-script/src/tests/constant_in_range/test_air_plonky3.rs new file mode 100644 index 000000000..e1959450e --- /dev/null +++ b/air-script/src/tests/constant_in_range/test_air_plonky3.rs @@ -0,0 +1,66 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::constant_in_range::constant_in_range_plonky3::{ConstantInRangeAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::from_canonical_checked(3).unwrap(); + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + let col_8_prev = rows[i - 1][8]; + let col_9_prev = rows[i - 1][9]; + let col_10_prev = rows[i - 1][10]; + let col_11_prev = rows[i - 1][11]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + rows[i][7] = col_7_prev; + rows[i][8] = col_8_prev; + rows[i][9] = col_9_prev; + rows[i][10] = col_10_prev; + rows[i][11] = col_11_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, ConstantInRangeAir); diff --git a/air-script/tests/constant_in_range/test_air.rs b/air-script/src/tests/constant_in_range/test_air_winterfell.rs similarity index 81% rename from air-script/tests/constant_in_range/test_air.rs rename to air-script/src/tests/constant_in_range/test_air_winterfell.rs index 064e5eb19..c08fa18ab 100644 --- a/air-script/tests/constant_in_range/test_air.rs +++ b/air-script/src/tests/constant_in_range/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - constant_in_range::constant_in_range::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::constant_in_range::constant_in_range::PublicInputs, }; #[derive(Clone)] @@ -46,9 +46,9 @@ impl AirTester for ConstantInRangeAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_constant_in_range_air, - crate::constant_in_range::constant_in_range::ConstantInRangeAir, + crate::tests::constant_in_range::constant_in_range::ConstantInRangeAir, ConstantInRangeAirTester, 1024 ); diff --git a/air-script/tests/constants/constants.air b/air-script/src/tests/constants/constants.air similarity index 100% rename from air-script/tests/constants/constants.air rename to air-script/src/tests/constants/constants.air diff --git a/air-script/tests/constants/constants.rs b/air-script/src/tests/constants/constants.rs similarity index 100% rename from air-script/tests/constants/constants.rs rename to air-script/src/tests/constants/constants.rs diff --git a/air-script/src/tests/constants/constants_plonky3.rs b/air-script/src/tests/constants/constants_plonky3.rs new file mode 100644 index 000000000..72218cfbb --- /dev/null +++ b/air-script/src/tests/constants/constants_plonky3.rs @@ -0,0 +1,51 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 7; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 32; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ConstantsAir; + +impl MidenAir for ConstantsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into() - AB::Expr::ONE); + builder.when_first_row().assert_zero(main_current[1].clone().into() - AB::Expr::ONE); + builder.when_first_row().assert_zero(main_current[2].clone().into()); + builder.when_first_row().assert_zero(main_current[3].clone().into() - AB::Expr::ONE); + builder.when_first_row().assert_zero(main_current[4].clone().into() - AB::Expr::ONE); + builder.when_last_row().assert_zero(main_current[6].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[0].clone().into() - (main_current[0].clone().into() + AB::Expr::ONE)); + builder.when_transition().assert_zero(main_next[1].clone().into()); + builder.when_transition().assert_zero(main_next[2].clone().into() - main_current[2].clone().into()); + builder.when_transition().assert_zero(main_next[5].clone().into() - (main_current[5].clone().into() + AB::Expr::ONE)); + builder.assert_zero(main_current[4].clone().into() - AB::Expr::ONE); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/constants/mod.rs b/air-script/src/tests/constants/mod.rs new file mode 100644 index 000000000..1540ba337 --- /dev/null +++ b/air-script/src/tests/constants/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod constants; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod constants_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/constants/test_air_plonky3.rs b/air-script/src/tests/constants/test_air_plonky3.rs new file mode 100644 index 000000000..5c906ff3b --- /dev/null +++ b/air-script/src/tests/constants/test_air_plonky3.rs @@ -0,0 +1,62 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::constants::constants_plonky3::{ConstantsAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ONE; + rows[0][1] = F::ONE; + rows[0][2] = F::ZERO; + rows[0][3] = F::ONE; + rows[0][4] = F::ONE; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + + // Update current row based on previous values + rows[i][0] = col_0_prev + F::ONE; + rows[i][1] = F::ZERO; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev + F::ONE; + rows[i][6] = col_6_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 32] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, ConstantsAir); diff --git a/air-script/tests/constants/test_air.rs b/air-script/src/tests/constants/test_air_winterfell.rs similarity index 83% rename from air-script/tests/constants/test_air.rs rename to air-script/src/tests/constants/test_air_winterfell.rs index 97a048a31..926c8c77c 100644 --- a/air-script/tests/constants/test_air.rs +++ b/air-script/src/tests/constants/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - constants::constants::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::constants::constants::PublicInputs, }; #[derive(Clone)] @@ -44,9 +44,9 @@ impl AirTester for ConstantsAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_constants_air, - crate::constants::constants::ConstantsAir, + crate::tests::constants::constants::ConstantsAir, ConstantsAirTester, 1024 ); diff --git a/air-script/tests/constraint_comprehension/cc_with_evaluators.air b/air-script/src/tests/constraint_comprehension/cc_with_evaluators.air similarity index 100% rename from air-script/tests/constraint_comprehension/cc_with_evaluators.air rename to air-script/src/tests/constraint_comprehension/cc_with_evaluators.air diff --git a/air-script/tests/constraint_comprehension/constraint_comprehension.air b/air-script/src/tests/constraint_comprehension/constraint_comprehension.air similarity index 100% rename from air-script/tests/constraint_comprehension/constraint_comprehension.air rename to air-script/src/tests/constraint_comprehension/constraint_comprehension.air diff --git a/air-script/tests/constraint_comprehension/constraint_comprehension.rs b/air-script/src/tests/constraint_comprehension/constraint_comprehension.rs similarity index 100% rename from air-script/tests/constraint_comprehension/constraint_comprehension.rs rename to air-script/src/tests/constraint_comprehension/constraint_comprehension.rs diff --git a/air-script/src/tests/constraint_comprehension/constraint_comprehension_plonky3.rs b/air-script/src/tests/constraint_comprehension/constraint_comprehension_plonky3.rs new file mode 100644 index 000000000..b34c6181b --- /dev/null +++ b/air-script/src/tests/constraint_comprehension/constraint_comprehension_plonky3.rs @@ -0,0 +1,45 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 14; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ConstraintComprehensionAir; + +impl MidenAir for ConstraintComprehensionAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[8].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[6].clone().into() - main_current[10].clone().into()); + builder.assert_zero(main_current[7].clone().into() - main_current[11].clone().into()); + builder.assert_zero(main_current[8].clone().into() - main_current[12].clone().into()); + builder.assert_zero(main_current[9].clone().into() - main_current[13].clone().into()); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/constraint_comprehension/mod.rs b/air-script/src/tests/constraint_comprehension/mod.rs new file mode 100644 index 000000000..960d9a5a6 --- /dev/null +++ b/air-script/src/tests/constraint_comprehension/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod constraint_comprehension; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod constraint_comprehension_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/constraint_comprehension/test_air_plonky3.rs b/air-script/src/tests/constraint_comprehension/test_air_plonky3.rs new file mode 100644 index 000000000..09f942eb3 --- /dev/null +++ b/air-script/src/tests/constraint_comprehension/test_air_plonky3.rs @@ -0,0 +1,49 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::constraint_comprehension::constraint_comprehension_plonky3::{ + ConstraintComprehensionAir, MAIN_WIDTH, + }, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::from_canonical_checked(inputs[0]).unwrap(); + rows[0][1] = F::ONE; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let a_prev = rows[i - 1][0]; + let b_prev = rows[i - 1][1]; + + // Update current row based on previous values + rows[i][0] = F::ONE - a_prev; + rows[i][1] = F::ONE - b_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, ConstraintComprehensionAir); diff --git a/air-script/tests/constraint_comprehension/test_air.rs b/air-script/src/tests/constraint_comprehension/test_air_winterfell.rs similarity index 81% rename from air-script/tests/constraint_comprehension/test_air.rs rename to air-script/src/tests/constraint_comprehension/test_air_winterfell.rs index 6bdb9cc8d..d4bec18d1 100644 --- a/air-script/tests/constraint_comprehension/test_air.rs +++ b/air-script/src/tests/constraint_comprehension/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - constraint_comprehension::constraint_comprehension::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::constraint_comprehension::constraint_comprehension::PublicInputs, }; #[derive(Clone)] @@ -48,9 +48,9 @@ impl AirTester for ConstraintComprehensionAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_constraint_comprehension_air, - crate::constraint_comprehension::constraint_comprehension::ConstraintComprehensionAir, + crate::tests::constraint_comprehension::constraint_comprehension::ConstraintComprehensionAir, ConstraintComprehensionAirTester, 1024 ); diff --git a/air-script/tests/docs_sync.rs b/air-script/src/tests/docs_sync.rs similarity index 100% rename from air-script/tests/docs_sync.rs rename to air-script/src/tests/docs_sync.rs diff --git a/air-script/tests/evaluators/evaluators.air b/air-script/src/tests/evaluators/evaluators.air similarity index 100% rename from air-script/tests/evaluators/evaluators.air rename to air-script/src/tests/evaluators/evaluators.air diff --git a/air-script/tests/evaluators/evaluators.rs b/air-script/src/tests/evaluators/evaluators.rs similarity index 100% rename from air-script/tests/evaluators/evaluators.rs rename to air-script/src/tests/evaluators/evaluators.rs diff --git a/air-script/tests/evaluators/evaluators_nested_slice_call.air b/air-script/src/tests/evaluators/evaluators_nested_slice_call.air similarity index 100% rename from air-script/tests/evaluators/evaluators_nested_slice_call.air rename to air-script/src/tests/evaluators/evaluators_nested_slice_call.air diff --git a/air-script/tests/evaluators/evaluators_nested_slice_call.rs b/air-script/src/tests/evaluators/evaluators_nested_slice_call.rs similarity index 100% rename from air-script/tests/evaluators/evaluators_nested_slice_call.rs rename to air-script/src/tests/evaluators/evaluators_nested_slice_call.rs diff --git a/air-script/src/tests/evaluators/evaluators_nested_slice_call_plonky3.rs b/air-script/src/tests/evaluators/evaluators_nested_slice_call_plonky3.rs new file mode 100644 index 000000000..45e132b6c --- /dev/null +++ b/air-script/src/tests/evaluators/evaluators_nested_slice_call_plonky3.rs @@ -0,0 +1,65 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 20; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct EvaluatorsSliceAir; + +impl MidenAir for EvaluatorsSliceAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + builder.when_first_row().assert_zero(main_current[1].clone().into()); + builder.when_first_row().assert_zero(main_current[2].clone().into()); + builder.when_first_row().assert_zero(main_current[3].clone().into()); + builder.when_first_row().assert_zero(main_current[4].clone().into()); + builder.when_first_row().assert_zero(main_current[5].clone().into()); + builder.when_first_row().assert_zero(main_current[6].clone().into()); + builder.when_first_row().assert_zero(main_current[7].clone().into()); + builder.when_first_row().assert_zero(main_current[8].clone().into()); + builder.when_first_row().assert_zero(main_current[9].clone().into()); + builder.when_first_row().assert_zero(main_current[10].clone().into()); + builder.when_first_row().assert_zero(main_current[11].clone().into()); + builder.when_first_row().assert_zero(main_current[12].clone().into()); + builder.when_first_row().assert_zero(main_current[13].clone().into()); + builder.when_first_row().assert_zero(main_current[14].clone().into()); + builder.when_first_row().assert_zero(main_current[15].clone().into()); + builder.when_first_row().assert_zero(main_current[16].clone().into()); + builder.when_first_row().assert_zero(main_current[17].clone().into()); + builder.when_first_row().assert_zero(main_current[18].clone().into()); + builder.when_first_row().assert_zero(main_current[19].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[5].clone().into() * main_current[5].clone().into() - main_current[5].clone().into()); + builder.assert_zero(main_current[5].clone().into() * (main_current[6].clone().into() * main_current[6].clone().into() - main_current[6].clone().into())); + builder.assert_zero(main_current[5].clone().into() * main_current[6].clone().into() * (main_current[7].clone().into() * main_current[7].clone().into() - main_current[7].clone().into())); + builder.assert_zero(main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() * (main_current[8].clone().into() * main_current[8].clone().into() - main_current[8].clone().into())); + builder.assert_zero(main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() * main_current[8].clone().into() * (main_current[9].clone().into() * main_current[9].clone().into() - main_current[9].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/evaluators/evaluators_plonky3.rs b/air-script/src/tests/evaluators/evaluators_plonky3.rs new file mode 100644 index 000000000..5c0ad3895 --- /dev/null +++ b/air-script/src/tests/evaluators/evaluators_plonky3.rs @@ -0,0 +1,51 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 7; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct EvaluatorsAir; + +impl MidenAir for EvaluatorsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[0].clone().into() - main_current[0].clone().into()); + builder.when_transition().assert_zero(main_next[2].clone().into() - main_current[2].clone().into()); + builder.when_transition().assert_zero(main_next[6].clone().into() - main_current[6].clone().into()); + builder.assert_zero(main_current[0].clone().into() * main_current[0].clone().into() - main_current[0].clone().into()); + builder.assert_zero(main_current[1].clone().into() * main_current[1].clone().into() - main_current[1].clone().into()); + builder.assert_zero(main_current[2].clone().into() * main_current[2].clone().into() - main_current[2].clone().into()); + builder.assert_zero(main_current[3].clone().into() * main_current[3].clone().into() - main_current[3].clone().into()); + builder.assert_zero(main_current[4].clone().into()); + builder.assert_zero(main_current[5].clone().into() - AB::Expr::ONE); + builder.assert_zero(main_current[6].clone().into() - AB::Expr::from_u64(4)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/evaluators/evaluators_slice.air b/air-script/src/tests/evaluators/evaluators_slice.air similarity index 100% rename from air-script/tests/evaluators/evaluators_slice.air rename to air-script/src/tests/evaluators/evaluators_slice.air diff --git a/air-script/tests/evaluators/evaluators_slice.rs b/air-script/src/tests/evaluators/evaluators_slice.rs similarity index 100% rename from air-script/tests/evaluators/evaluators_slice.rs rename to air-script/src/tests/evaluators/evaluators_slice.rs diff --git a/air-script/src/tests/evaluators/evaluators_slice_plonky3.rs b/air-script/src/tests/evaluators/evaluators_slice_plonky3.rs new file mode 100644 index 000000000..cd19cef43 --- /dev/null +++ b/air-script/src/tests/evaluators/evaluators_slice_plonky3.rs @@ -0,0 +1,65 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 20; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct EvaluatorsSliceAir; + +impl MidenAir for EvaluatorsSliceAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + builder.when_first_row().assert_zero(main_current[1].clone().into()); + builder.when_first_row().assert_zero(main_current[2].clone().into()); + builder.when_first_row().assert_zero(main_current[3].clone().into()); + builder.when_first_row().assert_zero(main_current[4].clone().into()); + builder.when_first_row().assert_zero(main_current[5].clone().into()); + builder.when_first_row().assert_zero(main_current[6].clone().into()); + builder.when_first_row().assert_zero(main_current[7].clone().into()); + builder.when_first_row().assert_zero(main_current[8].clone().into()); + builder.when_first_row().assert_zero(main_current[9].clone().into()); + builder.when_first_row().assert_zero(main_current[10].clone().into()); + builder.when_first_row().assert_zero(main_current[11].clone().into()); + builder.when_first_row().assert_zero(main_current[12].clone().into()); + builder.when_first_row().assert_zero(main_current[13].clone().into()); + builder.when_first_row().assert_zero(main_current[14].clone().into()); + builder.when_first_row().assert_zero(main_current[15].clone().into()); + builder.when_first_row().assert_zero(main_current[16].clone().into()); + builder.when_first_row().assert_zero(main_current[17].clone().into()); + builder.when_first_row().assert_zero(main_current[18].clone().into()); + builder.when_first_row().assert_zero(main_current[19].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() * main_current[0].clone().into() - main_current[0].clone().into()); + builder.assert_zero(main_current[0].clone().into() * (main_current[1].clone().into() * main_current[1].clone().into() - main_current[1].clone().into())); + builder.assert_zero(main_current[0].clone().into() * main_current[1].clone().into() * (main_current[2].clone().into() * main_current[2].clone().into() - main_current[2].clone().into())); + builder.assert_zero(main_current[0].clone().into() * main_current[1].clone().into() * main_current[2].clone().into() * (main_current[3].clone().into() * main_current[3].clone().into() - main_current[3].clone().into())); + builder.assert_zero(main_current[0].clone().into() * main_current[1].clone().into() * main_current[2].clone().into() * main_current[3].clone().into() * (main_current[4].clone().into() * main_current[4].clone().into() - main_current[4].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/evaluators/evaluators_slice_slicing.air b/air-script/src/tests/evaluators/evaluators_slice_slicing.air similarity index 100% rename from air-script/tests/evaluators/evaluators_slice_slicing.air rename to air-script/src/tests/evaluators/evaluators_slice_slicing.air diff --git a/air-script/src/tests/evaluators/mod.rs b/air-script/src/tests/evaluators/mod.rs new file mode 100644 index 000000000..8513a7ba3 --- /dev/null +++ b/air-script/src/tests/evaluators/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod evaluators; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod evaluators_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/evaluators/test_air_plonky3.rs b/air-script/src/tests/evaluators/test_air_plonky3.rs new file mode 100644 index 000000000..1d70dcb45 --- /dev/null +++ b/air-script/src/tests/evaluators/test_air_plonky3.rs @@ -0,0 +1,62 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::evaluators::evaluators_plonky3::{EvaluatorsAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + rows[0][5] = F::ONE; + rows[0][6] = F::from_canonical_checked(4).unwrap(); + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, EvaluatorsAir); diff --git a/air-script/tests/evaluators/test_air.rs b/air-script/src/tests/evaluators/test_air_winterfell.rs similarity index 81% rename from air-script/tests/evaluators/test_air.rs rename to air-script/src/tests/evaluators/test_air_winterfell.rs index 6bf42ed83..bbaeb5ecd 100644 --- a/air-script/tests/evaluators/test_air.rs +++ b/air-script/src/tests/evaluators/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::{FieldElement, fields::f64::BaseElement as Felt}; use winterfell::{Trace, TraceTable}; use crate::{ - evaluators::evaluators::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::evaluators::evaluators::PublicInputs, }; #[derive(Clone)] @@ -41,9 +41,9 @@ impl AirTester for EvaluatorsAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_evaluators_air, - crate::evaluators::evaluators::EvaluatorsAir, + crate::tests::evaluators::evaluators::EvaluatorsAir, EvaluatorsAirTester, 1024 ); diff --git a/air-script/tests/fibonacci/fibonacci.air b/air-script/src/tests/fibonacci/fibonacci.air similarity index 100% rename from air-script/tests/fibonacci/fibonacci.air rename to air-script/src/tests/fibonacci/fibonacci.air diff --git a/air-script/tests/fibonacci/fibonacci.rs b/air-script/src/tests/fibonacci/fibonacci.rs similarity index 100% rename from air-script/tests/fibonacci/fibonacci.rs rename to air-script/src/tests/fibonacci/fibonacci.rs diff --git a/air-script/src/tests/fibonacci/fibonacci_plonky3.rs b/air-script/src/tests/fibonacci/fibonacci_plonky3.rs new file mode 100644 index 000000000..15e40b79d --- /dev/null +++ b/air-script/src/tests/fibonacci/fibonacci_plonky3.rs @@ -0,0 +1,45 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 2; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 3; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct FibonacciAir; + +impl MidenAir for FibonacciAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into() - public_values[0].into()); + builder.when_first_row().assert_zero(main_current[1].clone().into() - public_values[1].into()); + builder.when_last_row().assert_zero(main_current[1].clone().into() - public_values[2].into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[1].clone().into() - (main_current[0].clone().into() + main_current[1].clone().into())); + builder.when_transition().assert_zero(main_next[0].clone().into() - main_current[1].clone().into()); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/fibonacci/mod.rs b/air-script/src/tests/fibonacci/mod.rs new file mode 100644 index 000000000..57c53c30e --- /dev/null +++ b/air-script/src/tests/fibonacci/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod fibonacci; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod fibonacci_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/fibonacci/test_air_plonky3.rs b/air-script/src/tests/fibonacci/test_air_plonky3.rs new file mode 100644 index 000000000..e41da1047 --- /dev/null +++ b/air-script/src/tests/fibonacci/test_air_plonky3.rs @@ -0,0 +1,79 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::fibonacci::fibonacci_plonky3::{FibonacciAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::from_canonical_checked(inputs[0]).unwrap(); + rows[0][1] = F::from_canonical_checked(inputs[1]).unwrap(); + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let cur_a = rows[i - 1][0]; + let cur_b = rows[i - 1][1]; + + // Update current row based on previous values + rows[i][0] = cur_b; + rows[i][1] = cur_a + cur_b; + } + + trace +} + +fn generate_inputs() -> Vec { + let zero = 0; + let one = 1; + let last = fibonacci_field::(512).as_canonical_u64(); // 512nd Fibonacci number in Goldilock's field + vec![zero, one, last] +} + +fn fibonacci_field(n: i32) -> F { + if n < 0 { + panic!("{} is negative!", n); + } else if n == 0 { + return F::ZERO; + } else if n == 1 { + return F::ONE; + } + + let mut sum = F::ZERO; + let mut last = F::ZERO; + let mut curr = F::ONE; + for _i in 1..n { + sum = last + curr; + last = curr; + curr = sum; + } + sum +} + +#[test] +fn test_goldilocks_fibonacci_computation() { + type F = p3_goldilocks::Goldilocks; + let f_32 = fibonacci_field::(32); + let f_512 = fibonacci_field::(512); + assert_eq!(f_32, F::new(2178309)); + assert_eq!(f_512, F::new(12556846397060607923)); +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, FibonacciAir); diff --git a/air-script/tests/fibonacci/test_air.rs b/air-script/src/tests/fibonacci/test_air_winterfell.rs similarity index 84% rename from air-script/tests/fibonacci/test_air.rs rename to air-script/src/tests/fibonacci/test_air_winterfell.rs index e71a8dc1f..9d2313823 100644 --- a/air-script/tests/fibonacci/test_air.rs +++ b/air-script/src/tests/fibonacci/test_air_winterfell.rs @@ -5,9 +5,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{AuxTraceWithMetadata, Trace, TraceTable, matrix::ColMatrix}; use crate::{ - fibonacci::fibonacci::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::fibonacci::fibonacci::PublicInputs, }; #[derive(Clone)] @@ -30,8 +30,8 @@ impl AirTester for FibonacciAirTester { |_, state| { let cur_a = state[0]; let cur_b = state[1]; - state[1] = cur_a + cur_b; state[0] = cur_b; + state[1] = cur_a + cur_b; }, ); @@ -45,9 +45,9 @@ impl AirTester for FibonacciAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_fibonacci_air, - crate::fibonacci::fibonacci::FibonacciAir, + crate::tests::fibonacci::fibonacci::FibonacciAir, FibonacciAirTester, 32 ); diff --git a/air-script/tests/function_import/function_import.air b/air-script/src/tests/function_import/function_import.air similarity index 100% rename from air-script/tests/function_import/function_import.air rename to air-script/src/tests/function_import/function_import.air diff --git a/air-script/tests/function_import/function_import.rs b/air-script/src/tests/function_import/function_import.rs similarity index 100% rename from air-script/tests/function_import/function_import.rs rename to air-script/src/tests/function_import/function_import.rs diff --git a/air-script/tests/function_import/mod.rs b/air-script/src/tests/function_import/mod.rs similarity index 100% rename from air-script/tests/function_import/mod.rs rename to air-script/src/tests/function_import/mod.rs diff --git a/air-script/tests/function_import/utils.air b/air-script/src/tests/function_import/utils.air similarity index 100% rename from air-script/tests/function_import/utils.air rename to air-script/src/tests/function_import/utils.air diff --git a/air-script/tests/functions/functions_complex.air b/air-script/src/tests/functions/functions_complex.air similarity index 92% rename from air-script/tests/functions/functions_complex.air rename to air-script/src/tests/functions/functions_complex.air index 2b512e262..e427b5b93 100644 --- a/air-script/tests/functions/functions_complex.air +++ b/air-script/src/tests/functions/functions_complex.air @@ -29,7 +29,7 @@ boundary_constraints { integrity_constraints { let f = get_multiplicity_flags(s0, s1); - let z = v^7 * f[3] + v^2 * f[2] + v * f[1] + f[0]; + let z = v^5 * f[3] + v^2 * f[2] + v * f[1] + f[0]; enf b_range' = b_range * (z * t - t + 1); let y = fold_scalar_and_vec(v, b); enf v' = y; diff --git a/air-script/tests/functions/functions_complex.rs b/air-script/src/tests/functions/functions_complex.rs similarity index 89% rename from air-script/tests/functions/functions_complex.rs rename to air-script/src/tests/functions/functions_complex.rs index f165c2d0e..9a438bb07 100644 --- a/air-script/tests/functions/functions_complex.rs +++ b/air-script/src/tests/functions/functions_complex.rs @@ -47,7 +47,7 @@ impl Air for FunctionsAir { } fn new(trace_info: TraceInfo, public_inputs: PublicInputs, options: WinterProofOptions) -> Self { - let main_degrees = vec![TransitionConstraintDegree::new(11), TransitionConstraintDegree::new(1)]; + let main_degrees = vec![TransitionConstraintDegree::new(9), TransitionConstraintDegree::new(1)]; let aux_degrees = vec![]; let num_main_assertions = 1; let num_aux_assertions = 0; @@ -82,7 +82,7 @@ impl Air for FunctionsAir { fn evaluate_transition>(&self, frame: &EvaluationFrame, periodic_values: &[E], result: &mut [E]) { let main_current = frame.current(); let main_next = frame.next(); - result[0] = main_next[16] - main_current[16] * ((main_current[3] * main_current[3] * main_current[3] * main_current[3] * main_current[3] * main_current[3] * main_current[3] * main_current[1] * main_current[2] + main_current[3] * main_current[3] * (E::ONE - main_current[1]) * main_current[2] + main_current[3] * main_current[1] * (E::ONE - main_current[2]) + (E::ONE - main_current[1]) * (E::ONE - main_current[2])) * main_current[0] - main_current[0] + E::ONE); + result[0] = main_next[16] - main_current[16] * ((main_current[3] * main_current[3] * main_current[3] * main_current[3] * main_current[3] * main_current[1] * main_current[2] + main_current[3] * main_current[3] * (E::ONE - main_current[1]) * main_current[2] + main_current[3] * main_current[1] * (E::ONE - main_current[2]) + (E::ONE - main_current[1]) * (E::ONE - main_current[2])) * main_current[0] - main_current[0] + E::ONE); result[1] = main_next[3] - (main_current[4] + main_current[5] + main_current[6] + main_current[7] + main_current[8] + main_current[9] + main_current[10] + main_current[11] + main_current[12] + main_current[13] + main_current[14] + main_current[15] + E::ONE) * E::from(Felt::new(2_u64)); } diff --git a/air-script/src/tests/functions/functions_complex_plonky3.rs b/air-script/src/tests/functions/functions_complex_plonky3.rs new file mode 100644 index 000000000..238cc3741 --- /dev/null +++ b/air-script/src/tests/functions/functions_complex_plonky3.rs @@ -0,0 +1,43 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 17; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct FunctionsAir; + +impl MidenAir for FunctionsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[3].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[16].clone().into() - main_current[16].clone().into() * ((main_current[3].clone().into() * main_current[3].clone().into() * main_current[3].clone().into() * main_current[3].clone().into() * main_current[3].clone().into() * main_current[1].clone().into() * main_current[2].clone().into() + main_current[3].clone().into() * main_current[3].clone().into() * (AB::Expr::ONE - main_current[1].clone().into()) * main_current[2].clone().into() + main_current[3].clone().into() * main_current[1].clone().into() * (AB::Expr::ONE - main_current[2].clone().into()) + (AB::Expr::ONE - main_current[1].clone().into()) * (AB::Expr::ONE - main_current[2].clone().into())) * main_current[0].clone().into() - main_current[0].clone().into() + AB::Expr::ONE)); + builder.when_transition().assert_zero(main_next[3].clone().into() - (main_current[4].clone().into() + main_current[5].clone().into() + main_current[6].clone().into() + main_current[7].clone().into() + main_current[8].clone().into() + main_current[9].clone().into() + main_current[10].clone().into() + main_current[11].clone().into() + main_current[12].clone().into() + main_current[13].clone().into() + main_current[14].clone().into() + main_current[15].clone().into() + AB::Expr::ONE).double()); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/functions/functions_simple.air b/air-script/src/tests/functions/functions_simple.air similarity index 100% rename from air-script/tests/functions/functions_simple.air rename to air-script/src/tests/functions/functions_simple.air diff --git a/air-script/tests/functions/functions_simple.rs b/air-script/src/tests/functions/functions_simple.rs similarity index 100% rename from air-script/tests/functions/functions_simple.rs rename to air-script/src/tests/functions/functions_simple.rs diff --git a/air-script/src/tests/functions/functions_simple_plonky3.rs b/air-script/src/tests/functions/functions_simple_plonky3.rs new file mode 100644 index 000000000..1b72a7dfb --- /dev/null +++ b/air-script/src/tests/functions/functions_simple_plonky3.rs @@ -0,0 +1,49 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 9; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct FunctionsAir; + +impl MidenAir for FunctionsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[3].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() * main_current[3].clone().into() - AB::Expr::ONE); + builder.assert_zero(main_current[4].clone().into() * main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() * main_current[3].clone().into() - AB::Expr::ONE); + builder.assert_zero((main_current[4].clone().into() + main_current[5].clone().into() + main_current[6].clone().into() + main_current[7].clone().into()) * main_current[4].clone().into() * main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() - AB::Expr::ONE); + builder.assert_zero(main_current[4].clone().into() * main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() - AB::Expr::ONE); + builder.assert_zero(main_current[0].clone().into() * main_current[4].clone().into() * main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() - AB::Expr::ONE); + builder.assert_zero(main_current[1].clone().into() + (main_current[4].clone().into() + main_current[5].clone().into() + main_current[6].clone().into() + main_current[7].clone().into()) * main_current[4].clone().into() * main_current[5].clone().into() * main_current[6].clone().into() * main_current[7].clone().into() - AB::Expr::ONE); + builder.assert_zero(main_current[4].clone().into() + main_current[5].clone().into() + main_current[6].clone().into() + main_current[7].clone().into() - AB::Expr::ONE); + builder.assert_zero((main_current[4].clone().into() + main_current[5].clone().into() + main_current[6].clone().into() + main_current[7].clone().into()) * AB::Expr::from_u64(4) - AB::Expr::ONE); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/functions/inlined_functions_simple.air b/air-script/src/tests/functions/inlined_functions_simple.air similarity index 100% rename from air-script/tests/functions/inlined_functions_simple.air rename to air-script/src/tests/functions/inlined_functions_simple.air diff --git a/air-script/src/tests/functions/mod.rs b/air-script/src/tests/functions/mod.rs new file mode 100644 index 000000000..af284a496 --- /dev/null +++ b/air-script/src/tests/functions/mod.rs @@ -0,0 +1,17 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod functions_complex; +#[rustfmt::skip] +#[allow(clippy::all)] +mod functions_simple; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod functions_complex_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod functions_simple_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/functions/test_air_plonky3.rs b/air-script/src/tests/functions/test_air_plonky3.rs new file mode 100644 index 000000000..d9f6c39d6 --- /dev/null +++ b/air-script/src/tests/functions/test_air_plonky3.rs @@ -0,0 +1,92 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::functions::functions_complex_plonky3::{FunctionsAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + rows[0][7] = F::ZERO; + rows[0][8] = F::ZERO; + rows[0][9] = F::ZERO; + rows[0][10] = F::ZERO; + rows[0][11] = F::ZERO; + rows[0][12] = F::ZERO; + rows[0][13] = F::ZERO; + rows[0][14] = F::ZERO; + rows[0][15] = F::ZERO; + rows[0][16] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + let col_8_prev = rows[i - 1][8]; + let col_9_prev = rows[i - 1][9]; + let col_10_prev = rows[i - 1][10]; + let col_11_prev = rows[i - 1][11]; + let col_12_prev = rows[i - 1][12]; + let col_13_prev = rows[i - 1][13]; + let col_14_prev = rows[i - 1][14]; + let col_15_prev = rows[i - 1][15]; + let col_16_prev = rows[i - 1][16]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = F::from_canonical_checked(2).unwrap(); + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + rows[i][7] = col_7_prev; + rows[i][8] = col_8_prev; + rows[i][9] = col_9_prev; + rows[i][10] = col_10_prev; + rows[i][11] = col_11_prev; + rows[i][12] = col_12_prev; + rows[i][13] = col_13_prev; + rows[i][14] = col_14_prev; + rows[i][15] = col_15_prev; + rows[i][16] = col_16_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, FunctionsAir); diff --git a/air-script/tests/functions/test_air.rs b/air-script/src/tests/functions/test_air_winterfell.rs similarity index 89% rename from air-script/tests/functions/test_air.rs rename to air-script/src/tests/functions/test_air_winterfell.rs index ae578bf0d..76aa0b6f3 100644 --- a/air-script/tests/functions/test_air.rs +++ b/air-script/src/tests/functions/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - functions::functions_complex::PublicInputs, - generate_air_test, - helpers::{AirTester, MyTraceTable}, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::functions::functions_complex::PublicInputs, }; #[derive(Clone)] @@ -68,9 +68,9 @@ impl AirTester for FunctionsAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_functions_complex_air, - crate::functions::functions_complex::FunctionsAir, + crate::tests::functions::functions_complex::FunctionsAir, FunctionsAirTester, 1024 ); diff --git a/air-script/tests/indexed_trace_access/indexed_trace_access.air b/air-script/src/tests/indexed_trace_access/indexed_trace_access.air similarity index 100% rename from air-script/tests/indexed_trace_access/indexed_trace_access.air rename to air-script/src/tests/indexed_trace_access/indexed_trace_access.air diff --git a/air-script/tests/indexed_trace_access/indexed_trace_access.rs b/air-script/src/tests/indexed_trace_access/indexed_trace_access.rs similarity index 100% rename from air-script/tests/indexed_trace_access/indexed_trace_access.rs rename to air-script/src/tests/indexed_trace_access/indexed_trace_access.rs diff --git a/air-script/src/tests/indexed_trace_access/indexed_trace_access_plonky3.rs b/air-script/src/tests/indexed_trace_access/indexed_trace_access_plonky3.rs new file mode 100644 index 000000000..9b1bea28c --- /dev/null +++ b/air-script/src/tests/indexed_trace_access/indexed_trace_access_plonky3.rs @@ -0,0 +1,42 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct TraceAccessAir; + +impl MidenAir for TraceAccessAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[0].clone().into() - (main_current[1].clone().into() + AB::Expr::ONE)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/indexed_trace_access/mod.rs b/air-script/src/tests/indexed_trace_access/mod.rs new file mode 100644 index 000000000..f23106386 --- /dev/null +++ b/air-script/src/tests/indexed_trace_access/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod indexed_trace_access; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod indexed_trace_access_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/indexed_trace_access/test_air_plonky3.rs b/air-script/src/tests/indexed_trace_access/test_air_plonky3.rs new file mode 100644 index 000000000..74d0b60d4 --- /dev/null +++ b/air-script/src/tests/indexed_trace_access/test_air_plonky3.rs @@ -0,0 +1,49 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::indexed_trace_access::indexed_trace_access_plonky3::{MAIN_WIDTH, TraceAccessAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + #[allow(clippy::needless_range_loop)] + for i in 1..num_rows { + // Update current row + rows[i][0] = F::ONE; + rows[i][1] = F::ZERO; + rows[i][2] = F::ZERO; + rows[i][3] = F::ZERO; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, TraceAccessAir); diff --git a/air-script/tests/indexed_trace_access/test_air.rs b/air-script/src/tests/indexed_trace_access/test_air_winterfell.rs similarity index 78% rename from air-script/tests/indexed_trace_access/test_air.rs rename to air-script/src/tests/indexed_trace_access/test_air_winterfell.rs index 707dbba10..44b7dfcec 100644 --- a/air-script/tests/indexed_trace_access/test_air.rs +++ b/air-script/src/tests/indexed_trace_access/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - indexed_trace_access::indexed_trace_access::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::indexed_trace_access::indexed_trace_access::PublicInputs, }; #[derive(Clone)] @@ -40,9 +40,9 @@ impl AirTester for TraceAccessAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_indexed_trace_access_air, - crate::indexed_trace_access::indexed_trace_access::TraceAccessAir, + crate::tests::indexed_trace_access::indexed_trace_access::TraceAccessAir, TraceAccessAirTester, 1024 ); diff --git a/air-script/tests/list_comprehension/list_comprehension.air b/air-script/src/tests/list_comprehension/list_comprehension.air similarity index 100% rename from air-script/tests/list_comprehension/list_comprehension.air rename to air-script/src/tests/list_comprehension/list_comprehension.air diff --git a/air-script/tests/list_comprehension/list_comprehension.rs b/air-script/src/tests/list_comprehension/list_comprehension.rs similarity index 100% rename from air-script/tests/list_comprehension/list_comprehension.rs rename to air-script/src/tests/list_comprehension/list_comprehension.rs diff --git a/air-script/tests/list_comprehension/list_comprehension_nested.air b/air-script/src/tests/list_comprehension/list_comprehension_nested.air similarity index 100% rename from air-script/tests/list_comprehension/list_comprehension_nested.air rename to air-script/src/tests/list_comprehension/list_comprehension_nested.air diff --git a/air-script/tests/list_comprehension/list_comprehension_nested.rs b/air-script/src/tests/list_comprehension/list_comprehension_nested.rs similarity index 100% rename from air-script/tests/list_comprehension/list_comprehension_nested.rs rename to air-script/src/tests/list_comprehension/list_comprehension_nested.rs diff --git a/air-script/src/tests/list_comprehension/list_comprehension_nested_plonky3.rs b/air-script/src/tests/list_comprehension/list_comprehension_nested_plonky3.rs new file mode 100644 index 000000000..5525f2068 --- /dev/null +++ b/air-script/src/tests/list_comprehension/list_comprehension_nested_plonky3.rs @@ -0,0 +1,44 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 2; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 1; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ListComprehensionAir; + +impl MidenAir for ListComprehensionAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() + main_current[1].clone().into().double() - AB::Expr::from_u64(3)); + builder.assert_zero(main_current[0].clone().into().double() + main_current[1].clone().into() * AB::Expr::from_u64(3) - AB::Expr::from_u64(5)); + builder.assert_zero(main_current[0].clone().into() * AB::Expr::from_u64(3) + main_current[1].clone().into() * AB::Expr::from_u64(4) - AB::Expr::from_u64(7)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/list_comprehension/list_comprehension_plonky3.rs b/air-script/src/tests/list_comprehension/list_comprehension_plonky3.rs new file mode 100644 index 000000000..9d13e2e8b --- /dev/null +++ b/air-script/src/tests/list_comprehension/list_comprehension_plonky3.rs @@ -0,0 +1,47 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 16; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ListComprehensionAir; + +impl MidenAir for ListComprehensionAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[10].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() - main_current[2].clone().into()); + builder.assert_zero(main_current[4].clone().into() - main_current[0].clone().into() * AB::Expr::from_u64(8) * main_current[11].clone().into()); + builder.when_transition().assert_zero(main_current[4].clone().into() - main_current[0].clone().into() * (main_next[8].clone().into() - main_next[12].clone().into())); + builder.assert_zero(main_current[6].clone().into() - main_current[0].clone().into() * (main_current[9].clone().into() - main_current[14].clone().into())); + builder.assert_zero(main_current[1].clone().into() - (main_current[5].clone().into() - main_current[8].clone().into() - main_current[12].clone().into() + AB::Expr::from_u64(10) + main_current[6].clone().into() - main_current[9].clone().into() - main_current[13].clone().into() + AB::Expr::from_u64(20) + main_current[7].clone().into() - main_current[10].clone().into() - main_current[14].clone().into())); + builder.assert_zero(main_current[14].clone().into() - AB::Expr::from_u64(10)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/list_comprehension/mod.rs b/air-script/src/tests/list_comprehension/mod.rs new file mode 100644 index 000000000..e33f216df --- /dev/null +++ b/air-script/src/tests/list_comprehension/mod.rs @@ -0,0 +1,17 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod list_comprehension_nested; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod list_comprehension_nested_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +mod list_comprehension; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod list_comprehension_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/list_comprehension/test_air_plonky3.rs b/air-script/src/tests/list_comprehension/test_air_plonky3.rs new file mode 100644 index 000000000..42080ad1f --- /dev/null +++ b/air-script/src/tests/list_comprehension/test_air_plonky3.rs @@ -0,0 +1,89 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::list_comprehension::list_comprehension_plonky3::{ListComprehensionAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::from_canonical_checked(20).unwrap(); + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + rows[0][7] = F::ZERO; + rows[0][8] = F::ZERO; + rows[0][9] = F::ZERO; + rows[0][10] = F::ZERO; + rows[0][11] = F::ZERO; + rows[0][12] = F::ZERO; + rows[0][13] = F::ZERO; + rows[0][14] = F::from_canonical_checked(10).unwrap(); + rows[0][15] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + let col_8_prev = rows[i - 1][8]; + let col_9_prev = rows[i - 1][9]; + let col_10_prev = rows[i - 1][10]; + let col_11_prev = rows[i - 1][11]; + let col_12_prev = rows[i - 1][12]; + let col_13_prev = rows[i - 1][13]; + let col_14_prev = rows[i - 1][14]; + let col_15_prev = rows[i - 1][15]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = F::from_canonical_checked(2).unwrap(); + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + rows[i][7] = col_7_prev; + rows[i][8] = col_8_prev; + rows[i][9] = col_9_prev; + rows[i][10] = col_10_prev; + rows[i][11] = col_11_prev; + rows[i][12] = col_12_prev; + rows[i][13] = col_13_prev; + rows[i][14] = col_14_prev; + rows[i][15] = col_15_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, ListComprehensionAir); diff --git a/air-script/tests/list_comprehension/test_air.rs b/air-script/src/tests/list_comprehension/test_air_winterfell.rs similarity index 83% rename from air-script/tests/list_comprehension/test_air.rs rename to air-script/src/tests/list_comprehension/test_air_winterfell.rs index c3e9b396f..749541299 100644 --- a/air-script/tests/list_comprehension/test_air.rs +++ b/air-script/src/tests/list_comprehension/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - list_comprehension::list_comprehension::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::list_comprehension::list_comprehension::PublicInputs, }; #[derive(Clone)] @@ -52,9 +52,9 @@ impl AirTester for ListComprehensionAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_list_comprehension_air, - crate::list_comprehension::list_comprehension::ListComprehensionAir, + crate::tests::list_comprehension::list_comprehension::ListComprehensionAir, ListComprehensionAirTester, 1024 ); diff --git a/air-script/tests/list_folding/list_folding.air b/air-script/src/tests/list_folding/list_folding.air similarity index 100% rename from air-script/tests/list_folding/list_folding.air rename to air-script/src/tests/list_folding/list_folding.air diff --git a/air-script/tests/list_folding/list_folding.rs b/air-script/src/tests/list_folding/list_folding.rs similarity index 100% rename from air-script/tests/list_folding/list_folding.rs rename to air-script/src/tests/list_folding/list_folding.rs diff --git a/air-script/src/tests/list_folding/list_folding_plonky3.rs b/air-script/src/tests/list_folding/list_folding_plonky3.rs new file mode 100644 index 000000000..44f4753a3 --- /dev/null +++ b/air-script/src/tests/list_folding/list_folding_plonky3.rs @@ -0,0 +1,45 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 17; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct ListFoldingAir; + +impl MidenAir for ListFoldingAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[11].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[5].clone().into() - (main_current[9].clone().into() + main_current[10].clone().into() + main_current[11].clone().into() + main_current[12].clone().into() + main_current[13].clone().into() * main_current[14].clone().into() * main_current[15].clone().into() * main_current[16].clone().into())); + builder.when_transition().assert_zero(main_next[6].clone().into() - (main_current[9].clone().into() + main_current[10].clone().into() + main_current[11].clone().into() + main_current[12].clone().into() + main_current[13].clone().into() * main_current[14].clone().into() * main_current[15].clone().into() * main_current[16].clone().into())); + builder.when_transition().assert_zero(main_next[7].clone().into() - (main_current[9].clone().into() * main_current[13].clone().into() + main_current[10].clone().into() * main_current[14].clone().into() + main_current[11].clone().into() * main_current[15].clone().into() + main_current[12].clone().into() * main_current[16].clone().into() + (main_current[9].clone().into() + main_current[13].clone().into()) * (main_current[10].clone().into() + main_current[14].clone().into()) * (main_current[11].clone().into() + main_current[15].clone().into()) * (main_current[12].clone().into() + main_current[16].clone().into()))); + builder.when_transition().assert_zero(main_next[8].clone().into() - (main_current[1].clone().into() + main_current[9].clone().into() * main_current[13].clone().into() + main_current[10].clone().into() * main_current[14].clone().into() + main_current[11].clone().into() * main_current[15].clone().into() + main_current[12].clone().into() * main_current[16].clone().into() + main_current[9].clone().into() * main_current[13].clone().into() + main_current[10].clone().into() * main_current[14].clone().into() + main_current[11].clone().into() * main_current[15].clone().into() + main_current[12].clone().into() * main_current[16].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/list_folding/mod.rs b/air-script/src/tests/list_folding/mod.rs new file mode 100644 index 000000000..0339dce98 --- /dev/null +++ b/air-script/src/tests/list_folding/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod list_folding; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod list_folding_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/src/tests/list_folding/test_air_plonky3.rs b/air-script/src/tests/list_folding/test_air_plonky3.rs new file mode 100644 index 000000000..144e4f27b --- /dev/null +++ b/air-script/src/tests/list_folding/test_air_plonky3.rs @@ -0,0 +1,92 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::list_folding::list_folding_plonky3::{ListFoldingAir, MAIN_WIDTH}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + rows[0][7] = F::ZERO; + rows[0][8] = F::ZERO; + rows[0][9] = F::ZERO; + rows[0][10] = F::ZERO; + rows[0][11] = F::ZERO; + rows[0][12] = F::ZERO; + rows[0][13] = F::ZERO; + rows[0][14] = F::ZERO; + rows[0][15] = F::ZERO; + rows[0][16] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + let col_8_prev = rows[i - 1][8]; + let col_9_prev = rows[i - 1][9]; + let col_10_prev = rows[i - 1][10]; + let col_11_prev = rows[i - 1][11]; + let col_12_prev = rows[i - 1][12]; + let col_13_prev = rows[i - 1][13]; + let col_14_prev = rows[i - 1][14]; + let col_15_prev = rows[i - 1][15]; + let col_16_prev = rows[i - 1][16]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = F::from_canonical_checked(2).unwrap(); + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + rows[i][7] = col_7_prev; + rows[i][8] = col_8_prev; + rows[i][9] = col_9_prev; + rows[i][10] = col_10_prev; + rows[i][11] = col_11_prev; + rows[i][12] = col_12_prev; + rows[i][13] = col_13_prev; + rows[i][14] = col_14_prev; + rows[i][15] = col_15_prev; + rows[i][16] = col_16_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, ListFoldingAir); diff --git a/air-script/tests/list_folding/test_air.rs b/air-script/src/tests/list_folding/test_air_winterfell.rs similarity index 85% rename from air-script/tests/list_folding/test_air.rs rename to air-script/src/tests/list_folding/test_air_winterfell.rs index 9d79fad28..a957f18f9 100644 --- a/air-script/tests/list_folding/test_air.rs +++ b/air-script/src/tests/list_folding/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - list_folding::list_folding::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::list_folding::list_folding::PublicInputs, }; #[derive(Clone)] @@ -53,9 +53,9 @@ impl AirTester for ListFoldingAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_list_folding_air, - crate::list_folding::list_folding::ListFoldingAir, + crate::tests::list_folding::list_folding::ListFoldingAir, ListFoldingAirTester, 1024 ); diff --git a/air-script/tests/mod.rs b/air-script/src/tests/mod.rs similarity index 97% rename from air-script/tests/mod.rs rename to air-script/src/tests/mod.rs index e96e603d8..a140647c8 100644 --- a/air-script/tests/mod.rs +++ b/air-script/src/tests/mod.rs @@ -1,7 +1,3 @@ -mod codegen; - -pub mod helpers; - #[allow(unused_variables, dead_code, unused_mut)] mod binary; #[allow(unused_variables, dead_code, unused_mut)] @@ -44,3 +40,5 @@ mod trace_col_groups; mod variables; mod docs_sync; +mod plonky3; +mod winterfell; diff --git a/air-script/src/tests/periodic_columns/mod.rs b/air-script/src/tests/periodic_columns/mod.rs new file mode 100644 index 000000000..0053e1aa8 --- /dev/null +++ b/air-script/src/tests/periodic_columns/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod periodic_columns; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod periodic_columns_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/tests/periodic_columns/periodic_columns.air b/air-script/src/tests/periodic_columns/periodic_columns.air similarity index 100% rename from air-script/tests/periodic_columns/periodic_columns.air rename to air-script/src/tests/periodic_columns/periodic_columns.air diff --git a/air-script/tests/periodic_columns/periodic_columns.rs b/air-script/src/tests/periodic_columns/periodic_columns.rs similarity index 100% rename from air-script/tests/periodic_columns/periodic_columns.rs rename to air-script/src/tests/periodic_columns/periodic_columns.rs diff --git a/air-script/src/tests/periodic_columns/periodic_columns_plonky3.rs b/air-script/src/tests/periodic_columns/periodic_columns_plonky3.rs new file mode 100644 index 000000000..df36acbd7 --- /dev/null +++ b/air-script/src/tests/periodic_columns/periodic_columns_plonky3.rs @@ -0,0 +1,57 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 3; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 2; +pub const PERIOD: usize = 8; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct PeriodicColumnsAir; + +impl MidenAir for PeriodicColumnsAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_public_values(&self) -> usize { + NUM_PUBLIC_VALUES + } + + fn periodic_table(&self) -> Vec> { + vec![ + vec![F::from_u64(1), F::from_u64(0), F::from_u64(0), F::from_u64(0)], + vec![F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(0)], + ] + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero_ext(AB::ExprEF::from(periodic_values[0].clone().into()) * (AB::ExprEF::from(main_current[1].clone().into()) + AB::ExprEF::from(main_current[2].clone().into()))); + builder.when_transition().assert_zero_ext(AB::ExprEF::from(periodic_values[1].clone().into()) * (AB::ExprEF::from(main_next[0].clone().into()) - AB::ExprEF::from(main_current[0].clone().into()))); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/periodic_columns/test_air_plonky3.rs b/air-script/src/tests/periodic_columns/test_air_plonky3.rs new file mode 100644 index 000000000..d732ffbed --- /dev/null +++ b/air-script/src/tests/periodic_columns/test_air_plonky3.rs @@ -0,0 +1,50 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::periodic_columns::periodic_columns_plonky3::{MAIN_WIDTH, PeriodicColumnsAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, PeriodicColumnsAir); diff --git a/air-script/tests/periodic_columns/test_air.rs b/air-script/src/tests/periodic_columns/test_air_winterfell.rs similarity index 77% rename from air-script/tests/periodic_columns/test_air.rs rename to air-script/src/tests/periodic_columns/test_air_winterfell.rs index f157a98df..cf92e1101 100644 --- a/air-script/tests/periodic_columns/test_air.rs +++ b/air-script/src/tests/periodic_columns/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - periodic_columns::periodic_columns::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::periodic_columns::periodic_columns::PublicInputs, }; #[derive(Clone)] @@ -37,9 +37,9 @@ impl AirTester for PeriodicColumnsAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_periodic_columns_air, - crate::periodic_columns::periodic_columns::PeriodicColumnsAir, + crate::tests::periodic_columns::periodic_columns::PeriodicColumnsAir, PeriodicColumnsAirTester, 1024 ); diff --git a/air-script/src/tests/plonky3.rs b/air-script/src/tests/plonky3.rs new file mode 100644 index 000000000..baf9c3453 --- /dev/null +++ b/air-script/src/tests/plonky3.rs @@ -0,0 +1,370 @@ +use expect_test::expect_file; + +use crate::test_utils::codegen::{Target, Test}; + +#[test] +fn binary() { + let generated_air = Test::new("src/tests/binary/binary.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["binary/binary_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn bitwise() { + let generated_air = Test::new("src/tests/bitwise/bitwise.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["bitwise/bitwise_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_complex() { + let generated_air = Test::new("src/tests/buses/buses_complex.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["buses/buses_complex_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_simple() { + let generated_air = Test::new("src/tests/buses/buses_simple.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["buses/buses_simple_plonky3.rs"]; + expected.assert_eq(&generated_air); +} +#[test] +fn buses_simple_with_evaluators() { + let generated_air = Test::new("src/tests/buses/buses_simple_with_evaluators.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["buses/buses_simple_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_varlen_boundary_both() { + let generated_air = Test::new("src/tests/buses/buses_varlen_boundary_both.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["buses/buses_varlen_boundary_both_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_varlen_boundary_first() { + let generated_air = Test::new("src/tests/buses/buses_varlen_boundary_first.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["buses/buses_varlen_boundary_first_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_varlen_boundary_last() { + let generated_air = Test::new("src/tests/buses/buses_varlen_boundary_last.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["buses/buses_varlen_boundary_last_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn computed_indices_complex() { + let generated_air = + Test::new("src/tests/computed_indices/computed_indices_complex.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["computed_indices/computed_indices_complex_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn computed_indices_simple() { + let generated_air = + Test::new("src/tests/computed_indices/computed_indices_simple.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["computed_indices/computed_indices_simple_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn constant_in_range() { + let generated_air = Test::new("src/tests/constant_in_range/constant_in_range.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["constant_in_range/constant_in_range_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn constants() { + let generated_air = Test::new("src/tests/constants/constants.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["constants/constants_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn evaluators_nested_slice_call() { + let generated_air = + Test::new("src/tests/evaluators/evaluators_nested_slice_call.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["evaluators/evaluators_nested_slice_call_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +// TODO: add support for nested slicing in general expressions. +// +// #[test] +// fn evaluators_slice_slicing() { +// let generated_air = +// Test::new("src/tests/evaluators/evaluators_slice_slicing.air".to_string()) +// .transpile(Target::Plonky3) +// .unwrap(); +// +// let expected = expect_file!["evaluators/evaluators_slice_slicing_plonky3.rs"]; +// expected.assert_eq(&generated_air); +// } + +#[test] +fn evaluators_slice() { + let generated_air = Test::new("src/tests/evaluators/evaluators_slice.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["evaluators/evaluators_slice_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn evaluators() { + let generated_air = Test::new("src/tests/evaluators/evaluators.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["evaluators/evaluators_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn fibonacci() { + let generated_air = Test::new("src/tests/fibonacci/fibonacci.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["fibonacci/fibonacci_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn functions_complex() { + let generated_air = Test::new("src/tests/functions/functions_complex.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["functions/functions_complex_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn functions_simple() { + let generated_air = Test::new("src/tests/functions/functions_simple.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["functions/functions_simple_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn functions_simple_inlined() { + // make sure that the constraints generated using inlined functions are the same as the ones + // generated using regular functions + let generated_air = Test::new("src/tests/functions/inlined_functions_simple.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["functions/functions_simple_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn indexed_trace_access() { + let generated_air = + Test::new("src/tests/indexed_trace_access/indexed_trace_access.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["indexed_trace_access/indexed_trace_access_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn list_comprehension_nested() { + let generated_air = + Test::new("src/tests/list_comprehension/list_comprehension_nested.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["list_comprehension/list_comprehension_nested_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn list_comprehension() { + let generated_air = + Test::new("src/tests/list_comprehension/list_comprehension.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["list_comprehension/list_comprehension_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn list_folding() { + let generated_air = Test::new("src/tests/list_folding/list_folding.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["list_folding/list_folding_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn periodic_columns() { + let generated_air = Test::new("src/tests/periodic_columns/periodic_columns.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["periodic_columns/periodic_columns_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn pub_inputs() { + let generated_air = Test::new("src/tests/pub_inputs/pub_inputs.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["pub_inputs/pub_inputs_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors_combine_complex() { + let generated_air = Test::new("src/tests/selectors/selectors_combine_complex.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["selectors/selectors_combine_complex_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors_combine_simple() { + let generated_air = Test::new("src/tests/selectors/selectors_combine_simple.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["selectors/selectors_combine_simple_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors_combine_with_list_comprehensions() { + let generated_air = + Test::new("src/tests/selectors/selectors_combine_with_list_comprehensions.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["selectors/selectors_combine_with_list_comprehensions_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors() { + let generated_air = Test::new("src/tests/selectors/selectors.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["selectors/selectors_plonky3.rs"]; + expected.assert_eq(&generated_air); + + let generated_air = Test::new("src/tests/selectors/selectors_with_evaluators.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["selectors/selectors_with_evaluators_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn system() { + let generated_air = Test::new("src/tests/system/system.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["system/system_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn trace_col_groups() { + let generated_air = Test::new("src/tests/trace_col_groups/trace_col_groups.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["trace_col_groups/trace_col_groups_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn variables() { + let generated_air = Test::new("src/tests/variables/variables.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["variables/variables_plonky3.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn constraint_comprehension() { + let generated_air = + Test::new("src/tests/constraint_comprehension/constraint_comprehension.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["constraint_comprehension/constraint_comprehension_plonky3.rs"]; + expected.assert_eq(&generated_air); + + let generated_air = + Test::new("src/tests/constraint_comprehension/cc_with_evaluators.air".to_string()) + .transpile(Target::Plonky3) + .unwrap(); + + let expected = expect_file!["constraint_comprehension/constraint_comprehension_plonky3.rs"]; + expected.assert_eq(&generated_air); +} diff --git a/air-script/src/tests/pub_inputs/mod.rs b/air-script/src/tests/pub_inputs/mod.rs new file mode 100644 index 000000000..139601bc2 --- /dev/null +++ b/air-script/src/tests/pub_inputs/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod pub_inputs; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod pub_inputs_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/tests/pub_inputs/pub_inputs.air b/air-script/src/tests/pub_inputs/pub_inputs.air similarity index 100% rename from air-script/tests/pub_inputs/pub_inputs.air rename to air-script/src/tests/pub_inputs/pub_inputs.air diff --git a/air-script/tests/pub_inputs/pub_inputs.rs b/air-script/src/tests/pub_inputs/pub_inputs.rs similarity index 100% rename from air-script/tests/pub_inputs/pub_inputs.rs rename to air-script/src/tests/pub_inputs/pub_inputs.rs diff --git a/air-script/src/tests/pub_inputs/pub_inputs_plonky3.rs b/air-script/src/tests/pub_inputs/pub_inputs_plonky3.rs new file mode 100644 index 000000000..4cc281a74 --- /dev/null +++ b/air-script/src/tests/pub_inputs/pub_inputs_plonky3.rs @@ -0,0 +1,49 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 32; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct PubInputsAir; + +impl MidenAir for PubInputsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into() - public_values[8].into()); + builder.when_first_row().assert_zero(main_current[1].clone().into() - public_values[9].into()); + builder.when_first_row().assert_zero(main_current[2].clone().into() - public_values[10].into()); + builder.when_first_row().assert_zero(main_current[3].clone().into() - public_values[11].into()); + builder.when_last_row().assert_zero(main_current[0].clone().into() - public_values[12].into()); + builder.when_last_row().assert_zero(main_current[1].clone().into() - public_values[13].into()); + builder.when_last_row().assert_zero(main_current[2].clone().into() - public_values[14].into()); + builder.when_last_row().assert_zero(main_current[3].clone().into() - public_values[15].into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[0].clone().into() - (main_current[1].clone().into() + main_current[2].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/pub_inputs/test_air_plonky3.rs b/air-script/src/tests/pub_inputs/test_air_plonky3.rs new file mode 100644 index 000000000..2a7ad24d0 --- /dev/null +++ b/air-script/src/tests/pub_inputs/test_air_plonky3.rs @@ -0,0 +1,53 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::pub_inputs::pub_inputs_plonky3::{MAIN_WIDTH, PubInputsAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![0; 32] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, PubInputsAir); diff --git a/air-script/tests/pub_inputs/test_air.rs b/air-script/src/tests/pub_inputs/test_air_winterfell.rs similarity index 80% rename from air-script/tests/pub_inputs/test_air.rs rename to air-script/src/tests/pub_inputs/test_air_winterfell.rs index ad929a857..62b8be849 100644 --- a/air-script/tests/pub_inputs/test_air.rs +++ b/air-script/src/tests/pub_inputs/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - pub_inputs::pub_inputs::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::pub_inputs::pub_inputs::PublicInputs, }; #[derive(Clone)] @@ -38,9 +38,9 @@ impl AirTester for PubInputsAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_pub_inputs_air, - crate::pub_inputs::pub_inputs::PubInputsAir, + crate::tests::pub_inputs::pub_inputs::PubInputsAir, PubInputsAirTester, 1024 ); diff --git a/air-script/src/tests/selectors/mod.rs b/air-script/src/tests/selectors/mod.rs new file mode 100644 index 000000000..a8007ac23 --- /dev/null +++ b/air-script/src/tests/selectors/mod.rs @@ -0,0 +1,38 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod selectors; +#[rustfmt::skip] +#[allow(clippy::all)] +mod selectors_combine_simple; +#[rustfmt::skip] +#[allow(clippy::all)] +mod selectors_combine_complex; +#[rustfmt::skip] +#[allow(clippy::all)] +mod selectors_combine_with_list_comprehensions; +#[rustfmt::skip] +#[allow(clippy::all)] +mod selectors_with_evaluators; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod selectors_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod selectors_combine_simple_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod selectors_combine_complex_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod selectors_with_evaluators_plonky3; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod selectors_combine_with_list_comprehensions_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/tests/selectors/selectors.air b/air-script/src/tests/selectors/selectors.air similarity index 100% rename from air-script/tests/selectors/selectors.air rename to air-script/src/tests/selectors/selectors.air diff --git a/air-script/tests/selectors/selectors.rs b/air-script/src/tests/selectors/selectors.rs similarity index 100% rename from air-script/tests/selectors/selectors.rs rename to air-script/src/tests/selectors/selectors.rs diff --git a/air-script/tests/selectors/selectors_combine_complex.air b/air-script/src/tests/selectors/selectors_combine_complex.air similarity index 100% rename from air-script/tests/selectors/selectors_combine_complex.air rename to air-script/src/tests/selectors/selectors_combine_complex.air diff --git a/air-script/tests/selectors/selectors_combine_complex.rs b/air-script/src/tests/selectors/selectors_combine_complex.rs similarity index 100% rename from air-script/tests/selectors/selectors_combine_complex.rs rename to air-script/src/tests/selectors/selectors_combine_complex.rs diff --git a/air-script/src/tests/selectors/selectors_combine_complex_plonky3.rs b/air-script/src/tests/selectors/selectors_combine_complex_plonky3.rs new file mode 100644 index 000000000..a89f39d38 --- /dev/null +++ b/air-script/src/tests/selectors/selectors_combine_complex_plonky3.rs @@ -0,0 +1,138 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 6; +pub const AUX_WIDTH: usize = 1; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 1; +pub const MAX_BETA_CHALLENGE_POWER: usize = 2; + +pub struct SelectorsAir; + +impl MidenAir for SelectorsAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_randomness(&self) -> usize { + 1 + MAX_BETA_CHALLENGE_POWER + } + + fn aux_width(&self) -> usize { + AUX_WIDTH + } + + fn bus_types(&self) -> Vec { + vec![ + BusType::Multiset, + ] + } + + fn build_aux_trace(&self, _main: &RowMajorMatrix, _challenges: &[EF]) -> Option> { + // Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders. + + let num_rows = _main.height(); + let trace_length = num_rows * AUX_WIDTH; + let mut long_trace = EF::zero_vec(trace_length); + let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH); + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + // Initialize first row + let initial_values = Self::buses_initial_values::(); + for j in 0..AUX_WIDTH { + rows[0][j] = initial_values[j]; + } + // Fill subsequent rows using direct access to the rows array + for i in 0..num_rows-1 { + let i_next = (i + 1) % num_rows; + let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail. + let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail. + let main = VerticalPair::new( + RowMajorMatrixView::new_row(&*main_local), + RowMajorMatrixView::new_row(&*main_next), + ); + let periodic_values: [_; NUM_PERIODIC_VALUES] = >::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect("Wrong number of periodic values"); + let prev_row = &rows[i]; + let next_row = Self::buses_transitions::( + &main, + _challenges, + &periodic_values, + prev_row, + ); + for j in 0..AUX_WIDTH { + rows[i+1][j] = next_row[j]; + } + } + let trace_f = trace.flatten_to_base(); + Some(trace_f) + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect("Wrong number of aux bus boundary values"); + let aux = builder.permutation(); + let (aux_current, aux_next) = ( + aux.row_slice(0).unwrap(), + aux.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[5].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero((main_current[0].clone().into() + (AB::Expr::ONE - main_current[0].clone().into()) * main_current[1].clone().into()) * (main_current[3].clone().into() - AB::Expr::from_u64(16)) + (AB::Expr::ONE - main_current[0].clone().into()) * (AB::Expr::ONE - main_current[1].clone().into()) * (main_current[4].clone().into() - AB::Expr::from_u64(5))); + builder.assert_zero((AB::Expr::ONE - main_current[0].clone().into()) * (main_current[5].clone().into() - AB::Expr::from_u64(5)) + main_current[0].clone().into() * (main_current[4].clone().into() - AB::Expr::from_u64(4))); + builder.assert_zero(main_current[0].clone().into() * (main_current[5].clone().into() - AB::Expr::from_u64(20)) + (AB::Expr::ONE - main_current[0].clone().into()) * main_current[1].clone().into() * (main_current[4].clone().into() - AB::Expr::from_u64(31))); + + // Aux integrity/transition constraints + builder.when_transition().assert_zero_ext(((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * AB::ExprEF::from(main_current[0].clone().into()) * AB::ExprEF::from(main_current[5].clone().into()) + AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into()) * AB::ExprEF::from(main_current[5].clone().into())) * ((alpha.into() + beta_challenges[0].into() + beta_challenges[1].into().double()) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * AB::ExprEF::from(main_current[1].clone().into()) * AB::ExprEF::from(main_current[5].clone().into()) + AB::ExprEF::ONE - (AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * AB::ExprEF::from(main_current[1].clone().into()) * AB::ExprEF::from(main_current[5].clone().into())) * AB::ExprEF::from(aux_current[0].clone().into()) - ((alpha.into() + AB::ExprEF::from_u64(3) * beta_challenges[0].into() + AB::ExprEF::from_u64(4) * beta_challenges[1].into()) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[1].clone().into())) * AB::ExprEF::from(main_current[4].clone().into()) + AB::ExprEF::ONE - (AB::ExprEF::ONE - AB::ExprEF::from(main_current[0].clone().into())) * (AB::ExprEF::ONE - AB::ExprEF::from(main_current[1].clone().into())) * AB::ExprEF::from(main_current[4].clone().into())) * AB::ExprEF::from(aux_next[0].clone().into())); + } +} + +impl SelectorsAir { + fn buses_initial_values() -> Vec + where F: Field, + EF: ExtensionField, + { + vec![ + EF::ONE, + ] + } + + fn buses_transitions(main: &VerticalPair, RowMajorMatrixView>, challenges: &[EF], periodic_evals: &[F], aux_current: &[EF]) -> Vec + where F: Field, + EF: ExtensionField, + { + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + let (&alpha, beta_challenges) = challenges.split_first().expect("Wrong number of randomness"); + let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect("Wrong number of randomness"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect("Wrong number of periodic values"); + vec![ + (((alpha + beta_challenges[0] + beta_challenges[1].double()) * EF::from(main_current[0].clone()) * EF::from(main_current[5].clone()) + EF::ONE - EF::from(main_current[0].clone()) * EF::from(main_current[5].clone())) * ((alpha + beta_challenges[0] + beta_challenges[1].double()) * (EF::ONE - EF::from(main_current[0].clone())) * EF::from(main_current[1].clone()) * EF::from(main_current[5].clone()) + EF::ONE - (EF::ONE - EF::from(main_current[0].clone())) * EF::from(main_current[1].clone()) * EF::from(main_current[5].clone())) * EF::from(aux_current[0].clone())) * ((alpha + EF::from_u64(3) * beta_challenges[0] + EF::from_u64(4) * beta_challenges[1]) * (EF::ONE - EF::from(main_current[0].clone())) * (EF::ONE - EF::from(main_current[1].clone())) * EF::from(main_current[4].clone()) + EF::ONE - (EF::ONE - EF::from(main_current[0].clone())) * (EF::ONE - EF::from(main_current[1].clone())) * EF::from(main_current[4].clone())).inverse(), + ] + } +} \ No newline at end of file diff --git a/air-script/tests/selectors/selectors_combine_simple.air b/air-script/src/tests/selectors/selectors_combine_simple.air similarity index 100% rename from air-script/tests/selectors/selectors_combine_simple.air rename to air-script/src/tests/selectors/selectors_combine_simple.air diff --git a/air-script/tests/selectors/selectors_combine_simple.rs b/air-script/src/tests/selectors/selectors_combine_simple.rs similarity index 100% rename from air-script/tests/selectors/selectors_combine_simple.rs rename to air-script/src/tests/selectors/selectors_combine_simple.rs diff --git a/air-script/src/tests/selectors/selectors_combine_simple_plonky3.rs b/air-script/src/tests/selectors/selectors_combine_simple_plonky3.rs new file mode 100644 index 000000000..ed223440f --- /dev/null +++ b/air-script/src/tests/selectors/selectors_combine_simple_plonky3.rs @@ -0,0 +1,43 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 1; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct SelectorsAir; + +impl MidenAir for SelectorsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[3].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[1].clone().into() - main_current[2].clone().into()); + builder.when_transition().assert_zero(main_current[3].clone().into() * (main_next[0].clone().into() - (main_current[0].clone().into() + main_current[1].clone().into())) + (AB::Expr::ONE - main_current[3].clone().into()) * (main_next[0].clone().into() - main_current[0].clone().into() * main_current[1].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/selectors/selectors_combine_with_list_comprehensions.air b/air-script/src/tests/selectors/selectors_combine_with_list_comprehensions.air similarity index 100% rename from air-script/tests/selectors/selectors_combine_with_list_comprehensions.air rename to air-script/src/tests/selectors/selectors_combine_with_list_comprehensions.air diff --git a/air-script/tests/selectors/selectors_combine_with_list_comprehensions.rs b/air-script/src/tests/selectors/selectors_combine_with_list_comprehensions.rs similarity index 100% rename from air-script/tests/selectors/selectors_combine_with_list_comprehensions.rs rename to air-script/src/tests/selectors/selectors_combine_with_list_comprehensions.rs diff --git a/air-script/src/tests/selectors/selectors_combine_with_list_comprehensions_plonky3.rs b/air-script/src/tests/selectors/selectors_combine_with_list_comprehensions_plonky3.rs new file mode 100644 index 000000000..d482f2c9d --- /dev/null +++ b/air-script/src/tests/selectors/selectors_combine_with_list_comprehensions_plonky3.rs @@ -0,0 +1,44 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 6; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 1; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct SelectorsAir; + +impl MidenAir for SelectorsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[5].clone().into()); + + // Main integrity/transition constraints + builder.assert_zero((main_current[0].clone().into() + (AB::Expr::ONE - main_current[0].clone().into()) * main_current[1].clone().into()) * main_current[3].clone().into() + (AB::Expr::ONE - main_current[0].clone().into()) * (AB::Expr::ONE - main_current[1].clone().into()) * (main_current[4].clone().into() - AB::Expr::from_u64(8))); + builder.assert_zero((AB::Expr::ONE - main_current[0].clone().into()) * (main_current[5].clone().into() - AB::Expr::from_u64(8)) + main_current[0].clone().into() * (main_current[4].clone().into() - AB::Expr::from_u64(2))); + builder.assert_zero(main_current[0].clone().into() * (main_current[5].clone().into() - AB::Expr::from_u64(4)) + (AB::Expr::ONE - main_current[0].clone().into()) * main_current[1].clone().into() * (main_current[4].clone().into() - AB::Expr::from_u64(6))); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/selectors/selectors_plonky3.rs b/air-script/src/tests/selectors/selectors_plonky3.rs new file mode 100644 index 000000000..e16eb2f28 --- /dev/null +++ b/air-script/src/tests/selectors/selectors_plonky3.rs @@ -0,0 +1,43 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct SelectorsAir; + +impl MidenAir for SelectorsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[3].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_current[0].clone().into() * (AB::Expr::ONE - main_current[1].clone().into()) * main_next[3].clone().into()); + builder.when_transition().assert_zero(main_current[0].clone().into() * main_current[1].clone().into() * main_current[2].clone().into() * (main_next[3].clone().into() - main_current[3].clone().into()) + (AB::Expr::ONE - main_current[1].clone().into()) * (AB::Expr::ONE - main_current[2].clone().into()) * (main_next[3].clone().into() - AB::Expr::ONE)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/tests/selectors/selectors_with_evaluators.air b/air-script/src/tests/selectors/selectors_with_evaluators.air similarity index 100% rename from air-script/tests/selectors/selectors_with_evaluators.air rename to air-script/src/tests/selectors/selectors_with_evaluators.air diff --git a/air-script/tests/selectors/selectors_with_evaluators.rs b/air-script/src/tests/selectors/selectors_with_evaluators.rs similarity index 100% rename from air-script/tests/selectors/selectors_with_evaluators.rs rename to air-script/src/tests/selectors/selectors_with_evaluators.rs diff --git a/air-script/src/tests/selectors/selectors_with_evaluators_plonky3.rs b/air-script/src/tests/selectors/selectors_with_evaluators_plonky3.rs new file mode 100644 index 000000000..42affcf13 --- /dev/null +++ b/air-script/src/tests/selectors/selectors_with_evaluators_plonky3.rs @@ -0,0 +1,43 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct SelectorsAir; + +impl MidenAir for SelectorsAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[3].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_current[0].clone().into() * (AB::Expr::ONE - main_current[1].clone().into()) * main_next[3].clone().into()); + builder.when_transition().assert_zero(main_current[1].clone().into() * main_current[2].clone().into() * main_current[0].clone().into() * (main_next[3].clone().into() - main_current[3].clone().into()) + (AB::Expr::ONE - main_current[1].clone().into()) * (AB::Expr::ONE - main_current[2].clone().into()) * (main_next[3].clone().into() - AB::Expr::ONE)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/selectors/test_air_plonky3.rs b/air-script/src/tests/selectors/test_air_plonky3.rs new file mode 100644 index 000000000..c8cfbffa2 --- /dev/null +++ b/air-script/src/tests/selectors/test_air_plonky3.rs @@ -0,0 +1,53 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::selectors::selectors_with_evaluators_plonky3::{MAIN_WIDTH, SelectorsAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + rows[i][3] = F::ONE; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, SelectorsAir); diff --git a/air-script/tests/selectors/test_air.rs b/air-script/src/tests/selectors/test_air_winterfell.rs similarity index 78% rename from air-script/tests/selectors/test_air.rs rename to air-script/src/tests/selectors/test_air_winterfell.rs index 4f2f3378b..3bb3ac337 100644 --- a/air-script/tests/selectors/test_air.rs +++ b/air-script/src/tests/selectors/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - selectors::selectors_with_evaluators::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::selectors::selectors_with_evaluators::PublicInputs, }; #[derive(Clone)] @@ -40,9 +40,9 @@ impl AirTester for SelectorsAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_selectors_with_evaluators_air, - crate::selectors::selectors_with_evaluators::SelectorsAir, + crate::tests::selectors::selectors_with_evaluators::SelectorsAir, SelectorsAirTester, 1024 ); diff --git a/air-script/src/tests/system/mod.rs b/air-script/src/tests/system/mod.rs new file mode 100644 index 000000000..7d8522c99 --- /dev/null +++ b/air-script/src/tests/system/mod.rs @@ -0,0 +1,10 @@ +#[rustfmt::skip] +#[allow(clippy::all)] +mod system; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod system_plonky3; + +mod test_air_plonky3; +mod test_air_winterfell; diff --git a/air-script/tests/system/system.air b/air-script/src/tests/system/system.air similarity index 100% rename from air-script/tests/system/system.air rename to air-script/src/tests/system/system.air diff --git a/air-script/tests/system/system.rs b/air-script/src/tests/system/system.rs similarity index 100% rename from air-script/tests/system/system.rs rename to air-script/src/tests/system/system.rs diff --git a/air-script/src/tests/system/system_plonky3.rs b/air-script/src/tests/system/system_plonky3.rs new file mode 100644 index 000000000..acc5e98be --- /dev/null +++ b/air-script/src/tests/system/system_plonky3.rs @@ -0,0 +1,42 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 3; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct SystemAir; + +impl MidenAir for SystemAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[0].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[0].clone().into() - (main_current[0].clone().into() + AB::Expr::ONE)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/system/test_air_plonky3.rs b/air-script/src/tests/system/test_air_plonky3.rs new file mode 100644 index 000000000..952a7412f --- /dev/null +++ b/air-script/src/tests/system/test_air_plonky3.rs @@ -0,0 +1,50 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::system::system_plonky3::{MAIN_WIDTH, SystemAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + + // Update current row based on previous values + rows[i][0] = col_0_prev + F::ONE; + rows[i][1] = col_1_prev; + rows[i][2] = col_2_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, SystemAir); diff --git a/air-script/tests/system/test_air.rs b/air-script/src/tests/system/test_air_winterfell.rs similarity index 76% rename from air-script/tests/system/test_air.rs rename to air-script/src/tests/system/test_air_winterfell.rs index 3aa6815c6..99a717f78 100644 --- a/air-script/tests/system/test_air.rs +++ b/air-script/src/tests/system/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - system::system::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::system::system::PublicInputs, }; #[derive(Clone)] @@ -39,4 +39,9 @@ impl AirTester for SystemAirTester { } } -generate_air_test!(test_system_air, crate::system::system::SystemAir, SystemAirTester, 1024); +generate_air_winterfell_test!( + test_system_air, + crate::tests::system::system::SystemAir, + SystemAirTester, + 1024 +); diff --git a/air-script/src/tests/trace_col_groups/mod.rs b/air-script/src/tests/trace_col_groups/mod.rs new file mode 100644 index 000000000..c2bb1aa0a --- /dev/null +++ b/air-script/src/tests/trace_col_groups/mod.rs @@ -0,0 +1,10 @@ +mod test_air_plonky3; +mod test_air_winterfell; + +#[rustfmt::skip] +#[allow(clippy::all)] +mod trace_col_groups; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod trace_col_groups_plonky3; diff --git a/air-script/src/tests/trace_col_groups/test_air_plonky3.rs b/air-script/src/tests/trace_col_groups/test_air_plonky3.rs new file mode 100644 index 000000000..920a7f3db --- /dev/null +++ b/air-script/src/tests/trace_col_groups/test_air_plonky3.rs @@ -0,0 +1,68 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::trace_col_groups::trace_col_groups_plonky3::{MAIN_WIDTH, TraceColGroupAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ZERO; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + rows[0][4] = F::ZERO; + rows[0][5] = F::ZERO; + rows[0][6] = F::ZERO; + rows[0][7] = F::ZERO; + rows[0][8] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + let col_4_prev = rows[i - 1][4]; + let col_5_prev = rows[i - 1][5]; + let col_6_prev = rows[i - 1][6]; + let col_7_prev = rows[i - 1][7]; + let col_8_prev = rows[i - 1][8]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = col_1_prev - F::ONE; + rows[i][2] = col_2_prev + F::ONE; + rows[i][3] = col_3_prev; + rows[i][4] = col_4_prev; + rows[i][5] = col_5_prev; + rows[i][6] = col_6_prev; + rows[i][7] = col_7_prev; + rows[i][8] = col_8_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 16] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, TraceColGroupAir); diff --git a/air-script/tests/trace_col_groups/test_air.rs b/air-script/src/tests/trace_col_groups/test_air_winterfell.rs similarity index 82% rename from air-script/tests/trace_col_groups/test_air.rs rename to air-script/src/tests/trace_col_groups/test_air_winterfell.rs index cb62434e9..53a5af721 100644 --- a/air-script/tests/trace_col_groups/test_air.rs +++ b/air-script/src/tests/trace_col_groups/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - trace_col_groups::trace_col_groups::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::trace_col_groups::trace_col_groups::PublicInputs, }; #[derive(Clone)] @@ -46,9 +46,9 @@ impl AirTester for TraceColGroupAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_trace_col_groups_air, - crate::trace_col_groups::trace_col_groups::TraceColGroupAir, + crate::tests::trace_col_groups::trace_col_groups::TraceColGroupAir, TraceColGroupAirTester, 1024 ); diff --git a/air-script/tests/trace_col_groups/trace_col_groups.air b/air-script/src/tests/trace_col_groups/trace_col_groups.air similarity index 100% rename from air-script/tests/trace_col_groups/trace_col_groups.air rename to air-script/src/tests/trace_col_groups/trace_col_groups.air diff --git a/air-script/tests/trace_col_groups/trace_col_groups.rs b/air-script/src/tests/trace_col_groups/trace_col_groups.rs similarity index 100% rename from air-script/tests/trace_col_groups/trace_col_groups.rs rename to air-script/src/tests/trace_col_groups/trace_col_groups.rs diff --git a/air-script/src/tests/trace_col_groups/trace_col_groups_plonky3.rs b/air-script/src/tests/trace_col_groups/trace_col_groups_plonky3.rs new file mode 100644 index 000000000..5a0b38979 --- /dev/null +++ b/air-script/src/tests/trace_col_groups/trace_col_groups_plonky3.rs @@ -0,0 +1,43 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 9; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 0; +pub const PERIOD: usize = 0; +pub const NUM_PUBLIC_VALUES: usize = 16; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct TraceColGroupAir; + +impl MidenAir for TraceColGroupAir { + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[8].clone().into()); + + // Main integrity/transition constraints + builder.when_transition().assert_zero(main_next[2].clone().into() - (main_current[2].clone().into() + AB::Expr::ONE)); + builder.when_transition().assert_zero(main_next[1].clone().into() - (main_current[1].clone().into() - AB::Expr::ONE)); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/variables/mod.rs b/air-script/src/tests/variables/mod.rs new file mode 100644 index 000000000..98685f537 --- /dev/null +++ b/air-script/src/tests/variables/mod.rs @@ -0,0 +1,9 @@ +mod test_air_plonky3; +mod test_air_winterfell; +#[rustfmt::skip] +#[allow(clippy::all)] +mod variables; +#[rustfmt::skip] +#[allow(clippy::all)] +#[allow(unused_imports)] +mod variables_plonky3; diff --git a/air-script/src/tests/variables/test_air_plonky3.rs b/air-script/src/tests/variables/test_air_plonky3.rs new file mode 100644 index 000000000..356a4d9fb --- /dev/null +++ b/air-script/src/tests/variables/test_air_plonky3.rs @@ -0,0 +1,53 @@ +use p3_field::PrimeField64; +use p3_miden_air::RowMajorMatrix; + +use crate::{ + generate_air_plonky3_test_with_airscript_traits, + tests::variables::variables_plonky3::{MAIN_WIDTH, VariablesAir}, +}; + +pub fn generate_trace_rows(inputs: Vec) -> RowMajorMatrix { + let num_rows = 512; + let trace_length = num_rows * MAIN_WIDTH; + + let mut long_trace = F::zero_vec(trace_length); + + let mut trace = RowMajorMatrix::new(long_trace, MAIN_WIDTH); + + let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[F; MAIN_WIDTH]>() }; + assert!(prefix.is_empty(), "Alignment should match"); + assert!(suffix.is_empty(), "Alignment should match"); + assert_eq!(rows.len(), num_rows); + + // Initialize first row + rows[0][0] = F::ONE; + rows[0][1] = F::ZERO; + rows[0][2] = F::ZERO; + rows[0][3] = F::ZERO; + + // Fill subsequent rows using direct access to the rows array + for i in 1..num_rows { + let col_0_prev = rows[i - 1][0]; + let col_1_prev = rows[i - 1][1]; + let col_2_prev = rows[i - 1][2]; + let col_3_prev = rows[i - 1][3]; + + // Update current row based on previous values + rows[i][0] = col_0_prev; + rows[i][1] = F::ONE; + rows[i][2] = col_2_prev; + rows[i][3] = col_3_prev; + } + + trace +} + +fn generate_inputs() -> Vec { + vec![1; 32] +} + +fn generate_var_len_pub_inputs<'a>() -> Vec>> { + vec![] +} + +generate_air_plonky3_test_with_airscript_traits!(test_air_plonky3, VariablesAir); diff --git a/air-script/tests/variables/test_air.rs b/air-script/src/tests/variables/test_air_winterfell.rs similarity index 81% rename from air-script/tests/variables/test_air.rs rename to air-script/src/tests/variables/test_air_winterfell.rs index c45e18516..d384975b3 100644 --- a/air-script/tests/variables/test_air.rs +++ b/air-script/src/tests/variables/test_air_winterfell.rs @@ -3,9 +3,9 @@ use winter_math::fields::f64::BaseElement as Felt; use winterfell::{Trace, TraceTable}; use crate::{ - generate_air_test, - helpers::{AirTester, MyTraceTable}, - variables::variables::PublicInputs, + generate_air_winterfell_test, + test_utils::winterfell_traits::{AirTester, MyTraceTable}, + tests::variables::variables::PublicInputs, }; #[derive(Clone)] @@ -40,9 +40,9 @@ impl AirTester for VariablesAirTester { } } -generate_air_test!( +generate_air_winterfell_test!( test_variables_air, - crate::variables::variables::VariablesAir, + crate::tests::variables::variables::VariablesAir, VariablesAirTester, 1024 ); diff --git a/air-script/tests/variables/variables.air b/air-script/src/tests/variables/variables.air similarity index 100% rename from air-script/tests/variables/variables.air rename to air-script/src/tests/variables/variables.air diff --git a/air-script/tests/variables/variables.rs b/air-script/src/tests/variables/variables.rs similarity index 100% rename from air-script/tests/variables/variables.rs rename to air-script/src/tests/variables/variables.rs diff --git a/air-script/src/tests/variables/variables_plonky3.rs b/air-script/src/tests/variables/variables_plonky3.rs new file mode 100644 index 000000000..57cbc460d --- /dev/null +++ b/air-script/src/tests/variables/variables_plonky3.rs @@ -0,0 +1,59 @@ +use p3_field::{ExtensionField, Field, PrimeCharacteristicRing}; +use p3_matrix::Matrix; +use p3_matrix::dense::RowMajorMatrixView; +use p3_matrix::stack::VerticalPair; +use p3_miden_air::{BusType, MidenAir, MidenAirBuilder, RowMajorMatrix}; + +pub const MAIN_WIDTH: usize = 4; +pub const AUX_WIDTH: usize = 0; +pub const NUM_PERIODIC_VALUES: usize = 1; +pub const PERIOD: usize = 8; +pub const NUM_PUBLIC_VALUES: usize = 32; +pub const MAX_BETA_CHALLENGE_POWER: usize = 0; + +pub struct VariablesAir; + +impl MidenAir for VariablesAir +where F: Field, + EF: ExtensionField, +{ + fn width(&self) -> usize { + MAIN_WIDTH + } + + fn num_public_values(&self) -> usize { + NUM_PUBLIC_VALUES + } + + fn periodic_table(&self) -> Vec> { + vec![ + vec![F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(1), F::from_u64(0)], + ] + } + + fn eval(&self, builder: &mut AB) + where AB: MidenAirBuilder, + { + let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect("Wrong number of public values"); + let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect("Wrong number of periodic values"); + // Note: for now, we do not have any preprocessed values + // let preprocessed = builder.preprocessed(); + let main = builder.main(); + let (main_current, main_next) = ( + main.row_slice(0).unwrap(), + main.row_slice(1).unwrap(), + ); + + // Main boundary constraints + builder.when_first_row().assert_zero(main_current[1].clone().into()); + builder.when_last_row().assert_zero(main_current[1].clone().into() - AB::Expr::ONE); + + // Main integrity/transition constraints + builder.assert_zero(main_current[0].clone().into() * main_current[0].clone().into() - main_current[0].clone().into()); + builder.when_transition().assert_zero_ext(AB::ExprEF::from(periodic_values[0].clone().into()) * (AB::ExprEF::from(main_next[0].clone().into()) - AB::ExprEF::from(main_current[0].clone().into()))); + builder.assert_zero((AB::Expr::ONE - main_current[0].clone().into()) * (main_current[3].clone().into() - main_current[1].clone().into() - main_current[2].clone().into()) - (AB::Expr::from_u64(6) - (AB::Expr::from_u64(7) - main_current[0].clone().into()))); + builder.when_transition().assert_zero(main_current[0].clone().into() * (main_current[3].clone().into() - main_current[1].clone().into() * main_current[2].clone().into()) - (AB::Expr::ONE - main_next[0].clone().into())); + + // Aux integrity/transition constraints + } +} \ No newline at end of file diff --git a/air-script/src/tests/winterfell.rs b/air-script/src/tests/winterfell.rs new file mode 100644 index 000000000..5e9f044df --- /dev/null +++ b/air-script/src/tests/winterfell.rs @@ -0,0 +1,371 @@ +use expect_test::expect_file; + +use crate::test_utils::codegen::{Target, Test}; + +#[test] +fn binary() { + let generated_air = Test::new("src/tests/binary/binary.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["binary/binary.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn bitwise() { + let generated_air = Test::new("src/tests/bitwise/bitwise.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["bitwise/bitwise.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_complex() { + let generated_air = Test::new("src/tests/buses/buses_complex.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["buses/buses_complex.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_simple() { + let generated_air = Test::new("src/tests/buses/buses_simple.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["buses/buses_simple.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_simple_with_evaluators() { + let generated_air = Test::new("src/tests/buses/buses_simple_with_evaluators.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["buses/buses_simple.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_varlen_boundary_both() { + let generated_air = Test::new("src/tests/buses/buses_varlen_boundary_both.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["buses/buses_varlen_boundary_both.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_varlen_boundary_first() { + let generated_air = Test::new("src/tests/buses/buses_varlen_boundary_first.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["buses/buses_varlen_boundary_first.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn buses_varlen_boundary_last() { + let generated_air = Test::new("src/tests/buses/buses_varlen_boundary_last.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["buses/buses_varlen_boundary_last.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn computed_indices_complex() { + let generated_air = + Test::new("src/tests/computed_indices/computed_indices_complex.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["computed_indices/computed_indices_complex.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn computed_indices_simple() { + let generated_air = + Test::new("src/tests/computed_indices/computed_indices_simple.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["computed_indices/computed_indices_simple.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn constant_in_range() { + let generated_air = Test::new("src/tests/constant_in_range/constant_in_range.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["constant_in_range/constant_in_range.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn constants() { + let generated_air = Test::new("src/tests/constants/constants.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["constants/constants.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn constraint_comprehension() { + let generated_air = + Test::new("src/tests/constraint_comprehension/constraint_comprehension.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["constraint_comprehension/constraint_comprehension.rs"]; + expected.assert_eq(&generated_air); + + let generated_air = + Test::new("src/tests/constraint_comprehension/cc_with_evaluators.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["constraint_comprehension/constraint_comprehension.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn evaluators_nested_slice_call() { + let generated_air = + Test::new("src/tests/evaluators/evaluators_nested_slice_call.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["evaluators/evaluators_nested_slice_call.rs"]; + expected.assert_eq(&generated_air); +} + +// TODO: add support for nested slicing in general expressions. +// +// #[test] +// fn evaluators_slice_slicing() { +// let generated_air = +// Test::new("src/tests/evaluators/evaluators_slice_slicing.air".to_string()) +// .transpile(Target::Winterfell) +// .unwrap(); +// +// let expected = expect_file!["evaluators/evaluators_slice_slicing.rs"]; +// expected.assert_eq(&generated_air); +// } + +#[test] +fn evaluators_slice() { + let generated_air = Test::new("src/tests/evaluators/evaluators_slice.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["evaluators/evaluators_slice.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn evaluators() { + let generated_air = Test::new("src/tests/evaluators/evaluators.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["evaluators/evaluators.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn fibonacci() { + let generated_air = Test::new("src/tests/fibonacci/fibonacci.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["fibonacci/fibonacci.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn functions_complex() { + let generated_air = Test::new("src/tests/functions/functions_complex.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["functions/functions_complex.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn functions_simple() { + let generated_air = Test::new("src/tests/functions/functions_simple.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["functions/functions_simple.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn functions_simple_inlined() { + // make sure that the constraints generated using inlined functions are the same as the ones + // generated using regular functions + let generated_air = Test::new("src/tests/functions/inlined_functions_simple.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["functions/functions_simple.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn indexed_trace_access() { + let generated_air = + Test::new("src/tests/indexed_trace_access/indexed_trace_access.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["indexed_trace_access/indexed_trace_access.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn list_comprehension() { + let generated_air = + Test::new("src/tests/list_comprehension/list_comprehension.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["list_comprehension/list_comprehension.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn list_comprehension_nested() { + let generated_air = + Test::new("src/tests/list_comprehension/list_comprehension_nested.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["list_comprehension/list_comprehension_nested.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn list_folding() { + let generated_air = Test::new("src/tests/list_folding/list_folding.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["list_folding/list_folding.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn periodic_columns() { + let generated_air = Test::new("src/tests/periodic_columns/periodic_columns.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["periodic_columns/periodic_columns.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn pub_inputs() { + let generated_air = Test::new("src/tests/pub_inputs/pub_inputs.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["pub_inputs/pub_inputs.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors() { + let generated_air = Test::new("src/tests/selectors/selectors.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["selectors/selectors.rs"]; + expected.assert_eq(&generated_air); + + let generated_air = Test::new("src/tests/selectors/selectors_with_evaluators.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["selectors/selectors_with_evaluators.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors_combine_simple() { + let generated_air = Test::new("src/tests/selectors/selectors_combine_simple.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["selectors/selectors_combine_simple.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors_combine_complex() { + let generated_air = Test::new("src/tests/selectors/selectors_combine_complex.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["selectors/selectors_combine_complex.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn selectors_combine_with_list_comprehensions() { + let generated_air = + Test::new("src/tests/selectors/selectors_combine_with_list_comprehensions.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["selectors/selectors_combine_with_list_comprehensions.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn system() { + let generated_air = Test::new("src/tests/system/system.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["system/system.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn trace_col_groups() { + let generated_air = Test::new("src/tests/trace_col_groups/trace_col_groups.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["trace_col_groups/trace_col_groups.rs"]; + expected.assert_eq(&generated_air); +} + +#[test] +fn variables() { + let generated_air = Test::new("src/tests/variables/variables.air".to_string()) + .transpile(Target::Winterfell) + .unwrap(); + + let expected = expect_file!["variables/variables.rs"]; + expected.assert_eq(&generated_air); +} diff --git a/air-script/tests/binary/mod.rs b/air-script/tests/binary/mod.rs deleted file mode 100644 index 0582511b7..000000000 --- a/air-script/tests/binary/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod binary; -mod test_air; diff --git a/air-script/tests/bitwise/mod.rs b/air-script/tests/bitwise/mod.rs deleted file mode 100644 index 5798d90dc..000000000 --- a/air-script/tests/bitwise/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod bitwise; -mod test_air; diff --git a/air-script/tests/buses/buses_varlen_boundary_last.air b/air-script/tests/buses/buses_varlen_boundary_last.air deleted file mode 100644 index 9819cbc21..000000000 --- a/air-script/tests/buses/buses_varlen_boundary_last.air +++ /dev/null @@ -1,29 +0,0 @@ -def BusesAir - -trace_columns { - main: [a], -} - -buses { - multiset p, - logup q, -} - -public_inputs { - outputs: [[2]], -} - -boundary_constraints { - enf p.first = null; - enf q.first = null; - enf p.last = outputs; - enf q.last = outputs; -} - -integrity_constraints { - p.insert(1) when a; - p.remove(1) when (a - 1); - q.insert(1, 2) when a; - q.insert(1, 2) when a; - q.remove(1, 2) with 2; -} diff --git a/air-script/tests/buses/mod.rs b/air-script/tests/buses/mod.rs deleted file mode 100644 index 933d13ee5..000000000 --- a/air-script/tests/buses/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod buses_complex; -#[rustfmt::skip] -#[allow(clippy::all)] -mod buses_simple; -#[rustfmt::skip] -#[allow(clippy::all)] -mod buses_varlen_boundary_both; -#[rustfmt::skip] -#[allow(clippy::all)] -mod buses_varlen_boundary_first; -#[rustfmt::skip] -#[allow(clippy::all)] -mod buses_varlen_boundary_last; -mod test_air; diff --git a/air-script/tests/codegen/mod.rs b/air-script/tests/codegen/mod.rs deleted file mode 100644 index d802f40b1..000000000 --- a/air-script/tests/codegen/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod helpers; -mod winterfell; diff --git a/air-script/tests/codegen/winterfell.rs b/air-script/tests/codegen/winterfell.rs deleted file mode 100644 index 2fa96ccb8..000000000 --- a/air-script/tests/codegen/winterfell.rs +++ /dev/null @@ -1,367 +0,0 @@ -use expect_test::expect_file; - -use super::helpers::{Target, Test}; - -#[test] -fn binary() { - let generated_air = Test::new("tests/binary/binary.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../binary/binary.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn bitwise() { - let generated_air = Test::new("tests/bitwise/bitwise.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../bitwise/bitwise.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn buses_complex() { - let generated_air = Test::new("tests/buses/buses_complex.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../buses/buses_complex.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn buses_simple() { - let generated_air = Test::new("tests/buses/buses_simple.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../buses/buses_simple.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn buses_simple_with_evaluators() { - let generated_air = Test::new("tests/buses/buses_simple_with_evaluators.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../buses/buses_simple.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn buses_varlen_boundary_both() { - let generated_air = Test::new("tests/buses/buses_varlen_boundary_both.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../buses/buses_varlen_boundary_both.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn buses_varlen_boundary_first() { - let generated_air = Test::new("tests/buses/buses_varlen_boundary_first.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../buses/buses_varlen_boundary_first.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn buses_varlen_boundary_last() { - let generated_air = Test::new("tests/buses/buses_varlen_boundary_last.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../buses/buses_varlen_boundary_last.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn computed_indices_complex() { - let generated_air = - Test::new("tests/computed_indices/computed_indices_complex.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../computed_indices/computed_indices_complex.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn computed_indices_simple() { - let generated_air = Test::new("tests/computed_indices/computed_indices_simple.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../computed_indices/computed_indices_simple.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn constant_in_range() { - let generated_air = Test::new("tests/constant_in_range/constant_in_range.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../constant_in_range/constant_in_range.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn constants() { - let generated_air = Test::new("tests/constants/constants.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../constants/constants.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn constraint_comprehension() { - let generated_air = - Test::new("tests/constraint_comprehension/constraint_comprehension.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../constraint_comprehension/constraint_comprehension.rs"]; - expected.assert_eq(&generated_air); - - let generated_air = - Test::new("tests/constraint_comprehension/cc_with_evaluators.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../constraint_comprehension/constraint_comprehension.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn evaluators() { - let generated_air = Test::new("tests/evaluators/evaluators.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../evaluators/evaluators.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn evaluators_slice() { - let generated_air = Test::new("tests/evaluators/evaluators_slice.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../evaluators/evaluators_slice.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn evaluators_nested_slice_call() { - let generated_air = Test::new("tests/evaluators/evaluators_nested_slice_call.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../evaluators/evaluators_nested_slice_call.rs"]; - expected.assert_eq(&generated_air); -} - -// TODO: add support for nested slicing in general expressions. -// -// #[test] -// fn evaluators_slice_slicing() { -// let generated_air = Test::new("tests/evaluators/evaluators_slice_slicing.air".to_string()) -// .transpile(Target::Winterfell) -// .unwrap(); -// -// let expected = expect_file!["../evaluators/evaluators_slice_slicing.rs"]; -// expected.assert_eq(&generated_air); -// } - -#[test] -fn fibonacci() { - let generated_air = Test::new("tests/fibonacci/fibonacci.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../fibonacci/fibonacci.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn functions_complex() { - let generated_air = Test::new("tests/functions/functions_complex.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../functions/functions_complex.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn functions_simple() { - let generated_air = Test::new("tests/functions/functions_simple.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../functions/functions_simple.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn functions_simple_inlined() { - // make sure that the constraints generated using inlined functions are the same as the ones - // generated using regular functions - let generated_air = Test::new("tests/functions/inlined_functions_simple.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../functions/functions_simple.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn indexed_trace_access() { - let generated_air = - Test::new("tests/indexed_trace_access/indexed_trace_access.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../indexed_trace_access/indexed_trace_access.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn list_comprehension() { - let generated_air = Test::new("tests/list_comprehension/list_comprehension.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../list_comprehension/list_comprehension.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn list_comprehension_nested() { - let generated_air = - Test::new("tests/list_comprehension/list_comprehension_nested.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../list_comprehension/list_comprehension_nested.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn list_folding() { - let generated_air = Test::new("tests/list_folding/list_folding.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../list_folding/list_folding.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn periodic_columns() { - let generated_air = Test::new("tests/periodic_columns/periodic_columns.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../periodic_columns/periodic_columns.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn pub_inputs() { - let generated_air = Test::new("tests/pub_inputs/pub_inputs.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../pub_inputs/pub_inputs.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn selectors() { - let generated_air = Test::new("tests/selectors/selectors.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../selectors/selectors.rs"]; - expected.assert_eq(&generated_air); - - let generated_air = Test::new("tests/selectors/selectors_with_evaluators.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../selectors/selectors_with_evaluators.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn selectors_combine_simple() { - let generated_air = Test::new("tests/selectors/selectors_combine_simple.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../selectors/selectors_combine_simple.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn selectors_combine_complex() { - let generated_air = Test::new("tests/selectors/selectors_combine_complex.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../selectors/selectors_combine_complex.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn selectors_combine_with_list_comprehensions() { - let generated_air = - Test::new("tests/selectors/selectors_combine_with_list_comprehensions.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../selectors/selectors_combine_with_list_comprehensions.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn system() { - let generated_air = Test::new("tests/system/system.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../system/system.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn trace_col_groups() { - let generated_air = Test::new("tests/trace_col_groups/trace_col_groups.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../trace_col_groups/trace_col_groups.rs"]; - expected.assert_eq(&generated_air); -} - -#[test] -fn variables() { - let generated_air = Test::new("tests/variables/variables.air".to_string()) - .transpile(Target::Winterfell) - .unwrap(); - - let expected = expect_file!["../variables/variables.rs"]; - expected.assert_eq(&generated_air); -} diff --git a/air-script/tests/computed_indices/mod.rs b/air-script/tests/computed_indices/mod.rs deleted file mode 100644 index 5f582b136..000000000 --- a/air-script/tests/computed_indices/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod computed_indices_complex; -#[rustfmt::skip] -#[allow(clippy::all)] -mod computed_indices_simple; -mod test_air; diff --git a/air-script/tests/constant_in_range/mod.rs b/air-script/tests/constant_in_range/mod.rs deleted file mode 100644 index a071cd958..000000000 --- a/air-script/tests/constant_in_range/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod constant_in_range; -mod test_air; diff --git a/air-script/tests/constants/mod.rs b/air-script/tests/constants/mod.rs deleted file mode 100644 index 526f76f5e..000000000 --- a/air-script/tests/constants/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod constants; -mod test_air; diff --git a/air-script/tests/constraint_comprehension/mod.rs b/air-script/tests/constraint_comprehension/mod.rs deleted file mode 100644 index 16222fbd1..000000000 --- a/air-script/tests/constraint_comprehension/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod constraint_comprehension; -mod test_air; diff --git a/air-script/tests/evaluators/mod.rs b/air-script/tests/evaluators/mod.rs deleted file mode 100644 index cf380f979..000000000 --- a/air-script/tests/evaluators/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod evaluators; -mod test_air; diff --git a/air-script/tests/fibonacci/mod.rs b/air-script/tests/fibonacci/mod.rs deleted file mode 100644 index 8fa9af072..000000000 --- a/air-script/tests/fibonacci/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod fibonacci; -mod test_air; diff --git a/air-script/tests/functions/mod.rs b/air-script/tests/functions/mod.rs deleted file mode 100644 index 4a85b16cc..000000000 --- a/air-script/tests/functions/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod functions_complex; -#[rustfmt::skip] -#[allow(clippy::all)] -mod functions_simple; -mod test_air; diff --git a/air-script/tests/helpers/macros.rs b/air-script/tests/helpers/macros.rs deleted file mode 100644 index e134b8d48..000000000 --- a/air-script/tests/helpers/macros.rs +++ /dev/null @@ -1,29 +0,0 @@ -// Helper macros for test generation - -/// Generates an AIR test function with the standard boilerplate -/// -/// # Arguments -/// * `test_name` - The identifier for the test function (e.g., `test_binary_air`) -/// * `air_name` - The identifier for the AIR struct (e.g., `BinaryAir`) -/// * `tester_name` - The identifier for the `AirTester` struct (e.g., `BinaryAirTester`) -/// * `trace_length` - The length of the trace for the test (e.g., `32` or `1024`) -#[macro_export] -macro_rules! generate_air_test { - ($test_name:ident, $air_name:path, $tester_name:ident, $trace_length:expr) => { - #[test] - fn $test_name() { - use winter_math::fields::f64::BaseElement as Felt; - let air_tester = Box::new($tester_name {}); - let length = $trace_length; - - let main_trace = air_tester.build_main_trace(length); - let aux_trace = air_tester.build_aux_trace(length); - let pub_inputs = air_tester.public_inputs(); - let trace_info = air_tester.build_trace_info(length); - let options = air_tester.build_proof_options(); - - let air = <$air_name>::new(trace_info, pub_inputs, options); - main_trace.validate::<$air_name, Felt>(&air, aux_trace.as_ref()); - } - }; -} diff --git a/air-script/tests/indexed_trace_access/mod.rs b/air-script/tests/indexed_trace_access/mod.rs deleted file mode 100644 index 7aee9d4e8..000000000 --- a/air-script/tests/indexed_trace_access/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod indexed_trace_access; -mod test_air; diff --git a/air-script/tests/list_comprehension/mod.rs b/air-script/tests/list_comprehension/mod.rs deleted file mode 100644 index 5f090bea2..000000000 --- a/air-script/tests/list_comprehension/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod list_comprehension; -mod test_air; diff --git a/air-script/tests/list_folding/mod.rs b/air-script/tests/list_folding/mod.rs deleted file mode 100644 index 5e992bc22..000000000 --- a/air-script/tests/list_folding/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod list_folding; -mod test_air; diff --git a/air-script/tests/periodic_columns/mod.rs b/air-script/tests/periodic_columns/mod.rs deleted file mode 100644 index fd9a501e7..000000000 --- a/air-script/tests/periodic_columns/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod periodic_columns; -mod test_air; diff --git a/air-script/tests/pub_inputs/mod.rs b/air-script/tests/pub_inputs/mod.rs deleted file mode 100644 index afb27e904..000000000 --- a/air-script/tests/pub_inputs/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod pub_inputs; -mod test_air; diff --git a/air-script/tests/selectors/mod.rs b/air-script/tests/selectors/mod.rs deleted file mode 100644 index 9e7f0b7ff..000000000 --- a/air-script/tests/selectors/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod selectors; -#[rustfmt::skip] -#[allow(clippy::all)] -mod selectors_combine_simple; -#[rustfmt::skip] -#[allow(clippy::all)] -mod selectors_combine_complex; -#[rustfmt::skip] -#[allow(clippy::all)] -mod selectors_with_evaluators; -#[rustfmt::skip] -#[allow(clippy::all)] -mod selectors_combine_with_list_comprehensions; -mod test_air; diff --git a/air-script/tests/system/mod.rs b/air-script/tests/system/mod.rs deleted file mode 100644 index 10cac5f2b..000000000 --- a/air-script/tests/system/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[rustfmt::skip] -#[allow(clippy::all)] -mod system; -mod test_air; diff --git a/air-script/tests/trace_col_groups/mod.rs b/air-script/tests/trace_col_groups/mod.rs deleted file mode 100644 index 706feb822..000000000 --- a/air-script/tests/trace_col_groups/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod test_air; -#[rustfmt::skip] -#[allow(clippy::all)] -mod trace_col_groups; diff --git a/air-script/tests/variables/mod.rs b/air-script/tests/variables/mod.rs deleted file mode 100644 index 31656aa8c..000000000 --- a/air-script/tests/variables/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod test_air; -#[rustfmt::skip] -#[allow(clippy::all)] -mod variables; diff --git a/air/src/ir/mod.rs b/air/src/ir/mod.rs index 0393558ba..06758392c 100644 --- a/air/src/ir/mod.rs +++ b/air/src/ir/mod.rs @@ -162,7 +162,7 @@ use alloc::collections::BTreeMap; use miden_diagnostics::{SourceSpan, Spanned}; -use crate::graph::AlgebraicGraph; +use crate::{NodeIndex, graph::AlgebraicGraph}; /// The intermediate representation of a complete AirScript program /// @@ -195,6 +195,18 @@ pub struct Air { /// /// Only their name, type, and the first and last boundary constraints are stored here. pub buses: BTreeMap, + /// Buses initial values used for auxiliary trace generation (indexed by bus index) + pub buses_initial_values: BTreeMap, + /// Buses transition expressions used for auxiliary trace generation (indexed by bus index) + /// The tuple contains the numerator and an optional denominator operation (p_prime = numerator + /// if None or numerator / denominator), computed in the `ExpandBuses` Air pass depending on + /// the bus type + /// - for multiset buses: p' = p * columns_inserted_in_bus / columns_removed_from_bus + /// - for logup buses, if u corresponds to the columns inserted when s1 and v to the columns + /// removed when s2 + /// q' = q + s1 / u - s2 / v + /// = (q * u * v + s1 * v - s2 * u) / (u * v) + pub buses_transitions: BTreeMap)>, } impl Default for Air { fn default() -> Self { @@ -217,6 +229,8 @@ impl Air { num_random_values: 0, constraints: Default::default(), buses: Default::default(), + buses_initial_values: Default::default(), + buses_transitions: Default::default(), } } diff --git a/air/src/passes/common_subexpression_elimination.rs b/air/src/passes/common_subexpression_elimination.rs index bc162b0da..38711c81c 100644 --- a/air/src/passes/common_subexpression_elimination.rs +++ b/air/src/passes/common_subexpression_elimination.rs @@ -32,6 +32,28 @@ impl Pass for CommonSubexpressionElimination<'_> { // Update constraints with the new node indices ir.constraints.renumber_and_deduplicate_constraints(&renumbering_map); + // Iterate over all bus transition expression and renumber their node indices + for (_, value) in ir.buses_initial_values.iter_mut() { + let new_value_index = *renumbering_map + .get(value) + .expect("Error: cannot find value index in renumbering map"); + *value = new_value_index; + } + + // Iterate over all bus transition expression and renumber their node indices + for (_, (numerator, denominator)) in ir.buses_transitions.iter_mut() { + let new_numerator_index = *renumbering_map + .get(numerator) + .expect("Error: cannot find numerator index in renumbering map"); + *numerator = new_numerator_index; + if let Some(denominator) = denominator { + let new_denominator_index = *renumbering_map + .get(denominator) + .expect("Error: cannot find denominator index in renumbering map"); + *denominator = new_denominator_index; + } + } + Ok(ir) } } diff --git a/air/src/passes/expand_buses.rs b/air/src/passes/expand_buses.rs index ff02487dd..fb5a33513 100644 --- a/air/src/passes/expand_buses.rs +++ b/air/src/passes/expand_buses.rs @@ -63,6 +63,7 @@ impl Pass for BusOpExpand<'_> { bus_ops, bus_access, bus_access_with_offset, + bus_index, ); }, BusType::Logup => { @@ -71,6 +72,7 @@ impl Pass for BusOpExpand<'_> { bus_ops, bus_access, bus_access_with_offset, + bus_index, ); }, } @@ -139,6 +141,19 @@ impl<'a> BusOpExpand<'a> { }; // Store the generated constraint ir.constraints.insert_constraint(TraceSegmentId::Aux, root, domain); + + // Also store the initial value for auxiliary trace generation + if boundary == Boundary::First { + // TODO: May be invalid? For now, we put its value to zero + if let BusBoundary::PublicInputTable(_) = bus_boundary { + let value = ir + .constraint_graph_mut() + .insert_node(Operation::Value(crate::Value::Constant(0))); + ir.buses_initial_values.insert(bus_index, value); + } else { + ir.buses_initial_values.insert(bus_index, value); + } + } } /// Helper function to expand the integrity constraint of a multiset bus @@ -148,6 +163,7 @@ impl<'a> BusOpExpand<'a> { bus_ops: Vec, bus_access: NodeIndex, bus_access_with_offset: NodeIndex, + bus_index: usize, ) { let graph = ir.constraint_graph_mut(); @@ -234,8 +250,14 @@ impl<'a> BusOpExpand<'a> { // 6. Create the resulting constraint and insert it into the graph let root = graph.insert_node(Operation::Sub(p_prod, p_prime_prod)); - ir.constraints - .insert_constraint(TraceSegmentId::Aux, root, ConstraintDomain::EveryRow); + ir.constraints.insert_constraint( + TraceSegmentId::Aux, + root, + ConstraintDomain::EveryFrame(2), + ); + + // Also store the expression to computed p_prime for auxiliary trace generation + ir.buses_transitions.insert(bus_index, (p_prod, p_prime_factor)); } /// Helper function to expand the integrity constraint of a logup bus @@ -245,6 +267,7 @@ impl<'a> BusOpExpand<'a> { bus_ops: Vec, bus_access: NodeIndex, bus_access_with_offset: NodeIndex, + bus_index: usize, ) { let graph = ir.constraint_graph_mut(); // Example: @@ -372,7 +395,23 @@ impl<'a> BusOpExpand<'a> { // 5. Create the resulting constraint let root = graph.insert_node(Operation::Sub(q_term, q_prime_term)); - ir.constraints - .insert_constraint(TraceSegmentId::Aux, root, ConstraintDomain::EveryRow); + + // Also store the expression to computed q_prime for auxiliary trace generation + // Note: TODO: Potentially adapt CSE to handle this properly, otherwise indices might + // change... + let numerator = match terms_removed_from_bus { + Some(terms_removed_from_bus) => { + graph.insert_node(Operation::Sub(q_term, terms_removed_from_bus)) + }, + None => q_term, + }; + + ir.constraints.insert_constraint( + TraceSegmentId::Aux, + root, + ConstraintDomain::EveryFrame(2), + ); + + ir.buses_transitions.insert(bus_index, (numerator, total_factors)); } } diff --git a/codegen/plonky3/Cargo.toml b/codegen/plonky3/Cargo.toml new file mode 100644 index 000000000..eb7766ea2 --- /dev/null +++ b/codegen/plonky3/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "air-codegen-plonky3" +version = "0.5.0" +description = "Plonky3 code generator for the AirScript language" +authors.workspace = true +readme = "README.md" +license.workspace = true +repository.workspace = true +categories = ["compilers", "cryptography"] +keywords = ["air", "stark", "plonky3", "zero-knowledge", "zkp"] +edition.workspace = true +rust-version.workspace = true + +[dependencies] +air-ir = { package = "air-ir", path = "../../air", version = "0.5" } +anyhow = { workspace = true } +codegen = "0.2" diff --git a/codegen/plonky3/README.md b/codegen/plonky3/README.md new file mode 100644 index 000000000..8ee666dfd --- /dev/null +++ b/codegen/plonky3/README.md @@ -0,0 +1,3 @@ +# Plonky3 Code Generator + +This crate contains a code generator targeting the [Plonky3 prover](https://github.com/Plonky3/Plonky3) Rust library. diff --git a/codegen/plonky3/src/air/boundary_constraints.rs b/codegen/plonky3/src/air/boundary_constraints.rs new file mode 100644 index 000000000..79a4f4f7a --- /dev/null +++ b/codegen/plonky3/src/air/boundary_constraints.rs @@ -0,0 +1,29 @@ +use air_ir::{Air, TraceSegmentId}; +use codegen::Function; + +use crate::air::graph::constraint_to_string; + +/// Adds the main boundary constraints to the generated code. +pub(super) fn add_main_boundary_constraints(eval_func: &mut Function, ir: &Air) { + eval_func.line(""); + eval_func.line("// Main boundary constraints"); + for constraint in ir.boundary_constraints(TraceSegmentId::Main) { + let assertion = constraint_to_string(ir, constraint, true); + eval_func.line(assertion); + } +} + +#[allow(dead_code)] +/// Adds the aux boundary constraints to the generated code. +pub(super) fn add_aux_boundary_constraints(eval_func: &mut Function, ir: &Air) { + eval_func.line(""); + eval_func.line("// Aux boundary constraints"); + for constraint in ir.boundary_constraints(TraceSegmentId::Aux) { + let assertion = constraint_to_string(ir, constraint, true); + eval_func.line(assertion); + + // TODO: better check assumptions on aux boundary constraints: + // - start only with empty buses, + // - end with values derived from builder.aux_bus_boundary_value() + } +} diff --git a/codegen/plonky3/src/air/graph.rs b/codegen/plonky3/src/air/graph.rs new file mode 100644 index 000000000..f9c27ed7c --- /dev/null +++ b/codegen/plonky3/src/air/graph.rs @@ -0,0 +1,222 @@ +use air_ir::{ + Air, ConstraintDomain, ConstraintRoot, NodeIndex, Operation, TraceAccess, TraceSegmentId, Value, +}; + +use crate::air::ElemType; + +// RUST STRING GENERATION FOR THE CONSTRAINT GRAPH +// ================================================================================================ + +/// Code generation trait for generating Rust code strings from IR types related to constraints and +/// the [AlgebraicGraph]. +pub trait Codegen { + fn to_string(&self, ir: &Air, elem_type: ElemType) -> String; +} + +impl Codegen for TraceAccess { + fn to_string(&self, _ir: &Air, elem_type: ElemType) -> String { + let frame = self.segment.to_string(); + let row_offset = match self.row_offset { + 0 => { + format!("current[{}]", self.column) + }, + 1 => { + format!("next[{}]", self.column) + }, + _ => panic!("Plonky3 doesn't support row offsets greater than 1."), + }; + match elem_type { + ElemType::Base => format!("{frame}_{row_offset}.clone().into()"), + ElemType::Ext => format!("AB::ExprEF::from({frame}_{row_offset}.clone().into())"), + ElemType::ExtFieldElem => format!("EF::from({frame}_{row_offset}.clone())"), + } + } +} + +impl Codegen for NodeIndex { + fn to_string(&self, ir: &Air, elem_type: ElemType) -> String { + let op = ir.constraint_graph().node(self).op(); + op.to_string(ir, elem_type) + } +} + +impl Codegen for Operation { + fn to_string(&self, ir: &Air, elem_type: ElemType) -> String { + match self { + Operation::Value(value) => value.to_string(ir, elem_type), + Operation::Add(..) => binary_op_to_string(ir, elem_type, self), + Operation::Sub(..) => binary_op_to_string(ir, elem_type, self), + Operation::Mul(..) => binary_op_to_string(ir, elem_type, self), + } + } +} + +impl Codegen for Value { + fn to_string(&self, ir: &Air, elem_type: ElemType) -> String { + match self { + Value::Constant(0) => match elem_type { + ElemType::Base => format!("AB::Expr::ZERO"), + ElemType::Ext => format!("AB::ExprEF::ZERO"), + ElemType::ExtFieldElem => format!("EF::ZERO"), + }, + Value::Constant(1) => match elem_type { + ElemType::Base => format!("AB::Expr::ONE"), + ElemType::Ext => format!("AB::ExprEF::ONE"), + ElemType::ExtFieldElem => format!("EF::ONE"), + }, + Value::Constant(value) => match elem_type { + ElemType::Base => format!("AB::Expr::from_u64({value})"), + ElemType::Ext => format!("AB::ExprEF::from_u64({value})"), + ElemType::ExtFieldElem => format!("EF::from_u64({value})"), + }, + Value::TraceAccess(trace_access) => trace_access.to_string(ir, elem_type), + Value::PublicInput(air_ir::PublicInputAccess { name, index }) => { + let get_public_input_offset = |name: &str| { + ir.public_inputs() + .take_while(|pi| pi.name() != name) + .map(|pi| pi.size()) + .sum::() + }; + format!("public_values[{}].into()", get_public_input_offset(name.as_str()) + index) + }, + Value::PeriodicColumn(pc) => { + let index = + ir.periodic_columns.iter().position(|(qid, _)| qid == &pc.name).unwrap(); + match elem_type { + ElemType::Base => format!("AB::Expr::from(periodic_values[{index}].clone())"), + ElemType::Ext => { + format!("AB::ExprEF::from(periodic_values[{index}].clone().into())") + }, + ElemType::ExtFieldElem => { + format!("AB::EF::from(periodic_values[{index}].clone())") + }, + } + }, + Value::PublicInputTable(public_input_table_access) => { + let idx = ir + .reduced_public_input_table_accesses() + .iter() + .position(|pi| pi == public_input_table_access) + .unwrap(); + format!("aux_bus_boundary_values[{idx}].into()") + }, + Value::RandomValue(idx) => { + if *idx == 0 { + if let ElemType::ExtFieldElem = elem_type { + format!("alpha") + } else { + format!("alpha.into()") + } + } else { + if let ElemType::ExtFieldElem = elem_type { + format!("beta_challenges[{}]", idx - 1) + } else { + format!("beta_challenges[{}].into()", idx - 1) + } + } + }, + } + } +} + +/// Returns a string representation of a binary operation. +fn binary_op_to_string(ir: &Air, elem_type: ElemType, op: &Operation) -> String { + match op { + Operation::Add(l_idx, r_idx) => { + let lhs = l_idx.to_string(ir, elem_type); + let rhs = r_idx.to_string(ir, elem_type); + format!("{lhs} + {rhs}") + }, + Operation::Sub(l_idx, r_idx) => { + let lhs = l_idx.to_string(ir, elem_type); + let rhs = if ir.constraint_graph().node(r_idx).op().precedence() <= op.precedence() { + format!("({})", r_idx.to_string(ir, elem_type)) + } else { + r_idx.to_string(ir, elem_type) + }; + format!("{lhs} - {rhs}") + }, + Operation::Mul(l_idx, r_idx) => { + let lhs_op = ir.constraint_graph().node(l_idx).op(); + let rhs_op = ir.constraint_graph().node(r_idx).op(); + + let lhs = if lhs_op.precedence() < op.precedence() { + format!("({})", l_idx.to_string(ir, elem_type)) + } else { + l_idx.to_string(ir, elem_type) + }; + let rhs = if rhs_op.precedence() < op.precedence() { + format!("({})", r_idx.to_string(ir, elem_type)) + } else { + r_idx.to_string(ir, elem_type) + }; + + match (lhs_op, rhs_op) { + (_, Operation::Value(Value::Constant(2))) => format!("{lhs}.double()"), + (Operation::Value(Value::Constant(2)), _) => format!("{rhs}.double()"), + _ => format!("{lhs} * {rhs}"), + } + }, + _ => panic!("unsupported operation"), + } +} + +/// Recursively determines if the expression depends on extension field values (i.e., aux trace, +/// random values, periodic columns, or public input tables). +pub fn needs_extension_field(ir: &Air, expr_root: NodeIndex) -> bool { + let op = ir.constraint_graph().node(&expr_root).op(); + match op { + Operation::Value(value) => match value { + Value::TraceAccess(trace_access) => trace_access.segment == TraceSegmentId::Aux, + Value::Constant(_) => false, + Value::PeriodicColumn(_) => true, + Value::PublicInput(_) => false, + Value::PublicInputTable(_) => true, + Value::RandomValue(_) => true, + }, + Operation::Add(lhs, rhs) | Operation::Sub(lhs, rhs) | Operation::Mul(lhs, rhs) => { + needs_extension_field(ir, *lhs) || needs_extension_field(ir, *rhs) + }, + } +} + +/// Returns the appropriate domain flag string for the given [ConstraintDomain]. +fn get_boundary_domain_flag_str(domain: &ConstraintDomain) -> &'static str { + match domain { + ConstraintDomain::FirstRow => ".when_first_row()", + ConstraintDomain::LastRow => ".when_last_row()", + _ => unreachable!("Invalid domain for boundary constraints"), + } +} + +/// Returns the appropriate domain flag string for the given [ConstraintDomain]. +fn get_integrity_domain_flag_str(domain: &ConstraintDomain) -> &'static str { + match domain { + ConstraintDomain::EveryFrame(_) => ".when_transition()", + ConstraintDomain::EveryRow => "", + _ => unreachable!("Invalid domain for integrity constraints"), + } +} + +pub fn constraint_to_string(ir: &Air, constraint: &ConstraintRoot, in_boundary: bool) -> String { + let expr_root = constraint.node_index(); + let needs_extension_field = needs_extension_field(ir, *expr_root); + let elem_type = if needs_extension_field { + ElemType::Ext + } else { + ElemType::Base + }; + let expr_root_string = expr_root.to_string(ir, elem_type); + + // If the constraint is a transition constraint (depends on the next row), we do not + // evaluate it in the last row, with the `when_transition` method. + let domain_flag = if in_boundary { + get_boundary_domain_flag_str(&constraint.domain()) + } else { + get_integrity_domain_flag_str(&constraint.domain()) + }; + let extension_field_flag = if needs_extension_field { "_ext" } else { "" }; + let assertion = + format!("builder{domain_flag}.assert_zero{extension_field_flag}({expr_root_string});"); + assertion +} diff --git a/codegen/plonky3/src/air/integrity_constraints.rs b/codegen/plonky3/src/air/integrity_constraints.rs new file mode 100644 index 000000000..bdcce45e1 --- /dev/null +++ b/codegen/plonky3/src/air/integrity_constraints.rs @@ -0,0 +1,24 @@ +use air_ir::{Air, TraceSegmentId}; +use codegen::Function; + +use crate::air::graph::constraint_to_string; + +/// Adds the main integrity constraints to the generated code. +pub(super) fn add_main_integrity_constraints(eval_func: &mut Function, ir: &Air) { + eval_func.line(""); + eval_func.line("// Main integrity/transition constraints"); + for constraint in ir.integrity_constraints(TraceSegmentId::Main) { + let assertion = constraint_to_string(ir, constraint, false); + eval_func.line(assertion); + } +} + +/// Adds the aux integrity constraints to the generated code. +pub(super) fn add_aux_integrity_constraints(eval_func: &mut Function, ir: &Air) { + eval_func.line(""); + eval_func.line("// Aux integrity/transition constraints"); + for constraint in ir.integrity_constraints(TraceSegmentId::Aux) { + let assertion = constraint_to_string(ir, constraint, false); + eval_func.line(assertion); + } +} diff --git a/codegen/plonky3/src/air/mod.rs b/codegen/plonky3/src/air/mod.rs new file mode 100644 index 000000000..32a585ce9 --- /dev/null +++ b/codegen/plonky3/src/air/mod.rs @@ -0,0 +1,278 @@ +mod boundary_constraints; +mod graph; +mod integrity_constraints; + +use air_ir::Air; + +use super::Scope; +use crate::air::{ + boundary_constraints::add_main_boundary_constraints, + graph::Codegen, + integrity_constraints::{add_aux_integrity_constraints, add_main_integrity_constraints}, +}; + +#[derive(Debug, Clone, Copy)] +pub enum ElemType { + Base, + Ext, + ExtFieldElem, +} + +// HELPERS TO GENERATE AN IMPLEMENTATION OF THE PLONKY3 AIR TRAIT +// ================================================================================================ + +/// Updates the provided scope with a new Air struct and Plonky3 Air trait implementation +/// which are equivalent the provided AirIR. +pub(super) fn add_air(scope: &mut Scope, ir: &Air) { + let name = ir.name(); + + // add the constants needed (outside any traits for object safety). + add_constants(scope, ir); + + // add the Air struct and its base implementation. + add_air_struct(scope, ir, name); + + // add the aux trace generation utils if needed + if ir.num_random_values > 0 { + add_aux_trace_utils(scope, ir, name); + } +} + +/// Updates the provided scope with constants needed for the custom Air struct and trait +/// implementations. +fn add_constants(scope: &mut Scope, ir: &Air) { + let main_width = ir.trace_segment_widths[0]; + let aux_width = ir.trace_segment_widths.get(1).cloned().unwrap_or(0); + let num_periodic_values = ir.periodic_columns().count(); + let period = ir.periodic_columns().map(|col| col.period()).max().unwrap_or(0); + let num_public_values = ir + .public_inputs() + .map(|public_input| match public_input { + air_ir::PublicInput::Vector { size, .. } => size, + air_ir::PublicInput::Table { .. } => &0, + }) + .sum::(); + let max_beta_challenge_power = ir.num_random_values.saturating_sub(1); + + let constants = [ + format!("pub const MAIN_WIDTH: usize = {main_width};"), + format!("pub const AUX_WIDTH: usize = {aux_width};"), + format!("pub const NUM_PERIODIC_VALUES: usize = {num_periodic_values};"), + format!("pub const PERIOD: usize = {period};"), + format!("pub const NUM_PUBLIC_VALUES: usize = {num_public_values};"), + format!("pub const MAX_BETA_CHALLENGE_POWER: usize = {max_beta_challenge_power};"), + ]; + + scope.raw(constants.join("\n")); +} + +/// Updates the provided scope with a custom Air struct. +fn add_air_struct(scope: &mut Scope, ir: &Air, name: &str) { + // define the custom Air struct. + scope.new_struct(name).vis("pub"); + + // add the custom MidenAir implementation block + let miden_air_impl = + scope.new_impl(name).generic("F").generic("EF").impl_trait("MidenAir"); + + if ir.num_random_values > 0 || ir.periodic_columns().count() > 0 { + miden_air_impl.bound("F", "Field").bound("EF", "ExtensionField"); + } + + // add the width function + miden_air_impl.new_fn("width").arg_ref_self().ret("usize").line("MAIN_WIDTH"); + + // add the num_public_values function if needed + if ir.periodic_columns().count() > 0 { + // add the custom BaseAirWithPublicValues implementation block + miden_air_impl + .new_fn("num_public_values") + .arg_ref_self() + .ret("usize") + .line("NUM_PUBLIC_VALUES"); + } + + // add the periodic_table function if needed + if ir.periodic_columns().count() > 0 { + let periodic_table_func = + miden_air_impl.new_fn("periodic_table").arg_ref_self().ret("Vec>"); + periodic_table_func.line("vec!["); + for col in ir.periodic_columns() { + let values_str = col.values + .iter() + .map(|v| format!("F::from_u64({v})")) // or use a custom formatter if needed + .collect::>() + .join(", "); + periodic_table_func.line(format!(" vec![{values_str}],")); + } + periodic_table_func.line("]"); + } + + // add the num_randomness and aux_width functions if needed + if ir.num_random_values > 0 { + miden_air_impl + .new_fn("num_randomness") + .arg_ref_self() + .ret("usize") + .line("1 + MAX_BETA_CHALLENGE_POWER"); + + miden_air_impl.new_fn("aux_width").arg_ref_self().ret("usize").line("AUX_WIDTH"); + + let bus_types_fn = miden_air_impl.new_fn("bus_types").arg_ref_self().ret("Vec"); + bus_types_fn.line("vec!["); + for (_id, bus) in &ir.buses { + let bus_type_str = match bus.bus_type { + air_ir::BusType::Multiset => "BusType::Multiset", + air_ir::BusType::Logup => "BusType::Logup", + }; + bus_types_fn.line(format!(" {bus_type_str},")); + } + bus_types_fn.line("]"); + } + + // add the build_aux_trace function if needed + if ir.num_random_values > 0 { + let build_aux_trace_func = miden_air_impl + .new_fn("build_aux_trace") + .arg_ref_self() + .arg("_main", "&RowMajorMatrix") + .arg("_challenges", "&[EF]") + .ret("Option>"); + build_aux_trace_func.line("// Note: consider using Some(build_aux_trace_with_miden_vm::(_main, _challenges, module)) if you want to build the aux trace using Miden VM aux trace builders."); + build_aux_trace_func.line(""); + build_aux_trace_func.line("let num_rows = _main.height();"); + build_aux_trace_func.line("let trace_length = num_rows * AUX_WIDTH;"); + build_aux_trace_func.line("let mut long_trace = EF::zero_vec(trace_length);"); + build_aux_trace_func.line("let mut trace = RowMajorMatrix::new(long_trace, AUX_WIDTH);"); + build_aux_trace_func.line("let (prefix, rows, suffix) = unsafe { trace.values.align_to_mut::<[EF; AUX_WIDTH]>() };"); + build_aux_trace_func.line("assert!(prefix.is_empty(), \"Alignment should match\");"); + build_aux_trace_func.line("assert!(suffix.is_empty(), \"Alignment should match\");"); + build_aux_trace_func.line("assert_eq!(rows.len(), num_rows);"); + build_aux_trace_func.line("// Initialize first row"); + build_aux_trace_func.line("let initial_values = Self::buses_initial_values::();"); + build_aux_trace_func.line("for j in 0..AUX_WIDTH {"); + build_aux_trace_func.line(" rows[0][j] = initial_values[j];"); + build_aux_trace_func.line("}"); + build_aux_trace_func.line("// Fill subsequent rows using direct access to the rows array"); + build_aux_trace_func.line("for i in 0..num_rows-1 {"); + build_aux_trace_func.line(" let i_next = (i + 1) % num_rows;"); + build_aux_trace_func.line(" let main_local = _main.row_slice(i).unwrap(); // i < height so unwrap should never fail."); + build_aux_trace_func.line(" let main_next = _main.row_slice(i_next).unwrap(); // i_next < height so unwrap should never fail."); + build_aux_trace_func.line(" let main = VerticalPair::new("); + build_aux_trace_func.line(" RowMajorMatrixView::new_row(&*main_local),"); + build_aux_trace_func.line(" RowMajorMatrixView::new_row(&*main_next),"); + build_aux_trace_func.line(" );"); + build_aux_trace_func.line(format!(" let periodic_values: [_; NUM_PERIODIC_VALUES] = <{name} as MidenAir>::periodic_table(self).iter().map(|col| col[i % col.len()]).collect::>().try_into().expect(\"Wrong number of periodic values\");")); + build_aux_trace_func.line(" let prev_row = &rows[i];"); + build_aux_trace_func.line(" let next_row = Self::buses_transitions::("); + build_aux_trace_func.line(" &main,"); + build_aux_trace_func.line(" _challenges,"); + build_aux_trace_func.line(" &periodic_values,"); + build_aux_trace_func.line(" prev_row,"); + build_aux_trace_func.line(" );"); + build_aux_trace_func.line(" for j in 0..AUX_WIDTH {"); + build_aux_trace_func.line(" rows[i+1][j] = next_row[j];"); + build_aux_trace_func.line(" }"); + build_aux_trace_func.line("}"); + build_aux_trace_func.line("let trace_f = trace.flatten_to_base();"); + build_aux_trace_func.line("Some(trace_f)"); + } + + // add the eval function + let eval_func = miden_air_impl + .new_fn("eval") + .generic("AB") + .bound("AB", "MidenAirBuilder") + .arg_ref_self() + .arg("builder", "&mut AB"); + eval_func.line("let public_values: [_; NUM_PUBLIC_VALUES] = builder.public_values().try_into().expect(\"Wrong number of public values\");"); + eval_func.line("let periodic_values: [_; NUM_PERIODIC_VALUES] = builder.periodic_evals().try_into().expect(\"Wrong number of periodic values\");"); + + eval_func.line("// Note: for now, we do not have any preprocessed values"); + eval_func.line("// let preprocessed = builder.preprocessed();"); + + eval_func.line("let main = builder.main();"); + eval_func.line("let (main_current, main_next) = ("); + eval_func.line(" main.row_slice(0).unwrap(),"); + eval_func.line(" main.row_slice(1).unwrap(),"); + eval_func.line(");"); + + // Only add aux if there are random values + if ir.num_random_values > 0 { + eval_func.line("let (&alpha, beta_challenges) = builder.permutation_randomness().split_first().expect(\"Wrong number of randomness\");"); + eval_func.line("let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect(\"Wrong number of randomness\");"); + eval_func.line("let aux_bus_boundary_values: [_; AUX_WIDTH] = builder.aux_bus_boundary_values().try_into().expect(\"Wrong number of aux bus boundary values\");"); + eval_func.line("let aux = builder.permutation();"); + eval_func.line("let (aux_current, aux_next) = ("); + eval_func.line(" aux.row_slice(0).unwrap(),"); + eval_func.line(" aux.row_slice(1).unwrap(),"); + eval_func.line(");"); + } + + add_main_boundary_constraints(eval_func, ir); + + add_main_integrity_constraints(eval_func, ir); + + // Note: Plonky3 automatically adds aux boundary constraints + //add_aux_boundary_constraints(eval_func, ir); + + add_aux_integrity_constraints(eval_func, ir); +} + +/// Updates the provided scope with aux trace generation utilities. +fn add_aux_trace_utils(scope: &mut Scope, ir: &Air, name: &str) { + let aux_generation_impl = scope.new_impl(name); + + // add the bus_initial_values function + let buses_initial_values_func = aux_generation_impl + .new_fn("buses_initial_values") + .generic("F") + .generic("EF") + .bound("F", "Field") + .bound("EF", "ExtensionField") + .ret("Vec"); + buses_initial_values_func.line("vec!["); + for (_bus_id, value) in ir.buses_initial_values.iter() { + let value_str = value.to_string(ir, ElemType::ExtFieldElem); + + buses_initial_values_func.line(format!(" {},", value_str)); + } + buses_initial_values_func.line("]"); + + // add the bus_transitions function + let buses_transitions_func = aux_generation_impl + .new_fn("buses_transitions") + .generic("F") + .generic("EF") + .bound("F", "Field") + .bound("EF", "ExtensionField") + .arg("main", "&VerticalPair, RowMajorMatrixView>") + .arg("challenges", "&[EF]") + .arg("periodic_evals", "&[F]") + .arg("aux_current", "&[EF]") + .ret("Vec"); + + buses_transitions_func.line("let (main_current, main_next) = ("); + buses_transitions_func.line(" main.row_slice(0).unwrap(),"); + buses_transitions_func.line(" main.row_slice(1).unwrap(),"); + buses_transitions_func.line(");"); + buses_transitions_func.line("let (&alpha, beta_challenges) = challenges.split_first().expect(\"Wrong number of randomness\");"); + buses_transitions_func.line("let beta_challenges: [_; MAX_BETA_CHALLENGE_POWER] = beta_challenges.try_into().expect(\"Wrong number of randomness\");"); + + buses_transitions_func.line("let periodic_values: [_; NUM_PERIODIC_VALUES] = periodic_evals.try_into().expect(\"Wrong number of periodic values\");"); + + buses_transitions_func.line("vec!["); + for (_bus_id, (numerator, denominator)) in ir.buses_transitions.iter() { + let numerator_str = numerator.to_string(ir, ElemType::ExtFieldElem); + + let aux_next_value_str = if let Some(denom) = denominator { + let denominator_str = denom.to_string(ir, ElemType::ExtFieldElem); + format!("({}) * ({}).inverse()", numerator_str, denominator_str) + } else { + numerator_str + }; + + buses_transitions_func.line(format!(" {},", aux_next_value_str)); + } + buses_transitions_func.line("]"); +} diff --git a/codegen/plonky3/src/imports.rs b/codegen/plonky3/src/imports.rs new file mode 100644 index 000000000..d96c0dd40 --- /dev/null +++ b/codegen/plonky3/src/imports.rs @@ -0,0 +1,16 @@ +use super::Scope; + +/// Adds the required imports to the provided scope. +pub(super) fn add_imports(scope: &mut Scope) { + // add plonky3 imports + scope.import("p3_field", "ExtensionField"); + scope.import("p3_field", "Field"); + scope.import("p3_field", "PrimeCharacteristicRing"); + scope.import("p3_matrix", "Matrix"); + scope.import("p3_matrix::dense", "RowMajorMatrixView"); + scope.import("p3_matrix::stack", "VerticalPair"); + scope.import("p3_miden_air", "BusType"); + scope.import("p3_miden_air", "MidenAir"); + scope.import("p3_miden_air", "MidenAirBuilder"); + scope.import("p3_miden_air", "RowMajorMatrix"); +} diff --git a/codegen/plonky3/src/lib.rs b/codegen/plonky3/src/lib.rs new file mode 100644 index 000000000..703a8ee94 --- /dev/null +++ b/codegen/plonky3/src/lib.rs @@ -0,0 +1,28 @@ +use air_ir::Air; +use codegen::Scope; + +mod air; +mod imports; + +// GENERATE RUST CODE FOR WINTERFELL AIR +// ================================================================================================ + +/// CodeGenerator is used to generate a Rust implementation of the Plonky3 STARK prover library's +/// Air trait. The generated Air expresses the constraints specified by the AirIR used to build the +/// CodeGenerator. +pub struct CodeGenerator; +impl air_ir::CodeGenerator for CodeGenerator { + type Output = String; + + fn generate(&self, ir: &Air) -> anyhow::Result { + let mut scope = Scope::new(); + + // add plonky3 imports. + imports::add_imports(&mut scope); + + // add an Air struct and plonky3 Air trait implementation for the provided AirIR. + air::add_air(&mut scope, ir); + + Ok(scope.to_string()) + } +} diff --git a/docs/src/backends.md b/docs/src/backends.md index ce8b023e7..1a7f883fe 100644 --- a/docs/src/backends.md +++ b/docs/src/backends.md @@ -1,14 +1,16 @@ # Backends -AirScript currently comes bundled with two backends: +AirScript currently comes bundled with three backends: - [Winterfell backend](https://github.com/0xMiden/air-script/tree/main/codegen/winterfell) which outputs `Air` trait implementation for the [Winterfell prover](https://github.com/facebook/winterfell) (Rust). +- [Plonky3 backend](https://github.com/0xMiden/air-script/tree/main/codegen/plonky3) which outputs `Air` trait implementation for the [Plonky3 prover](https://github.com/Plonky3/Plonky3) (Rust). - [ACE backend](https://github.com/0xMiden/air-script/tree/main/codegen/ace) which outputs arithmetic circuits for Miden VM's ACE (Arithmetic Circuit Evaluation) chiplet for recursive STARK proof verification. -These backends can be used programmatically as crates. +These backends can be used programmatically as crates. -The Winterfell backend can also be used via AirScript CLI by specifying `--target` flag. For example, the following will output Winterfell `Air` trait implementation for AIR constraints described in `example.air` file: +The Winterfell and Plonky3 backends can also be used via AirScript CLI by specifying `--target` flag. For example, the following will output Winterfell and Plonky3 `Air` trait implementation for AIR constraints described in `example.air` file: ```bash # Make sure to run from the project root directory ./target/release/airc transpile examples/example.air --target winterfell +./target/release/airc transpile examples/example.air --target plonky3 ``` In both cases we assumed that the CLI has been compiled as described [here](./introduction.md#cli). diff --git a/docs/src/introduction.md b/docs/src/introduction.md index b6b565286..d39ae08d3 100644 --- a/docs/src/introduction.md +++ b/docs/src/introduction.md @@ -7,6 +7,7 @@ Currently, AirScript is on version 0.3, which includes about 95% of features needed to describe Miden VM constraints, and supports generation of constraint evaluation code for the following backends: - **Winterfell**: Generates Rust code implementing the `Air` trait for the [Winterfell prover](https://github.com/facebook/winterfell) +- **Plonky3**: Generates Rust code implementing the `Air` trait implementation for the [Plonky3 prover](https://github.com/Plonky3/Plonky3) - **ACE**: Generates arithmetic circuits for Miden VM's ACE (Arithmetic Circuit Evaluation) chiplet for recursive proof verification AirScript includes the following features: @@ -57,6 +58,8 @@ Then, run the `airc` target with the `transpile` option. For example: ``` This will output constraint evaluation code targeted for the Winterfell prover. +Using the `--target plonky3` argument will output constraint evaluation code targeted for the Plonky3 prover instead. + You can use the `help` option to see other available options. ```ignore @@ -75,6 +78,5 @@ The following changes are some of the improvements under consideration for futur - removing unnecessary nodes from the `AlgebraicGraph` of boundary and integrity constraints. - combining integrity constraints with mutually exclusive selectors to reduce the total number of constraints. - additional language targets for simplifying verifier implementations: - - Plonky3 AirBuilder. - JSON-based constraint syntax. - formal verification diff --git a/parser/src/ast/module.rs b/parser/src/ast/module.rs index 0b7a0eacc..bfec2ed0c 100644 --- a/parser/src/ast/module.rs +++ b/parser/src/ast/module.rs @@ -435,9 +435,6 @@ impl Module { conflicting_declaration(diagnostics, "function", prev.span(), function.name.span()); return Err(SemanticAnalysisError::NameConflict(function.name.span())); } - - println!("Declared function: {:?}", function.name); - self.functions.insert(function.name, function); Ok(()) diff --git a/scripts/generate_all_e2e_tests.sh b/scripts/generate_all_e2e_tests.sh new file mode 100644 index 000000000..9725910d1 --- /dev/null +++ b/scripts/generate_all_e2e_tests.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# Notes: +# - Run from root of repository +# - We avoid looping on all found air-script/src/tests/**/*.air files to make it easier to notice changes + +cargo build --release + +# Winterfell Backend + +./target/release/airc transpile --target winterfell ./air-script/src/tests/binary/binary.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/bitwise/bitwise.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/buses/buses_complex.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/buses/buses_simple.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/buses/buses_simple_with_evaluators.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/buses/buses_varlen_boundary_both.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/buses/buses_varlen_boundary_last.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/buses/buses_varlen_boundary_first.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/constant_in_range/constant_in_range.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/constants/constants.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/constraint_comprehension/constraint_comprehension.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/evaluators/evaluators_nested_slice_call.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/evaluators/evaluators_slice.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/evaluators/evaluators.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/fibonacci/fibonacci.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/functions/functions_simple.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/functions/functions_complex.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/indexed_trace_access/indexed_trace_access.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/list_comprehension/list_comprehension_nested.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/list_comprehension/list_comprehension.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/list_folding/list_folding.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/periodic_columns/periodic_columns.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/pub_inputs/pub_inputs.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/selectors/selectors_combine_complex.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/selectors/selectors_combine_simple.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/selectors/selectors_combine_with_list_comprehensions.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/selectors/selectors_with_evaluators.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/selectors/selectors.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/system/system.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/trace_col_groups/trace_col_groups.air +./target/release/airc transpile --target winterfell ./air-script/src/tests/variables/variables.air + +# Plonky3 Backend +./target/release/airc transpile --target plonky3 ./air-script/src/tests/binary/binary.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/bitwise/bitwise.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/buses/buses_complex.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/buses/buses_simple.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/buses/buses_simple_with_evaluators.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/buses/buses_varlen_boundary_both.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/buses/buses_varlen_boundary_last.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/buses/buses_varlen_boundary_first.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/computed_indices/computed_indices_complex.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/computed_indices/computed_indices_simple.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/constant_in_range/constant_in_range.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/constants/constants.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/constraint_comprehension/constraint_comprehension.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/evaluators/evaluators_nested_slice_call.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/evaluators/evaluators_slice.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/evaluators/evaluators.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/fibonacci/fibonacci.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/functions/functions_simple.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/functions/functions_complex.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/indexed_trace_access/indexed_trace_access.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/list_comprehension/list_comprehension_nested.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/list_comprehension/list_comprehension.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/list_folding/list_folding.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/periodic_columns/periodic_columns.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/pub_inputs/pub_inputs.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/selectors/selectors_combine_complex.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/selectors/selectors_combine_simple.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/selectors/selectors_combine_with_list_comprehensions.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/selectors/selectors_with_evaluators.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/selectors/selectors.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/system/system.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/trace_col_groups/trace_col_groups.air +./target/release/airc transpile --target plonky3 ./air-script/src/tests/variables/variables.air