From 3846b4d388babd694a735ec6eb2143d8a45b5eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Thu, 6 Jun 2024 12:23:53 -0400 Subject: [PATCH 1/3] refactor: rename WithEvents::Events -> WithEvents::InputEvents --- core/src/air/machine.rs | 10 +-- core/src/alu/add_sub/mod.rs | 2 +- core/src/alu/bitwise/mod.rs | 2 +- core/src/alu/divrem/mod.rs | 2 +- core/src/alu/lt/mod.rs | 2 +- core/src/alu/mul/mod.rs | 2 +- core/src/alu/sll/mod.rs | 2 +- core/src/alu/sr/mod.rs | 2 +- core/src/bytes/trace.rs | 2 +- core/src/cpu/trace.rs | 2 +- core/src/memory/global.rs | 2 +- core/src/memory/program.rs | 2 +- .../field/extensions/quadratic/mod.rs | 4 +- .../field/extensions/quadratic/sqrt.rs | 4 +- core/src/operations/field/field_den.rs | 4 +- .../operations/field/field_inner_product.rs | 4 +- core/src/operations/field/field_op.rs | 4 +- core/src/operations/field/field_sqrt.rs | 4 +- core/src/program/mod.rs | 2 +- core/src/runtime/record.rs | 68 +++++++++---------- .../precompiles/blake3/compress/trace.rs | 2 +- .../precompiles/bls12_381/g1_decompress.rs | 2 +- .../syscall/precompiles/bls12_381/g2_add.rs | 2 +- .../precompiles/bls12_381/g2_double.rs | 2 +- .../src/syscall/precompiles/edwards/ed_add.rs | 2 +- .../precompiles/edwards/ed_decompress.rs | 2 +- core/src/syscall/precompiles/field/add.rs | 2 +- core/src/syscall/precompiles/field/mul.rs | 2 +- core/src/syscall/precompiles/field/sub.rs | 2 +- .../syscall/precompiles/keccak256/trace.rs | 2 +- .../src/syscall/precompiles/quad_field/add.rs | 2 +- .../src/syscall/precompiles/quad_field/mul.rs | 2 +- .../src/syscall/precompiles/quad_field/sub.rs | 2 +- .../precompiles/secp256k1/decompress.rs | 2 +- .../precompiles/sha256/compress/trace.rs | 2 +- .../precompiles/sha256/extend/trace.rs | 2 +- .../weierstrass/weierstrass_add.rs | 2 +- .../weierstrass/weierstrass_double.rs | 2 +- derive/src/lib.rs | 10 +-- recursion/core/src/cpu/trace.rs | 2 +- recursion/core/src/fri_fold/mod.rs | 2 +- recursion/core/src/memory/air.rs | 2 +- recursion/core/src/multi/mod.rs | 14 ++-- recursion/core/src/poseidon2/trace.rs | 2 +- recursion/core/src/poseidon2_wide/external.rs | 2 +- recursion/core/src/program/mod.rs | 2 +- recursion/core/src/range_check/trace.rs | 2 +- recursion/core/src/runtime/record.rs | 16 ++--- 48 files changed, 108 insertions(+), 108 deletions(-) diff --git a/core/src/air/machine.rs b/core/src/air/machine.rs index da71e27db..5ed3a05f6 100644 --- a/core/src/air/machine.rs +++ b/core/src/air/machine.rs @@ -14,7 +14,7 @@ use crate::{ pub trait WithEvents<'a>: Sized { /// output of a functional lens from the Record to /// refs of those events relative to the AIR. - type Events: 'a; + type InputEvents: 'a; } /// A trait intended for implementation on Records that may store events related to Chips, @@ -23,7 +23,7 @@ pub trait WithEvents<'a>: Sized { /// /// The name is inspired by (but not conformant to) functional optics ( https://doi.org/10.1145/1232420.1232424 ) pub trait EventLens WithEvents<'b>>: Indexed { - fn events(&self) -> >::Events; + fn events(&self) -> >::InputEvents; } //////////////// Derive macro shenanigans //////////////////////////////////////////////// @@ -63,10 +63,10 @@ where R: EventLens, U: for<'b> WithEvents<'b>, // see https://github.com/rust-lang/rust/issues/86702 for the empty parameter - F: for<'c> Fn(>::Events, &'c ()) -> >::Events, + F: for<'c> Fn(>::InputEvents, &'c ()) -> >::InputEvents, { - fn events<'c>(&'c self) -> >::Events { - let events: >::Events = self.record.events(); + fn events<'c>(&'c self) -> >::InputEvents { + let events: >::InputEvents = self.record.events(); (self.projection)(events, &()) } } diff --git a/core/src/alu/add_sub/mod.rs b/core/src/alu/add_sub/mod.rs index 55e200a9c..fe4869dac 100644 --- a/core/src/alu/add_sub/mod.rs +++ b/core/src/alu/add_sub/mod.rs @@ -58,7 +58,7 @@ pub struct AddSubCols { } impl<'a> WithEvents<'a> for AddSubChip { - type Events = ( + type InputEvents = ( // add events &'a [AluEvent], // sub events diff --git a/core/src/alu/bitwise/mod.rs b/core/src/alu/bitwise/mod.rs index 2b3e067b2..807597f9b 100644 --- a/core/src/alu/bitwise/mod.rs +++ b/core/src/alu/bitwise/mod.rs @@ -52,7 +52,7 @@ pub struct BitwiseCols { } impl<'a> WithEvents<'a> for BitwiseChip { - type Events = &'a [AluEvent]; + type InputEvents = &'a [AluEvent]; } impl MachineAir for BitwiseChip { diff --git a/core/src/alu/divrem/mod.rs b/core/src/alu/divrem/mod.rs index e12472be7..6a537a9ad 100644 --- a/core/src/alu/divrem/mod.rs +++ b/core/src/alu/divrem/mod.rs @@ -188,7 +188,7 @@ pub struct DivRemCols { } impl<'a> WithEvents<'a> for DivRemChip { - type Events = &'a [AluEvent]; + type InputEvents = &'a [AluEvent]; } impl MachineAir for DivRemChip { diff --git a/core/src/alu/lt/mod.rs b/core/src/alu/lt/mod.rs index c70b84f93..aabaef024 100644 --- a/core/src/alu/lt/mod.rs +++ b/core/src/alu/lt/mod.rs @@ -94,7 +94,7 @@ impl LtCols { } impl<'a> WithEvents<'a> for LtChip { - type Events = &'a [AluEvent]; + type InputEvents = &'a [AluEvent]; } impl MachineAir for LtChip { diff --git a/core/src/alu/mul/mod.rs b/core/src/alu/mul/mod.rs index 8681e5a4d..9fd35c3c7 100644 --- a/core/src/alu/mul/mod.rs +++ b/core/src/alu/mul/mod.rs @@ -124,7 +124,7 @@ pub struct MulCols { } impl<'a> WithEvents<'a> for MulChip { - type Events = &'a [AluEvent]; + type InputEvents = &'a [AluEvent]; } impl MachineAir for MulChip { diff --git a/core/src/alu/sll/mod.rs b/core/src/alu/sll/mod.rs index 39e3ad9f9..d914a4c47 100644 --- a/core/src/alu/sll/mod.rs +++ b/core/src/alu/sll/mod.rs @@ -99,7 +99,7 @@ pub struct ShiftLeftCols { } impl<'a> WithEvents<'a> for ShiftLeft { - type Events = &'a [AluEvent]; + type InputEvents = &'a [AluEvent]; } impl MachineAir for ShiftLeft { diff --git a/core/src/alu/sr/mod.rs b/core/src/alu/sr/mod.rs index a9d85e9e9..65141e0ec 100644 --- a/core/src/alu/sr/mod.rs +++ b/core/src/alu/sr/mod.rs @@ -131,7 +131,7 @@ pub struct ShiftRightCols { } impl<'a> WithEvents<'a> for ShiftRightChip { - type Events = &'a [AluEvent]; + type InputEvents = &'a [AluEvent]; } impl MachineAir for ShiftRightChip { diff --git a/core/src/bytes/trace.rs b/core/src/bytes/trace.rs index a792b0202..5a2d0de79 100644 --- a/core/src/bytes/trace.rs +++ b/core/src/bytes/trace.rs @@ -16,7 +16,7 @@ pub const NUM_ROWS: usize = 1 << 16; impl<'a, F: Field> WithEvents<'a> for ByteChip { // the byte lookups - type Events = &'a BTreeMap>; + type InputEvents = &'a BTreeMap>; } impl MachineAir for ByteChip { diff --git a/core/src/cpu/trace.rs b/core/src/cpu/trace.rs index 5aa4e02a1..b317cad43 100644 --- a/core/src/cpu/trace.rs +++ b/core/src/cpu/trace.rs @@ -19,7 +19,7 @@ use crate::runtime::{ExecutionRecord, Opcode, Program}; use crate::runtime::{MemoryRecordEnum, SyscallCode}; impl<'a> WithEvents<'a> for CpuChip { - type Events = &'a [CpuEvent]; + type InputEvents = &'a [CpuEvent]; } impl MachineAir for CpuChip { diff --git a/core/src/memory/global.rs b/core/src/memory/global.rs index 0003d5d02..2a64ea5ed 100644 --- a/core/src/memory/global.rs +++ b/core/src/memory/global.rs @@ -43,7 +43,7 @@ impl BaseAir for MemoryChip { } impl<'a> WithEvents<'a> for MemoryChip { - type Events = ( + type InputEvents = ( // initialize events &'a [MemoryInitializeFinalizeEvent], // finalize events diff --git a/core/src/memory/program.rs b/core/src/memory/program.rs index 074603b72..295c2a05e 100644 --- a/core/src/memory/program.rs +++ b/core/src/memory/program.rs @@ -51,7 +51,7 @@ impl MemoryProgramChip { } impl<'a> WithEvents<'a> for MemoryProgramChip { - type Events = &'a BTreeMap; + type InputEvents = &'a BTreeMap; } impl MachineAir for MemoryProgramChip { diff --git a/core/src/operations/field/extensions/quadratic/mod.rs b/core/src/operations/field/extensions/quadratic/mod.rs index b91674b0f..709347ec7 100644 --- a/core/src/operations/field/extensions/quadratic/mod.rs +++ b/core/src/operations/field/extensions/quadratic/mod.rs @@ -412,11 +412,11 @@ mod tests { } impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for QuadFieldOpChip

{ - type Events = &'a (); + type InputEvents = &'a (); } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &() } } diff --git a/core/src/operations/field/extensions/quadratic/sqrt.rs b/core/src/operations/field/extensions/quadratic/sqrt.rs index cfcd6c07b..ebaa08729 100644 --- a/core/src/operations/field/extensions/quadratic/sqrt.rs +++ b/core/src/operations/field/extensions/quadratic/sqrt.rs @@ -151,11 +151,11 @@ mod tests { } impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for QuadSqrtChip

{ - type Events = &'a (); + type InputEvents = &'a (); } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &() } } diff --git a/core/src/operations/field/field_den.rs b/core/src/operations/field/field_den.rs index ff96412f1..e522cabce 100644 --- a/core/src/operations/field/field_den.rs +++ b/core/src/operations/field/field_den.rs @@ -198,11 +198,11 @@ mod tests { } impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for FieldDenChip

{ - type Events = &'a (); + type InputEvents = &'a (); } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &() } } diff --git a/core/src/operations/field/field_inner_product.rs b/core/src/operations/field/field_inner_product.rs index 0d1ca42a4..100c67bcd 100644 --- a/core/src/operations/field/field_inner_product.rs +++ b/core/src/operations/field/field_inner_product.rs @@ -186,11 +186,11 @@ mod tests { } impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for FieldIpChip

{ - type Events = &'a (); + type InputEvents = &'a (); } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &() } } diff --git a/core/src/operations/field/field_op.rs b/core/src/operations/field/field_op.rs index 654a8d10e..be812c6fd 100644 --- a/core/src/operations/field/field_op.rs +++ b/core/src/operations/field/field_op.rs @@ -279,11 +279,11 @@ mod tests { } impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for FieldOpChip

{ - type Events = &'a (); + type InputEvents = &'a (); } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &() } } diff --git a/core/src/operations/field/field_sqrt.rs b/core/src/operations/field/field_sqrt.rs index cfd77c19e..08d4a8619 100644 --- a/core/src/operations/field/field_sqrt.rs +++ b/core/src/operations/field/field_sqrt.rs @@ -152,11 +152,11 @@ mod tests { } impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for EdSqrtChip

