Skip to content

Commit

Permalink
derive lookup_data
Browse files Browse the repository at this point in the history
  • Loading branch information
ohad-starkware committed Dec 17, 2024
1 parent 3833267 commit dccdc01
Show file tree
Hide file tree
Showing 26 changed files with 72 additions and 431 deletions.
23 changes: 22 additions & 1 deletion stwo_cairo_prover/crates/air_structs_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ pub fn derive_sub_component_inputs(input: proc_macro::TokenStream) -> proc_macro
proc_macro::TokenStream::from(expanded)
}

#[proc_macro_derive(LookupData)]
pub fn derive_lookup_data(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
assert_is_struct(&input);
let name = &input.ident;
let vec_array_fields = extract_vec_array_fields(&input);

// TODO(Ohad): deprecate with_capacity.
let with_capacity_method = generate_with_capacity_method(&vec_array_fields);
let uninitialized_method = generate_uninitialized_method(&vec_array_fields);

let expanded = quote! {
impl #name {
#with_capacity_method
#uninitialized_method
}
};

proc_macro::TokenStream::from(expanded)
}

#[derive(Clone)]
struct VecArrayField {
name: syn::Ident,
Expand All @@ -34,7 +55,7 @@ struct VecArrayField {

fn assert_is_struct(input: &DeriveInput) {
if !matches!(input.data, Data::Struct(_)) {
panic!("Derive(SubComponentInputs) can only be applied to structs.");
panic!("Derive(SubComponentInputs/LookupData) can only be applied to structs.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -301,23 +301,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 1],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 1],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [Vec::with_capacity(capacity)],
memoryidtobig: [Vec::with_capacity(capacity)],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -301,23 +301,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 1],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 1],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [Vec::with_capacity(capacity)],
memoryidtobig: [Vec::with_capacity(capacity)],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -285,23 +285,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 1],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 1],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [Vec::with_capacity(capacity)],
memoryidtobig: [Vec::with_capacity(capacity)],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -658,31 +658,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 3],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 3],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
memoryidtobig: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -620,31 +620,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 3],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 3],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
memoryidtobig: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -532,31 +532,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 3],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 3],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
memoryidtobig: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -494,31 +494,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 3],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 3],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
memoryidtobig: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -316,21 +316,12 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 2],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -279,21 +279,12 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 2],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use air_structs_derive::SubComponentInputs;
use air_structs_derive::{LookupData, SubComponentInputs};
use itertools::{chain, zip_eq, Itertools};
use num_traits::{One, Zero};
use prover_types::cpu::*;
Expand Down Expand Up @@ -393,27 +393,13 @@ pub fn write_trace_simd(
(trace, sub_components_inputs, lookup_data)
}

#[derive(LookupData)]
pub struct LookupData {
pub memoryaddresstoid: [Vec<[PackedM31; 2]>; 3],
pub memoryidtobig: [Vec<[PackedM31; 29]>; 1],
pub opcodes: [Vec<[PackedM31; 3]>; 2],
pub verifyinstruction: [Vec<[PackedM31; 19]>; 1],
}
impl LookupData {
#[allow(unused_variables)]
fn with_capacity(capacity: usize) -> Self {
Self {
memoryaddresstoid: [
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
Vec::with_capacity(capacity),
],
memoryidtobig: [Vec::with_capacity(capacity)],
opcodes: [Vec::with_capacity(capacity), Vec::with_capacity(capacity)],
verifyinstruction: [Vec::with_capacity(capacity)],
}
}
}

pub struct InteractionClaimGenerator {
pub n_calls: usize,
Expand Down
Loading

0 comments on commit dccdc01

Please sign in to comment.