{ - type Events = &'a (); + type InputEvents = &'a (); } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &() } } diff --git a/core/src/program/mod.rs b/core/src/program/mod.rs index 623bb2dde..ef81a9816 100644 --- a/core/src/program/mod.rs +++ b/core/src/program/mod.rs @@ -53,7 +53,7 @@ impl ProgramChip { } impl<'a> WithEvents<'a> for ProgramChip { - type Events = ( + type InputEvents = ( // CPU events &'a [CpuEvent], // the Program diff --git a/core/src/runtime/record.rs b/core/src/runtime/record.rs index 180b6e22f..a8b5f1f71 100644 --- a/core/src/runtime/record.rs +++ b/core/src/runtime/record.rs @@ -146,139 +146,139 @@ pub struct ExecutionRecord { // Event lenses connect the record to the events relative to a particular chip impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { (&self.add_events, &self.sub_events) } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.bitwise_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.divrem_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.lt_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.mul_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.shift_left_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.shift_right_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.byte_lookups } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.cpu_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { (&self.memory_initialize_events, &self.memory_finalize_events) } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.program.memory_image } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { (&self.cpu_events, &self.program) } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.sha_extend_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.sha_compress_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.blake3_compress_inner_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.keccak_permute_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.bls12381_g1_decompress_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.secp256k1_decompress_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.bls12381_g2_add_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::Events { + fn events(&self) -> >::InputEvents { &self.bls12381_g2_double_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp_add_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp_sub_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp_mul_events } } @@ -286,7 +286,7 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp2_add_events } } @@ -294,7 +294,7 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp2_sub_events } } @@ -302,7 +302,7 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp2_mul_events } } @@ -310,19 +310,19 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.secp256k1_add_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_g1_add_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bn254_add_events } } @@ -330,7 +330,7 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.secp256k1_double_events } } @@ -338,19 +338,19 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_g1_double_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bn254_double_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::Events { + fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { &self.ed_add_events } } @@ -358,7 +358,7 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { fn events( &self, - ) -> as crate::air::WithEvents<'_>>::Events { + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.ed_decompress_events } } diff --git a/core/src/syscall/precompiles/blake3/compress/trace.rs b/core/src/syscall/precompiles/blake3/compress/trace.rs index 166bcd79d..2226eb173 100644 --- a/core/src/syscall/precompiles/blake3/compress/trace.rs +++ b/core/src/syscall/precompiles/blake3/compress/trace.rs @@ -20,7 +20,7 @@ use crate::{ }; impl<'a> WithEvents<'a> for Blake3CompressInnerChip { - type Events = &'a [Blake3CompressInnerEvent]; + type InputEvents = &'a [Blake3CompressInnerEvent]; } impl MachineAir for Blake3CompressInnerChip { diff --git a/core/src/syscall/precompiles/bls12_381/g1_decompress.rs b/core/src/syscall/precompiles/bls12_381/g1_decompress.rs index b01c18c46..de7e6ebee 100644 --- a/core/src/syscall/precompiles/bls12_381/g1_decompress.rs +++ b/core/src/syscall/precompiles/bls12_381/g1_decompress.rs @@ -230,7 +230,7 @@ impl Bls12381G1DecompressChip { } impl<'a> WithEvents<'a> for Bls12381G1DecompressChip { - type Events = &'a [Bls12381G1DecompressEvent]; + type InputEvents = &'a [Bls12381G1DecompressEvent]; } impl MachineAir for Bls12381G1DecompressChip { diff --git a/core/src/syscall/precompiles/bls12_381/g2_add.rs b/core/src/syscall/precompiles/bls12_381/g2_add.rs index a5e4ba019..0b4cdd3e7 100644 --- a/core/src/syscall/precompiles/bls12_381/g2_add.rs +++ b/core/src/syscall/precompiles/bls12_381/g2_add.rs @@ -277,7 +277,7 @@ impl BaseAir for Bls12381G2AffineAddChip { } impl<'a> WithEvents<'a> for Bls12381G2AffineAddChip { - type Events = &'a [Bls12381G2AffineAddEvent]; + type InputEvents = &'a [Bls12381G2AffineAddEvent]; } impl MachineAir for Bls12381G2AffineAddChip { diff --git a/core/src/syscall/precompiles/bls12_381/g2_double.rs b/core/src/syscall/precompiles/bls12_381/g2_double.rs index b43253add..315da8748 100644 --- a/core/src/syscall/precompiles/bls12_381/g2_double.rs +++ b/core/src/syscall/precompiles/bls12_381/g2_double.rs @@ -226,7 +226,7 @@ impl BaseAir for Bls12381G2AffineDoubleChip { } impl<'a> WithEvents<'a> for Bls12381G2AffineDoubleChip { - type Events = &'a [Bls12381G2AffineDoubleEvent]; + type InputEvents = &'a [Bls12381G2AffineDoubleEvent]; } impl MachineAir for Bls12381G2AffineDoubleChip { diff --git a/core/src/syscall/precompiles/edwards/ed_add.rs b/core/src/syscall/precompiles/edwards/ed_add.rs index e8cf48cf3..e0645207e 100644 --- a/core/src/syscall/precompiles/edwards/ed_add.rs +++ b/core/src/syscall/precompiles/edwards/ed_add.rs @@ -144,7 +144,7 @@ impl< } impl<'a, E: EllipticCurve + EdwardsParameters> WithEvents<'a> for EdAddAssignChip { - type Events = &'a [ECAddEvent]; + type InputEvents = &'a [ECAddEvent]; } impl MachineAir for EdAddAssignChip diff --git a/core/src/syscall/precompiles/edwards/ed_decompress.rs b/core/src/syscall/precompiles/edwards/ed_decompress.rs index 0478668da..396a06f6d 100644 --- a/core/src/syscall/precompiles/edwards/ed_decompress.rs +++ b/core/src/syscall/precompiles/edwards/ed_decompress.rs @@ -334,7 +334,7 @@ impl EdDecompressChip { } impl<'a, E: EdwardsParameters> WithEvents<'a> for EdDecompressChip { - type Events = &'a [EdDecompressEvent]; + type InputEvents = &'a [EdDecompressEvent]; } impl MachineAir for EdDecompressChip diff --git a/core/src/syscall/precompiles/field/add.rs b/core/src/syscall/precompiles/field/add.rs index 542e02739..299ca454b 100644 --- a/core/src/syscall/precompiles/field/add.rs +++ b/core/src/syscall/precompiles/field/add.rs @@ -114,7 +114,7 @@ pub fn create_fp_add_event( } impl<'a, FP: FieldParameters> WithEvents<'a> for FieldAddChip { - type Events = &'a [FieldAddEvent]; + type InputEvents = &'a [FieldAddEvent]; } impl MachineAir for FieldAddChip diff --git a/core/src/syscall/precompiles/field/mul.rs b/core/src/syscall/precompiles/field/mul.rs index 2d1fd8d48..8aa0bf5c5 100644 --- a/core/src/syscall/precompiles/field/mul.rs +++ b/core/src/syscall/precompiles/field/mul.rs @@ -114,7 +114,7 @@ pub fn create_fp_mul_event( } impl<'a, FP: FieldParameters> WithEvents<'a> for FieldMulChip { - type Events = &'a [FieldMulEvent]; + type InputEvents = &'a [FieldMulEvent]; } impl MachineAir for FieldMulChip diff --git a/core/src/syscall/precompiles/field/sub.rs b/core/src/syscall/precompiles/field/sub.rs index 65e772d0b..ba06f620a 100644 --- a/core/src/syscall/precompiles/field/sub.rs +++ b/core/src/syscall/precompiles/field/sub.rs @@ -114,7 +114,7 @@ pub fn create_fp_sub_event( } impl<'a, FP: FieldParameters> WithEvents<'a> for FieldSubChip { - type Events = &'a [FieldSubEvent]; + type InputEvents = &'a [FieldSubEvent]; } impl MachineAir for FieldSubChip diff --git a/core/src/syscall/precompiles/keccak256/trace.rs b/core/src/syscall/precompiles/keccak256/trace.rs index dd814d1c0..3ababeb09 100644 --- a/core/src/syscall/precompiles/keccak256/trace.rs +++ b/core/src/syscall/precompiles/keccak256/trace.rs @@ -18,7 +18,7 @@ use super::{ }; impl<'a> WithEvents<'a> for KeccakPermuteChip { - type Events = &'a [KeccakPermuteEvent]; + type InputEvents = &'a [KeccakPermuteEvent]; } impl MachineAir for KeccakPermuteChip { diff --git a/core/src/syscall/precompiles/quad_field/add.rs b/core/src/syscall/precompiles/quad_field/add.rs index c96547783..94d9a627e 100644 --- a/core/src/syscall/precompiles/quad_field/add.rs +++ b/core/src/syscall/precompiles/quad_field/add.rs @@ -140,7 +140,7 @@ pub fn create_fp2_add_event( } impl<'a, FP: FieldParameters> WithEvents<'a> for QuadFieldAddChip { - type Events = &'a [QuadFieldAddEvent]; + type InputEvents = &'a [QuadFieldAddEvent]; } impl MachineAir for QuadFieldAddChip diff --git a/core/src/syscall/precompiles/quad_field/mul.rs b/core/src/syscall/precompiles/quad_field/mul.rs index 265ba1dd0..51982b8df 100644 --- a/core/src/syscall/precompiles/quad_field/mul.rs +++ b/core/src/syscall/precompiles/quad_field/mul.rs @@ -149,7 +149,7 @@ pub fn create_fp2_mul_event( } impl<'a, FP: FieldParameters> WithEvents<'a> for QuadFieldMulChip { - type Events = &'a [QuadFieldMulEvent]; + type InputEvents = &'a [QuadFieldMulEvent]; } impl MachineAir for QuadFieldMulChip diff --git a/core/src/syscall/precompiles/quad_field/sub.rs b/core/src/syscall/precompiles/quad_field/sub.rs index ae8780a3f..9b9476f53 100644 --- a/core/src/syscall/precompiles/quad_field/sub.rs +++ b/core/src/syscall/precompiles/quad_field/sub.rs @@ -140,7 +140,7 @@ pub fn create_fp2_sub_event( } impl<'a, FP: FieldParameters> WithEvents<'a> for QuadFieldSubChip { - type Events = &'a [QuadFieldSubEvent]; + type InputEvents = &'a [QuadFieldSubEvent]; } impl MachineAir for QuadFieldSubChip diff --git a/core/src/syscall/precompiles/secp256k1/decompress.rs b/core/src/syscall/precompiles/secp256k1/decompress.rs index 723a4047b..ae0f1e021 100644 --- a/core/src/syscall/precompiles/secp256k1/decompress.rs +++ b/core/src/syscall/precompiles/secp256k1/decompress.rs @@ -215,7 +215,7 @@ impl Secp256k1DecompressChip { } impl<'a> WithEvents<'a> for Secp256k1DecompressChip { - type Events = &'a [Secp256k1DecompressEvent]; + type InputEvents = &'a [Secp256k1DecompressEvent]; } impl MachineAir for Secp256k1DecompressChip { diff --git a/core/src/syscall/precompiles/sha256/compress/trace.rs b/core/src/syscall/precompiles/sha256/compress/trace.rs index 059d14bf8..98af5b904 100644 --- a/core/src/syscall/precompiles/sha256/compress/trace.rs +++ b/core/src/syscall/precompiles/sha256/compress/trace.rs @@ -15,7 +15,7 @@ use crate::{ }; impl<'a> WithEvents<'a> for ShaCompressChip { - type Events = &'a [ShaCompressEvent]; + type InputEvents = &'a [ShaCompressEvent]; } impl MachineAir for ShaCompressChip { diff --git a/core/src/syscall/precompiles/sha256/extend/trace.rs b/core/src/syscall/precompiles/sha256/extend/trace.rs index 719ba0af6..0cd1d773c 100644 --- a/core/src/syscall/precompiles/sha256/extend/trace.rs +++ b/core/src/syscall/precompiles/sha256/extend/trace.rs @@ -11,7 +11,7 @@ use crate::{ }; impl<'a> WithEvents<'a> for ShaExtendChip { - type Events = &'a [ShaExtendEvent]; + type InputEvents = &'a [ShaExtendEvent]; } impl MachineAir for ShaExtendChip { diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs index d527d6e8e..42223ec8e 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs @@ -150,7 +150,7 @@ impl WeierstrassAddAssignChip { } impl<'a, E: EllipticCurve + WeierstrassParameters> WithEvents<'a> for WeierstrassAddAssignChip { - type Events = &'a [ECAddEvent<::NB_LIMBS>]; + type InputEvents = &'a [ECAddEvent<::NB_LIMBS>]; } impl MachineAir diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs index eff920ac7..df46c8e36 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs @@ -176,7 +176,7 @@ impl WeierstrassDoubleAssignChip { impl<'a, E: EllipticCurve + WeierstrassParameters> WithEvents<'a> for WeierstrassDoubleAssignChip { - type Events = &'a [ECDoubleEvent<::NB_LIMBS>]; + type InputEvents = &'a [ECDoubleEvent<::NB_LIMBS>]; } impl MachineAir diff --git a/derive/src/lib.rs b/derive/src/lib.rs index c1bff2797..557c3c423 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -123,12 +123,12 @@ pub fn with_events_air_derive(input: TokenStream) -> TokenStream { let field_ty_events = fields.iter().map(|field| { let field_ty = &field.ty; quote! { - <#field_ty as #sphinx_core_path::air::WithEvents<'a>>::Events + <#field_ty as #sphinx_core_path::air::WithEvents<'a>>::InputEvents } }); quote!{ impl <'a, #(#type_params),*, #(#const_params),*> #sphinx_core_path::air::WithEvents<'a> for #name #ty_generics #where_clause { - type Events = (#(#field_ty_events,)*); + type InputEvents = (#(#field_ty_events,)*); } }.into() } @@ -210,7 +210,7 @@ pub fn event_lens_air_derive(input: TokenStream) -> TokenStream { }); let res = quote! { impl #impl_generics #sphinx_core_path::air::EventLens<#name #ty_generics> for #rec_ty { - fn events(&self) -> <#name #ty_generics as #sphinx_core_path::air::WithEvents>::Events { + fn events(&self) -> <#name #ty_generics as #sphinx_core_path::air::WithEvents>::InputEvents { (#(#field_events,)*) } } @@ -308,7 +308,7 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream { let idx = syn::Index::from(i); quote! { #name::#variant_name(x) => { - fn f <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::Events, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::Events { evs.#idx } + fn f <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::InputEvents, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::InputEvents { evs.#idx } <#field_ty as #sphinx_core_path::air::MachineAir>::generate_trace(x, &#sphinx_core_path::air::Proj::new(input, f #turbo_ty), output) } @@ -321,7 +321,7 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream { quote! { #name::#variant_name(x) => { - fn f <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::Events, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::Events { evs.#idx } + fn f <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::InputEvents, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::InputEvents { evs.#idx } <#field_ty as #sphinx_core_path::air::MachineAir>::generate_dependencies(x, &#sphinx_core_path::air::Proj::new(input, f #turbo_ty), output) } diff --git a/recursion/core/src/cpu/trace.rs b/recursion/core/src/cpu/trace.rs index 995969156..d1328afcc 100644 --- a/recursion/core/src/cpu/trace.rs +++ b/recursion/core/src/cpu/trace.rs @@ -17,7 +17,7 @@ use crate::{ use super::{CpuChip, CpuCols, CpuEvent, CPU_COL_MAP, NUM_CPU_COLS}; impl<'a, F: Field> WithEvents<'a> for CpuChip { - type Events = &'a [CpuEvent]; + type InputEvents = &'a [CpuEvent]; } impl> MachineAir for CpuChip diff --git a/recursion/core/src/fri_fold/mod.rs b/recursion/core/src/fri_fold/mod.rs index 3f163399c..27f29626d 100644 --- a/recursion/core/src/fri_fold/mod.rs +++ b/recursion/core/src/fri_fold/mod.rs @@ -94,7 +94,7 @@ impl BaseAir for FriFoldChip { } impl<'a, F: Field, const DEGREE: usize> WithEvents<'a> for FriFoldChip { - type Events = &'a [FriFoldEvent]; + type InputEvents = &'a [FriFoldEvent]; } impl MachineAir for FriFoldChip { diff --git a/recursion/core/src/memory/air.rs b/recursion/core/src/memory/air.rs index bd39961bf..403a9c7a8 100644 --- a/recursion/core/src/memory/air.rs +++ b/recursion/core/src/memory/air.rs @@ -31,7 +31,7 @@ impl MemoryGlobalChip { } impl<'a, F: Field> WithEvents<'a> for MemoryGlobalChip { - type Events = ( + type InputEvents = ( // first memory event &'a [(F, Block)], // last memory event diff --git a/recursion/core/src/multi/mod.rs b/recursion/core/src/multi/mod.rs index 132ba0229..c474e958d 100644 --- a/recursion/core/src/multi/mod.rs +++ b/recursion/core/src/multi/mod.rs @@ -51,9 +51,9 @@ impl BaseAir for MultiChip { } impl<'a, F: Field, const DEGREE: usize> WithEvents<'a> for MultiChip { - type Events = ( - as WithEvents<'a>>::Events, - as WithEvents<'a>>::Events, + type InputEvents = ( + as WithEvents<'a>>::InputEvents, + as WithEvents<'a>>::InputEvents, ); } @@ -79,16 +79,16 @@ impl MachineAir for MultiChip( - evs: as WithEvents<'c>>::Events, + evs: as WithEvents<'c>>::InputEvents, _v: &'c (), - ) -> as WithEvents<'c>>::Events { + ) -> as WithEvents<'c>>::InputEvents { evs.0 } fn to_poseidon<'c, F: PrimeField32, const DEGREE: usize>( - evs: as WithEvents<'c>>::Events, + evs: as WithEvents<'c>>::InputEvents, _v: &'c (), - ) -> as WithEvents<'c>>::Events { + ) -> as WithEvents<'c>>::InputEvents { evs.1 } diff --git a/recursion/core/src/poseidon2/trace.rs b/recursion/core/src/poseidon2/trace.rs index b872be2ae..4d7a6417a 100644 --- a/recursion/core/src/poseidon2/trace.rs +++ b/recursion/core/src/poseidon2/trace.rs @@ -20,7 +20,7 @@ use super::{ }; impl<'a, F: Field> WithEvents<'a> for Poseidon2Chip { - type Events = &'a [Poseidon2Event]; + type InputEvents = &'a [Poseidon2Event]; } impl MachineAir for Poseidon2Chip { diff --git a/recursion/core/src/poseidon2_wide/external.rs b/recursion/core/src/poseidon2_wide/external.rs index d0ffc30b1..1e3cd9da2 100644 --- a/recursion/core/src/poseidon2_wide/external.rs +++ b/recursion/core/src/poseidon2_wide/external.rs @@ -39,7 +39,7 @@ pub struct Poseidon2WideChip { } impl<'a, F: Field, const DEGREE: usize> WithEvents<'a> for Poseidon2WideChip { - type Events = &'a [Poseidon2Event]; + type InputEvents = &'a [Poseidon2Event]; } impl MachineAir for Poseidon2WideChip { diff --git a/recursion/core/src/program/mod.rs b/recursion/core/src/program/mod.rs index c2ff61158..fda22f327 100644 --- a/recursion/core/src/program/mod.rs +++ b/recursion/core/src/program/mod.rs @@ -49,7 +49,7 @@ impl ProgramChip { } impl<'a, F: Field> WithEvents<'a> for ProgramChip { - type Events = ( + type InputEvents = ( // program.instructions &'a [Instruction], // cpu_events diff --git a/recursion/core/src/range_check/trace.rs b/recursion/core/src/range_check/trace.rs index 21005d212..869c84fef 100644 --- a/recursion/core/src/range_check/trace.rs +++ b/recursion/core/src/range_check/trace.rs @@ -13,7 +13,7 @@ use crate::runtime::{ExecutionRecord, RecursionProgram}; pub const NUM_ROWS: usize = 1 << 16; impl<'a, F: Field> WithEvents<'a> for RangeCheckChip { - type Events = &'a BTreeMap; + type InputEvents = &'a BTreeMap; } impl MachineAir for RangeCheckChip { diff --git a/recursion/core/src/runtime/record.rs b/recursion/core/src/runtime/record.rs index 88149bbac..ccdebba48 100644 --- a/recursion/core/src/runtime/record.rs +++ b/recursion/core/src/runtime/record.rs @@ -101,7 +101,7 @@ impl MachineRecord for ExecutionRecord { } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { &self.cpu_events } } @@ -109,13 +109,13 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { &self.fri_fold_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { &self.poseidon2_events } } @@ -123,31 +123,31 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { &self.poseidon2_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { (&self.first_memory_record, &self.last_memory_record) } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { (&self.program.instructions, &self.cpu_events) } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { &self.range_check_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::Events { + fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { ( >>::events(self), >>::events(self), From 3607bdc2ce0e2b2eb5fc34979af3c026e4917f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Thu, 6 Jun 2024 13:26:46 -0400 Subject: [PATCH 2/3] refactor: De-couple Chips from a specific ExecutionRecord, part II Previously: https://github.com/lurk-lab/sphinx/pull/9 This applies the same logic to output events in `generate_trace`. ```rust // implemented on the chip trait WithEvents<'a> { type InputEvents : 'a; type OutputEvents: 'a; } // implemented on the record trait EventLens WithEvent<'a>> { fn events<'a>(&'a self) -> >::InputEvents; } // implemented on the record trait EventMutLens WithEvent<'a>> { fn events<'a>(&'a mut self, events: >::OutputEvents); } ``` Now, one would wish that this would be a bit more ergonomic in Rust, because we do have anonymous cartesian products of arbitrary arity (tuples), but we do not have anonymous coproducts - either crate notwithstanding. ```rust pub enum DivRemEvent<'a> { ByteLookupEvent(&'a ByteLookupEvent), MulEvent(&'a AluEvent), LtEvent(&'a AluEvent), } impl<'a> WithEvents<'a> for DivRemChip { type InputEvents = &'a [AluEvent]; type OutputEvents = [DivRemEvent<'a>]; } impl EventMutLens for ExecutionRecord { fn add_events(&mut self, events: >::OutputEvents) { for event in events { match event { DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(*e), DivRemEvent::MulEvent(e) => self.add_mul_event(*e), DivRemEvent::LtEvent(e) => self.add_lt_event(*e), } } } } ``` --- core/src/air/machine.rs | 66 ++- core/src/alu/add_sub/mod.rs | 15 +- core/src/alu/bitwise/mod.rs | 10 +- core/src/alu/divrem/mod.rs | 38 +- core/src/alu/lt/mod.rs | 9 +- core/src/alu/mul/mod.rs | 14 +- core/src/alu/sll/mod.rs | 22 +- core/src/alu/sr/mod.rs | 25 +- core/src/bytes/trace.rs | 11 +- core/src/cpu/trace.rs | 53 ++- core/src/memory/global.rs | 12 +- core/src/memory/program.rs | 13 +- .../field/extensions/quadratic/mod.rs | 21 +- .../field/extensions/quadratic/sqrt.rs | 21 +- core/src/operations/field/field_den.rs | 24 +- .../operations/field/field_inner_product.rs | 25 +- core/src/operations/field/field_op.rs | 21 +- core/src/operations/field/field_sqrt.rs | 21 +- core/src/program/mod.rs | 11 +- core/src/runtime/record.rs | 395 +++++++++++++++++- .../precompiles/blake3/compress/trace.rs | 15 +- .../precompiles/bls12_381/g1_decompress.rs | 11 +- .../syscall/precompiles/bls12_381/g2_add.rs | 12 +- .../precompiles/bls12_381/g2_double.rs | 10 +- .../src/syscall/precompiles/edwards/ed_add.rs | 11 +- .../precompiles/edwards/ed_decompress.rs | 12 +- core/src/syscall/precompiles/field/add.rs | 13 +- core/src/syscall/precompiles/field/mul.rs | 13 +- core/src/syscall/precompiles/field/sub.rs | 13 +- .../syscall/precompiles/keccak256/trace.rs | 22 +- .../src/syscall/precompiles/quad_field/add.rs | 13 +- .../src/syscall/precompiles/quad_field/mul.rs | 13 +- .../src/syscall/precompiles/quad_field/sub.rs | 13 +- .../precompiles/secp256k1/decompress.rs | 11 +- .../precompiles/sha256/compress/trace.rs | 122 ++++-- .../precompiles/sha256/extend/trace.rs | 35 +- .../weierstrass/weierstrass_add.rs | 13 +- .../weierstrass/weierstrass_double.rs | 17 +- derive/src/lib.rs | 94 +++-- recursion/core/src/cpu/trace.rs | 13 +- recursion/core/src/fri_fold/mod.rs | 14 +- recursion/core/src/memory/air.rs | 13 +- recursion/core/src/multi/mod.rs | 42 +- recursion/core/src/poseidon2/trace.rs | 13 +- recursion/core/src/poseidon2_wide/external.rs | 13 +- recursion/core/src/program/mod.rs | 13 +- recursion/core/src/range_check/trace.rs | 13 +- recursion/core/src/runtime/record.rs | 26 +- 48 files changed, 1101 insertions(+), 349 deletions(-) diff --git a/core/src/air/machine.rs b/core/src/air/machine.rs index 5ed3a05f6..2e2c0eba7 100644 --- a/core/src/air/machine.rs +++ b/core/src/air/machine.rs @@ -12,9 +12,11 @@ use crate::{ /// A description of the events related to this AIR. pub trait WithEvents<'a>: Sized { - /// output of a functional lens from the Record to - /// refs of those events relative to the AIR. + /// the input events that this AIR needs to get a reference to in order to lay out its trace type InputEvents: 'a; + + // the output events that this AIR produces + type OutputEvents: 'a; } /// A trait intended for implementation on Records that may store events related to Chips, @@ -26,6 +28,10 @@ pub trait EventLens WithEvents<'b>>: Indexed { fn events(&self) -> >::InputEvents; } +pub trait EventMutLens WithEvents<'b>> { + fn add_events(&mut self, events: >::OutputEvents); +} + //////////////// Derive macro shenanigans //////////////////////////////////////////////// // The following is *only* useful for the derive macros, you should *not* use this directly. // @@ -80,12 +86,56 @@ where self.record.index() } } + +/// if I have an EventMutLens from T::Events, and a way (F) to deduce T::Events from U::Events, +/// I can compose them to get an EventMutLens from U::Events. +pub struct Inj<'a, T, R, F> +where + T: for<'b> WithEvents<'b>, + R: EventMutLens, +{ + record: &'a mut R, + injection: F, + _phantom: PhantomData, +} + +/// A constructor for the projection from T::Events to U::Events. +impl<'a, T, R, F> Inj<'a, T, R, F> +where + T: for<'b> WithEvents<'b>, + R: EventMutLens, +{ + pub fn new(record: &'a mut R, injection: F) -> Self { + Self { + record, + injection, + _phantom: PhantomData, + } + } +} + +impl<'a, T, R, U, F> EventMutLens for Inj<'a, T, R, F> +where + T: for<'b> WithEvents<'b>, + R: EventMutLens, + U: for<'b> WithEvents<'b>, + // see https://github.com/rust-lang/rust/issues/86702 for the empty parameter + F: for<'c> Fn( + >::OutputEvents, + &'c (), + ) -> >::OutputEvents, +{ + fn add_events(&mut self, events: >::OutputEvents) { + let events: >::OutputEvents = (self.injection)(events, &()); + self.record.add_events(events); + } +} //////////////// end of shenanigans destined for the derive macros. //////////////// /// An AIR that is part of a multi table AIR arithmetization. pub trait MachineAir: BaseAir + for<'a> WithEvents<'a> { /// The execution record containing events for producing the air trace. - type Record: MachineRecord + EventLens; + type Record: MachineRecord + EventLens + EventMutLens; type Program: MachineProgram; @@ -97,14 +147,18 @@ pub trait MachineAir: BaseAir + for<'a> WithEvents<'a> { /// - `input` is the execution record containing the events to be written to the trace. /// - `output` is the execution record containing events that the `MachineAir` can add to /// the record such as byte lookup requests. - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut Self::Record, + output: &mut OL, ) -> RowMajorMatrix; /// Generate the dependencies for a given execution record. - fn generate_dependencies>(&self, input: &EL, output: &mut Self::Record) { + fn generate_dependencies, OL: EventMutLens>( + &self, + input: &EL, + output: &mut OL, + ) { self.generate_trace(input, output); } diff --git a/core/src/alu/add_sub/mod.rs b/core/src/alu/add_sub/mod.rs index fe4869dac..76325f28d 100644 --- a/core/src/alu/add_sub/mod.rs +++ b/core/src/alu/add_sub/mod.rs @@ -12,10 +12,10 @@ use p3_maybe_rayon::prelude::ParallelSlice; use sphinx_derive::AlignedBorrow; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, WithEvents, Word}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, WithEvents, Word}, + bytes::ByteLookupEvent, operations::AddOperation, runtime::{ExecutionRecord, Opcode, Program}, - stark::MachineRecord, utils::pad_to_power_of_two, }; @@ -64,6 +64,7 @@ impl<'a> WithEvents<'a> for AddSubChip { // sub events &'a [AluEvent], ); + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for AddSubChip { @@ -75,10 +76,10 @@ impl MachineAir for AddSubChip { "AddSub".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut Self::Record, + output: &mut OL, ) -> RowMajorMatrix { let (add_events, sub_events) = input.events(); // Generate the rows for the trace. @@ -91,7 +92,7 @@ impl MachineAir for AddSubChip { let rows_and_records = merged_events .par_chunks(chunk_size) .map(|events| { - let mut record = ExecutionRecord::default(); + let mut record = Vec::new(); let rows = events .iter() .map(|event| { @@ -117,9 +118,9 @@ impl MachineAir for AddSubChip { .collect::>(); let mut rows: Vec<[F; NUM_ADD_SUB_COLS]> = vec![]; - for mut row_and_record in rows_and_records { + for row_and_record in rows_and_records { rows.extend(row_and_record.0); - output.append(&mut row_and_record.1); + output.add_events(&row_and_record.1); } // Convert the trace to a row major matrix. diff --git a/core/src/alu/bitwise/mod.rs b/core/src/alu/bitwise/mod.rs index 807597f9b..26988797c 100644 --- a/core/src/alu/bitwise/mod.rs +++ b/core/src/alu/bitwise/mod.rs @@ -9,9 +9,8 @@ use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, ByteAirBuilder, MachineAir}; +use crate::air::{AluAirBuilder, ByteAirBuilder, EventMutLens, MachineAir}; use crate::air::{EventLens, WithEvents, Word}; -use crate::bytes::event::ByteRecord; use crate::bytes::{ByteLookupEvent, ByteOpcode}; use crate::runtime::{ExecutionRecord, Opcode, Program}; use crate::utils::pad_to_power_of_two; @@ -53,6 +52,7 @@ pub struct BitwiseCols { impl<'a> WithEvents<'a> for BitwiseChip { type InputEvents = &'a [AluEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for BitwiseChip { @@ -64,10 +64,10 @@ impl MachineAir for BitwiseChip { "Bitwise".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // Generate the trace rows for each event. let rows = input @@ -98,7 +98,7 @@ impl MachineAir for BitwiseChip { b: u32::from(b_b), c: u32::from(b_c), }; - output.add_byte_lookup_event(byte_event); + output.add_events(&[byte_event]); } row diff --git a/core/src/alu/divrem/mod.rs b/core/src/alu/divrem/mod.rs index 6a537a9ad..9fd5bd74e 100644 --- a/core/src/alu/divrem/mod.rs +++ b/core/src/alu/divrem/mod.rs @@ -75,7 +75,7 @@ use p3_matrix::Matrix; use sphinx_derive::AlignedBorrow; use self::utils::eval_abs_value; -use crate::air::{AluAirBuilder, ByteAirBuilder, MachineAir, WordAirBuilder}; +use crate::air::{AluAirBuilder, ByteAirBuilder, EventMutLens, MachineAir, WordAirBuilder}; use crate::air::{EventLens, WithEvents, Word}; use crate::alu::divrem::utils::{get_msb, get_quotient_and_remainder, is_signed_operation}; use crate::alu::AluEvent; @@ -187,8 +187,15 @@ pub struct DivRemCols { pub is_real: T, } +pub enum DivRemEvent<'a> { + ByteLookupEvent(&'a ByteLookupEvent), + MulEvent(&'a AluEvent), + LtEvent(&'a AluEvent), +} + impl<'a> WithEvents<'a> for DivRemChip { type InputEvents = &'a [AluEvent]; + type OutputEvents = &'a [DivRemEvent<'a>]; } impl MachineAir for DivRemChip { @@ -200,11 +207,15 @@ impl MachineAir for DivRemChip { "DivRem".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { + let mut byte_events = Vec::new(); + let mut mul_events = Vec::new(); + let mut lt_events = Vec::new(); + // Generate the trace rows for each event. let divrem_events = input.events(); let mut rows: Vec<[F; NUM_DIVREM_COLS]> = Vec::with_capacity(divrem_events.len()); @@ -273,7 +284,7 @@ impl MachineAir for DivRemChip { c: 0, }); } - output.add_byte_lookup_events(blu_events); + byte_events.extend(blu_events); } } @@ -332,7 +343,7 @@ impl MachineAir for DivRemChip { c: event.c, b: quotient, }; - output.add_mul_event(lower_multiplication); + mul_events.push(lower_multiplication); let upper_multiplication = AluEvent { shard: event.shard, @@ -349,7 +360,7 @@ impl MachineAir for DivRemChip { b: quotient, }; - output.add_mul_event(upper_multiplication); + mul_events.push(upper_multiplication); let lt_event = if is_signed_operation(event.opcode) { AluEvent { @@ -370,16 +381,23 @@ impl MachineAir for DivRemChip { clk: event.clk, } }; - output.add_lt_event(lt_event); + lt_events.push(lt_event); } // Range check. { - output.add_u8_range_checks(event.shard, "ient.to_le_bytes()); - output.add_u8_range_checks(event.shard, &remainder.to_le_bytes()); - output.add_u8_range_checks(event.shard, &c_times_quotient); + byte_events.add_u8_range_checks(event.shard, "ient.to_le_bytes()); + byte_events.add_u8_range_checks(event.shard, &remainder.to_le_bytes()); + byte_events.add_u8_range_checks(event.shard, &c_times_quotient); } } + let events = byte_events + .iter() + .map(DivRemEvent::ByteLookupEvent) + .chain(mul_events.iter().map(DivRemEvent::MulEvent)) + .chain(lt_events.iter().map(DivRemEvent::LtEvent)) + .collect::>(); + output.add_events(&events); rows.push(row); } diff --git a/core/src/alu/lt/mod.rs b/core/src/alu/lt/mod.rs index aabaef024..f36bb461d 100644 --- a/core/src/alu/lt/mod.rs +++ b/core/src/alu/lt/mod.rs @@ -12,7 +12,7 @@ use p3_matrix::Matrix; use p3_maybe_rayon::prelude::*; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, BaseAirBuilder, ByteAirBuilder, MachineAir}; +use crate::air::{AluAirBuilder, BaseAirBuilder, ByteAirBuilder, EventMutLens, MachineAir}; use crate::air::{EventLens, WithEvents, Word}; use crate::bytes::event::ByteRecord; use crate::bytes::{ByteLookupEvent, ByteOpcode}; @@ -95,6 +95,7 @@ impl LtCols { impl<'a> WithEvents<'a> for LtChip { type InputEvents = &'a [AluEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for LtChip { @@ -106,10 +107,10 @@ impl MachineAir for LtChip { "Lt".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // Generate the trace rows for each event. let (rows, new_byte_lookup_events): (Vec<_>, Vec<_>) = input @@ -211,7 +212,7 @@ impl MachineAir for LtChip { .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } // Convert the trace to a row major matrix. diff --git a/core/src/alu/mul/mod.rs b/core/src/alu/mul/mod.rs index 9fd35c3c7..cbc379de6 100644 --- a/core/src/alu/mul/mod.rs +++ b/core/src/alu/mul/mod.rs @@ -44,14 +44,13 @@ use p3_maybe_rayon::prelude::ParallelIterator; use p3_maybe_rayon::prelude::ParallelSlice; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, ByteAirBuilder, MachineAir, WordAirBuilder}; +use crate::air::{AluAirBuilder, ByteAirBuilder, EventMutLens, MachineAir, WordAirBuilder}; use crate::air::{EventLens, WithEvents, Word}; use crate::alu::mul::utils::get_msb; use crate::bytes::event::ByteRecord; use crate::bytes::{ByteLookupEvent, ByteOpcode}; use crate::disassembler::WORD_SIZE; use crate::runtime::{ExecutionRecord, Opcode, Program}; -use crate::stark::MachineRecord; use crate::utils::pad_to_power_of_two; use super::AluEvent; @@ -125,6 +124,7 @@ pub struct MulCols { impl<'a> WithEvents<'a> for MulChip { type InputEvents = &'a [AluEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for MulChip { @@ -136,10 +136,10 @@ impl MachineAir for MulChip { "Mul".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mul_events = input.events(); // Compute the chunk size based on the number of events and the number of CPUs. @@ -149,7 +149,7 @@ impl MachineAir for MulChip { let rows_and_records = mul_events .par_chunks(chunk_size) .map(|events| { - let mut record = ExecutionRecord::default(); + let mut record = Vec::new(); let rows = events .iter() .map(|event| { @@ -257,9 +257,9 @@ impl MachineAir for MulChip { // Generate the trace rows for each event. let mut rows: Vec<[F; NUM_MUL_COLS]> = vec![]; - for mut row_and_record in rows_and_records { + for row_and_record in rows_and_records { rows.extend(row_and_record.0); - output.append(&mut row_and_record.1); + output.add_events(&row_and_record.1); } // Convert the trace to a row major matrix. diff --git a/core/src/alu/sll/mod.rs b/core/src/alu/sll/mod.rs index d914a4c47..6cb55f585 100644 --- a/core/src/alu/sll/mod.rs +++ b/core/src/alu/sll/mod.rs @@ -42,12 +42,17 @@ use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, ByteAirBuilder, MachineAir, WordAirBuilder}; -use crate::air::{EventLens, WithEvents, Word}; -use crate::bytes::event::ByteRecord; use crate::disassembler::WORD_SIZE; use crate::runtime::{ExecutionRecord, Opcode, Program}; use crate::utils::pad_to_power_of_two; +use crate::{ + air::{AluAirBuilder, ByteAirBuilder, MachineAir, WordAirBuilder}, + bytes::ByteLookupEvent, +}; +use crate::{ + air::{EventLens, EventMutLens, WithEvents, Word}, + bytes::event::ByteRecord, +}; use super::AluEvent; @@ -100,6 +105,7 @@ pub struct ShiftLeftCols { impl<'a> WithEvents<'a> for ShiftLeft { type InputEvents = &'a [AluEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for ShiftLeft { @@ -111,10 +117,10 @@ impl MachineAir for ShiftLeft { "ShiftLeft".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // Generate the trace rows for each event. let mut rows: Vec<[F; NUM_SHIFT_LEFT_COLS]> = vec![]; @@ -164,8 +170,10 @@ impl MachineAir for ShiftLeft { // Range checks. { - output.add_u8_range_checks(event.shard, &bit_shift_result); - output.add_u8_range_checks(event.shard, &bit_shift_result_carry); + let mut v = Vec::new(); + v.add_u8_range_checks(event.shard, &bit_shift_result); + v.add_u8_range_checks(event.shard, &bit_shift_result_carry); + output.add_events(&v) } // Sanity check. diff --git a/core/src/alu/sr/mod.rs b/core/src/alu/sr/mod.rs index 65141e0ec..cb9840818 100644 --- a/core/src/alu/sr/mod.rs +++ b/core/src/alu/sr/mod.rs @@ -55,15 +55,17 @@ use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, ByteAirBuilder, MachineAir, WordAirBuilder}; use crate::air::{EventLens, WithEvents, Word}; use crate::alu::sr::utils::{nb_bits_to_shift, nb_bytes_to_shift}; -use crate::bytes::event::ByteRecord; use crate::bytes::utils::shr_carry; use crate::bytes::{ByteLookupEvent, ByteOpcode}; use crate::disassembler::WORD_SIZE; use crate::runtime::{ExecutionRecord, Opcode, Program}; use crate::utils::pad_to_power_of_two; +use crate::{ + air::{AluAirBuilder, ByteAirBuilder, EventMutLens, MachineAir, WordAirBuilder}, + bytes::event::ByteRecord, +}; use super::AluEvent; @@ -132,6 +134,7 @@ pub struct ShiftRightCols { impl<'a> WithEvents<'a> for ShiftRightChip { type InputEvents = &'a [AluEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for ShiftRightChip { @@ -143,10 +146,10 @@ impl MachineAir for ShiftRightChip { "ShiftRight".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // Generate the trace rows for each event. let mut rows: Vec<[F; NUM_SHIFT_RIGHT_COLS]> = Vec::new(); @@ -175,7 +178,7 @@ impl MachineAir for ShiftRightChip { // Insert the MSB lookup event. let most_significant_byte = event.b.to_le_bytes()[WORD_SIZE - 1]; - output.add_byte_lookup_events(vec![ByteLookupEvent { + output.add_events(&[ByteLookupEvent { shard: event.shard, opcode: ByteOpcode::MSB, a1: u32::from((most_significant_byte >> 7) & 1), @@ -232,7 +235,7 @@ impl MachineAir for ShiftRightChip { b: u32::from(byte_shift_result[i]), c: num_bits_to_shift as u32, }; - output.add_byte_lookup_event(byte_event); + output.add_events(&[byte_event]); shr_carry_output_carry[i] = carry; shr_carry_output_shifted_byte[i] = shift; @@ -248,10 +251,12 @@ impl MachineAir for ShiftRightChip { debug_assert_eq!(cols.a[i], cols.bit_shift_result[i].clone()); } // Range checks. - output.add_u8_range_checks(event.shard, &byte_shift_result); - output.add_u8_range_checks(event.shard, &bit_shift_result); - output.add_u8_range_checks(event.shard, &shr_carry_output_carry); - output.add_u8_range_checks(event.shard, &shr_carry_output_shifted_byte); + let mut v = Vec::new(); + v.add_u8_range_checks(event.shard, &byte_shift_result); + v.add_u8_range_checks(event.shard, &bit_shift_result); + v.add_u8_range_checks(event.shard, &shr_carry_output_carry); + v.add_u8_range_checks(event.shard, &shr_carry_output_shifted_byte); + output.add_events(&v); } rows.push(row); diff --git a/core/src/bytes/trace.rs b/core/src/bytes/trace.rs index 5a2d0de79..57a3dd831 100644 --- a/core/src/bytes/trace.rs +++ b/core/src/bytes/trace.rs @@ -8,7 +8,7 @@ use super::{ ByteChip, ByteLookupEvent, }; use crate::{ - air::{EventLens, MachineAir, WithEvents}, + air::{EventLens, EventMutLens, MachineAir, WithEvents}, runtime::{ExecutionRecord, Program}, }; @@ -17,6 +17,7 @@ pub const NUM_ROWS: usize = 1 << 16; impl<'a, F: Field> WithEvents<'a> for ByteChip { // the byte lookups type InputEvents = &'a BTreeMap>; + type OutputEvents = &'a (); } impl MachineAir for ByteChip { @@ -40,18 +41,18 @@ impl MachineAir for ByteChip { Some(trace) } - fn generate_dependencies>( + fn generate_dependencies, OL: EventMutLens>( &self, _input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) { // Do nothing since this chip has no dependencies. } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) -> RowMajorMatrix { let shard = input.index(); let (_, event_map) = Self::trace_and_map(shard); diff --git a/core/src/cpu/trace.rs b/core/src/cpu/trace.rs index b317cad43..f659efde2 100644 --- a/core/src/cpu/trace.rs +++ b/core/src/cpu/trace.rs @@ -7,9 +7,8 @@ use tracing::instrument; use super::columns::{CPU_COL_MAP, NUM_CPU_COLS}; use super::{CpuChip, CpuEvent}; -use crate::air::{EventLens, MachineAir, WithEvents}; +use crate::air::{EventLens, EventMutLens, MachineAir, WithEvents}; use crate::alu::AluEvent; -use crate::bytes::event::ByteRecord; use crate::bytes::{ByteLookupEvent, ByteOpcode}; use crate::cpu::columns::CpuCols; use crate::cpu::trace::ByteOpcode::{U16Range, U8Range}; @@ -18,8 +17,14 @@ use crate::memory::MemoryCols; use crate::runtime::{ExecutionRecord, Opcode, Program}; use crate::runtime::{MemoryRecordEnum, SyscallCode}; +pub enum CpuOutputEvent<'a> { + ByteLookupEvent(&'a ByteLookupEvent), + AluEvent(&'a AluEvent), +} + impl<'a> WithEvents<'a> for CpuChip { type InputEvents = &'a [CpuEvent]; + type OutputEvents = &'a [CpuOutputEvent<'a>]; } impl MachineAir for CpuChip { @@ -31,10 +36,10 @@ impl MachineAir for CpuChip { "CPU".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut new_alu_events = HashMap::new(); let mut new_blu_events = Vec::new(); @@ -69,8 +74,20 @@ impl MachineAir for CpuChip { value.sort_unstable_by_key(|event| event.clk); } new_blu_events.sort_unstable_by_key(|event| event.a1); - output.add_alu_events(&new_alu_events); - output.add_byte_lookup_events(new_blu_events); + for alu_events in new_alu_events.values() { + output.add_events( + &alu_events + .iter() + .map(CpuOutputEvent::AluEvent) + .collect::>(), + ); + } + output.add_events( + &new_blu_events + .iter() + .map(CpuOutputEvent::ByteLookupEvent) + .collect::>(), + ); // Convert the trace to a row major matrix. let mut trace = RowMajorMatrix::new(rows, NUM_CPU_COLS); @@ -82,7 +99,13 @@ impl MachineAir for CpuChip { } #[instrument(name = "generate cpu dependencies", level = "debug", skip_all)] - fn generate_dependencies>(&self, input: &EL, output: &mut ExecutionRecord) { + fn generate_dependencies, OL: EventMutLens>( + &self, + input: &EL, + output: &mut OL, + ) { + // let (byte_events, add_events, mul_events, sub_events, bitwise_events, shift_left_events, shift_right_events, lt_events) = output.events(); + // Generate the trace rows for each event. let chunk_size = std::cmp::max(input.events().len() / num_cpus::get(), 1); let events = input @@ -107,9 +130,21 @@ impl MachineAir for CpuChip { value.sort_unstable_by_key(|event| event.clk); } // Add the dependency events to the shard. - output.add_alu_events(&alu_events); + for alu_events in alu_events.values() { + output.add_events( + &alu_events + .iter() + .map(CpuOutputEvent::AluEvent) + .collect::>(), + ); + } blu_events.sort_unstable_by_key(|event| event.a1); - output.add_byte_lookup_events(blu_events); + output.add_events( + &blu_events + .iter() + .map(CpuOutputEvent::ByteLookupEvent) + .collect::>(), + ); } } diff --git a/core/src/memory/global.rs b/core/src/memory/global.rs index 2a64ea5ed..bf084bddc 100644 --- a/core/src/memory/global.rs +++ b/core/src/memory/global.rs @@ -11,7 +11,8 @@ use sphinx_derive::AlignedBorrow; use super::MemoryInitializeFinalizeEvent; use crate::{ air::{ - AirInteraction, BaseAirBuilder, EventLens, MachineAir, WithEvents, Word, WordAirBuilder, + AirInteraction, BaseAirBuilder, EventLens, EventMutLens, MachineAir, WithEvents, Word, + WordAirBuilder, }, runtime::{ExecutionRecord, Program}, utils::pad_to_power_of_two, @@ -49,6 +50,7 @@ impl<'a> WithEvents<'a> for MemoryChip { // finalize events &'a [MemoryInitializeFinalizeEvent], ); + type OutputEvents = &'a (); } impl MachineAir for MemoryChip { @@ -63,10 +65,10 @@ impl MachineAir for MemoryChip { } } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) -> RowMajorMatrix { let mut memory_events: Vec = match self.kind { MemoryChipType::Initialize => input.events().0, @@ -105,10 +107,10 @@ impl MachineAir for MemoryChip { trace } - fn generate_dependencies>( + fn generate_dependencies, OL: EventMutLens>( &self, _input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) { // Do nothing since this chip has no dependencies. } diff --git a/core/src/memory/program.rs b/core/src/memory/program.rs index 295c2a05e..466a96ba9 100644 --- a/core/src/memory/program.rs +++ b/core/src/memory/program.rs @@ -9,7 +9,9 @@ use std::collections::BTreeMap; use sphinx_derive::AlignedBorrow; -use crate::air::{AirInteraction, BaseAirBuilder, EventLens, PublicValues, WithEvents}; +use crate::air::{ + AirInteraction, BaseAirBuilder, EventLens, EventMutLens, PublicValues, WithEvents, +}; use crate::air::{MachineAir, Word}; use crate::operations::IsZeroOperation; use crate::runtime::{ExecutionRecord, Program}; @@ -52,6 +54,7 @@ impl MemoryProgramChip { impl<'a> WithEvents<'a> for MemoryProgramChip { type InputEvents = &'a BTreeMap; + type OutputEvents = &'a (); } impl MachineAir for MemoryProgramChip { @@ -96,18 +99,18 @@ impl MachineAir for MemoryProgramChip { Some(trace) } - fn generate_dependencies>( + fn generate_dependencies, OL: EventMutLens>( &self, _input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) { // Do nothing since this chip has no dependencies. } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) -> RowMajorMatrix { let program_memory_addrs = input.events().keys().copied().collect::>(); diff --git a/core/src/operations/field/extensions/quadratic/mod.rs b/core/src/operations/field/extensions/quadratic/mod.rs index 709347ec7..546359274 100644 --- a/core/src/operations/field/extensions/quadratic/mod.rs +++ b/core/src/operations/field/extensions/quadratic/mod.rs @@ -365,7 +365,7 @@ impl QuadFieldOpCols { #[cfg(test)] mod tests { - use crate::air::{EventLens, WordAirBuilder}; + use crate::air::{EventLens, EventMutLens, WordAirBuilder}; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; @@ -381,6 +381,7 @@ mod tests { use super::{QuadFieldOpCols, QuadFieldOperation}; use crate::air::MachineAir; use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::{FieldParameters, Limbs}; use crate::runtime::{ExecutionRecord, Program}; use crate::stark::StarkGenericConfig; @@ -413,6 +414,7 @@ mod tests { impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for QuadFieldOpChip

{ type InputEvents = &'a (); + type OutputEvents = &'a [ByteLookupEvent]; } impl EventLens> for ExecutionRecord { @@ -421,6 +423,17 @@ mod tests { } } + impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } + } + impl MachineAir for QuadFieldOpChip

{ type Record = ExecutionRecord; @@ -430,10 +443,10 @@ mod tests { format!("QuadFieldOp{:?}", self.operation) } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, _: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rng = thread_rng(); // Hardcoded edge cases. We purposely include 0 / 0. While mathematically, that is not @@ -508,7 +521,7 @@ mod tests { cols.b = [P::to_limbs_field::(&b[0]), P::to_limbs_field::(&b[1])]; cols.a_op_b .populate(&mut blu_events, 1, &a, &b, self.operation); - output.add_byte_lookup_events(blu_events); + output.add_events(&blu_events); row }) .collect::>(); diff --git a/core/src/operations/field/extensions/quadratic/sqrt.rs b/core/src/operations/field/extensions/quadratic/sqrt.rs index ebaa08729..528cb6560 100644 --- a/core/src/operations/field/extensions/quadratic/sqrt.rs +++ b/core/src/operations/field/extensions/quadratic/sqrt.rs @@ -111,8 +111,9 @@ mod tests { use super::QuadFieldSqrtCols; use crate::air::MachineAir; - use crate::air::{EventLens, WordAirBuilder}; + use crate::air::{EventLens, EventMutLens, WordAirBuilder}; use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::{FieldParameters, Limbs}; use crate::runtime::ExecutionRecord; use crate::runtime::Program; @@ -152,6 +153,7 @@ mod tests { impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for QuadSqrtChip

{ type InputEvents = &'a (); + type OutputEvents = &'a [ByteLookupEvent]; } impl EventLens> for ExecutionRecord { @@ -160,6 +162,17 @@ mod tests { } } + impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } + } + impl MachineAir for QuadSqrtChip

{ type Record = ExecutionRecord; @@ -169,10 +182,10 @@ mod tests { "QuadSqrtChip".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, _: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let num_test_cols = size_of::>(); let mut rng = thread_rng(); @@ -215,7 +228,7 @@ mod tests { let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); cols.a = [P::to_limbs_field::(&a[0]), P::to_limbs_field::(&a[1])]; cols.sqrt.populate(&mut blu_events, 1, a, self.sqrt_fn); - output.add_byte_lookup_events(blu_events); + output.add_events(&blu_events); row }) .collect::>(); diff --git a/core/src/operations/field/field_den.rs b/core/src/operations/field/field_den.rs index e522cabce..a112429d0 100644 --- a/core/src/operations/field/field_den.rs +++ b/core/src/operations/field/field_den.rs @@ -152,7 +152,7 @@ mod tests { mem::size_of, }; - use crate::air::{EventLens, WordAirBuilder}; + use crate::air::{EventLens, EventMutLens, WordAirBuilder}; use num::{bigint::RandBigInt, BigUint}; use p3_air::{Air, BaseAir}; use p3_baby_bear::BabyBear; @@ -165,6 +165,8 @@ mod tests { use crate::{ air::MachineAir, + bytes::event::ByteRecord, + bytes::ByteLookupEvent, utils::ec::weierstrass::{bls12_381::Bls12381BaseField, secp256k1::Secp256k1BaseField}, }; @@ -199,6 +201,7 @@ mod tests { impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for FieldDenChip

{ type InputEvents = &'a (); + type OutputEvents = &'a [ByteLookupEvent]; } impl EventLens> for ExecutionRecord { @@ -207,6 +210,17 @@ mod tests { } } + impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } + } + impl MachineAir for FieldDenChip

{ type Record = ExecutionRecord; @@ -216,10 +230,10 @@ mod tests { "FieldDen".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, _: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rng = thread_rng(); let num_rows = 1 << 8; @@ -250,7 +264,9 @@ mod tests { let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); cols.a = P::to_limbs_field::(a); cols.b = P::to_limbs_field::(b); - cols.a_den_b.populate(output, 1, a, b, self.sign); + let mut v = Vec::new(); + cols.a_den_b.populate(&mut v, 1, a, b, self.sign); + output.add_events(&v); row }) .collect::>(); diff --git a/core/src/operations/field/field_inner_product.rs b/core/src/operations/field/field_inner_product.rs index 100c67bcd..e2e9ebecb 100644 --- a/core/src/operations/field/field_inner_product.rs +++ b/core/src/operations/field/field_inner_product.rs @@ -152,9 +152,11 @@ mod tests { use super::{FieldInnerProductCols, Limbs}; - use crate::air::{EventLens, WordAirBuilder}; + use crate::air::{EventLens, EventMutLens, WordAirBuilder}; use crate::{ air::MachineAir, + bytes::event::ByteRecord, + bytes::ByteLookupEvent, utils::ec::weierstrass::{bls12_381::Bls12381BaseField, secp256k1::Secp256k1BaseField}, }; @@ -166,6 +168,7 @@ mod tests { use crate::utils::{pad_to_power_of_two_nongeneric, BabyBearPoseidon2}; use crate::utils::{uni_stark_prove as prove, uni_stark_verify as verify}; use p3_field::AbstractField; + #[derive(AlignedBorrow, Debug, Clone)] pub struct TestCols { pub a: [Limbs; 1], @@ -187,6 +190,7 @@ mod tests { impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for FieldIpChip

{ type InputEvents = &'a (); + type OutputEvents = &'a [ByteLookupEvent]; } impl EventLens> for ExecutionRecord { @@ -195,6 +199,17 @@ mod tests { } } + impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } + } + impl MachineAir for FieldIpChip

{ type Record = ExecutionRecord; @@ -204,10 +219,10 @@ mod tests { "FieldInnerProduct".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, _: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rng = thread_rng(); let num_rows = 1 << 8; @@ -233,7 +248,9 @@ mod tests { let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); cols.a[0] = P::to_limbs_field::(&a[0]); cols.b[0] = P::to_limbs_field::(&b[0]); - cols.a_ip_b.populate(output, 1, a, b); + let mut v = Vec::new(); + cols.a_ip_b.populate(&mut v, 1, a, b); + output.add_events(&v); row }) .collect::>(); diff --git a/core/src/operations/field/field_op.rs b/core/src/operations/field/field_op.rs index be812c6fd..77f74df57 100644 --- a/core/src/operations/field/field_op.rs +++ b/core/src/operations/field/field_op.rs @@ -243,8 +243,9 @@ mod tests { use crate::{air::MachineAir, utils::ec::weierstrass::bls12_381::Bls12381BaseField}; - use crate::air::{EventLens, WordAirBuilder}; + use crate::air::{EventLens, EventMutLens, WordAirBuilder}; use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::FieldParameters; use crate::runtime::ExecutionRecord; use crate::runtime::Program; @@ -280,6 +281,7 @@ mod tests { impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for FieldOpChip

{ type InputEvents = &'a (); + type OutputEvents = &'a [ByteLookupEvent]; } impl EventLens> for ExecutionRecord { @@ -288,6 +290,17 @@ mod tests { } } + impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } + } + impl MachineAir for FieldOpChip

{ type Record = ExecutionRecord; @@ -297,10 +310,10 @@ mod tests { format!("FieldOp{:?}", self.operation) } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, _: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rng = thread_rng(); let num_rows = 1 << 8; @@ -334,7 +347,7 @@ mod tests { cols.b = P::to_limbs_field::(b); cols.a_op_b .populate(&mut blu_events, 1, a, b, self.operation); - output.add_byte_lookup_events(blu_events); + output.add_events(&blu_events); row }) .collect::>(); diff --git a/core/src/operations/field/field_sqrt.rs b/core/src/operations/field/field_sqrt.rs index 08d4a8619..284617cfc 100644 --- a/core/src/operations/field/field_sqrt.rs +++ b/core/src/operations/field/field_sqrt.rs @@ -120,8 +120,9 @@ mod tests { use super::{FieldSqrtCols, Limbs}; use crate::air::WordAirBuilder; - use crate::air::{EventLens, MachineAir}; + use crate::air::{EventLens, EventMutLens, MachineAir}; use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::{FieldParameters, DEFAULT_NUM_LIMBS_T}; use crate::runtime::ExecutionRecord; use crate::runtime::Program; @@ -153,6 +154,7 @@ mod tests { impl<'a, P: FieldParameters> crate::air::WithEvents<'a> for EdSqrtChip

{ type InputEvents = &'a (); + type OutputEvents = &'a [ByteLookupEvent]; } impl EventLens> for ExecutionRecord { @@ -161,6 +163,17 @@ mod tests { } } + impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } + } + impl> MachineAir for EdSqrtChip

{ @@ -172,10 +185,10 @@ mod tests { "EdSqrtChip".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, _: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rng = thread_rng(); let num_rows = 1 << 8; @@ -200,7 +213,7 @@ mod tests { let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); cols.a = P::to_limbs_field::(a); cols.sqrt.populate(&mut blu_events, 1, a, ed25519_sqrt); - output.add_byte_lookup_events(blu_events); + output.add_events(&blu_events); row }) .collect::>(); diff --git a/core/src/program/mod.rs b/core/src/program/mod.rs index ef81a9816..142305a25 100644 --- a/core/src/program/mod.rs +++ b/core/src/program/mod.rs @@ -10,7 +10,7 @@ use p3_matrix::{dense::RowMajorMatrix, Matrix}; use sphinx_derive::AlignedBorrow; use crate::{ - air::{EventLens, MachineAir, ProgramAirBuilder, WithEvents}, + air::{EventLens, EventMutLens, MachineAir, ProgramAirBuilder, WithEvents}, cpu::{ columns::{InstructionCols, OpcodeSelectorCols}, CpuEvent, @@ -59,6 +59,7 @@ impl<'a> WithEvents<'a> for ProgramChip { // the Program &'a Program, ); + type OutputEvents = &'a (); } impl MachineAir for ProgramChip { @@ -104,18 +105,18 @@ impl MachineAir for ProgramChip { Some(trace) } - fn generate_dependencies>( + fn generate_dependencies, OL: EventMutLens>( &self, _input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) { // Do nothing since this chip has no dependencies. } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OL, ) -> RowMajorMatrix { // Generate the trace rows for each event. diff --git a/core/src/runtime/record.rs b/core/src/runtime/record.rs index a8b5f1f71..1aa12b6fb 100644 --- a/core/src/runtime/record.rs +++ b/core/src/runtime/record.rs @@ -9,7 +9,6 @@ use p3_field::{AbstractField, Field}; use serde::{Deserialize, Serialize}; use super::{program::Program, Opcode}; -use crate::bytes::ByteLookupEvent; use crate::cpu::CpuEvent; use crate::runtime::MemoryInitializeFinalizeEvent; use crate::runtime::MemoryRecordEnum; @@ -23,7 +22,18 @@ use crate::syscall::precompiles::sha256::{ShaCompressEvent, ShaExtendEvent}; use crate::syscall::precompiles::{ECAddEvent, ECDoubleEvent}; use crate::utils::env; use crate::{ - air::EventLens, + air::PublicValues, + operations::field::params::FieldParameters, + syscall::precompiles::{ + bls12_381::g1_decompress::Bls12381G1DecompressEvent, + field::{add::FieldAddEvent, mul::FieldMulEvent, sub::FieldSubEvent}, + quad_field::{add::QuadFieldAddEvent, mul::QuadFieldMulEvent, sub::QuadFieldSubEvent}, + secp256k1::decompress::Secp256k1DecompressEvent, + }, + utils::ec::weierstrass::bls12_381::Bls12381BaseField, +}; +use crate::{ + air::{EventLens, EventMutLens}, alu::AluEvent, memory::MemoryProgramChip, stark::{ @@ -45,17 +55,7 @@ use crate::{ weierstrass::{bls12_381::Bls12381, bn254::Bn254, secp256k1::Secp256k1}, }, }; -use crate::{ - air::PublicValues, - operations::field::params::FieldParameters, - syscall::precompiles::{ - bls12_381::g1_decompress::Bls12381G1DecompressEvent, - field::{add::FieldAddEvent, mul::FieldMulEvent, sub::FieldSubEvent}, - quad_field::{add::QuadFieldAddEvent, mul::QuadFieldMulEvent, sub::QuadFieldSubEvent}, - secp256k1::decompress::Secp256k1DecompressEvent, - }, - utils::ec::weierstrass::bls12_381::Bls12381BaseField, -}; +use crate::{alu::DivRemEvent, bytes::ByteLookupEvent, cpu::trace::CpuOutputEvent}; use crate::{bytes::event::ByteRecord, stark::Indexed}; /// A record of the execution of a program. Contains event data for everything that happened during @@ -266,19 +266,25 @@ impl EventLens for ExecutionRecord { } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events( + &self, + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp_add_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events( + &self, + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp_sub_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events( + &self, + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_fp_mul_events } } @@ -316,13 +322,17 @@ impl EventLens> for ExecutionRecord { } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events( + &self, + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bls12381_g1_add_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events( + &self, + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bn254_add_events } } @@ -344,7 +354,9 @@ impl EventLens> for ExecutionRecord { } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events( + &self, + ) -> as crate::air::WithEvents<'_>>::InputEvents { &self.bn254_double_events } } @@ -363,6 +375,351 @@ impl EventLens> for ExecutionRecord { } } +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + match event { + DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(**e), + DivRemEvent::MulEvent(e) => self.add_mul_event(**e), + DivRemEvent::LtEvent(e) => self.add_lt_event(**e), + } + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events(&mut self, _events: as crate::air::WithEvents<'_>>::OutputEvents) {} +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + match event { + CpuOutputEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(**e), + CpuOutputEvent::AluEvent(e) => match e.opcode { + Opcode::ADD => { + self.add_events.push(**e); + } + Opcode::MUL | Opcode::MULH | Opcode::MULHU | Opcode::MULHSU => { + self.mul_events.push(**e) + } + Opcode::SUB => self.sub_events.push(**e), + Opcode::XOR | Opcode::OR | Opcode::AND => self.bitwise_events.push(**e), + Opcode::SLL => self.shift_left_events.push(**e), + Opcode::SRL | Opcode::SRA => self.shift_right_events.push(**e), + Opcode::SLT | Opcode::SLTU => self.lt_events.push(**e), + _ => { + panic!("Invalid opcode: {:?}", e.opcode); + } + }, + } + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, _events: >::OutputEvents) {} +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + _events: >::OutputEvents, + ) { + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, _events: >::OutputEvents) {} +} + +impl EventMutLens for ExecutionRecord { + fn add_events(&mut self, events: >::OutputEvents) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens for ExecutionRecord { + fn add_events( + &mut self, + events: >::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + +impl EventMutLens> for ExecutionRecord { + fn add_events( + &mut self, + events: as crate::air::WithEvents<'_>>::OutputEvents, + ) { + for event in events { + self.add_byte_lookup_event(*event); + } + } +} + pub struct ShardingConfig { pub shard_size: usize, pub add_len: usize, diff --git a/core/src/syscall/precompiles/blake3/compress/trace.rs b/core/src/syscall/precompiles/blake3/compress/trace.rs index 2226eb173..0d4159773 100644 --- a/core/src/syscall/precompiles/blake3/compress/trace.rs +++ b/core/src/syscall/precompiles/blake3/compress/trace.rs @@ -8,8 +8,8 @@ use super::{ columns::Blake3CompressInnerCols, G_INDEX, G_INPUT_SIZE, MSG_SCHEDULE, NUM_MSG_WORDS_PER_CALL, NUM_STATE_WORDS_PER_CALL, OPERATION_COUNT, }; -use crate::air::{EventLens, WithEvents}; -use crate::bytes::event::ByteRecord; +use crate::air::{EventLens, EventMutLens, WithEvents}; +use crate::bytes::ByteLookupEvent; use crate::{ air::MachineAir, runtime::{ExecutionRecord, MemoryRecordEnum, Program}, @@ -21,6 +21,7 @@ use crate::{ impl<'a> WithEvents<'a> for Blake3CompressInnerChip { type InputEvents = &'a [Blake3CompressInnerEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for Blake3CompressInnerChip { @@ -31,10 +32,10 @@ impl MachineAir for Blake3CompressInnerChip { "Blake3CompressInner".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = Vec::new(); @@ -104,7 +105,9 @@ impl MachineAir for Blake3CompressInnerChip { event.message_reads[round][operation][1].value, ]; - cols.g.populate(output, shard, input); + let mut v = Vec::new(); + cols.g.populate(&mut v, shard, input); + output.add_events(&v) } clk += 1; @@ -116,7 +119,7 @@ impl MachineAir for Blake3CompressInnerChip { } } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_rows(&mut rows, || [F::zero(); NUM_BLAKE3_COMPRESS_INNER_COLS]); diff --git a/core/src/syscall/precompiles/bls12_381/g1_decompress.rs b/core/src/syscall/precompiles/bls12_381/g1_decompress.rs index de7e6ebee..4c92b644a 100644 --- a/core/src/syscall/precompiles/bls12_381/g1_decompress.rs +++ b/core/src/syscall/precompiles/bls12_381/g1_decompress.rs @@ -11,7 +11,9 @@ use p3_matrix::{dense::RowMajorMatrix, Matrix}; use serde::{Deserialize, Serialize}; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, ByteAirBuilder, EventLens, MemoryAirBuilder, WithEvents}; +use crate::air::{ + AluAirBuilder, ByteAirBuilder, EventLens, EventMutLens, MemoryAirBuilder, WithEvents, +}; use crate::bytes::{ByteLookupEvent, ByteOpcode}; use crate::operations::field::params::FieldParameters; use crate::operations::field::range::FieldRangeCols; @@ -231,6 +233,7 @@ impl Bls12381G1DecompressChip { impl<'a> WithEvents<'a> for Bls12381G1DecompressChip { type InputEvents = &'a [Bls12381G1DecompressEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for Bls12381G1DecompressChip { @@ -241,10 +244,10 @@ impl MachineAir for Bls12381G1DecompressChip { "Bls12381G1Decompress".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = Vec::new(); @@ -280,7 +283,7 @@ impl MachineAir for Bls12381G1DecompressChip { rows.push(row); } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_rows(&mut rows, || { let mut row = [F::zero(); size_of::>()]; diff --git a/core/src/syscall/precompiles/bls12_381/g2_add.rs b/core/src/syscall/precompiles/bls12_381/g2_add.rs index 0b4cdd3e7..b81e1543a 100644 --- a/core/src/syscall/precompiles/bls12_381/g2_add.rs +++ b/core/src/syscall/precompiles/bls12_381/g2_add.rs @@ -1,5 +1,8 @@ -use crate::air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}; +use crate::air::{ + AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents, +}; use crate::bytes::event::ByteRecord; +use crate::bytes::ByteLookupEvent; use crate::memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}; use crate::operations::field::extensions::quadratic::{QuadFieldOpCols, QuadFieldOperation}; use crate::operations::field::params::{FieldParameters, WORDS_QUAD_EXT_CURVEPOINT}; @@ -278,6 +281,7 @@ impl BaseAir for Bls12381G2AffineAddChip { impl<'a> WithEvents<'a> for Bls12381G2AffineAddChip { type InputEvents = &'a [Bls12381G2AffineAddEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for Bls12381G2AffineAddChip { @@ -288,10 +292,10 @@ impl MachineAir for Bls12381G2AffineAddChip { "G2AffineAdd".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut Self::Record, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = vec![]; @@ -343,7 +347,7 @@ impl MachineAir for Bls12381G2AffineAddChip { rows.push(row); } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_vec_rows(&mut rows, || { let mut row = vec![F::zero(); width]; diff --git a/core/src/syscall/precompiles/bls12_381/g2_double.rs b/core/src/syscall/precompiles/bls12_381/g2_double.rs index 315da8748..c7870a2f5 100644 --- a/core/src/syscall/precompiles/bls12_381/g2_double.rs +++ b/core/src/syscall/precompiles/bls12_381/g2_double.rs @@ -1,5 +1,6 @@ -use crate::air::{EventLens, MachineAir, WithEvents}; +use crate::air::{EventLens, EventMutLens, MachineAir, WithEvents}; use crate::bytes::event::ByteRecord; +use crate::bytes::ByteLookupEvent; use crate::memory::{MemoryCols, MemoryWriteCols}; use crate::operations::field::extensions::quadratic::{QuadFieldOpCols, QuadFieldOperation}; use crate::operations::field::params::{FieldParameters, Limbs, WORDS_QUAD_EXT_CURVEPOINT}; @@ -227,6 +228,7 @@ impl BaseAir for Bls12381G2AffineDoubleChip { impl<'a> WithEvents<'a> for Bls12381G2AffineDoubleChip { type InputEvents = &'a [Bls12381G2AffineDoubleEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for Bls12381G2AffineDoubleChip { @@ -237,10 +239,10 @@ impl MachineAir for Bls12381G2AffineDoubleChip { "Bls12381G2AffineDoubleChip".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut Self::Record, + output: &mut OL, ) -> RowMajorMatrix { let mut rows: Vec> = vec![]; @@ -280,7 +282,7 @@ impl MachineAir for Bls12381G2AffineDoubleChip { rows.push(row); } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_vec_rows(&mut rows, || { let mut row = vec![F::zero(); width]; diff --git a/core/src/syscall/precompiles/edwards/ed_add.rs b/core/src/syscall/precompiles/edwards/ed_add.rs index e0645207e..3bb48e55a 100644 --- a/core/src/syscall/precompiles/edwards/ed_add.rs +++ b/core/src/syscall/precompiles/edwards/ed_add.rs @@ -44,7 +44,7 @@ use crate::utils::limbs_from_prev_access; use crate::utils::pad_vec_rows; use crate::{air::MachineAir, utils::ec::EllipticCurveParameters}; use crate::{ - air::{AluAirBuilder, EventLens, MemoryAirBuilder, WithEvents}, + air::{AluAirBuilder, EventLens, EventMutLens, MemoryAirBuilder, WithEvents}, syscall::precompiles::ECAddEvent, }; @@ -145,11 +145,12 @@ impl< impl<'a, E: EllipticCurve + EdwardsParameters> WithEvents<'a> for EdAddAssignChip { type InputEvents = &'a [ECAddEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for EdAddAssignChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; @@ -159,10 +160,10 @@ where "EdAddAssign".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let (mut rows, new_byte_lookup_events): (Vec>, Vec>) = input .events() @@ -212,7 +213,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/edwards/ed_decompress.rs b/core/src/syscall/precompiles/edwards/ed_decompress.rs index 396a06f6d..3cdf6470d 100644 --- a/core/src/syscall/precompiles/edwards/ed_decompress.rs +++ b/core/src/syscall/precompiles/edwards/ed_decompress.rs @@ -14,6 +14,7 @@ use serde::{Deserialize, Serialize}; use sphinx_derive::AlignedBorrow; use crate::air::EventLens; +use crate::air::EventMutLens; use crate::air::MachineAir; use crate::air::MachineAirBuilder; use crate::air::WithEvents; @@ -335,11 +336,12 @@ impl EdDecompressChip { impl<'a, E: EdwardsParameters> WithEvents<'a> for EdDecompressChip { type InputEvents = &'a [EdDecompressEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for EdDecompressChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; @@ -349,10 +351,10 @@ where "EdDecompress".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = Vec::new(); @@ -360,7 +362,9 @@ where let event = &input.events()[i]; let mut row = vec![F::zero(); size_of::>()]; let cols: &mut EdDecompressCols = row.as_mut_slice().borrow_mut(); - cols.populate::(event, output); + let mut v = Vec::new(); + cols.populate::(event, &mut v); + output.add_events(&v); rows.push(row); } diff --git a/core/src/syscall/precompiles/field/add.rs b/core/src/syscall/precompiles/field/add.rs index 299ca454b..c59896d57 100644 --- a/core/src/syscall/precompiles/field/add.rs +++ b/core/src/syscall/precompiles/field/add.rs @@ -15,8 +15,8 @@ use sphinx_derive::AlignedBorrow; use tracing::instrument; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, - bytes::{event::ByteRecord, ByteLookupEvent}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, + bytes::ByteLookupEvent, memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}, operations::field::{ field_op::{FieldOpCols, FieldOperation}, @@ -115,11 +115,12 @@ pub fn create_fp_add_event( impl<'a, FP: FieldParameters> WithEvents<'a> for FieldAddChip { type InputEvents = &'a [FieldAddEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for FieldAddChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -132,10 +133,10 @@ where } #[instrument(name = "generate field add trace", level = "debug", skip_all)] - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the field type. let events = input.events(); @@ -184,7 +185,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/field/mul.rs b/core/src/syscall/precompiles/field/mul.rs index 8aa0bf5c5..112098015 100644 --- a/core/src/syscall/precompiles/field/mul.rs +++ b/core/src/syscall/precompiles/field/mul.rs @@ -15,8 +15,8 @@ use sphinx_derive::AlignedBorrow; use tracing::instrument; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, - bytes::{event::ByteRecord, ByteLookupEvent}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, + bytes::ByteLookupEvent, memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}, operations::field::{ field_op::{FieldOpCols, FieldOperation}, @@ -115,11 +115,12 @@ pub fn create_fp_mul_event( impl<'a, FP: FieldParameters> WithEvents<'a> for FieldMulChip { type InputEvents = &'a [FieldMulEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for FieldMulChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -132,10 +133,10 @@ where } #[instrument(name = "generate field mul trace", level = "debug", skip_all)] - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the field type. let events = input.events(); @@ -183,7 +184,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/field/sub.rs b/core/src/syscall/precompiles/field/sub.rs index ba06f620a..d217e1348 100644 --- a/core/src/syscall/precompiles/field/sub.rs +++ b/core/src/syscall/precompiles/field/sub.rs @@ -15,8 +15,8 @@ use sphinx_derive::AlignedBorrow; use tracing::instrument; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, - bytes::{event::ByteRecord, ByteLookupEvent}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, + bytes::ByteLookupEvent, memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}, operations::field::{ field_op::{FieldOpCols, FieldOperation}, @@ -115,11 +115,12 @@ pub fn create_fp_sub_event( impl<'a, FP: FieldParameters> WithEvents<'a> for FieldSubChip { type InputEvents = &'a [FieldSubEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for FieldSubChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -132,10 +133,10 @@ where } #[instrument(name = "generate field sub trace", level = "debug", skip_all)] - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the field type. let events = input.events(); @@ -183,7 +184,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/keccak256/trace.rs b/core/src/syscall/precompiles/keccak256/trace.rs index 3ababeb09..84f10655b 100644 --- a/core/src/syscall/precompiles/keccak256/trace.rs +++ b/core/src/syscall/precompiles/keccak256/trace.rs @@ -5,10 +5,10 @@ use p3_keccak_air::{generate_trace_rows, NUM_KECCAK_COLS, NUM_ROUNDS}; use p3_matrix::{dense::RowMajorMatrix, Matrix}; use p3_maybe_rayon::prelude::{ParallelIterator, ParallelSlice}; -use crate::air::{EventLens, WithEvents}; +use crate::air::{EventLens, EventMutLens, WithEvents}; use crate::bytes::event::ByteRecord; -use crate::{runtime::Program, stark::MachineRecord}; - +use crate::bytes::ByteLookupEvent; +use crate::runtime::Program; use crate::{air::MachineAir, runtime::ExecutionRecord}; use super::KeccakPermuteEvent; @@ -19,6 +19,7 @@ use super::{ impl<'a> WithEvents<'a> for KeccakPermuteChip { type InputEvents = &'a [KeccakPermuteEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for KeccakPermuteChip { @@ -29,10 +30,10 @@ impl MachineAir for KeccakPermuteChip { "KeccakPermute".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let num_events = input.events().len(); let chunk_size = std::cmp::max(num_events / num_cpus::get(), 1); @@ -42,7 +43,7 @@ impl MachineAir for KeccakPermuteChip { .events() .par_chunks(chunk_size) .map(|chunk_events| { - let mut record = ExecutionRecord::default(); + let mut event_record = Vec::new(); let mut new_byte_lookup_events = Vec::new(); // First generate all the p3_keccak_air traces at once. @@ -105,17 +106,16 @@ impl MachineAir for KeccakPermuteChip { rows }) .collect::>(); - record.add_byte_lookup_events(new_byte_lookup_events); - (rows, record) + event_record.add_byte_lookup_events(new_byte_lookup_events); + (rows, event_record) }) .collect::>(); // Generate the trace rows for each event. let mut rows: Vec<[F; NUM_KECCAK_MEM_COLS]> = vec![]; - for (mut row, mut record) in rows_and_records { + for (mut row, record) in rows_and_records { rows.append(&mut row); - record.index = output.index; - output.append(&mut record); + output.add_events(&record); } let nb_rows = rows.len(); diff --git a/core/src/syscall/precompiles/quad_field/add.rs b/core/src/syscall/precompiles/quad_field/add.rs index 94d9a627e..9722d084e 100644 --- a/core/src/syscall/precompiles/quad_field/add.rs +++ b/core/src/syscall/precompiles/quad_field/add.rs @@ -15,8 +15,8 @@ use sphinx_derive::AlignedBorrow; use tracing::instrument; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, - bytes::{event::ByteRecord, ByteLookupEvent}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, + bytes::ByteLookupEvent, memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}, operations::field::{ extensions::quadratic::{QuadFieldOpCols, QuadFieldOperation}, @@ -141,11 +141,12 @@ pub fn create_fp2_add_event( impl<'a, FP: FieldParameters> WithEvents<'a> for QuadFieldAddChip { type InputEvents = &'a [QuadFieldAddEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for QuadFieldAddChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -158,10 +159,10 @@ where } #[instrument(name = "generate bls12381 fp2 add trace", level = "debug", skip_all)] - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the field type. let events = input.events(); @@ -215,7 +216,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/quad_field/mul.rs b/core/src/syscall/precompiles/quad_field/mul.rs index 51982b8df..5df34e7e6 100644 --- a/core/src/syscall/precompiles/quad_field/mul.rs +++ b/core/src/syscall/precompiles/quad_field/mul.rs @@ -15,8 +15,8 @@ use sphinx_derive::AlignedBorrow; use tracing::instrument; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, - bytes::{event::ByteRecord, ByteLookupEvent}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, + bytes::ByteLookupEvent, memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}, operations::field::{ extensions::quadratic::{QuadFieldOpCols, QuadFieldOperation}, @@ -150,11 +150,12 @@ pub fn create_fp2_mul_event( impl<'a, FP: FieldParameters> WithEvents<'a> for QuadFieldMulChip { type InputEvents = &'a [QuadFieldMulEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for QuadFieldMulChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -167,10 +168,10 @@ where } #[instrument(name = "generate bls12381 fp2 mul trace", level = "debug", skip_all)] - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the field type. let events = input.events(); @@ -224,7 +225,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/quad_field/sub.rs b/core/src/syscall/precompiles/quad_field/sub.rs index 9b9476f53..f500f5d4b 100644 --- a/core/src/syscall/precompiles/quad_field/sub.rs +++ b/core/src/syscall/precompiles/quad_field/sub.rs @@ -15,8 +15,8 @@ use sphinx_derive::AlignedBorrow; use tracing::instrument; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, - bytes::{event::ByteRecord, ByteLookupEvent}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, + bytes::ByteLookupEvent, memory::{MemoryCols, MemoryReadCols, MemoryWriteCols}, operations::field::{ extensions::quadratic::{QuadFieldOpCols, QuadFieldOperation}, @@ -141,11 +141,12 @@ pub fn create_fp2_sub_event( impl<'a, FP: FieldParameters> WithEvents<'a> for QuadFieldSubChip { type InputEvents = &'a [QuadFieldSubEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for QuadFieldSubChip where - ExecutionRecord: EventLens>, + ExecutionRecord: EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -158,10 +159,10 @@ where } #[instrument(name = "generate bls12381 fp2 sub trace", level = "debug", skip_all)] - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the field type. let events = input.events(); @@ -215,7 +216,7 @@ where .unzip(); for byte_lookup_events in new_byte_lookup_events { - output.add_byte_lookup_events(byte_lookup_events); + output.add_events(&byte_lookup_events); } pad_vec_rows(&mut rows, || { diff --git a/core/src/syscall/precompiles/secp256k1/decompress.rs b/core/src/syscall/precompiles/secp256k1/decompress.rs index ae0f1e021..db5191135 100644 --- a/core/src/syscall/precompiles/secp256k1/decompress.rs +++ b/core/src/syscall/precompiles/secp256k1/decompress.rs @@ -11,7 +11,9 @@ use p3_matrix::{dense::RowMajorMatrix, Matrix}; use serde::{Deserialize, Serialize}; use sphinx_derive::AlignedBorrow; -use crate::air::{AluAirBuilder, ByteAirBuilder, EventLens, MemoryAirBuilder, WithEvents}; +use crate::air::{ + AluAirBuilder, ByteAirBuilder, EventLens, EventMutLens, MemoryAirBuilder, WithEvents, +}; use crate::bytes::event::ByteRecord; use crate::bytes::{ByteLookupEvent, ByteOpcode}; use crate::operations::field::range::FieldRangeCols; @@ -216,6 +218,7 @@ impl Secp256k1DecompressChip { impl<'a> WithEvents<'a> for Secp256k1DecompressChip { type InputEvents = &'a [Secp256k1DecompressEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for Secp256k1DecompressChip { @@ -226,10 +229,10 @@ impl MachineAir for Secp256k1DecompressChip { "Secp256k1Decompress".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = Vec::new(); @@ -257,7 +260,7 @@ impl MachineAir for Secp256k1DecompressChip { rows.push(row); } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_rows(&mut rows, || { let mut row = [F::zero(); size_of::>()]; diff --git a/core/src/syscall/precompiles/sha256/compress/trace.rs b/core/src/syscall/precompiles/sha256/compress/trace.rs index 98af5b904..cb40afd09 100644 --- a/core/src/syscall/precompiles/sha256/compress/trace.rs +++ b/core/src/syscall/precompiles/sha256/compress/trace.rs @@ -8,14 +8,15 @@ use super::{ ShaCompressChip, ShaCompressEvent, SHA_COMPRESS_K, }; use crate::{ - air::{EventLens, MachineAir, WithEvents, Word}, - bytes::event::ByteRecord, + air::{EventLens, EventMutLens, MachineAir, WithEvents, Word}, + bytes::ByteLookupEvent, runtime::{ExecutionRecord, Program}, utils::pad_rows, }; impl<'a> WithEvents<'a> for ShaCompressChip { type InputEvents = &'a [ShaCompressEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for ShaCompressChip { @@ -27,10 +28,10 @@ impl MachineAir for ShaCompressChip { "ShaCompress".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = Vec::new(); @@ -74,6 +75,7 @@ impl MachineAir for ShaCompressChip { rows.push(row); } + let mut compress_blu_events = Vec::new(); // Performs the compress operation. for j in 0..64 { if j % 8 == 0 { @@ -112,43 +114,76 @@ impl MachineAir for ShaCompressChip { cols.g = Word::from(g); cols.h = Word::from(h); - let e_rr_6 = cols.e_rr_6.populate(output, shard, e, 6); - let e_rr_11 = cols.e_rr_11.populate(output, shard, e, 11); - let e_rr_25 = cols.e_rr_25.populate(output, shard, e, 25); - let s1_intermediate = cols - .s1_intermediate - .populate(output, shard, e_rr_6, e_rr_11); - let s1 = cols.s1.populate(output, shard, s1_intermediate, e_rr_25); - - let e_and_f = cols.e_and_f.populate(output, shard, e, f); - let e_not = cols.e_not.populate(output, shard, e); - let e_not_and_g = cols.e_not_and_g.populate(output, shard, e_not, g); - let ch = cols.ch.populate(output, shard, e_and_f, e_not_and_g); - - let temp1 = - cols.temp1 - .populate(output, shard, h, s1, ch, event.w[j], SHA_COMPRESS_K[j]); - - let a_rr_2 = cols.a_rr_2.populate(output, shard, a, 2); - let a_rr_13 = cols.a_rr_13.populate(output, shard, a, 13); - let a_rr_22 = cols.a_rr_22.populate(output, shard, a, 22); - let s0_intermediate = cols - .s0_intermediate - .populate(output, shard, a_rr_2, a_rr_13); - let s0 = cols.s0.populate(output, shard, s0_intermediate, a_rr_22); - - let a_and_b = cols.a_and_b.populate(output, shard, a, b); - let a_and_c = cols.a_and_c.populate(output, shard, a, c); - let b_and_c = cols.b_and_c.populate(output, shard, b, c); - let maj_intermediate = cols - .maj_intermediate - .populate(output, shard, a_and_b, a_and_c); - let maj = cols.maj.populate(output, shard, maj_intermediate, b_and_c); - - let temp2 = cols.temp2.populate(output, shard, s0, maj); - - let d_add_temp1 = cols.d_add_temp1.populate(output, shard, d, temp1); - let temp1_add_temp2 = cols.temp1_add_temp2.populate(output, shard, temp1, temp2); + let e_rr_6 = cols.e_rr_6.populate(&mut compress_blu_events, shard, e, 6); + let e_rr_11 = cols + .e_rr_11 + .populate(&mut compress_blu_events, shard, e, 11); + let e_rr_25 = cols + .e_rr_25 + .populate(&mut compress_blu_events, shard, e, 25); + let s1_intermediate = + cols.s1_intermediate + .populate(&mut compress_blu_events, shard, e_rr_6, e_rr_11); + let s1 = + cols.s1 + .populate(&mut compress_blu_events, shard, s1_intermediate, e_rr_25); + + let e_and_f = cols.e_and_f.populate(&mut compress_blu_events, shard, e, f); + let e_not = cols.e_not.populate(&mut compress_blu_events, shard, e); + let e_not_and_g = + cols.e_not_and_g + .populate(&mut compress_blu_events, shard, e_not, g); + let ch = cols + .ch + .populate(&mut compress_blu_events, shard, e_and_f, e_not_and_g); + + let temp1 = cols.temp1.populate( + &mut compress_blu_events, + shard, + h, + s1, + ch, + event.w[j], + SHA_COMPRESS_K[j], + ); + + let a_rr_2 = cols.a_rr_2.populate(&mut compress_blu_events, shard, a, 2); + let a_rr_13 = cols + .a_rr_13 + .populate(&mut compress_blu_events, shard, a, 13); + let a_rr_22 = cols + .a_rr_22 + .populate(&mut compress_blu_events, shard, a, 22); + let s0_intermediate = + cols.s0_intermediate + .populate(&mut compress_blu_events, shard, a_rr_2, a_rr_13); + let s0 = + cols.s0 + .populate(&mut compress_blu_events, shard, s0_intermediate, a_rr_22); + + let a_and_b = cols.a_and_b.populate(&mut compress_blu_events, shard, a, b); + let a_and_c = cols.a_and_c.populate(&mut compress_blu_events, shard, a, c); + let b_and_c = cols.b_and_c.populate(&mut compress_blu_events, shard, b, c); + let maj_intermediate = cols.maj_intermediate.populate( + &mut compress_blu_events, + shard, + a_and_b, + a_and_c, + ); + let maj = + cols.maj + .populate(&mut compress_blu_events, shard, maj_intermediate, b_and_c); + + let temp2 = cols + .temp2 + .populate(&mut compress_blu_events, shard, s0, maj); + + let d_add_temp1 = + cols.d_add_temp1 + .populate(&mut compress_blu_events, shard, d, temp1); + let temp1_add_temp2 = + cols.temp1_add_temp2 + .populate(&mut compress_blu_events, shard, temp1, temp2); event.h[7] = g; event.h[6] = f; @@ -182,7 +217,7 @@ impl MachineAir for ShaCompressChip { cols.octet_num[octet_num_idx] = F::one(); cols.finalize_add - .populate(output, shard, og_h[j], event.h[j]); + .populate(&mut compress_blu_events, shard, og_h[j], event.h[j]); cols.mem .populate_write(event.h_write_records[j], &mut new_byte_lookup_events); cols.mem_addr = F::from_canonical_u32(event.h_ptr + (j * 4) as u32); @@ -215,9 +250,10 @@ impl MachineAir for ShaCompressChip { rows.push(row); } + output.add_events(&compress_blu_events) } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_rows(&mut rows, || [F::zero(); NUM_SHA_COMPRESS_COLS]); diff --git a/core/src/syscall/precompiles/sha256/extend/trace.rs b/core/src/syscall/precompiles/sha256/extend/trace.rs index 0cd1d773c..927c9b98c 100644 --- a/core/src/syscall/precompiles/sha256/extend/trace.rs +++ b/core/src/syscall/precompiles/sha256/extend/trace.rs @@ -5,13 +5,14 @@ use p3_matrix::dense::RowMajorMatrix; use super::{ShaExtendChip, ShaExtendCols, ShaExtendEvent, NUM_SHA_EXTEND_COLS}; use crate::{ - air::{EventLens, MachineAir, WithEvents}, - bytes::event::ByteRecord, + air::{EventLens, EventMutLens, MachineAir, WithEvents}, + bytes::ByteLookupEvent, runtime::{ExecutionRecord, Program}, }; impl<'a> WithEvents<'a> for ShaExtendChip { type InputEvents = &'a [ShaExtendEvent]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for ShaExtendChip { @@ -23,10 +24,10 @@ impl MachineAir for ShaExtendChip { "ShaExtend".to_string() } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { let mut rows = Vec::new(); @@ -53,61 +54,63 @@ impl MachineAir for ShaExtendChip { .populate(event.w_i_minus_7_reads[j], &mut new_byte_lookup_events); // `s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)`. + let mut v = Vec::new(); let w_i_minus_15 = event.w_i_minus_15_reads[j].value; let w_i_minus_15_rr_7 = cols.w_i_minus_15_rr_7 - .populate(output, shard, w_i_minus_15, 7); + .populate(&mut v, shard, w_i_minus_15, 7); let w_i_minus_15_rr_18 = cols.w_i_minus_15_rr_18 - .populate(output, shard, w_i_minus_15, 18); + .populate(&mut v, shard, w_i_minus_15, 18); let w_i_minus_15_rs_3 = cols.w_i_minus_15_rs_3 - .populate(output, shard, w_i_minus_15, 3); + .populate(&mut v, shard, w_i_minus_15, 3); let s0_intermediate = cols.s0_intermediate.populate( - output, + &mut v, shard, w_i_minus_15_rr_7, w_i_minus_15_rr_18, ); let s0 = cols .s0 - .populate(output, shard, s0_intermediate, w_i_minus_15_rs_3); + .populate(&mut v, shard, s0_intermediate, w_i_minus_15_rs_3); // `s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)`. let w_i_minus_2 = event.w_i_minus_2_reads[j].value; let w_i_minus_2_rr_17 = cols.w_i_minus_2_rr_17 - .populate(output, shard, w_i_minus_2, 17); + .populate(&mut v, shard, w_i_minus_2, 17); let w_i_minus_2_rr_19 = cols.w_i_minus_2_rr_19 - .populate(output, shard, w_i_minus_2, 19); + .populate(&mut v, shard, w_i_minus_2, 19); let w_i_minus_2_rs_10 = cols.w_i_minus_2_rs_10 - .populate(output, shard, w_i_minus_2, 10); + .populate(&mut v, shard, w_i_minus_2, 10); let s1_intermediate = cols.s1_intermediate.populate( - output, + &mut v, shard, w_i_minus_2_rr_17, w_i_minus_2_rr_19, ); let s1 = cols .s1 - .populate(output, shard, s1_intermediate, w_i_minus_2_rs_10); + .populate(&mut v, shard, s1_intermediate, w_i_minus_2_rs_10); // Compute `s2`. let w_i_minus_7 = event.w_i_minus_7_reads[j].value; let w_i_minus_16 = event.w_i_minus_16_reads[j].value; cols.s2 - .populate(output, shard, w_i_minus_16, s0, w_i_minus_7, s1); + .populate(&mut v, shard, w_i_minus_16, s0, w_i_minus_7, s1); cols.w_i .populate(event.w_i_writes[j], &mut new_byte_lookup_events); rows.push(row); + output.add_events(&v); } } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); let nb_rows = rows.len(); let mut padded_nb_rows = nb_rows.next_power_of_two(); diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs index 42223ec8e..633bc8d93 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs @@ -16,7 +16,6 @@ use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; use sphinx_derive::AlignedBorrow; -use crate::bytes::event::ByteRecord; use crate::bytes::ByteLookupEvent; use crate::memory::MemoryCols; use crate::memory::MemoryReadCols; @@ -36,7 +35,7 @@ use crate::utils::ec::EllipticCurve; use crate::utils::limbs_from_prev_access; use crate::utils::pad_vec_rows; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, syscall::precompiles::ECAddEvent, }; @@ -151,12 +150,14 @@ impl WeierstrassAddAssignChip { impl<'a, E: EllipticCurve + WeierstrassParameters> WithEvents<'a> for WeierstrassAddAssignChip { type InputEvents = &'a [ECAddEvent<::NB_LIMBS>]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for WeierstrassAddAssignChip where - ExecutionRecord: EventLens>, + ExecutionRecord: + EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -170,10 +171,10 @@ where } } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the curve type. let events = input.events(); @@ -224,7 +225,7 @@ where rows.push(row); } - output.add_byte_lookup_events(new_byte_lookup_events); + output.add_events(&new_byte_lookup_events); pad_vec_rows(&mut rows, || { let mut row = vec![F::zero(); size_of::>()]; diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs index df46c8e36..a253b1af5 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs @@ -29,7 +29,6 @@ use crate::operations::field::params::WORDS_FIELD_ELEMENT; use crate::runtime::ExecutionRecord; use crate::runtime::Program; use crate::runtime::SyscallCode; -use crate::stark::MachineRecord; use crate::syscall::precompiles::WORDS_CURVEPOINT; use crate::utils::ec::weierstrass::WeierstrassParameters; use crate::utils::ec::AffinePoint; @@ -39,7 +38,7 @@ use crate::utils::ec::EllipticCurve; use crate::utils::limbs_from_prev_access; use crate::utils::pad_vec_rows; use crate::{ - air::{AluAirBuilder, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, + air::{AluAirBuilder, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, syscall::precompiles::ECDoubleEvent, }; @@ -177,12 +176,14 @@ impl<'a, E: EllipticCurve + WeierstrassParameters> WithEvents<'a> for WeierstrassDoubleAssignChip { type InputEvents = &'a [ECDoubleEvent<::NB_LIMBS>]; + type OutputEvents = &'a [ByteLookupEvent]; } impl MachineAir for WeierstrassDoubleAssignChip where - ExecutionRecord: EventLens>, + ExecutionRecord: + EventLens> + EventMutLens>, { type Record = ExecutionRecord; type Program = Program; @@ -196,10 +197,10 @@ where } } - fn generate_trace>( + fn generate_trace, OL: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OL, ) -> RowMajorMatrix { // collects the events based on the curve type. let events = input.events(); @@ -210,7 +211,7 @@ where let rows_and_records = events .par_chunks(chunk_size) .map(|events| { - let mut record = ExecutionRecord::default(); + let mut record = Vec::new(); let mut new_byte_lookup_events = Vec::new(); let rows = events @@ -258,9 +259,9 @@ where // Generate the trace rows for each event. let mut rows = Vec::new(); - for (row, mut record) in rows_and_records { + for (row, record) in rows_and_records { rows.extend(row); - output.append(&mut record); + output.add_events(&record); } pad_vec_rows(&mut rows, || { diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 557c3c423..757164dcc 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -23,7 +23,7 @@ // THE SOFTWARE. use proc_macro::TokenStream; -use quote::quote; +use quote::{format_ident, quote}; use syn::{parse_macro_input, parse_quote, Data, DeriveInput, GenericParam, ItemFn}; #[proc_macro_derive(AlignedBorrow)] @@ -106,7 +106,7 @@ pub fn with_events_air_derive(input: TokenStream) -> TokenStream { match &ast.data { Data::Struct(_) | Data::Union(_) => unimplemented!("Only Enums are supported yet"), Data::Enum(e) => { - let fields = e + let (variants, fields): (Vec<_>, Vec<_>) = e .variants .iter() .map(|variant| { @@ -116,19 +116,45 @@ pub fn with_events_air_derive(input: TokenStream) -> TokenStream { fields.next().is_none(), "Only one field per variant is supported" ); - field + (variant.ident.clone(), field) }) - .collect::>(); + .unzip(); - let field_ty_events = fields.iter().map(|field| { + let field_ty_input_events = fields.iter().map(|field| { let field_ty = &field.ty; quote! { <#field_ty as #sphinx_core_path::air::WithEvents<'a>>::InputEvents } }); + let field_ty_output_events = fields.iter().map(|field| { + let field_ty = &field.ty; + quote! { + <#field_ty as #sphinx_core_path::air::WithEvents<'a>>::OutputEvents + } + }); + + let output_event_name = format_ident!("{}OutputEvents", name); + // we reuse the variant names for the original enum + let output_event_arms = variants.into_iter().zip(field_ty_output_events).map( + |(variant, output_event_ty)| { + quote! { + #variant(#output_event_ty) + } + }, + ); + + let type_params = type_params.collect::>(); + let const_params = const_params.collect::>(); + let type_params_idents = type_params.iter().map(|param| param.ident.clone()); + let const_params_idents = const_params.iter().map(|param| param.ident.clone()); quote!{ + pub enum #output_event_name <'a, #(#type_params),*, #(#const_params),*> { + #(#output_event_arms,)* + } + impl <'a, #(#type_params),*, #(#const_params),*> #sphinx_core_path::air::WithEvents<'a> for #name #ty_generics #where_clause { - type InputEvents = (#(#field_ty_events,)*); + type InputEvents = (#(#field_ty_input_events,)*); + type OutputEvents = #output_event_name<'a, #(#type_params_idents),*, #(#const_params_idents),*>; } }.into() } @@ -165,7 +191,7 @@ fn get_type_from_attrs(attrs: &[syn::Attribute], attr_name: &str) -> syn::Result ) } -/// Derives EventLens for an enum which every variant has one field, +/// Derives EventLens & EventMutLens for an enum which every variant has one field, /// each of which the input record implements EventLens for. /// /// The derived implementation is a delegation to the events of each variant, @@ -188,7 +214,7 @@ pub fn event_lens_air_derive(input: TokenStream) -> TokenStream { match &ast.data { Data::Struct(_) | Data::Union(_) => unimplemented!("Only Enums are supported yet"), Data::Enum(e) => { - let fields = e + let (variants, fields): (Vec<_>, Vec<_>) = e .variants .iter() .map(|variant| { @@ -198,11 +224,18 @@ pub fn event_lens_air_derive(input: TokenStream) -> TokenStream { fields.next().is_none(), "Only one field per variant is supported" ); - field + (variant.ident.clone(), field) }) - .collect::>(); + .unzip(); - let field_events = fields.iter().map(|field| { + let output_event_name = format_ident!("{}OutputEvents", name); + let field_output_events_match = variants.into_iter().zip(fields.iter()).map(|(variant, field)| { + let field_ty = &field.ty; + quote! { + #output_event_name::#variant(e) => #sphinx_core_path::air::EventMutLens::<#field_ty>::add_events(self, e) + } + }); + let field_input_events = fields.iter().map(|field| { let field_ty = &field.ty; quote! { #sphinx_core_path::air::EventLens::<#field_ty>::events(self) @@ -210,8 +243,15 @@ pub fn event_lens_air_derive(input: TokenStream) -> TokenStream { }); let res = quote! { impl #impl_generics #sphinx_core_path::air::EventLens<#name #ty_generics> for #rec_ty { - fn events(&self) -> <#name #ty_generics as #sphinx_core_path::air::WithEvents>::InputEvents { - (#(#field_events,)*) + fn events(&self) -> <#name #ty_generics as #sphinx_core_path::air::WithEvents<'_>>::InputEvents { + (#(#field_input_events,)*) + } + } + impl #impl_generics #sphinx_core_path::air::EventMutLens<#name #ty_generics> for #rec_ty { + fn add_events(&mut self, event: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'_>>::OutputEvents) { + match event { + #(#field_output_events_match,)* + } } } }; @@ -305,12 +345,16 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream { let generate_trace_arms = variants.iter().enumerate().map(|(i, (variant_name, field))| { let field_ty = &field.ty; + let output_event_name = format_ident!("{}OutputEvents", name); let idx = syn::Index::from(i); quote! { #name::#variant_name(x) => { - fn f <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::InputEvents, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::InputEvents { evs.#idx } + fn proj <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::InputEvents, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::InputEvents { evs.#idx } + fn inj <'c, #ty_params, #co_params> (evs: <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::OutputEvents, _v: &'c ()) -> <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::OutputEvents { + #output_event_name::#variant_name(evs) + } - <#field_ty as #sphinx_core_path::air::MachineAir>::generate_trace(x, &#sphinx_core_path::air::Proj::new(input, f #turbo_ty), output) + <#field_ty as #sphinx_core_path::air::MachineAir>::generate_trace(x, &#sphinx_core_path::air::Proj::new(input, proj #turbo_ty), &mut #sphinx_core_path::air::Inj::new(output, inj #turbo_ty)) } } }); @@ -319,11 +363,15 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream { let field_ty = &field.ty; let idx = syn::Index::from(i); + let output_event_name = format_ident!("{}OutputEvents", name); quote! { #name::#variant_name(x) => { - fn f <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::InputEvents, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::InputEvents { evs.#idx } + fn proj <'c, #ty_params, #co_params> (evs: <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::InputEvents, _v: &'c ()) -> <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::InputEvents { evs.#idx } + fn inj <'c, #ty_params, #co_params> (evs: <#field_ty as #sphinx_core_path::air::WithEvents<'c>>::OutputEvents, _v: &'c ()) -> <#name #ty_generics as #sphinx_core_path::air::WithEvents<'c>>::OutputEvents { + #output_event_name::#variant_name(evs) + } - <#field_ty as #sphinx_core_path::air::MachineAir>::generate_dependencies(x, &#sphinx_core_path::air::Proj::new(input, f #turbo_ty), output) + <#field_ty as #sphinx_core_path::air::MachineAir>::generate_dependencies(x, &#sphinx_core_path::air::Proj::new(input, proj #turbo_ty), &mut #sphinx_core_path::air::Inj::new(output, inj #turbo_ty)) } } }); @@ -362,21 +410,17 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream { } } - fn generate_trace>( + fn generate_trace, OL: #sphinx_core_path::air::EventMutLens>( &self, - input: &EL, - output: &mut #execution_record_path, - ) -> p3_matrix::dense::RowMajorMatrix { + input: &EL, output: &mut OL) -> p3_matrix::dense::RowMajorMatrix { match self { #(#generate_trace_arms,)* } } - fn generate_dependencies>( + fn generate_dependencies, OL: #sphinx_core_path::air::EventMutLens>( &self, - input: &EL, - output: &mut #execution_record_path, - ) { + input: &EL, output: &mut OL) { match self { #(#generate_dependencies_arms,)* } diff --git a/recursion/core/src/cpu/trace.rs b/recursion/core/src/cpu/trace.rs index d1328afcc..65acb877a 100644 --- a/recursion/core/src/cpu/trace.rs +++ b/recursion/core/src/cpu/trace.rs @@ -3,7 +3,7 @@ use std::borrow::BorrowMut; use p3_field::{extension::BinomiallyExtendable, Field, PrimeField32}; use p3_matrix::{dense::RowMajorMatrix, Matrix}; use sphinx_core::{ - air::{BinomialExtension, EventLens, MachineAir, WithEvents}, + air::{BinomialExtension, EventLens, EventMutLens, MachineAir, WithEvents}, utils::pad_rows_fixed, }; use tracing::instrument; @@ -18,6 +18,7 @@ use super::{CpuChip, CpuCols, CpuEvent, CPU_COL_MAP, NUM_CPU_COLS}; impl<'a, F: Field> WithEvents<'a> for CpuChip { type InputEvents = &'a [CpuEvent]; + type OutputEvents = &'a (); } impl> MachineAir for CpuChip @@ -31,15 +32,19 @@ where "CPU".to_string() } - fn generate_dependencies>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // There are no dependencies, since we do it all in the runtime. This is just a placeholder. } #[instrument(name = "generate cpu trace", level = "debug", skip_all, fields(rows = input.events().len()))] - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _: &mut ExecutionRecord, + _: &mut OR, ) -> RowMajorMatrix { let mut rows = input .events() diff --git a/recursion/core/src/fri_fold/mod.rs b/recursion/core/src/fri_fold/mod.rs index 27f29626d..05b3c32ae 100644 --- a/recursion/core/src/fri_fold/mod.rs +++ b/recursion/core/src/fri_fold/mod.rs @@ -11,7 +11,8 @@ use p3_field::{AbstractField, Field}; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; use sphinx_core::air::{ - BaseAirBuilder, BinomialExtension, EventLens, ExtensionAirBuilder, MachineAir, WithEvents, + BaseAirBuilder, BinomialExtension, EventLens, EventMutLens, ExtensionAirBuilder, MachineAir, + WithEvents, }; use sphinx_core::utils::pad_rows_fixed; use sphinx_derive::AlignedBorrow; @@ -95,6 +96,7 @@ impl BaseAir for FriFoldChip { impl<'a, F: Field, const DEGREE: usize> WithEvents<'a> for FriFoldChip { type InputEvents = &'a [FriFoldEvent]; + type OutputEvents = &'a (); } impl MachineAir for FriFoldChip { @@ -106,15 +108,19 @@ impl MachineAir for FriFoldChip>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } #[instrument(name = "generate fri fold trace", level = "debug", skip_all, fields(rows = input.events().len()))] - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _: &mut ExecutionRecord, + _: &mut OR, ) -> RowMajorMatrix { let mut rows = input .events() diff --git a/recursion/core/src/memory/air.rs b/recursion/core/src/memory/air.rs index 403a9c7a8..17643ea53 100644 --- a/recursion/core/src/memory/air.rs +++ b/recursion/core/src/memory/air.rs @@ -8,7 +8,7 @@ use p3_air::{Air, BaseAir}; use p3_field::{Field, PrimeField32}; use p3_matrix::{dense::RowMajorMatrix, Matrix}; use sphinx_core::{ - air::{AirInteraction, EventLens, MachineAir, MemoryAirBuilder, WithEvents}, + air::{AirInteraction, EventLens, EventMutLens, MachineAir, MemoryAirBuilder, WithEvents}, lookup::InteractionKind, utils::pad_rows_fixed, }; @@ -37,6 +37,7 @@ impl<'a, F: Field> WithEvents<'a> for MemoryGlobalChip { // last memory event &'a [(F, F, Block)], ); + type OutputEvents = &'a (); } impl MachineAir for MemoryGlobalChip { @@ -47,15 +48,19 @@ impl MachineAir for MemoryGlobalChip { "MemoryGlobalChip".to_string() } - fn generate_dependencies>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } #[instrument(name = "generate memory trace", level = "debug", skip_all, fields(first_rows = input.events().0.len(), last_rows = input.events().1.len()))] - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OR, ) -> RowMajorMatrix { let mut rows = Vec::new(); let (first_memory_events, last_memory_events) = input.events(); diff --git a/recursion/core/src/multi/mod.rs b/recursion/core/src/multi/mod.rs index c474e958d..d05e7b0b2 100644 --- a/recursion/core/src/multi/mod.rs +++ b/recursion/core/src/multi/mod.rs @@ -6,7 +6,9 @@ use p3_air::{Air, AirBuilder, BaseAir}; use p3_field::{Field, PrimeField32}; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; -use sphinx_core::air::{BaseAirBuilder, EventLens, MachineAir, Proj, WithEvents}; +use sphinx_core::air::{ + BaseAirBuilder, EventLens, EventMutLens, Inj, MachineAir, Proj, WithEvents, +}; use sphinx_core::utils::pad_rows_fixed; use sphinx_derive::AlignedBorrow; @@ -55,6 +57,8 @@ impl<'a, F: Field, const DEGREE: usize> WithEvents<'a> for MultiChip as WithEvents<'a>>::InputEvents, as WithEvents<'a>>::InputEvents, ); + // here we use the fact that the fri_fold and poseidon2 chips do not emit output events + type OutputEvents = &'a (); } impl MachineAir for MultiChip { @@ -66,14 +70,18 @@ impl MachineAir for MultiChip>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - output: &mut ExecutionRecord, + output: &mut OR, ) -> RowMajorMatrix { let fri_fold_chip = FriFoldChip::::default(); let poseidon2 = Poseidon2Chip::default(); @@ -92,10 +100,28 @@ impl MachineAir for MultiChip), output); - let mut poseidon2_trace = - poseidon2.generate_trace(&Proj::new(input, to_poseidon::), output); + // here we reuse the fact that output events for those recursive chips are empty + fn from_fri<'c, F: PrimeField32, const DEGREE: usize>( + _evs: as WithEvents<'c>>::OutputEvents, + _v: &'c (), + ) -> as WithEvents<'c>>::OutputEvents { + &() + } + fn from_poseidon<'c, F: PrimeField32, const DEGREE: usize>( + _evs: as WithEvents<'c>>::OutputEvents, + _v: &'c (), + ) -> as WithEvents<'c>>::OutputEvents { + &() + } + + let fri_fold_trace = fri_fold_chip.generate_trace( + &Proj::new(input, to_fri::), + &mut Inj::new(output, from_fri::), + ); + let mut poseidon2_trace = poseidon2.generate_trace( + &Proj::new(input, to_poseidon::), + &mut Inj::new(output, from_poseidon::), + ); let mut rows = fri_fold_trace .clone() diff --git a/recursion/core/src/poseidon2/trace.rs b/recursion/core/src/poseidon2/trace.rs index 4d7a6417a..4f20b2d26 100644 --- a/recursion/core/src/poseidon2/trace.rs +++ b/recursion/core/src/poseidon2/trace.rs @@ -3,7 +3,7 @@ use std::borrow::BorrowMut; use p3_field::{Field, PrimeField32}; use p3_matrix::dense::RowMajorMatrix; use sphinx_core::{ - air::{EventLens, MachineAir, WithEvents}, + air::{EventLens, EventMutLens, MachineAir, WithEvents}, utils::pad_rows_fixed, }; use sphinx_primitives::RC_16_30_U32; @@ -21,6 +21,7 @@ use super::{ impl<'a, F: Field> WithEvents<'a> for Poseidon2Chip { type InputEvents = &'a [Poseidon2Event]; + type OutputEvents = &'a (); } impl MachineAir for Poseidon2Chip { @@ -32,15 +33,19 @@ impl MachineAir for Poseidon2Chip { "Poseidon2".to_string() } - fn generate_dependencies>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } #[instrument(name = "generate poseidon2 trace", level = "debug", skip_all, fields(rows = input.events().len()))] - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _: &mut ExecutionRecord, + _: &mut OR, ) -> RowMajorMatrix { let mut rows = Vec::new(); diff --git a/recursion/core/src/poseidon2_wide/external.rs b/recursion/core/src/poseidon2_wide/external.rs index 1e3cd9da2..cac51fd35 100644 --- a/recursion/core/src/poseidon2_wide/external.rs +++ b/recursion/core/src/poseidon2_wide/external.rs @@ -9,7 +9,7 @@ use p3_air::{Air, BaseAir}; use p3_field::{AbstractField, Field, PrimeField32}; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; -use sphinx_core::air::{BaseAirBuilder, EventLens, MachineAir, WithEvents}; +use sphinx_core::air::{BaseAirBuilder, EventLens, EventMutLens, MachineAir, WithEvents}; use sphinx_core::utils::pad_rows_fixed; use sphinx_primitives::RC_16_30_U32; use std::borrow::BorrowMut; @@ -40,6 +40,7 @@ pub struct Poseidon2WideChip { impl<'a, F: Field, const DEGREE: usize> WithEvents<'a> for Poseidon2WideChip { type InputEvents = &'a [Poseidon2Event]; + type OutputEvents = &'a (); } impl MachineAir for Poseidon2WideChip { @@ -51,15 +52,19 @@ impl MachineAir for Poseidon2WideChip>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } #[instrument(name = "generate poseidon2 wide trace", level = "debug", skip_all, fields(rows = input.events().len()))] - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _: &mut ExecutionRecord, + _: &mut OR, ) -> RowMajorMatrix { let mut rows = Vec::new(); diff --git a/recursion/core/src/program/mod.rs b/recursion/core/src/program/mod.rs index fda22f327..8994e101a 100644 --- a/recursion/core/src/program/mod.rs +++ b/recursion/core/src/program/mod.rs @@ -6,7 +6,7 @@ use p3_air::{Air, BaseAir, PairBuilder}; use p3_field::{Field, PrimeField32}; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::Matrix; -use sphinx_core::air::{EventLens, MachineAir, WithEvents}; +use sphinx_core::air::{EventLens, EventMutLens, MachineAir, WithEvents}; use sphinx_core::utils::pad_rows_fixed; use std::collections::HashMap; use std::marker::PhantomData; @@ -55,6 +55,7 @@ impl<'a, F: Field> WithEvents<'a> for ProgramChip { // cpu_events &'a [CpuEvent], ); + type OutputEvents = &'a (); } impl MachineAir for ProgramChip { @@ -103,15 +104,19 @@ impl MachineAir for ProgramChip { )) } - fn generate_dependencies>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } #[instrument(name = "generate program trace", level = "debug", skip_all, fields(rows = input.events().0.len()))] - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OR, ) -> RowMajorMatrix { // Collect the number of times each instruction is called from the cpu events. // Store it as a map of PC -> count. diff --git a/recursion/core/src/range_check/trace.rs b/recursion/core/src/range_check/trace.rs index 869c84fef..0ef92b65e 100644 --- a/recursion/core/src/range_check/trace.rs +++ b/recursion/core/src/range_check/trace.rs @@ -2,7 +2,7 @@ use std::{borrow::BorrowMut, collections::BTreeMap}; use p3_field::{Field, PrimeField32}; use p3_matrix::dense::RowMajorMatrix; -use sphinx_core::air::{EventLens, MachineAir, WithEvents}; +use sphinx_core::air::{EventLens, EventMutLens, MachineAir, WithEvents}; use super::{ columns::{RangeCheckMultCols, NUM_RANGE_CHECK_MULT_COLS, NUM_RANGE_CHECK_PREPROCESSED_COLS}, @@ -14,6 +14,7 @@ pub const NUM_ROWS: usize = 1 << 16; impl<'a, F: Field> WithEvents<'a> for RangeCheckChip { type InputEvents = &'a BTreeMap; + type OutputEvents = &'a (); } impl MachineAir for RangeCheckChip { @@ -34,14 +35,18 @@ impl MachineAir for RangeCheckChip { Some(trace) } - fn generate_dependencies>(&self, _: &EL, _: &mut Self::Record) { + fn generate_dependencies, OR: EventMutLens>( + &self, + _: &EL, + _: &mut OR, + ) { // This is a no-op. } - fn generate_trace>( + fn generate_trace, OR: EventMutLens>( &self, input: &EL, - _output: &mut ExecutionRecord, + _output: &mut OR, ) -> RowMajorMatrix { let (_, event_map) = Self::trace_and_map(); diff --git a/recursion/core/src/runtime/record.rs b/recursion/core/src/runtime/record.rs index ccdebba48..516e43546 100644 --- a/recursion/core/src/runtime/record.rs +++ b/recursion/core/src/runtime/record.rs @@ -2,7 +2,7 @@ use std::collections::{BTreeMap, HashMap}; use std::sync::Arc; use p3_field::{AbstractField, PrimeField32}; -use sphinx_core::air::EventLens; +use sphinx_core::air::{EventLens, EventMutLens, WithEvents}; use sphinx_core::stark::{Indexed, MachineRecord, PROOF_MAX_NUM_PVS}; use super::RecursionProgram; @@ -101,7 +101,7 @@ impl MachineRecord for ExecutionRecord { } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.cpu_events } } @@ -109,13 +109,13 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.fri_fold_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.poseidon2_events } } @@ -123,34 +123,42 @@ impl EventLens> for ExecutionRecord { impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.poseidon2_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { (&self.first_memory_record, &self.last_memory_record) } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { (&self.program.instructions, &self.cpu_events) } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.range_check_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as sphinx_core::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { ( >>::events(self), >>::events(self), ) } } + +// For a recursive machine chip, there are no dependencies since we manage them in the runtime. +// The output events are always empty. +impl WithEvents<'a, OutputEvents = &'a ()>> EventMutLens + for ExecutionRecord +{ + fn add_events(&mut self, _events: >::OutputEvents) {} +} From 6300ea16207b8f60575a308c16494db3ce9fdb53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Tue, 11 Jun 2024 15:38:51 -0400 Subject: [PATCH 3/3] chore: use a blanket impl to simplify ExecutionRecord boilerplate --- .../field/extensions/quadratic/mod.rs | 13 +- .../field/extensions/quadratic/sqrt.rs | 13 +- core/src/operations/field/field_den.rs | 12 - .../operations/field/field_inner_product.rs | 12 - core/src/operations/field/field_op.rs | 13 +- core/src/operations/field/field_sqrt.rs | 13 +- core/src/runtime/record.rs | 398 ++---------------- 7 files changed, 50 insertions(+), 424 deletions(-) diff --git a/core/src/operations/field/extensions/quadratic/mod.rs b/core/src/operations/field/extensions/quadratic/mod.rs index 546359274..0130a5c9e 100644 --- a/core/src/operations/field/extensions/quadratic/mod.rs +++ b/core/src/operations/field/extensions/quadratic/mod.rs @@ -380,7 +380,7 @@ mod tests { use super::{QuadFieldOpCols, QuadFieldOperation}; use crate::air::MachineAir; - use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::{FieldParameters, Limbs}; use crate::runtime::{ExecutionRecord, Program}; @@ -423,17 +423,6 @@ mod tests { } } - impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } - } - impl MachineAir for QuadFieldOpChip

{ type Record = ExecutionRecord; diff --git a/core/src/operations/field/extensions/quadratic/sqrt.rs b/core/src/operations/field/extensions/quadratic/sqrt.rs index 528cb6560..d678d8273 100644 --- a/core/src/operations/field/extensions/quadratic/sqrt.rs +++ b/core/src/operations/field/extensions/quadratic/sqrt.rs @@ -112,7 +112,7 @@ mod tests { use crate::air::MachineAir; use crate::air::{EventLens, EventMutLens, WordAirBuilder}; - use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::{FieldParameters, Limbs}; use crate::runtime::ExecutionRecord; @@ -162,17 +162,6 @@ mod tests { } } - impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } - } - impl MachineAir for QuadSqrtChip

{ type Record = ExecutionRecord; diff --git a/core/src/operations/field/field_den.rs b/core/src/operations/field/field_den.rs index a112429d0..b3c4e871a 100644 --- a/core/src/operations/field/field_den.rs +++ b/core/src/operations/field/field_den.rs @@ -165,7 +165,6 @@ mod tests { use crate::{ air::MachineAir, - bytes::event::ByteRecord, bytes::ByteLookupEvent, utils::ec::weierstrass::{bls12_381::Bls12381BaseField, secp256k1::Secp256k1BaseField}, }; @@ -210,17 +209,6 @@ mod tests { } } - impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } - } - impl MachineAir for FieldDenChip

{ type Record = ExecutionRecord; diff --git a/core/src/operations/field/field_inner_product.rs b/core/src/operations/field/field_inner_product.rs index e2e9ebecb..f471c93a6 100644 --- a/core/src/operations/field/field_inner_product.rs +++ b/core/src/operations/field/field_inner_product.rs @@ -155,7 +155,6 @@ mod tests { use crate::air::{EventLens, EventMutLens, WordAirBuilder}; use crate::{ air::MachineAir, - bytes::event::ByteRecord, bytes::ByteLookupEvent, utils::ec::weierstrass::{bls12_381::Bls12381BaseField, secp256k1::Secp256k1BaseField}, }; @@ -199,17 +198,6 @@ mod tests { } } - impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } - } - impl MachineAir for FieldIpChip

{ type Record = ExecutionRecord; diff --git a/core/src/operations/field/field_op.rs b/core/src/operations/field/field_op.rs index 77f74df57..c69a8b5b1 100644 --- a/core/src/operations/field/field_op.rs +++ b/core/src/operations/field/field_op.rs @@ -244,7 +244,7 @@ mod tests { use crate::{air::MachineAir, utils::ec::weierstrass::bls12_381::Bls12381BaseField}; use crate::air::{EventLens, EventMutLens, WordAirBuilder}; - use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::FieldParameters; use crate::runtime::ExecutionRecord; @@ -290,17 +290,6 @@ mod tests { } } - impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } - } - impl MachineAir for FieldOpChip

{ type Record = ExecutionRecord; diff --git a/core/src/operations/field/field_sqrt.rs b/core/src/operations/field/field_sqrt.rs index 284617cfc..dfd9912fe 100644 --- a/core/src/operations/field/field_sqrt.rs +++ b/core/src/operations/field/field_sqrt.rs @@ -121,7 +121,7 @@ mod tests { use crate::air::WordAirBuilder; use crate::air::{EventLens, EventMutLens, MachineAir}; - use crate::bytes::event::ByteRecord; + use crate::bytes::ByteLookupEvent; use crate::operations::field::params::{FieldParameters, DEFAULT_NUM_LIMBS_T}; use crate::runtime::ExecutionRecord; @@ -163,17 +163,6 @@ mod tests { } } - impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } - } - impl> MachineAir for EdSqrtChip

{ diff --git a/core/src/runtime/record.rs b/core/src/runtime/record.rs index 1aa12b6fb..49955e156 100644 --- a/core/src/runtime/record.rs +++ b/core/src/runtime/record.rs @@ -9,7 +9,6 @@ use p3_field::{AbstractField, Field}; use serde::{Deserialize, Serialize}; use super::{program::Program, Opcode}; -use crate::cpu::CpuEvent; use crate::runtime::MemoryInitializeFinalizeEvent; use crate::runtime::MemoryRecordEnum; use crate::stark::MachineRecord; @@ -32,6 +31,7 @@ use crate::{ }, utils::ec::weierstrass::bls12_381::Bls12381BaseField, }; +use crate::{air::WithEvents, cpu::CpuEvent}; use crate::{ air::{EventLens, EventMutLens}, alu::AluEvent, @@ -146,253 +146,211 @@ pub struct ExecutionRecord { // Event lenses connect the record to the events relative to a particular chip impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { (&self.add_events, &self.sub_events) } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.bitwise_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.divrem_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.lt_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.mul_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.shift_left_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.shift_right_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.byte_lookups } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.cpu_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { (&self.memory_initialize_events, &self.memory_finalize_events) } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.program.memory_image } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { (&self.cpu_events, &self.program) } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.sha_extend_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.sha_compress_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.blake3_compress_inner_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.keccak_permute_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.bls12381_g1_decompress_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.secp256k1_decompress_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.bls12381_g2_add_events } } impl EventLens for ExecutionRecord { - fn events(&self) -> >::InputEvents { + fn events(&self) -> >::InputEvents { &self.bls12381_g2_double_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_fp_add_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_fp_sub_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_fp_mul_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_fp2_add_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_fp2_sub_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_fp2_mul_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.secp256k1_add_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_g1_add_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bn254_add_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.secp256k1_double_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bls12381_g1_double_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.bn254_double_events } } impl EventLens> for ExecutionRecord { - fn events(&self) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.ed_add_events } } impl EventLens> for ExecutionRecord { - fn events( - &self, - ) -> as crate::air::WithEvents<'_>>::InputEvents { + fn events(&self) -> as WithEvents<'_>>::InputEvents { &self.ed_decompress_events } } -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { + fn add_events(&mut self, events: >::OutputEvents) { for event in events { match event { DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(**e), @@ -403,44 +361,12 @@ impl EventMutLens for ExecutionRecord { } } -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - impl EventMutLens> for ExecutionRecord { - fn add_events(&mut self, _events: as crate::air::WithEvents<'_>>::OutputEvents) {} + fn add_events(&mut self, _events: as WithEvents<'_>>::OutputEvents) {} } impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { + fn add_events(&mut self, events: >::OutputEvents) { for event in events { match event { CpuOutputEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(**e), @@ -466,254 +392,22 @@ impl EventMutLens for ExecutionRecord { } impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, _events: >::OutputEvents) {} + fn add_events(&mut self, _events: >::OutputEvents) {} } impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - _events: >::OutputEvents, - ) { - } + fn add_events(&mut self, _events: >::OutputEvents) {} } impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, _events: >::OutputEvents) {} -} - -impl EventMutLens for ExecutionRecord { - fn add_events(&mut self, events: >::OutputEvents) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens for ExecutionRecord { - fn add_events( - &mut self, - events: >::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } -} - -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { - for event in events { - self.add_byte_lookup_event(*event); - } - } + fn add_events(&mut self, _events: >::OutputEvents) {} } -impl EventMutLens> for ExecutionRecord { - fn add_events( - &mut self, - events: as crate::air::WithEvents<'_>>::OutputEvents, - ) { +// For a straightforward addition to the byte record table, we can systematize: +impl WithEvents<'a, OutputEvents = &'a [ByteLookupEvent]>> EventMutLens + for ExecutionRecord +{ + fn add_events(&mut self, events: >::OutputEvents) { for event in events { self.add_byte_lookup_event(*event); }