From 84250bd91c038d50da4f517a9efd1c911013693b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 14:45:00 +0800 Subject: [PATCH 01/84] Revert "Remove counter-productive 'debug' option (#1547)" This reverts commit a271428e0e6a05099dbdf1ec4ddfbc9b66de4a92. --- circuits/src/expr.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 50f790f75..811f89a60 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -175,7 +175,15 @@ pub fn build_packed( .map(|c| c.map(|constraint| evaluator.eval(constraint))) .collect::>(); + let mozak_stark_debug = std::env::var("MOZAK_STARK_DEBUG").is_ok(); for c in evaluated { + if mozak_stark_debug && !c.term.is_zeros() { + log::error!( + "ConstraintConsumer - DEBUG trace (non-zero-constraint): {}", + c.location + ); + } + (match c.constraint_type { ConstraintType::FirstRow => ConstraintConsumer::constraint_first_row, ConstraintType::Always => ConstraintConsumer::constraint, From b1a937e69c34c271b7dab00057f455aa8940916a Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:35:09 +0800 Subject: [PATCH 02/84] Introduce new trait --- circuits/src/bitshift/stark.rs | 89 ++++++++++++++++++---------------- circuits/src/expr.rs | 8 ++- circuits/src/ops/add/stark.rs | 35 +++++++------ 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 733abd1ef..7d0804dad 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::BitshiftView; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; /// Bitshift Trace Constraints @@ -30,45 +30,50 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values.executed; - let nv = vars.next_values.executed; - let mut constraints = ConstraintBuilder::default(); - - // Constraints on shift amount - // They ensure: - // 1. Shift amount increases with each row by 0 or 1. - // (We allow increases of 0 in order to allow the table to add - // multiple same value rows. This is needed when we have multiple - // `SHL` or `SHR` operations with the same shift amount.) - // 2. We have shift amounts starting from 0 to max possible value of 31. - // (This is due to RISC-V max shift amount being 31.) - - let diff = nv.amount - lv.amount; - // Check: initial amount value is set to 0 - constraints.first_row(lv.amount); - // Check: amount value is increased by 1 or kept unchanged - constraints.transition(diff * (diff - 1)); - // Check: last amount value is set to 31 - constraints.last_row(lv.amount - 31); - - // Constraints on multiplier - // They ensure: - // 1. Shift multiplier is multiplied by 2 only if amount increases. - // 2. We have shift multiplier from 1 to max possible value of 2^31. - - // Check: initial multiplier value is set to 1 = 2^0 - constraints.first_row(lv.multiplier - 1); - // Check: multiplier value is doubled if amount is increased - constraints.transition(nv.multiplier - (1 + diff) * lv.multiplier); - // Check: last multiplier value is set to 2^31 - // (Note that based on the previous constraint, this is already - // satisfied if the last amount value is 31. We leave it for readability.) - constraints.last_row(lv.multiplier - (1 << 31)); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, T, BitshiftView>, NoColumns> + for BitshiftStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values.executed; + let nv = vars.next_values.executed; + let mut constraints = ConstraintBuilder::default(); + + // Constraints on shift amount + // They ensure: + // 1. Shift amount increases with each row by 0 or 1. + // (We allow increases of 0 in order to allow the table to add + // multiple same value rows. This is needed when we have multiple + // `SHL` or `SHR` operations with the same shift amount.) + // 2. We have shift amounts starting from 0 to max possible value of 31. + // (This is due to RISC-V max shift amount being 31.) + + let diff = nv.amount - lv.amount; + // Check: initial amount value is set to 0 + constraints.first_row(lv.amount); + // Check: amount value is increased by 1 or kept unchanged + constraints.transition(diff * (diff - 1)); + // Check: last amount value is set to 31 + constraints.last_row(lv.amount - 31); + + // Constraints on multiplier + // They ensure: + // 1. Shift multiplier is multiplied by 2 only if amount increases. + // 2. We have shift multiplier from 1 to max possible value of 2^31. + + // Check: initial multiplier value is set to 1 = 2^0 + constraints.first_row(lv.multiplier - 1); + // Check: multiplier value is doubled if amount is increased + constraints.transition(nv.multiplier - (1 + diff) * lv.multiplier); + // Check: last multiplier value is set to 2^31 + // (Note that based on the previous constraint, this is already + // satisfied if the last amount value is 31. We leave it for readability.) + constraints.last_row(lv.multiplier - (1 << 31)); + + constraints + } } impl, const D: usize> Stark for BitshiftStark { @@ -88,7 +93,7 @@ impl, const D: usize> Stark for BitshiftStark FE: FieldExtension, P: PackedField, { let expr_builder = ExprBuilder::default(); - let constraints = generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -101,7 +106,7 @@ impl, const D: usize> Stark for BitshiftStark constraint_consumer: &mut RecursiveConstraintConsumer, ) { let expr_builder = ExprBuilder::default(); - let constraints = generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } } diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 811f89a60..1e5cf2224 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,7 +1,7 @@ use std::panic::Location; pub use expr::PureEvaluator; -use expr::{BinOp, Cached, Evaluator, Expr, UnaOp}; +use expr::{BinOp, Cached, Evaluator, Expr, StarkFrameTyped, UnaOp}; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; @@ -192,3 +192,9 @@ pub fn build_packed( })(yield_constr, c.term); } } + +pub trait GenerateConstraints<'a, T, U, View, PublicInputs> { + fn generate_constraints( + vars: &StarkFrameTyped, + ) -> ConstraintBuilder>; +} diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index f4a19e966..2dcb651d3 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -13,7 +13,8 @@ use starky::stark::Stark; use super::columns::Add; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] @@ -28,21 +29,25 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy, U>( - vars: &StarkFrameTyped>, Vec>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, Add>, NoColumns> for AddStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); - let added = lv.op1_value + lv.op2_value + lv.inst.imm_value; - let wrapped = added - (1 << 32); + let added = lv.op1_value + lv.op2_value + lv.inst.imm_value; + let wrapped = added - (1 << 32); - // Check: the resulting sum is wrapped if necessary. - // As the result is range checked, this make the choice deterministic, - // even for a malicious prover. - constraints.always((lv.dst_value - added) * (lv.dst_value - wrapped)); + // Check: the resulting sum is wrapped if necessary. + // As the result is range checked, this make the choice deterministic, + // even for a malicious prover. + constraints.always((lv.dst_value - added) * (lv.dst_value - wrapped) + 1); - constraints + constraints + } } impl, const D: usize> Stark for AddStark { @@ -63,7 +68,7 @@ impl, const D: usize> Stark for AddStark, { let expr_builder = ExprBuilder::default(); let vars = expr_builder.to_typed_starkframe(vars); - let constraints = generate_constraints(&vars); + let constraints = Self::generate_constraints(&vars); build_packed(constraints, constraint_consumer); } @@ -74,7 +79,7 @@ impl, const D: usize> Stark for AddStark, ) { let expr_builder = ExprBuilder::default(); - let constraints = generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } From 0f6e437f99e5051790b07e862fb16332622a7911 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:41:01 +0800 Subject: [PATCH 03/84] Lifetime shenanigans --- circuits/src/bitshift/stark.rs | 8 ++-- circuits/src/cpu/stark.rs | 69 ++++++++++++++++++---------------- circuits/src/expr.rs | 2 +- circuits/src/ops/add/stark.rs | 5 ++- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 7d0804dad..33e77f0a1 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -35,7 +35,7 @@ impl<'a, F, T: Copy, U, const D: usize> for BitshiftStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &'a StarkFrameTyped>, NoColumns>, ) -> ConstraintBuilder> { let lv = vars.local_values.executed; let nv = vars.next_values.executed; @@ -93,7 +93,8 @@ impl, const D: usize> Stark for BitshiftStark FE: FieldExtension, P: PackedField, { let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let vars = expr_builder.to_typed_starkframe(vars); + let constraints = Self::generate_constraints(&vars); build_packed(constraints, constraint_consumer); } @@ -106,7 +107,8 @@ impl, const D: usize> Stark for BitshiftStark constraint_consumer: &mut RecursiveConstraintConsumer, ) { let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let vars = expr_builder.to_typed_starkframe(vars); + let constraints = Self::generate_constraints(&vars); build_ext(constraints, circuit_builder, constraint_consumer); } } diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 50595fca0..4e3712190 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -15,7 +15,8 @@ use super::columns::{CpuState, OpSelectors}; use super::{bitwise, branches, div, ecall, jalr, memory, mul, signed_comparison, sub}; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::cpu::shift; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::unstark::NoColumns; /// A Gadget for CPU Instructions /// @@ -77,35 +78,39 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy, U>( - vars: &'a StarkFrameTyped>, Vec>, -) -> ConstraintBuilder> { - let lv = &vars.local_values; - let mut constraints = ConstraintBuilder::default(); - - pc_ticks_up(lv, &mut constraints); - - binary_selectors(&lv.inst.ops, &mut constraints); - - // Registers - populate_op2_value(lv, &mut constraints); - - // ADD is now handled by its own table. - constraints.always(lv.inst.ops.add); - sub::constraints(lv, &mut constraints); - bitwise::constraints(lv, &mut constraints); - branches::comparison_constraints(lv, &mut constraints); - branches::constraints(lv, &mut constraints); - memory::constraints(lv, &mut constraints); - signed_comparison::signed_constraints(lv, &mut constraints); - signed_comparison::slt_constraints(lv, &mut constraints); - shift::constraints(lv, &mut constraints); - div::constraints(lv, &mut constraints); - mul::constraints(lv, &mut constraints); - jalr::constraints(lv, &mut constraints); - ecall::constraints(lv, &mut constraints); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, T, CpuState>, NoColumns> for CpuStark +{ + fn generate_constraints( + vars: &'a StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = &vars.local_values; + let mut constraints = ConstraintBuilder::default(); + + pc_ticks_up(lv, &mut constraints); + + binary_selectors(&lv.inst.ops, &mut constraints); + + // Registers + populate_op2_value(lv, &mut constraints); + + // ADD is now handled by its own table. + constraints.always(lv.inst.ops.add); + sub::constraints(lv, &mut constraints); + bitwise::constraints(lv, &mut constraints); + branches::comparison_constraints(lv, &mut constraints); + branches::constraints(lv, &mut constraints); + memory::constraints(lv, &mut constraints); + signed_comparison::signed_constraints(lv, &mut constraints); + signed_comparison::slt_constraints(lv, &mut constraints); + shift::constraints(lv, &mut constraints); + div::constraints(lv, &mut constraints); + mul::constraints(lv, &mut constraints); + jalr::constraints(lv, &mut constraints); + ecall::constraints(lv, &mut constraints); + + constraints + } } impl, const D: usize> Stark for CpuStark { @@ -126,7 +131,7 @@ impl, const D: usize> Stark for CpuStark, { let expr_builder = ExprBuilder::default(); let vars = expr_builder.to_typed_starkframe(vars); - let constraints = generate_constraints(&vars); + let constraints = Self::generate_constraints(&vars); build_packed(constraints, constraint_consumer); } @@ -140,7 +145,7 @@ impl, const D: usize> Stark for CpuStark( pub trait GenerateConstraints<'a, T, U, View, PublicInputs> { fn generate_constraints( - vars: &StarkFrameTyped, + vars: &'a StarkFrameTyped, ) -> ConstraintBuilder>; } diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 2dcb651d3..16d27c2ac 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -44,7 +44,7 @@ impl<'a, F, T: Copy, U, const D: usize> // Check: the resulting sum is wrapped if necessary. // As the result is range checked, this make the choice deterministic, // even for a malicious prover. - constraints.always((lv.dst_value - added) * (lv.dst_value - wrapped) + 1); + constraints.always((lv.dst_value - added) * (lv.dst_value - wrapped)); constraints } @@ -79,7 +79,8 @@ impl, const D: usize> Stark for AddStark, ) { let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let vars = expr_builder.to_typed_starkframe(vars); + let constraints = Self::generate_constraints(&vars); build_ext(constraints, circuit_builder, constraint_consumer); } From e453c64499d8e5378e2528d298f3156545ec9802 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:46:39 +0800 Subject: [PATCH 04/84] Introduce more --- circuits/src/cpu_skeleton/stark.rs | 84 ++++++++++++---------- circuits/src/memory/stark.rs | 112 +++++++++++++++-------------- 2 files changed, 104 insertions(+), 92 deletions(-) diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index f172484a8..54297def1 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::CpuSkeleton; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::stark::mozak_stark::PublicInputs; #[derive(Clone, Copy, Default, StarkNameDisplay)] @@ -30,42 +30,47 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, PublicInputs>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let nv = vars.next_values; - let public_inputs = vars.public_inputs; - let mut constraints = ConstraintBuilder::default(); - - constraints.first_row(lv.pc - public_inputs.entry_point); - // Clock starts at 2. This is to differentiate - // execution clocks (2 and above) from - // clk values `0` and `1` which are reserved for - // elf initialisation and zero initialisation respectively. - constraints.first_row(lv.clk - 2); - - let clock_diff = nv.clk - lv.clk; - constraints.transition(clock_diff.is_binary()); - - // clock only counts up when we are still running. - constraints.transition(clock_diff - lv.is_running); - - // We start in running state. - constraints.first_row(lv.is_running - 1); - - // We may transition to a non-running state. - constraints.transition(nv.is_running * (nv.is_running - lv.is_running)); - - // We end in a non-running state. - constraints.last_row(lv.is_running); - - // NOTE: in our old CPU table we had constraints that made sure nothing - // changes anymore, once we are halted. We don't need those - // anymore: the only thing that can change are memory or registers. And - // our CTLs make sure, that after we are halted, no more memory - // or register changes are allowed. - constraints +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, T, CpuSkeleton>, PublicInputs>> + for CpuSkeletonStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, PublicInputs>>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let nv = vars.next_values; + let public_inputs = vars.public_inputs; + let mut constraints = ConstraintBuilder::default(); + + constraints.first_row(lv.pc - public_inputs.entry_point); + // Clock starts at 2. This is to differentiate + // execution clocks (2 and above) from + // clk values `0` and `1` which are reserved for + // elf initialisation and zero initialisation respectively. + constraints.first_row(lv.clk - 2); + + let clock_diff = nv.clk - lv.clk; + constraints.transition(clock_diff.is_binary()); + + // clock only counts up when we are still running. + constraints.transition(clock_diff - lv.is_running); + + // We start in running state. + constraints.first_row(lv.is_running - 1); + + // We may transition to a non-running state. + constraints.transition(nv.is_running * (nv.is_running - lv.is_running)); + + // We end in a non-running state. + constraints.last_row(lv.is_running); + + // NOTE: in our old CPU table we had constraints that made sure nothing + // changes anymore, once we are halted. We don't need those + // anymore: the only thing that can change are memory or registers. And + // our CTLs make sure, that after we are halted, no more memory + // or register changes are allowed. + constraints + } } impl, const D: usize> Stark for CpuSkeletonStark { @@ -86,7 +91,7 @@ impl, const D: usize> Stark for CpuSkeletonSt P: PackedField, { let expr_builder = ExprBuilder::default(); let vars = expr_builder.to_typed_starkframe(vars); - let constraints = generate_constraints(&vars); + let constraints = Self::generate_constraints(&vars); build_packed(constraints, consumer); } @@ -97,7 +102,8 @@ impl, const D: usize> Stark for CpuSkeletonSt consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let vars = eb.to_typed_starkframe(vars); + let constraints = Self::generate_constraints(&vars); build_ext(constraints, builder, consumer); } diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index 88185f19a..d86dbb889 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -12,7 +12,7 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::memory::columns::Memory; use crate::unstark::NoColumns; @@ -29,56 +29,60 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let nv = vars.next_values; - let mut constraints = ConstraintBuilder::default(); - - // Boolean constraints - // ------------------- - // Constrain certain columns of the memory table to be only - // boolean values. - constraints.always(lv.is_writable.is_binary()); - constraints.always(lv.is_store.is_binary()); - constraints.always(lv.is_load.is_binary()); - constraints.always(lv.is_init.is_binary()); - constraints.always(lv.is_executed().is_binary()); - - // Address constraints - // ------------------- - - // We start address at 0 and end at u32::MAX - // This saves rangechecking the addresses - // themselves, we only rangecheck their difference. - constraints.first_row(lv.addr - 0); - constraints.last_row(lv.addr - i64::from(u32::MAX)); - - // Address can only change for init in the new row... - constraints.always((1 - nv.is_init) * (nv.addr - lv.addr)); - // ... and we have a range-check to make sure that addresses go up for each - // init. - - // Operation constraints - // --------------------- - - // writeable only changes for init: - constraints.always((1 - nv.is_init) * (nv.is_writable - lv.is_writable)); - - // No `SB` operation can be seen if memory address is not marked `writable` - constraints.always((1 - lv.is_writable) * lv.is_store); - - // For all "load" operations, the value cannot change between rows - constraints.always(nv.is_load * (nv.value - lv.value)); - - // Padding constraints - // ------------------- - // Once we have padding, all subsequent rows are padding; ie not - // `is_executed`. - constraints.transition((lv.is_executed() - nv.is_executed()) * nv.is_executed()); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, Memory>, NoColumns> for MemoryStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let nv = vars.next_values; + let mut constraints = ConstraintBuilder::default(); + + // Boolean constraints + // ------------------- + // Constrain certain columns of the memory table to be only + // boolean values. + constraints.always(lv.is_writable.is_binary()); + constraints.always(lv.is_store.is_binary()); + constraints.always(lv.is_load.is_binary()); + constraints.always(lv.is_init.is_binary()); + constraints.always(lv.is_executed().is_binary()); + + // Address constraints + // ------------------- + + // We start address at 0 and end at u32::MAX + // This saves rangechecking the addresses + // themselves, we only rangecheck their difference. + constraints.first_row(lv.addr - 0); + constraints.last_row(lv.addr - i64::from(u32::MAX)); + + // Address can only change for init in the new row... + constraints.always((1 - nv.is_init) * (nv.addr - lv.addr)); + // ... and we have a range-check to make sure that addresses go up for each + // init. + + // Operation constraints + // --------------------- + + // writeable only changes for init: + constraints.always((1 - nv.is_init) * (nv.is_writable - lv.is_writable)); + + // No `SB` operation can be seen if memory address is not marked `writable` + constraints.always((1 - lv.is_writable) * lv.is_store); + + // For all "load" operations, the value cannot change between rows + constraints.always(nv.is_load * (nv.value - lv.value)); + + // Padding constraints + // ------------------- + // Once we have padding, all subsequent rows are padding; ie not + // `is_executed`. + constraints.transition((lv.is_executed() - nv.is_executed()) * nv.is_executed()); + + constraints + } } impl, const D: usize> Stark for MemoryStark { @@ -98,7 +102,8 @@ impl, const D: usize> Stark for MemoryStark, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let vars = eb.to_typed_starkframe(vars); + let constraints = Self::generate_constraints(&vars); build_packed(constraints, consumer); } @@ -111,7 +116,8 @@ impl, const D: usize> Stark for MemoryStark, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let vars = eb.to_typed_starkframe(vars); + let constraints = Self::generate_constraints(&vars); build_ext(constraints, builder, consumer); } } From 7d425246e1590f2fb59f00af0622d87bc675c652 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:51:08 +0800 Subject: [PATCH 05/84] Remove unnecessary lifetime --- circuits/src/bitshift/stark.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 33e77f0a1..f3d296533 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -35,7 +35,7 @@ impl<'a, F, T: Copy, U, const D: usize> for BitshiftStark { fn generate_constraints( - vars: &'a StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>, ) -> ConstraintBuilder> { let lv = vars.local_values.executed; let nv = vars.next_values.executed; From bdb5838eb75d162d69093d36a2d463c60a1c3dcd Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:54:15 +0800 Subject: [PATCH 06/84] Fix lifetimes --- circuits/src/cpu/stark.rs | 4 ++-- circuits/src/expr.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 4e3712190..c0973016a 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -41,7 +41,7 @@ fn pc_ticks_up<'a, P: Copy>(lv: &CpuState>, cb: &mut ConstraintBuild /// Ie exactly one of them should be 1, and all others 0 in each row. /// See fn binary_selectors<'a, P: Copy>( - ops: &'a OpSelectors>, + ops: &OpSelectors>, cb: &mut ConstraintBuilder>, ) { // selectors have value 0 or 1. @@ -82,7 +82,7 @@ impl<'a, F, T: Copy, U, const D: usize> GenerateConstraints<'a, T, T, CpuState>, NoColumns> for CpuStark { fn generate_constraints( - vars: &'a StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>, ) -> ConstraintBuilder> { let lv = &vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index ffb9b75d8..1e5cf2224 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -195,6 +195,6 @@ pub fn build_packed( pub trait GenerateConstraints<'a, T, U, View, PublicInputs> { fn generate_constraints( - vars: &'a StarkFrameTyped, + vars: &StarkFrameTyped, ) -> ConstraintBuilder>; } From d8581ea7a4c01f57e214f9b4247a71be1b90c97b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:56:35 +0800 Subject: [PATCH 07/84] More conversion --- circuits/src/memory_fullword/stark.rs | 49 +++++++++++++++------------ 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 2f76c68fa..48fa202b3 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -13,7 +13,7 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::memory_fullword::columns::{FullWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; @@ -30,26 +30,31 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -// Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); - - constraints.always(lv.ops.is_store.is_binary()); - constraints.always(lv.ops.is_load.is_binary()); - constraints.always(lv.is_executed().is_binary()); - - // Check: the resulting sum is wrapped if necessary. - // As the result is range checked, this make the choice deterministic, - // even for a malicious prover. - for (i, addr) in izip!(0.., lv.addrs).skip(1) { - let target = lv.addrs[0] + i; - constraints.always(lv.is_executed() * (addr - target) * (addr + (1 << 32) - target)); - } +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, FullWordMemory>, NoColumns> + for FullWordMemoryStark +{ + // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); + + constraints.always(lv.ops.is_store.is_binary()); + constraints.always(lv.ops.is_load.is_binary()); + constraints.always(lv.is_executed().is_binary()); + + // Check: the resulting sum is wrapped if necessary. + // As the result is range checked, this make the choice deterministic, + // even for a malicious prover. + for (i, addr) in izip!(0.., lv.addrs).skip(1) { + let target = lv.addrs[0] + i; + constraints.always(lv.is_executed() * (addr - target) * (addr + (1 << 32) - target)); + } - constraints + constraints + } } impl, const D: usize> Stark for FullWordMemoryStark { @@ -70,7 +75,7 @@ impl, const D: usize> Stark for FullWordMemor FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -81,7 +86,7 @@ impl, const D: usize> Stark for FullWordMemor consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } From 59d08080fa9481605d61923cbcd12eeb418f7bfd Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:57:04 +0800 Subject: [PATCH 08/84] No vars --- circuits/src/bitshift/stark.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index f3d296533..7d0804dad 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -93,8 +93,7 @@ impl, const D: usize> Stark for BitshiftStark FE: FieldExtension, P: PackedField, { let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -107,8 +106,7 @@ impl, const D: usize> Stark for BitshiftStark constraint_consumer: &mut RecursiveConstraintConsumer, ) { let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } } From 986d1ccc156b958eeb89c22bb766a63e4258edcf Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 15:58:01 +0800 Subject: [PATCH 09/84] Inline vars --- circuits/src/cpu/stark.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index c0973016a..d8f434db9 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -130,8 +130,7 @@ impl, const D: usize> Stark for CpuStark, P: PackedField, { let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -144,8 +143,7 @@ impl, const D: usize> Stark for CpuStark, ) { let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } } From e3b6ed52e3b1a3470be333b63433dbaccfda608b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 16:01:38 +0800 Subject: [PATCH 10/84] Minimize diff --- circuits/src/ops/add/stark.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 16d27c2ac..620ca3df9 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -67,8 +67,7 @@ impl, const D: usize> Stark for AddStark, P: PackedField, { let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -79,8 +78,7 @@ impl, const D: usize> Stark for AddStark, ) { let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } From fe43ff12f263951ef0fb0428de8557bf8160d08e Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 16:03:53 +0800 Subject: [PATCH 11/84] Convert more --- circuits/src/memory_halfword/stark.rs | 51 +++++++++++++++------------ 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 9917e1fa3..021c189d8 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -12,7 +12,7 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::memory_halfword::columns::{HalfWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; @@ -26,26 +26,31 @@ impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -// Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); - - constraints.always(lv.ops.is_store.is_binary()); - constraints.always(lv.ops.is_load.is_binary()); - constraints.always(lv.is_executed().is_binary()); - - let added = lv.addrs[0] + 1; - let wrapped = added - (1 << 32); - - // Check: the resulting sum is wrapped if necessary. - // As the result is range checked, this make the choice deterministic, - // even for a malicious prover. - constraints.always(lv.is_executed() * (lv.addrs[1] - added) * (lv.addrs[1] - wrapped)); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, HalfWordMemory>, NoColumns> + for HalfWordMemoryStark +{ + // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); + + constraints.always(lv.ops.is_store.is_binary()); + constraints.always(lv.ops.is_load.is_binary()); + constraints.always(lv.is_executed().is_binary()); + + let added = lv.addrs[0] + 1; + let wrapped = added - (1 << 32); + + // Check: the resulting sum is wrapped if necessary. + // As the result is range checked, this make the choice deterministic, + // even for a malicious prover. + constraints.always(lv.is_executed() * (lv.addrs[1] - added) * (lv.addrs[1] - wrapped)); + + constraints + } } const COLUMNS: usize = NUM_HW_MEM_COLS; @@ -67,7 +72,7 @@ impl, const D: usize> Stark for HalfWordMemor FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -78,7 +83,7 @@ impl, const D: usize> Stark for HalfWordMemor consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } From 0062e8e1bc1e98c16614ff4f5def06789a476a8c Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:30:54 +0800 Subject: [PATCH 12/84] Fix --- circuits/src/memory_zeroinit/stark.rs | 25 +++++++++++++++---------- circuits/src/memoryinit/stark.rs | 25 +++++++++++++++---------- circuits/src/unstark.rs | 16 ++++++++++++++-- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 6d9785971..3c0812e67 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::MemoryZeroInit; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] @@ -29,15 +29,20 @@ impl HasNamedColumns for MemoryZeroInitStark { const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, MemoryZeroInit>, NoColumns> + for MemoryZeroInitStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); - constraints.always(lv.filter.is_binary()); + constraints.always(lv.filter.is_binary()); - constraints + constraints + } } impl, const D: usize> Stark for MemoryZeroInitStark { @@ -57,7 +62,7 @@ impl, const D: usize> Stark for MemoryZeroIni FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -68,7 +73,7 @@ impl, const D: usize> Stark for MemoryZeroIni consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 47dd7ab8f..9269254e6 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::MemoryInit; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] @@ -29,15 +29,20 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, MemoryInit>, NoColumns> + for MemoryInitStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); - constraints.always(lv.filter.is_binary()); + constraints.always(lv.filter.is_binary()); - constraints + constraints + } } impl, const D: usize> Stark for MemoryInitStark { @@ -57,7 +62,7 @@ impl, const D: usize> Stark for MemoryInitSta FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -68,7 +73,7 @@ impl, const D: usize> Stark for MemoryInitSta consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 3efcc5595..fd5274b68 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -1,5 +1,6 @@ use std::marker::PhantomData; +use expr::{Expr, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; @@ -11,6 +12,17 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; +use crate::expr::{ConstraintBuilder, GenerateConstraints}; + +impl<'a, F, T, U, const D: usize, Columns, const COLUMNS: usize> + GenerateConstraints<'a, T, U, Columns, NoColumns> for Unstark +{ + fn generate_constraints( + _vars: &StarkFrameTyped>, + ) -> ConstraintBuilder> { + ConstraintBuilder::default() + } +} /// Template for a STARK with zero internal constraints. Use this if the STARK /// itself does not need any built-in constraints, but rely on cross table @@ -48,7 +60,7 @@ impl< fn eval_packed_generic( &self, _vars: &Self::EvaluationFrame, - _yield_constr: &mut ConstraintConsumer

, + _constraint_consumer: &mut ConstraintConsumer

, ) where FE: FieldExtension, P: PackedField, { @@ -58,7 +70,7 @@ impl< &self, _builder: &mut CircuitBuilder, _vars: &Self::EvaluationFrameTarget, - _yield_constr: &mut RecursiveConstraintConsumer, + _constraint_consumer: &mut RecursiveConstraintConsumer, ) { } From 3a8eb3c90616dc64e5e39f6316dbf22374c0b616 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:39:19 +0800 Subject: [PATCH 13/84] Simpler --- circuits/src/poseidon2/stark.rs | 35 +++++++++++++-------------------- circuits/src/unstark.rs | 3 ++- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 5d7fa248e..da94ea908 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -3,8 +3,9 @@ use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; use plonky2::field::extension::{Extendable, FieldExtension}; +use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::field::packed::PackedField; -use plonky2::field::types::Field; +use plonky2::field::types::{Field, PrimeField64}; use plonky2::hash::hash_types::RichField; use plonky2::hash::poseidon2::Poseidon2; use plonky2::iop::ext_target::ExtensionTarget; @@ -20,11 +21,8 @@ use crate::poseidon2::columns::{NUM_POSEIDON2_COLS, ROUNDS_F, ROUNDS_P, STATE_SI use crate::unstark::NoColumns; // degree: 1 -fn add_rc<'a, V, W, const STATE_SIZE: usize>( - state: &mut [Expr<'a, V>; STATE_SIZE], - r: usize, - from_u64: &mut impl FnMut(u64) -> Expr<'a, V>, -) where +fn add_rc(state: &mut [Expr; STATE_SIZE], r: usize) +where V: Copy, W: Poseidon2, { for (i, val) in state.iter_mut().enumerate() { @@ -103,10 +101,8 @@ where } // degree: 1 -fn matmul_internal12<'a, V, U, const STATE_SIZE: usize>( - state: &mut [Expr<'a, V>; STATE_SIZE], - from_u64: &mut impl FnMut(u64) -> Expr<'a, V>, -) where +fn matmul_internal12<'a, V, U, const STATE_SIZE: usize>(state: &mut [Expr<'a, V>; STATE_SIZE]) +where V: Copy, U: Poseidon2, { let sum = state.iter().sum::>(); @@ -133,12 +129,13 @@ const PUBLIC_INPUTS: usize = 0; // Compile time assertion that STATE_SIZE equals 12 const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; +fn from_u64(u: u64) -> i64 { GoldilocksField::from_canonical_u64(u).to_canonical_i64() } + // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something // to take into account when providing a more geneeral API to plonky. fn generate_constraints<'a, V: Copy, U: Poseidon2>( vars: &StarkFrameTyped>, NoColumns>>, - from_u64: &mut impl FnMut(u64) -> Expr<'a, V>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -150,7 +147,7 @@ fn generate_constraints<'a, V: Copy, U: Poseidon2>( matmul_external12(&mut state); // first full rounds for r in 0..(ROUNDS_F / 2) { - add_rc::(&mut state, r, from_u64); + add_rc::(&mut state, r); for (i, item) in state.iter_mut().enumerate() { sbox_p( item, @@ -168,7 +165,7 @@ fn generate_constraints<'a, V: Copy, U: Poseidon2>( for i in 0..ROUNDS_P { state[0] += from_u64(::RC12_MID[i]); sbox_p(&mut state[0], &lv.s_box_input_qube_partial_rounds[i]); - matmul_internal12::(&mut state, from_u64); + matmul_internal12::(&mut state); constraints.always(state[0] - lv.state0_after_partial_rounds[i]); state[0] = lv.state0_after_partial_rounds[i]; } @@ -182,7 +179,7 @@ fn generate_constraints<'a, V: Copy, U: Poseidon2>( // last full rounds for i in 0..(ROUNDS_F / 2) { let r = (ROUNDS_F / 2) + i; - add_rc::(&mut state, r, from_u64); + add_rc::(&mut state, r); for (j, item) in state.iter_mut().enumerate() { sbox_p( item, @@ -215,9 +212,7 @@ impl, const D: usize> Stark for Poseidon2_12S FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints::(&eb.to_typed_starkframe(vars), &mut |u| { - eb.lit(P::from(FE::from_basefield(F::from_canonical_u64(u)))) - }); + let constraints = generate_constraints::(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -230,10 +225,8 @@ impl, const D: usize> Stark for Poseidon2_12S consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints::, F>( - &eb.to_typed_starkframe(vars), - &mut |u| eb.lit(builder.constant_extension(F::Extension::from_canonical_u64(u))), - ); + let constraints = + generate_constraints::, F>(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } } diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index fd5274b68..4cfd3701a 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,7 +15,8 @@ use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl<'a, F, T, U, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T, U, Columns, NoColumns> for Unstark + GenerateConstraints<'a, T, U, Columns, NoColumns> + for Unstark { fn generate_constraints( _vars: &StarkFrameTyped>, From 109ae4aad0ad803631e543009564f6e304a1555c Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:47:56 +0800 Subject: [PATCH 14/84] More uniform --- circuits/src/poseidon2/stark.rs | 153 ++++++++++++++++---------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index da94ea908..c62732bd3 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -16,30 +16,29 @@ use starky::stark::Stark; use super::columns::Poseidon2State; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::poseidon2::columns::{NUM_POSEIDON2_COLS, ROUNDS_F, ROUNDS_P, STATE_SIZE}; use crate::unstark::NoColumns; // degree: 1 -fn add_rc(state: &mut [Expr; STATE_SIZE], r: usize) +fn add_rc(state: &mut [Expr; STATE_SIZE], r: usize) where - V: Copy, - W: Poseidon2, { + T: Copy, { for (i, val) in state.iter_mut().enumerate() { - *val += from_u64(::RC12[r + i]); + *val += from_u64(GoldilocksField::RC12[r + i]); } } // degree: 3 -fn sbox_p<'a, V>(x: &mut Expr<'a, V>, x_qube: &Expr<'a, V>) +fn sbox_p<'a, T>(x: &mut Expr<'a, T>, x_qube: &Expr<'a, T>) where - V: Copy, { + T: Copy, { *x *= *x_qube * *x_qube; } -fn matmul_m4(state: &mut [Expr<'_, V>; STATE_SIZE]) +fn matmul_m4(state: &mut [Expr<'_, T>; STATE_SIZE]) where - V: Copy, { + T: Copy, { // input x = (x0, x1, x2, x3) let t4 = STATE_SIZE / 4; @@ -81,9 +80,9 @@ where } } -fn matmul_external12(state: &mut [Expr<'_, V>; STATE_SIZE]) +fn matmul_external12(state: &mut [Expr<'_, T>; STATE_SIZE]) where - V: Copy, { + T: Copy, { matmul_m4(state); let t4 = STATE_SIZE / 4; @@ -101,14 +100,13 @@ where } // degree: 1 -fn matmul_internal12<'a, V, U, const STATE_SIZE: usize>(state: &mut [Expr<'a, V>; STATE_SIZE]) +fn matmul_internal12<'a, T, const STATE_SIZE: usize>(state: &mut [Expr<'a, T>; STATE_SIZE]) where - V: Copy, - U: Poseidon2, { - let sum = state.iter().sum::>(); + T: Copy, { + let sum = state.iter().sum::>(); for (i, val) in state.iter_mut().enumerate() { - *val *= from_u64(::MAT_DIAG12_M_1[i]) - 1; + *val *= from_u64(GoldilocksField::MAT_DIAG12_M_1[i]) - 1; *val += sum; } } @@ -131,69 +129,75 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_canonical_u64(u).to_canonical_i64() } -// NOTE: This one has extra constraints compared to different implementations of -// `generate_constraints` that were have written so far. It will be something -// to take into account when providing a more geneeral API to plonky. -fn generate_constraints<'a, V: Copy, U: Poseidon2>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); - - // row can be execution or padding. - constraints.always(lv.is_exe.is_binary()); - - let mut state = lv.input; - matmul_external12(&mut state); - // first full rounds - for r in 0..(ROUNDS_F / 2) { - add_rc::(&mut state, r); - for (i, item) in state.iter_mut().enumerate() { - sbox_p( - item, - &lv.s_box_input_qube_first_full_rounds[r * STATE_SIZE + i], - ); - } +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, U, Poseidon2State>, NoColumns> + for Poseidon2_12Stark +{ + // NOTE: This one has extra constraints compared to different implementations of + // `generate_constraints` that were have written so far. It will be something + // to take into account when providing a more geneeral API to plonky. + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); + + // row can be execution or padding. + constraints.always(lv.is_exe.is_binary()); + + let mut state = lv.input; matmul_external12(&mut state); - for (i, state_i) in state.iter_mut().enumerate() { - constraints.always(*state_i - lv.state_after_first_full_rounds[r * STATE_SIZE + i]); - *state_i = lv.state_after_first_full_rounds[r * STATE_SIZE + i]; + // first full rounds + for r in 0..(ROUNDS_F / 2) { + add_rc::(&mut state, r); + for (i, item) in state.iter_mut().enumerate() { + sbox_p( + item, + &lv.s_box_input_qube_first_full_rounds[r * STATE_SIZE + i], + ); + } + matmul_external12(&mut state); + for (i, state_i) in state.iter_mut().enumerate() { + constraints.always(*state_i - lv.state_after_first_full_rounds[r * STATE_SIZE + i]); + *state_i = lv.state_after_first_full_rounds[r * STATE_SIZE + i]; + } } - } - // partial rounds - for i in 0..ROUNDS_P { - state[0] += from_u64(::RC12_MID[i]); - sbox_p(&mut state[0], &lv.s_box_input_qube_partial_rounds[i]); - matmul_internal12::(&mut state); - constraints.always(state[0] - lv.state0_after_partial_rounds[i]); - state[0] = lv.state0_after_partial_rounds[i]; - } - - // the state before last full rounds - for (i, state_i) in state.iter_mut().enumerate() { - constraints.always(*state_i - lv.state_after_partial_rounds[i]); - *state_i = lv.state_after_partial_rounds[i]; - } + // partial rounds + for i in 0..ROUNDS_P { + state[0] += from_u64(GoldilocksField::RC12_MID[i]); + sbox_p(&mut state[0], &lv.s_box_input_qube_partial_rounds[i]); + matmul_internal12::(&mut state); + constraints.always(state[0] - lv.state0_after_partial_rounds[i]); + state[0] = lv.state0_after_partial_rounds[i]; + } - // last full rounds - for i in 0..(ROUNDS_F / 2) { - let r = (ROUNDS_F / 2) + i; - add_rc::(&mut state, r); - for (j, item) in state.iter_mut().enumerate() { - sbox_p( - item, - &lv.s_box_input_qube_second_full_rounds[i * STATE_SIZE + j], - ); + // the state before last full rounds + for (i, state_i) in state.iter_mut().enumerate() { + constraints.always(*state_i - lv.state_after_partial_rounds[i]); + *state_i = lv.state_after_partial_rounds[i]; } - matmul_external12(&mut state); - for (j, state_j) in state.iter_mut().enumerate() { - constraints.always(*state_j - lv.state_after_second_full_rounds[i * STATE_SIZE + j]); - *state_j = lv.state_after_second_full_rounds[i * STATE_SIZE + j]; + + // last full rounds + for i in 0..(ROUNDS_F / 2) { + let r = (ROUNDS_F / 2) + i; + add_rc::(&mut state, r); + for (j, item) in state.iter_mut().enumerate() { + sbox_p( + item, + &lv.s_box_input_qube_second_full_rounds[i * STATE_SIZE + j], + ); + } + matmul_external12(&mut state); + for (j, state_j) in state.iter_mut().enumerate() { + constraints + .always(*state_j - lv.state_after_second_full_rounds[i * STATE_SIZE + j]); + *state_j = lv.state_after_second_full_rounds[i * STATE_SIZE + j]; + } } - } - constraints + constraints + } } impl, const D: usize> Stark for Poseidon2_12Stark { @@ -212,7 +216,7 @@ impl, const D: usize> Stark for Poseidon2_12S FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints::(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -225,8 +229,7 @@ impl, const D: usize> Stark for Poseidon2_12S consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = - generate_constraints::, F>(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } } From 39602715c72a84d30f0b0fe20752a5f48c4d5ccf Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:53:01 +0800 Subject: [PATCH 15/84] Simplify --- circuits/src/bitshift/stark.rs | 2 +- circuits/src/cpu/stark.rs | 2 +- circuits/src/cpu_skeleton/stark.rs | 2 +- circuits/src/expr.rs | 2 +- circuits/src/memory/stark.rs | 2 +- circuits/src/memory_fullword/stark.rs | 2 +- circuits/src/memory_halfword/stark.rs | 2 +- circuits/src/memory_zeroinit/stark.rs | 2 +- circuits/src/memoryinit/stark.rs | 2 +- circuits/src/ops/add/stark.rs | 2 +- circuits/src/poseidon2/stark.rs | 2 +- circuits/src/unstark.rs | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 7d0804dad..564762716 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -31,7 +31,7 @@ const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, T, BitshiftView>, NoColumns> + GenerateConstraints<'a, T, BitshiftView>, NoColumns> for BitshiftStark { fn generate_constraints( diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index d8f434db9..92ba4a4b5 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -79,7 +79,7 @@ const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, T, CpuState>, NoColumns> for CpuStark + GenerateConstraints<'a, T, CpuState>, NoColumns> for CpuStark { fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>, diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index 54297def1..ffc11d2e1 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -31,7 +31,7 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, T, CpuSkeleton>, PublicInputs>> + GenerateConstraints<'a, T, CpuSkeleton>, PublicInputs>> for CpuSkeletonStark { fn generate_constraints( diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 1e5cf2224..9c9574c0e 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -193,7 +193,7 @@ pub fn build_packed( } } -pub trait GenerateConstraints<'a, T, U, View, PublicInputs> { +pub trait GenerateConstraints<'a, T, View, PublicInputs> { fn generate_constraints( vars: &StarkFrameTyped, ) -> ConstraintBuilder>; diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index d86dbb889..edc6b5c1b 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -30,7 +30,7 @@ const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, Memory>, NoColumns> for MemoryStark + GenerateConstraints<'a, T, Memory>, NoColumns> for MemoryStark { fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>, diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 48fa202b3..a354c5df1 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -31,7 +31,7 @@ const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, FullWordMemory>, NoColumns> + GenerateConstraints<'a, T, FullWordMemory>, NoColumns> for FullWordMemoryStark { // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 021c189d8..43cc5ec92 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -27,7 +27,7 @@ impl HasNamedColumns for HalfWordMemoryStark { } impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, HalfWordMemory>, NoColumns> + GenerateConstraints<'a, T, HalfWordMemory>, NoColumns> for HalfWordMemoryStark { // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 3c0812e67..a4358dffc 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -30,7 +30,7 @@ const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, MemoryZeroInit>, NoColumns> + GenerateConstraints<'a, T, MemoryZeroInit>, NoColumns> for MemoryZeroInitStark { fn generate_constraints( diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 9269254e6..1506e70bc 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -30,7 +30,7 @@ const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, MemoryInit>, NoColumns> + GenerateConstraints<'a, T, MemoryInit>, NoColumns> for MemoryInitStark { fn generate_constraints( diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 620ca3df9..18de61a98 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -30,7 +30,7 @@ const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, Add>, NoColumns> for AddStark + GenerateConstraints<'a, T, Add>, NoColumns> for AddStark { fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>, diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index c62732bd3..4f0d866cb 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -130,7 +130,7 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_canonical_u64(u).to_canonical_i64() } impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, U, Poseidon2State>, NoColumns> + GenerateConstraints<'a, T, Poseidon2State>, NoColumns> for Poseidon2_12Stark { // NOTE: This one has extra constraints compared to different implementations of diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 4cfd3701a..8117658e6 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,7 +15,7 @@ use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl<'a, F, T, U, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T, U, Columns, NoColumns> + GenerateConstraints<'a, T, Columns, NoColumns> for Unstark { fn generate_constraints( From f724f1c3a6066e0399be26938b33a29dae3a655f Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:53:42 +0800 Subject: [PATCH 16/84] Fmt --- circuits/src/ops/add/stark.rs | 4 ++-- circuits/src/unstark.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 18de61a98..00781dc8e 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -29,8 +29,8 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, Add>, NoColumns> for AddStark +impl<'a, F, T: Copy, U, const D: usize> GenerateConstraints<'a, T, Add>, NoColumns> + for AddStark { fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>, diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 8117658e6..a89d216a6 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,8 +15,7 @@ use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl<'a, F, T, U, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T, Columns, NoColumns> - for Unstark + GenerateConstraints<'a, T, Columns, NoColumns> for Unstark { fn generate_constraints( _vars: &StarkFrameTyped>, From 548026b719b90e9cd6d83eaaf756b5dcd9ab96f4 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:55:39 +0800 Subject: [PATCH 17/84] Convert more --- circuits/src/poseidon2_output_bytes/stark.rs | 47 +++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index b3ba8d492..dc5b78f18 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::{FIELDS_COUNT, NUM_POSEIDON2_OUTPUT_BYTES_COLS}; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::poseidon2_output_bytes::columns::Poseidon2OutputBytes; use crate::unstark::NoColumns; @@ -30,25 +30,30 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); - - constraints.always(lv.is_executed.is_binary()); - for i in 0..FIELDS_COUNT { - let start_index = i * 8; - let end_index = i * 8 + 8; - constraints.always( - Expr::reduce_with_powers::>>( - lv.output_bytes[start_index..end_index].into(), - 256, - ) - lv.output_fields[i], - ); +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, Poseidon2OutputBytes>, NoColumns> + for Poseidon2OutputBytesStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); + + constraints.always(lv.is_executed.is_binary()); + for i in 0..FIELDS_COUNT { + let start_index = i * 8; + let end_index = i * 8 + 8; + constraints.always( + Expr::reduce_with_powers::>>( + lv.output_bytes[start_index..end_index].into(), + 256, + ) - lv.output_fields[i], + ); + } + + constraints } - - constraints } impl, const D: usize> Stark for Poseidon2OutputBytesStark { @@ -67,7 +72,7 @@ impl, const D: usize> Stark for Poseidon2Outp FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -78,7 +83,7 @@ impl, const D: usize> Stark for Poseidon2Outp consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } From 08ddd98a52037ff5698e303fa01f11b4a7d41f03 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 18:59:58 +0800 Subject: [PATCH 18/84] Poseidon convert --- circuits/src/poseidon2_sponge/stark.rs | 153 ++++++++++++------------- 1 file changed, 75 insertions(+), 78 deletions(-) diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index e16fdc405..5f152b4a7 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -6,7 +6,7 @@ use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; use plonky2::hash::hashing::PlonkyPermutation; -use plonky2::hash::poseidon2::Poseidon2Permutation; +use plonky2::hash::poseidon2::{Poseidon2, Poseidon2Permutation}; use plonky2::iop::ext_target::ExtensionTarget; use plonky2::plonk::circuit_builder::CircuitBuilder; use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; @@ -15,7 +15,7 @@ use starky::stark::Stark; use super::columns::NUM_POSEIDON2_SPONGE_COLS; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::poseidon2_sponge::columns::Poseidon2Sponge; use crate::unstark::NoColumns; @@ -32,73 +32,78 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -// For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, - rate: usize, - state_size: usize, -) -> ConstraintBuilder> { - // NOTE: clk and address will be used for CTL to CPU for is_init_permute rows - // only, and not be used for permute rows. - // For all non dummy rows we have CTL to Poseidon2 permute stark, with preimage - // and output columns. - - let rate = u8::try_from(rate).expect("rate > 255"); - let state_size = u8::try_from(state_size).expect("state_size > 255"); - let rate_scalar = i64::from(rate); - let lv = vars.local_values; - let nv = vars.next_values; - let mut constraints = ConstraintBuilder::default(); - - for val in [lv.ops.is_permute, lv.ops.is_init_permute, lv.gen_output] { - constraints.always(val.is_binary()); +impl<'a, F: Poseidon2, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, Poseidon2Sponge>, NoColumns> + for Poseidon2SpongeStark +{ + // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let rate = Poseidon2Permutation::::RATE; + let state_size = Poseidon2Permutation::::WIDTH; + // NOTE: clk and address will be used for CTL to CPU for is_init_permute rows + // only, and not be used for permute rows. + // For all non dummy rows we have CTL to Poseidon2 permute stark, with preimage + // and output columns. + + let rate = u8::try_from(rate).expect("rate > 255"); + let state_size = u8::try_from(state_size).expect("state_size > 255"); + let rate_scalar = i64::from(rate); + let lv = vars.local_values; + let nv = vars.next_values; + let mut constraints = ConstraintBuilder::default(); + + for val in [lv.ops.is_permute, lv.ops.is_init_permute, lv.gen_output] { + constraints.always(val.is_binary()); + } + let is_exe = lv.ops.is_init_permute + lv.ops.is_permute; + constraints.always(is_exe.is_binary()); + + let is_dummy = 1 - is_exe; + + // dummy row does not generate output + constraints.always(is_dummy * lv.gen_output); + + // if row generates output then it must be last rate sized + // chunk of input. + constraints.always(lv.gen_output * (lv.input_len - rate_scalar)); + + let is_init_or_dummy = |vars: &Poseidon2Sponge>| { + (1 - vars.ops.is_init_permute) * (vars.ops.is_init_permute + vars.ops.is_permute) + }; + + // First row must be init permute or dummy row. + constraints.first_row(is_init_or_dummy(&lv)); + // if row generates output then next row can be dummy or start of next hashing + constraints.always(lv.gen_output * is_init_or_dummy(&nv)); + + // Clk should not change within a sponge + constraints.transition(nv.ops.is_permute * (lv.clk - nv.clk)); + + let not_last_sponge = (1 - lv.gen_output) * (lv.ops.is_permute + lv.ops.is_init_permute); + // if current row consumes input and its not last sponge then next row must have + // length decreases by RATE, note that only actual execution row can consume + // input + constraints.transition(not_last_sponge * (lv.input_len - (nv.input_len + rate_scalar))); + // and input_addr increases by RATE + constraints.transition(not_last_sponge * (lv.input_addr - (nv.input_addr - rate_scalar))); + + // For each init_permute capacity bits are zero. + for i in rate..state_size { + constraints.always(lv.ops.is_init_permute * (lv.preimage[i as usize] - 0)); + } + + // For each permute capacity bits are copied from previous output. + for i in rate..state_size { + constraints.always( + (1 - nv.ops.is_init_permute) + * nv.ops.is_permute + * (nv.preimage[i as usize] - lv.output[i as usize]), + ); + } + constraints } - let is_exe = lv.ops.is_init_permute + lv.ops.is_permute; - constraints.always(is_exe.is_binary()); - - let is_dummy = 1 - is_exe; - - // dummy row does not generate output - constraints.always(is_dummy * lv.gen_output); - - // if row generates output then it must be last rate sized - // chunk of input. - constraints.always(lv.gen_output * (lv.input_len - rate_scalar)); - - let is_init_or_dummy = |vars: &Poseidon2Sponge>| { - (1 - vars.ops.is_init_permute) * (vars.ops.is_init_permute + vars.ops.is_permute) - }; - - // First row must be init permute or dummy row. - constraints.first_row(is_init_or_dummy(&lv)); - // if row generates output then next row can be dummy or start of next hashing - constraints.always(lv.gen_output * is_init_or_dummy(&nv)); - - // Clk should not change within a sponge - constraints.transition(nv.ops.is_permute * (lv.clk - nv.clk)); - - let not_last_sponge = (1 - lv.gen_output) * (lv.ops.is_permute + lv.ops.is_init_permute); - // if current row consumes input and its not last sponge then next row must have - // length decreases by RATE, note that only actual execution row can consume - // input - constraints.transition(not_last_sponge * (lv.input_len - (nv.input_len + rate_scalar))); - // and input_addr increases by RATE - constraints.transition(not_last_sponge * (lv.input_addr - (nv.input_addr - rate_scalar))); - - // For each init_permute capacity bits are zero. - for i in rate..state_size { - constraints.always(lv.ops.is_init_permute * (lv.preimage[i as usize] - 0)); - } - - // For each permute capacity bits are copied from previous output. - for i in rate..state_size { - constraints.always( - (1 - nv.ops.is_init_permute) - * nv.ops.is_permute - * (nv.preimage[i as usize] - lv.output[i as usize]), - ); - } - constraints } impl, const D: usize> Stark for Poseidon2SpongeStark { @@ -117,11 +122,7 @@ impl, const D: usize> Stark for Poseidon2Spon FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints( - &eb.to_typed_starkframe(vars), - Poseidon2Permutation::::RATE, - Poseidon2Permutation::::WIDTH, - ); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -133,11 +134,7 @@ impl, const D: usize> Stark for Poseidon2Spon consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints( - &eb.to_typed_starkframe(vars), - Poseidon2Permutation::::RATE, - Poseidon2Permutation::::WIDTH, - ); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } From 7d128b37c67e12a5c9f8f10106cba19e70eba877 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:02:49 +0800 Subject: [PATCH 19/84] Forward --- circuits/src/rangecheck_u8/stark.rs | 37 ++++---- circuits/src/register/general/stark.rs | 119 +++++++++++++------------ 2 files changed, 83 insertions(+), 73 deletions(-) diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 0ad55db4a..38584d3c5 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::RangeCheckU8; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] @@ -29,19 +29,24 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let nv = vars.next_values; - let mut constraints = ConstraintBuilder::default(); - - // Check: the `element`s form a sequence from 0 to 255 - constraints.first_row(lv.value); - constraints.transition(nv.value - lv.value - 1); - constraints.last_row(lv.value - i64::from(u8::MAX)); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, RangeCheckU8>, NoColumns> + for RangeCheckU8Stark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let nv = vars.next_values; + let mut constraints = ConstraintBuilder::default(); + + // Check: the `element`s form a sequence from 0 to 255 + constraints.first_row(lv.value); + constraints.transition(nv.value - lv.value - 1); + constraints.last_row(lv.value - i64::from(u8::MAX)); + + constraints + } } impl, const D: usize> Stark for RangeCheckU8Stark { @@ -61,7 +66,7 @@ impl, const D: usize> Stark for RangeCheckU8S FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -72,7 +77,7 @@ impl, const D: usize> Stark for RangeCheckU8S consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index bb3675637..d0693cc45 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::Register; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] @@ -29,60 +29,65 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -/// Constraints for the [`RegisterStark`]: -/// -/// 1) `is_init`, `is_read`, `is_write`, and the virtual `is_used` column are -/// binary columns. The `is_used` column is the sum of all the other ops -/// columns combined, to differentiate between real trace rows and padding -/// rows. -/// 2) The virtual `is_used` column only take values 0 or 1. -/// 3) Only rd changes. -/// 4) Address changes only when `nv.is_init` == 1. -/// 5) Address either stays the same or increments by 1. -/// 6) Addresses go from 1 to 31. Address 0 is handled by `RegisterZeroStark`. -/// -/// For more details, refer to the [Notion -/// document](https://www.notion.so/0xmozak/Register-File-STARK-62459d68aea648a0abf4e97aa0093ea2). -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let nv = vars.next_values; - let mut constraints = ConstraintBuilder::default(); - - // Constraint 1: filter columns take 0 or 1 values only. - constraints.always(lv.ops.is_init.is_binary()); - constraints.always(lv.ops.is_read.is_binary()); - constraints.always(lv.ops.is_write.is_binary()); - constraints.always(lv.is_used().is_binary()); - - // Constraint 2: virtual `is_used` column can only take values 0 or 1. - // (lv.is_used() - nv.is_used() - 1) is expressed as such, because - // lv.is_used() = 1 in the last real row, and - // nv.is_used() = 0 in the first padding row. - constraints.transition(nv.is_used() * (nv.is_used() - lv.is_used())); - - // Constraint 3: only rd changes. - // We reformulate the above constraint as such: - // For any register, only `is_write`, `is_init` or the virtual `is_used` - // column should be able to change values of registers. - // `is_read` should not change the values of registers. - constraints.transition(nv.ops.is_read * (nv.value - lv.value)); - - // Constraint 4: Address changes only when nv.is_init == 1. - // We reformulate the above constraint to be: - // if next `is_read` == 1 or next `is_write` == 1, the address cannot - // change. - constraints.transition((nv.ops.is_read + nv.ops.is_write) * (nv.addr - lv.addr)); - - // Constraint 5: Address either stays the same or increments by 1. - constraints.transition((nv.addr - lv.addr) * (nv.addr - lv.addr - 1)); - - // Constraint 6: addresses go from 1 to 31. - constraints.first_row(lv.addr - 1); - constraints.last_row(lv.addr - 31); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, Register>, NoColumns> for RegisterStark +{ + /// Constraints for the [`RegisterStark`]: + /// + /// 1) `is_init`, `is_read`, `is_write`, and the virtual `is_used` column + /// are binary columns. The `is_used` column is the sum of all the other + /// ops columns combined, to differentiate between real trace rows and + /// padding rows. + /// 2) The virtual `is_used` column only take values 0 or 1. + /// 3) Only rd changes. + /// 4) Address changes only when `nv.is_init` == 1. + /// 5) Address either stays the same or increments by 1. + /// 6) Addresses go from 1 to 31. Address 0 is handled by + /// `RegisterZeroStark`. + /// + /// For more details, refer to the [Notion + /// document](https://www.notion.so/0xmozak/Register-File-STARK-62459d68aea648a0abf4e97aa0093ea2). + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let nv = vars.next_values; + let mut constraints = ConstraintBuilder::default(); + + // Constraint 1: filter columns take 0 or 1 values only. + constraints.always(lv.ops.is_init.is_binary()); + constraints.always(lv.ops.is_read.is_binary()); + constraints.always(lv.ops.is_write.is_binary()); + constraints.always(lv.is_used().is_binary()); + + // Constraint 2: virtual `is_used` column can only take values 0 or 1. + // (lv.is_used() - nv.is_used() - 1) is expressed as such, because + // lv.is_used() = 1 in the last real row, and + // nv.is_used() = 0 in the first padding row. + constraints.transition(nv.is_used() * (nv.is_used() - lv.is_used())); + + // Constraint 3: only rd changes. + // We reformulate the above constraint as such: + // For any register, only `is_write`, `is_init` or the virtual `is_used` + // column should be able to change values of registers. + // `is_read` should not change the values of registers. + constraints.transition(nv.ops.is_read * (nv.value - lv.value)); + + // Constraint 4: Address changes only when nv.is_init == 1. + // We reformulate the above constraint to be: + // if next `is_read` == 1 or next `is_write` == 1, the address cannot + // change. + constraints.transition((nv.ops.is_read + nv.ops.is_write) * (nv.addr - lv.addr)); + + // Constraint 5: Address either stays the same or increments by 1. + constraints.transition((nv.addr - lv.addr) * (nv.addr - lv.addr - 1)); + + // Constraint 6: addresses go from 1 to 31. + constraints.first_row(lv.addr - 1); + constraints.last_row(lv.addr - 31); + + constraints + } } impl, const D: usize> Stark for RegisterStark { @@ -102,7 +107,7 @@ impl, const D: usize> Stark for RegisterStark FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -113,7 +118,7 @@ impl, const D: usize> Stark for RegisterStark consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } From bfbdf9a658cb2a8a2d6d51c707088295775b6548 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:03:55 +0800 Subject: [PATCH 20/84] More --- circuits/src/storage_device/stark.rs | 105 ++++++++++++++------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 6ed9de221..54114d85d 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -12,7 +12,7 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::storage_device::columns::{StorageDevice, NUM_STORAGE_DEVICE_COLS}; use crate::unstark::NoColumns; @@ -29,52 +29,59 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -// Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let nv = vars.next_values; - let mut constraints = ConstraintBuilder::default(); - - constraints.always(lv.ops.is_memory_store.is_binary()); - constraints.always(lv.ops.is_storage_device.is_binary()); - constraints.always(lv.is_executed().is_binary()); - - // If nv.is_storage_device() == 1: lv.size == 0, also forces the last row to be - // size == 0 ! This constraints ensures loop unrolling was done correctly - constraints.always(nv.ops.is_storage_device * lv.size); - // If lv.is_lv_and_nv_are_memory_rows == 1: - // nv.address == lv.address + 1 (wrapped) - // nv.size == lv.size - 1 (not-wrapped) - let added = lv.addr + 1; - let wrapped = added - (1 << 32); - // nv.address == lv.address + 1 (wrapped) - constraints.always(lv.is_lv_and_nv_are_memory_rows * (nv.addr - added) * (nv.addr - wrapped)); - // nv.size == lv.size - 1 (not-wrapped) - constraints.transition(nv.is_lv_and_nv_are_memory_rows * (nv.size - (lv.size - 1))); - // Edge cases: - // a) - storage_device with size = 0: <-- this case is solved since CTL from - // CPU a.1) is_lv_and_nv_are_memory_rows = 0 (no memory rows - // inserted) b) - storage_device with size = 1: <-- this case needs to be - // solved separately b.1) is_lv_and_nv_are_memory_rows = 0 (only one - // memory row inserted) To solve case-b: - // If lv.is_storage_device() == 1 && lv.size != 0: - // lv.addr == nv.addr <-- next row address must be the same !!! - // lv.size === nv.size - 1 <-- next row size is decreased - constraints.transition(lv.ops.is_storage_device * lv.size * (nv.addr - lv.addr)); - constraints.transition(lv.ops.is_storage_device * lv.size * (nv.size - (lv.size - 1))); - // If lv.is_storage_device() == 1 && lv.size == 0: - // nv.is_memory() == 0 <-- next op can be only io - since size == 0 - // This one is ensured by: - // 1) is_binary(storage_device or memory) - // 2) if nv.is_storage_device() == 1: lv.size == 0 - - // If lv.is_storage_device() == 1 && nv.size != 0: - // nv.is_lv_and_nv_are_memory_rows == 1 - constraints.always(lv.ops.is_storage_device * nv.size * (nv.is_lv_and_nv_are_memory_rows - 1)); - - constraints +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, StorageDevice>, NoColumns> + for StorageDeviceStark +{ + // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let nv = vars.next_values; + let mut constraints = ConstraintBuilder::default(); + + constraints.always(lv.ops.is_memory_store.is_binary()); + constraints.always(lv.ops.is_storage_device.is_binary()); + constraints.always(lv.is_executed().is_binary()); + + // If nv.is_storage_device() == 1: lv.size == 0, also forces the last row to be + // size == 0 ! This constraints ensures loop unrolling was done correctly + constraints.always(nv.ops.is_storage_device * lv.size); + // If lv.is_lv_and_nv_are_memory_rows == 1: + // nv.address == lv.address + 1 (wrapped) + // nv.size == lv.size - 1 (not-wrapped) + let added = lv.addr + 1; + let wrapped = added - (1 << 32); + // nv.address == lv.address + 1 (wrapped) + constraints + .always(lv.is_lv_and_nv_are_memory_rows * (nv.addr - added) * (nv.addr - wrapped)); + // nv.size == lv.size - 1 (not-wrapped) + constraints.transition(nv.is_lv_and_nv_are_memory_rows * (nv.size - (lv.size - 1))); + // Edge cases: + // a) - storage_device with size = 0: <-- this case is solved since CTL from + // CPU a.1) is_lv_and_nv_are_memory_rows = 0 (no memory rows + // inserted) b) - storage_device with size = 1: <-- this case needs to be + // solved separately b.1) is_lv_and_nv_are_memory_rows = 0 (only one + // memory row inserted) To solve case-b: + // If lv.is_storage_device() == 1 && lv.size != 0: + // lv.addr == nv.addr <-- next row address must be the same !!! + // lv.size === nv.size - 1 <-- next row size is decreased + constraints.transition(lv.ops.is_storage_device * lv.size * (nv.addr - lv.addr)); + constraints.transition(lv.ops.is_storage_device * lv.size * (nv.size - (lv.size - 1))); + // If lv.is_storage_device() == 1 && lv.size == 0: + // nv.is_memory() == 0 <-- next op can be only io - since size == 0 + // This one is ensured by: + // 1) is_binary(storage_device or memory) + // 2) if nv.is_storage_device() == 1: lv.size == 0 + + // If lv.is_storage_device() == 1 && nv.size != 0: + // nv.is_lv_and_nv_are_memory_rows == 1 + constraints + .always(lv.ops.is_storage_device * nv.size * (nv.is_lv_and_nv_are_memory_rows - 1)); + + constraints + } } impl, const D: usize> Stark for StorageDeviceStark { @@ -94,7 +101,7 @@ impl, const D: usize> Stark for StorageDevice FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -105,7 +112,7 @@ impl, const D: usize> Stark for StorageDevice consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } From 71222b229b1c9389952546b81031abff2dcb2546 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:04:57 +0800 Subject: [PATCH 21/84] Commitment --- circuits/src/tape_commitments/stark.rs | 43 +++++++++++++++----------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 3c55b7581..63c8ddd0c 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -13,23 +13,30 @@ use starky::stark::Stark; use super::columns::TapeCommitments; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv: &TapeCommitments> = &vars.local_values; - let mut constraint = ConstraintBuilder::default(); - constraint.always(lv.is_event_commitment_tape_row.is_binary()); - constraint.always(lv.is_castlist_commitment_tape_row.is_binary()); - constraint - .always((lv.is_castlist_commitment_tape_row + lv.is_event_commitment_tape_row).is_binary()); - constraint - .always(lv.event_commitment_tape_multiplicity * (1 - lv.is_event_commitment_tape_row)); - constraint.always( - lv.castlist_commitment_tape_multiplicity * (1 - lv.is_castlist_commitment_tape_row), - ); - constraint + +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, TapeCommitments>, NoColumns> + for TapeCommitmentsStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv: &TapeCommitments> = &vars.local_values; + let mut constraint = ConstraintBuilder::default(); + constraint.always(lv.is_event_commitment_tape_row.is_binary()); + constraint.always(lv.is_castlist_commitment_tape_row.is_binary()); + constraint.always( + (lv.is_castlist_commitment_tape_row + lv.is_event_commitment_tape_row).is_binary(), + ); + constraint + .always(lv.event_commitment_tape_multiplicity * (1 - lv.is_event_commitment_tape_row)); + constraint.always( + lv.castlist_commitment_tape_multiplicity * (1 - lv.is_castlist_commitment_tape_row), + ); + constraint + } } #[derive(Copy, Clone, Default, StarkNameDisplay)] @@ -62,7 +69,7 @@ impl, const D: usize> Stark for TapeCommitmen FE: FieldExtension, P: PackedField, { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -75,7 +82,7 @@ impl, const D: usize> Stark for TapeCommitmen consumer: &mut RecursiveConstraintConsumer, ) { let eb = ExprBuilder::default(); - let constraints = generate_constraints(&eb.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); build_ext(constraints, builder, consumer); } } From cea92e2816b7af06189ac9b7537cca0cc308b537 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:06:37 +0800 Subject: [PATCH 22/84] Convert all --- circuits/src/xor/stark.rs | 60 +++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index c94882ae0..5144ae07f 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -14,7 +14,7 @@ use starky::stark::Stark; use super::columns::XorColumnsView; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] @@ -30,32 +30,36 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -fn generate_constraints<'a, T: Copy>( - vars: &StarkFrameTyped>, NoColumns>>, -) -> ConstraintBuilder> { - let lv = vars.local_values; - let mut constraints = ConstraintBuilder::default(); - - // We first convert both input and output to bit representation - // We then work with the bit representations to check the Xor result. - - // Check: bit representation of inputs and output contains either 0 or 1. - for bit_value in chain!(lv.limbs.a, lv.limbs.b, lv.limbs.out) { - constraints.always(bit_value.is_binary()); - } - - // Check: bit representation of inputs and output were generated correctly. - for (opx, opx_limbs) in izip![lv.execution, lv.limbs] { - constraints.always(Expr::reduce_with_powers(opx_limbs, 2) - opx); +impl<'a, F, T: Copy, U, const D: usize> + GenerateConstraints<'a, T, XorColumnsView>, NoColumns> for XorStark +{ + fn generate_constraints( + vars: &StarkFrameTyped>, NoColumns>, + ) -> ConstraintBuilder> { + let lv = vars.local_values; + let mut constraints = ConstraintBuilder::default(); + + // We first convert both input and output to bit representation + // We then work with the bit representations to check the Xor result. + + // Check: bit representation of inputs and output contains either 0 or 1. + for bit_value in chain!(lv.limbs.a, lv.limbs.b, lv.limbs.out) { + constraints.always(bit_value.is_binary()); + } + + // Check: bit representation of inputs and output were generated correctly. + for (opx, opx_limbs) in izip![lv.execution, lv.limbs] { + constraints.always(Expr::reduce_with_powers(opx_limbs, 2) - opx); + } + + // Check: output bit representation is Xor of input a and b bit representations + for (a, b, out) in izip!(lv.limbs.a, lv.limbs.b, lv.limbs.out) { + // Xor behaves like addition in binary field, i.e. addition with wrap-around: + constraints.always((a + b - out) * (a + b - 2 - out)); + } + + constraints } - - // Check: output bit representation is Xor of input a and b bit representations - for (a, b, out) in izip!(lv.limbs.a, lv.limbs.b, lv.limbs.out) { - // Xor behaves like addition in binary field, i.e. addition with wrap-around: - constraints.always((a + b - out) * (a + b - 2 - out)); - } - - constraints } impl, const D: usize> Stark for XorStark { @@ -75,7 +79,7 @@ impl, const D: usize> Stark for XorStark, P: PackedField, { let expr_builder = ExprBuilder::default(); - let constraints = generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, consumer); } @@ -88,7 +92,7 @@ impl, const D: usize> Stark for XorStark, ) { let expr_builder = ExprBuilder::default(); - let constraints = generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, consumer); } } From 839a8b8b2cf4163f1662ac04d85e698825792542 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:15:47 +0800 Subject: [PATCH 23/84] PublicInputs --- circuits/src/bitshift/stark.rs | 1 + circuits/src/columns_view.rs | 1 + circuits/src/cpu/stark.rs | 1 + circuits/src/cpu_skeleton/stark.rs | 1 + circuits/src/generation.rs | 13 +++++++++---- circuits/src/memory/stark.rs | 1 + circuits/src/memory_fullword/stark.rs | 1 + circuits/src/memory_halfword/stark.rs | 1 + circuits/src/memory_zeroinit/stark.rs | 1 + circuits/src/memoryinit/stark.rs | 1 + circuits/src/ops/add/stark.rs | 1 + circuits/src/poseidon2/stark.rs | 1 + circuits/src/poseidon2_output_bytes/stark.rs | 1 + circuits/src/poseidon2_sponge/stark.rs | 1 + circuits/src/rangecheck_u8/stark.rs | 1 + circuits/src/register/general/stark.rs | 1 + circuits/src/storage_device/stark.rs | 1 + circuits/src/tape_commitments/stark.rs | 1 + circuits/src/unstark.rs | 1 + circuits/src/xor/stark.rs | 1 + 20 files changed, 28 insertions(+), 4 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 564762716..64ef39f1b 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -25,6 +25,7 @@ pub struct BitshiftStark { impl HasNamedColumns for BitshiftStark { type Columns = BitshiftView; + type PublicInputs = NoColumns; } const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index b2daf2eef..5b84a908e 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -29,6 +29,7 @@ pub(crate) const unsafe fn transmute_ref(t: &T) -> &U { pub trait HasNamedColumns { type Columns; + type PublicInputs; } pub trait NumberOfColumns { diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 92ba4a4b5..4224296fd 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -29,6 +29,7 @@ pub struct CpuStark { impl HasNamedColumns for CpuStark { type Columns = CpuState; + type PublicInputs = NoColumns; } /// Ensure that if opcode is straight line, then program counter is incremented diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index ffc11d2e1..ea2b9c331 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -24,6 +24,7 @@ pub struct CpuSkeletonStark { impl HasNamedColumns for CpuSkeletonStark { type Columns = CpuSkeleton; + type PublicInputs = PublicInputs; } const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 2f6a2ca5d..b35ab78c1 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -22,6 +22,7 @@ use crate::bitshift::generation::generate_shift_amount_trace; use crate::columns_view::HasNamedColumns; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; +use crate::expr::GenerateConstraints; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -214,13 +215,17 @@ pub fn debug_traces, const D: usize>( } pub fn debug_single_trace< + 'a, F: RichField + Extendable + Debug, const D: usize, - S: Stark + Display + HasNamedColumns, + S: Stark + + Display + + HasNamedColumns + + GenerateConstraints<'a, F, S::Columns, S::PublicInputs>, >( - stark: &S, - trace_rows: &[PolynomialValues], - public_inputs: &[F], + stark: &'a S, + trace_rows: &'a [PolynomialValues], + public_inputs: &'a [F], ) where S::Columns: FromIterator + Debug, { transpose_polys::(trace_rows.to_vec()) diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index edc6b5c1b..034d95989 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -24,6 +24,7 @@ pub struct MemoryStark { impl HasNamedColumns for MemoryStark { type Columns = Memory; + type PublicInputs = NoColumns; } const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index a354c5df1..a8b7a3deb 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -25,6 +25,7 @@ pub struct FullWordMemoryStark { impl HasNamedColumns for FullWordMemoryStark { type Columns = FullWordMemory; + type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_HW_MEM_COLS; diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 43cc5ec92..b483acf34 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -24,6 +24,7 @@ pub struct HalfWordMemoryStark { impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; + type PublicInputs = NoColumns; } impl<'a, F, T: Copy, U, const D: usize> diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index a4358dffc..b64db5796 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -24,6 +24,7 @@ pub struct MemoryZeroInitStark { impl HasNamedColumns for MemoryZeroInitStark { type Columns = MemoryZeroInit; + type PublicInputs = NoColumns; } const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 1506e70bc..0e7251205 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -24,6 +24,7 @@ pub struct MemoryInitStark { impl HasNamedColumns for MemoryInitStark { type Columns = MemoryInit; + type PublicInputs = NoColumns; } const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 00781dc8e..3b16e8cd9 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -24,6 +24,7 @@ pub struct AddStark { impl HasNamedColumns for AddStark { type Columns = Add; + type PublicInputs = NoColumns; } const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 4f0d866cb..b46bcfa81 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -119,6 +119,7 @@ pub struct Poseidon2_12Stark { impl HasNamedColumns for Poseidon2_12Stark { type Columns = Poseidon2State; + type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_POSEIDON2_COLS; diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index dc5b78f18..89734abbe 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -25,6 +25,7 @@ pub struct Poseidon2OutputBytesStark { impl HasNamedColumns for Poseidon2OutputBytesStark { type Columns = Poseidon2OutputBytes; + type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 5f152b4a7..6db41a233 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -27,6 +27,7 @@ pub struct Poseidon2SpongeStark { impl HasNamedColumns for Poseidon2SpongeStark { type Columns = Poseidon2Sponge; + type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 38584d3c5..9100f43b0 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -24,6 +24,7 @@ pub struct RangeCheckU8Stark { impl HasNamedColumns for RangeCheckU8Stark { type Columns = RangeCheckU8; + type PublicInputs = NoColumns; } const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index d0693cc45..553e8bc3f 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -24,6 +24,7 @@ pub struct RegisterStark { impl HasNamedColumns for RegisterStark { type Columns = Register; + type PublicInputs = NoColumns; } const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 54114d85d..0bd098103 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -24,6 +24,7 @@ pub struct StorageDeviceStark { impl HasNamedColumns for StorageDeviceStark { type Columns = StorageDevice; + type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 63c8ddd0c..a0e933200 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -47,6 +47,7 @@ pub struct TapeCommitmentsStark { impl HasNamedColumns for TapeCommitmentsStark { type Columns = TapeCommitments; + type PublicInputs = NoColumns; } const COLUMNS: usize = TapeCommitments::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index a89d216a6..080be3f13 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -38,6 +38,7 @@ impl HasNamedColumns for Unstark { type Columns = Columns; + type PublicInputs = NoColumns; } const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 5144ae07f..6c6d10ca5 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -25,6 +25,7 @@ pub struct XorStark { impl HasNamedColumns for XorStark { type Columns = XorColumnsView; + type PublicInputs = NoColumns; } const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; From e9c7559b10daf0cee99a68fa87dce6f2bdc8bee1 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:22:57 +0800 Subject: [PATCH 24/84] Stopgop --- circuits/src/generation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index b35ab78c1..b944268fd 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -22,7 +22,7 @@ use crate::bitshift::generation::generate_shift_amount_trace; use crate::columns_view::HasNamedColumns; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::GenerateConstraints; +// use crate::expr::GenerateConstraints; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -221,7 +221,7 @@ pub fn debug_single_trace< S: Stark + Display + HasNamedColumns - + GenerateConstraints<'a, F, S::Columns, S::PublicInputs>, + // + GenerateConstraints<'a, F, S::Columns, S::PublicInputs>, >( stark: &'a S, trace_rows: &'a [PolynomialValues], From f36f9039c4c2a5582505b75923e508a5711e5ad1 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 31 May 2024 19:27:03 +0800 Subject: [PATCH 25/84] Remove PublicInputs from HasNamedColumns --- circuits/src/bitshift/stark.rs | 1 - circuits/src/columns_view.rs | 1 - circuits/src/cpu/stark.rs | 1 - circuits/src/cpu_skeleton/stark.rs | 1 - circuits/src/generation.rs | 5 +---- circuits/src/memory/stark.rs | 1 - circuits/src/memory_fullword/stark.rs | 1 - circuits/src/memory_halfword/stark.rs | 1 - circuits/src/memory_zeroinit/stark.rs | 1 - circuits/src/memoryinit/stark.rs | 1 - circuits/src/ops/add/stark.rs | 1 - circuits/src/poseidon2/stark.rs | 1 - circuits/src/poseidon2_output_bytes/stark.rs | 1 - circuits/src/poseidon2_sponge/stark.rs | 1 - circuits/src/rangecheck_u8/stark.rs | 1 - circuits/src/register/general/stark.rs | 1 - circuits/src/storage_device/stark.rs | 1 - circuits/src/tape_commitments/stark.rs | 1 - circuits/src/unstark.rs | 1 - circuits/src/xor/stark.rs | 1 - 20 files changed, 1 insertion(+), 23 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 64ef39f1b..564762716 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -25,7 +25,6 @@ pub struct BitshiftStark { impl HasNamedColumns for BitshiftStark { type Columns = BitshiftView; - type PublicInputs = NoColumns; } const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index 5b84a908e..b2daf2eef 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -29,7 +29,6 @@ pub(crate) const unsafe fn transmute_ref(t: &T) -> &U { pub trait HasNamedColumns { type Columns; - type PublicInputs; } pub trait NumberOfColumns { diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 4224296fd..92ba4a4b5 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -29,7 +29,6 @@ pub struct CpuStark { impl HasNamedColumns for CpuStark { type Columns = CpuState; - type PublicInputs = NoColumns; } /// Ensure that if opcode is straight line, then program counter is incremented diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index ea2b9c331..ffc11d2e1 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -24,7 +24,6 @@ pub struct CpuSkeletonStark { impl HasNamedColumns for CpuSkeletonStark { type Columns = CpuSkeleton; - type PublicInputs = PublicInputs; } const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index b944268fd..1ceefe405 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -218,10 +218,7 @@ pub fn debug_single_trace< 'a, F: RichField + Extendable + Debug, const D: usize, - S: Stark - + Display - + HasNamedColumns - // + GenerateConstraints<'a, F, S::Columns, S::PublicInputs>, + S: Stark + Display + HasNamedColumns, // + GenerateConstraints<'a, F, S::Columns>, >( stark: &'a S, trace_rows: &'a [PolynomialValues], diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index 034d95989..edc6b5c1b 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -24,7 +24,6 @@ pub struct MemoryStark { impl HasNamedColumns for MemoryStark { type Columns = Memory; - type PublicInputs = NoColumns; } const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index a8b7a3deb..a354c5df1 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -25,7 +25,6 @@ pub struct FullWordMemoryStark { impl HasNamedColumns for FullWordMemoryStark { type Columns = FullWordMemory; - type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_HW_MEM_COLS; diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index b483acf34..43cc5ec92 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -24,7 +24,6 @@ pub struct HalfWordMemoryStark { impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; - type PublicInputs = NoColumns; } impl<'a, F, T: Copy, U, const D: usize> diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index b64db5796..a4358dffc 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -24,7 +24,6 @@ pub struct MemoryZeroInitStark { impl HasNamedColumns for MemoryZeroInitStark { type Columns = MemoryZeroInit; - type PublicInputs = NoColumns; } const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 0e7251205..1506e70bc 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -24,7 +24,6 @@ pub struct MemoryInitStark { impl HasNamedColumns for MemoryInitStark { type Columns = MemoryInit; - type PublicInputs = NoColumns; } const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 3b16e8cd9..00781dc8e 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -24,7 +24,6 @@ pub struct AddStark { impl HasNamedColumns for AddStark { type Columns = Add; - type PublicInputs = NoColumns; } const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index b46bcfa81..4f0d866cb 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -119,7 +119,6 @@ pub struct Poseidon2_12Stark { impl HasNamedColumns for Poseidon2_12Stark { type Columns = Poseidon2State; - type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_POSEIDON2_COLS; diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index 89734abbe..dc5b78f18 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -25,7 +25,6 @@ pub struct Poseidon2OutputBytesStark { impl HasNamedColumns for Poseidon2OutputBytesStark { type Columns = Poseidon2OutputBytes; - type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 6db41a233..5f152b4a7 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -27,7 +27,6 @@ pub struct Poseidon2SpongeStark { impl HasNamedColumns for Poseidon2SpongeStark { type Columns = Poseidon2Sponge; - type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 9100f43b0..38584d3c5 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -24,7 +24,6 @@ pub struct RangeCheckU8Stark { impl HasNamedColumns for RangeCheckU8Stark { type Columns = RangeCheckU8; - type PublicInputs = NoColumns; } const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 553e8bc3f..d0693cc45 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -24,7 +24,6 @@ pub struct RegisterStark { impl HasNamedColumns for RegisterStark { type Columns = Register; - type PublicInputs = NoColumns; } const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 0bd098103..54114d85d 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -24,7 +24,6 @@ pub struct StorageDeviceStark { impl HasNamedColumns for StorageDeviceStark { type Columns = StorageDevice; - type PublicInputs = NoColumns; } const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index a0e933200..63c8ddd0c 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -47,7 +47,6 @@ pub struct TapeCommitmentsStark { impl HasNamedColumns for TapeCommitmentsStark { type Columns = TapeCommitments; - type PublicInputs = NoColumns; } const COLUMNS: usize = TapeCommitments::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 080be3f13..a89d216a6 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -38,7 +38,6 @@ impl HasNamedColumns for Unstark { type Columns = Columns; - type PublicInputs = NoColumns; } const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 6c6d10ca5..5144ae07f 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -25,7 +25,6 @@ pub struct XorStark { impl HasNamedColumns for XorStark { type Columns = XorColumnsView; - type PublicInputs = NoColumns; } const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; From 4ff65b10523e9585a9f19af407b580b9abc35b22 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Wed, 5 Jun 2024 13:32:47 +0800 Subject: [PATCH 26/84] Fix --- circuits/src/poseidon2/stark.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 4f0d866cb..7e9958ee5 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -127,7 +127,7 @@ const PUBLIC_INPUTS: usize = 0; // Compile time assertion that STATE_SIZE equals 12 const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; -fn from_u64(u: u64) -> i64 { GoldilocksField::from_canonical_u64(u).to_canonical_i64() } +fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } impl<'a, F, T: Copy, U, const D: usize> GenerateConstraints<'a, T, Poseidon2State>, NoColumns> From d58eef6124072b6c0b4083eff71421d5258882cd Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 10 Jun 2024 19:52:47 +0200 Subject: [PATCH 27/84] Try using Expr<'a, T> instead of U in NoColumns --- circuits/src/bitshift/stark.rs | 6 +++--- circuits/src/cpu/stark.rs | 6 +++--- circuits/src/memory/stark.rs | 6 +++--- circuits/src/memory_fullword/stark.rs | 6 +++--- circuits/src/memory_halfword/stark.rs | 6 +++--- circuits/src/memory_zeroinit/stark.rs | 6 +++--- circuits/src/memoryinit/stark.rs | 6 +++--- circuits/src/ops/add/stark.rs | 4 ++-- circuits/src/poseidon2/stark.rs | 6 +++--- circuits/src/poseidon2_output_bytes/stark.rs | 6 +++--- circuits/src/poseidon2_sponge/stark.rs | 6 +++--- circuits/src/rangecheck_u8/stark.rs | 6 +++--- circuits/src/register/general/stark.rs | 6 +++--- circuits/src/storage_device/stark.rs | 6 +++--- circuits/src/tape_commitments/stark.rs | 6 +++--- circuits/src/unstark.rs | 6 +++--- circuits/src/xor/stark.rs | 6 +++--- 17 files changed, 50 insertions(+), 50 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 564762716..970fdaecb 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -30,12 +30,12 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, BitshiftView>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, BitshiftView>, NoColumns>> for BitshiftStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values.executed; let nv = vars.next_values.executed; diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 92ba4a4b5..69bc7a8c1 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -78,11 +78,11 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, CpuState>, NoColumns> for CpuStark +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, CpuState>, NoColumns>> for CpuStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = &vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index edc6b5c1b..afedef0a8 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -29,11 +29,11 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, Memory>, NoColumns> for MemoryStark +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, Memory>, NoColumns>> for MemoryStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index a354c5df1..55a3fca6c 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -30,13 +30,13 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, FullWordMemory>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, FullWordMemory>, NoColumns>> for FullWordMemoryStark { // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 43cc5ec92..665c6f39f 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -26,13 +26,13 @@ impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, HalfWordMemory>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, HalfWordMemory>, NoColumns>> for HalfWordMemoryStark { // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index a4358dffc..157c94d00 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -29,12 +29,12 @@ impl HasNamedColumns for MemoryZeroInitStark { const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, MemoryZeroInit>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, MemoryZeroInit>, NoColumns>> for MemoryZeroInitStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 1506e70bc..4984242e5 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -29,12 +29,12 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, MemoryInit>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, MemoryInit>, NoColumns>> for MemoryInitStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 00781dc8e..c1bdaf40a 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -29,11 +29,11 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> GenerateConstraints<'a, T, Add>, NoColumns> +impl<'a, F, T: Copy, const D: usize> GenerateConstraints<'a, T, Add>, NoColumns>> for AddStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 7e9958ee5..e36de86a1 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -129,15 +129,15 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, Poseidon2State>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, Poseidon2State>, NoColumns>> for Poseidon2_12Stark { // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something // to take into account when providing a more geneeral API to plonky. fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index dc5b78f18..d79c641be 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -30,12 +30,12 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, Poseidon2OutputBytes>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, Poseidon2OutputBytes>, NoColumns>> for Poseidon2OutputBytesStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 5f152b4a7..2635f2fca 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -32,13 +32,13 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: Poseidon2, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, Poseidon2Sponge>, NoColumns> +impl<'a, F: Poseidon2, T: Copy, const D: usize> + GenerateConstraints<'a, T, Poseidon2Sponge>, NoColumns>> for Poseidon2SpongeStark { // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let rate = Poseidon2Permutation::::RATE; let state_size = Poseidon2Permutation::::WIDTH; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 38584d3c5..5b7863b0b 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -29,12 +29,12 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, RangeCheckU8>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, RangeCheckU8>, NoColumns>> for RangeCheckU8Stark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index d0693cc45..2b800c6a7 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -29,8 +29,8 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, Register>, NoColumns> for RegisterStark +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, Register>, NoColumns>> for RegisterStark { /// Constraints for the [`RegisterStark`]: /// @@ -48,7 +48,7 @@ impl<'a, F, T: Copy, U, const D: usize> /// For more details, refer to the [Notion /// document](https://www.notion.so/0xmozak/Register-File-STARK-62459d68aea648a0abf4e97aa0093ea2). fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 54114d85d..d5c0d732d 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -29,13 +29,13 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, StorageDevice>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, StorageDevice>, NoColumns>> for StorageDeviceStark { // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 63c8ddd0c..20b097728 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -16,12 +16,12 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, TapeCommitments>, NoColumns> +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, TapeCommitments>, NoColumns>> for TapeCommitmentsStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv: &TapeCommitments> = &vars.local_values; let mut constraint = ConstraintBuilder::default(); diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index a89d216a6..a45c4f04c 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -14,11 +14,11 @@ use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, T, U, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T, Columns, NoColumns> for Unstark +impl<'a, F, T, const D: usize, Columns, const COLUMNS: usize> + GenerateConstraints<'a, T, Columns, NoColumns>> for Unstark { fn generate_constraints( - _vars: &StarkFrameTyped>, + _vars: &StarkFrameTyped>>, ) -> ConstraintBuilder> { ConstraintBuilder::default() } diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 5144ae07f..43445e3a8 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -30,11 +30,11 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, U, const D: usize> - GenerateConstraints<'a, T, XorColumnsView>, NoColumns> for XorStark +impl<'a, F, T: Copy, const D: usize> + GenerateConstraints<'a, T, XorColumnsView>, NoColumns>> for XorStark { fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>, + vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); From 11bac0cf6165364944e9103aa4812377cc6b7729 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 10 Jun 2024 20:17:41 +0200 Subject: [PATCH 28/84] Move PublicInputs to a GAT --- circuits/src/bitshift/stark.rs | 4 +++- circuits/src/cpu/stark.rs | 4 +++- circuits/src/cpu_skeleton/stark.rs | 4 +++- circuits/src/expr.rs | 6 ++++-- circuits/src/memory/stark.rs | 4 +++- circuits/src/memory_fullword/stark.rs | 4 +++- circuits/src/memory_halfword/stark.rs | 4 +++- circuits/src/memory_zeroinit/stark.rs | 4 +++- circuits/src/memoryinit/stark.rs | 4 +++- circuits/src/ops/add/stark.rs | 4 +++- circuits/src/poseidon2/stark.rs | 4 +++- circuits/src/poseidon2_output_bytes/stark.rs | 4 +++- circuits/src/poseidon2_sponge/stark.rs | 4 +++- circuits/src/rangecheck_u8/stark.rs | 4 +++- circuits/src/register/general/stark.rs | 4 +++- circuits/src/storage_device/stark.rs | 4 +++- circuits/src/tape_commitments/stark.rs | 4 +++- circuits/src/unstark.rs | 6 ++++-- circuits/src/xor/stark.rs | 4 +++- 19 files changed, 59 insertions(+), 21 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 970fdaecb..f90f8d375 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -31,9 +31,11 @@ const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, BitshiftView>, NoColumns>> + GenerateConstraints<'a, T, BitshiftView>> for BitshiftStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 69bc7a8c1..5d89a6a46 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -79,8 +79,10 @@ const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, CpuState>, NoColumns>> for CpuStark + GenerateConstraints<'a, T, CpuState>> for CpuStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index ffc11d2e1..d47c6853b 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -31,9 +31,11 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, CpuSkeleton>, PublicInputs>> + GenerateConstraints<'a, T, CpuSkeleton>> for CpuSkeletonStark { + type PublicInputs = PublicInputs; + fn generate_constraints( vars: &StarkFrameTyped>, PublicInputs>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 9c9574c0e..b0f305b60 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -193,8 +193,10 @@ pub fn build_packed( } } -pub trait GenerateConstraints<'a, T, View, PublicInputs> { +pub trait GenerateConstraints<'a, T, View> { + type PublicInputs where E: 'a, T: 'a; + fn generate_constraints( - vars: &StarkFrameTyped, + vars: &StarkFrameTyped>>, ) -> ConstraintBuilder>; } diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index afedef0a8..9fefcf509 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -30,8 +30,10 @@ const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Memory>, NoColumns>> for MemoryStark + GenerateConstraints<'a, T, Memory>> for MemoryStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 55a3fca6c..e114662ee 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -31,9 +31,11 @@ const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, FullWordMemory>, NoColumns>> + GenerateConstraints<'a, T, FullWordMemory>> for FullWordMemoryStark { + type PublicInputs = NoColumns; + // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 665c6f39f..3bc087e98 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -27,9 +27,11 @@ impl HasNamedColumns for HalfWordMemoryStark { } impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, HalfWordMemory>, NoColumns>> + GenerateConstraints<'a, T, HalfWordMemory>> for HalfWordMemoryStark { + type PublicInputs = NoColumns; + // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 157c94d00..777c1cd73 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -30,9 +30,11 @@ const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, MemoryZeroInit>, NoColumns>> + GenerateConstraints<'a, T, MemoryZeroInit>> for MemoryZeroInitStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 4984242e5..6f1eacade 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -30,9 +30,11 @@ const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, MemoryInit>, NoColumns>> + GenerateConstraints<'a, T, MemoryInit>> for MemoryInitStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index c1bdaf40a..c99980b33 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -29,9 +29,11 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> GenerateConstraints<'a, T, Add>, NoColumns>> +impl<'a, F, T: Copy, const D: usize> GenerateConstraints<'a, T, Add>> for AddStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index e36de86a1..014f9da10 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -130,9 +130,11 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Poseidon2State>, NoColumns>> + GenerateConstraints<'a, T, Poseidon2State>> for Poseidon2_12Stark { + type PublicInputs = NoColumns; + // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something // to take into account when providing a more geneeral API to plonky. diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index d79c641be..444348a92 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -31,9 +31,11 @@ const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Poseidon2OutputBytes>, NoColumns>> + GenerateConstraints<'a, T, Poseidon2OutputBytes>> for Poseidon2OutputBytesStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 2635f2fca..8ea53708c 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -33,9 +33,11 @@ const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; impl<'a, F: Poseidon2, T: Copy, const D: usize> - GenerateConstraints<'a, T, Poseidon2Sponge>, NoColumns>> + GenerateConstraints<'a, T, Poseidon2Sponge>> for Poseidon2SpongeStark { + type PublicInputs = NoColumns; + // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 5b7863b0b..feaa95e9d 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -30,9 +30,11 @@ const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, RangeCheckU8>, NoColumns>> + GenerateConstraints<'a, T, RangeCheckU8>> for RangeCheckU8Stark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 2b800c6a7..7bb683db7 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -30,8 +30,10 @@ const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Register>, NoColumns>> for RegisterStark + GenerateConstraints<'a, T, Register>> for RegisterStark { + type PublicInputs = NoColumns; + /// Constraints for the [`RegisterStark`]: /// /// 1) `is_init`, `is_read`, `is_write`, and the virtual `is_used` column diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index d5c0d732d..d59c1fc02 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -30,9 +30,11 @@ const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, StorageDevice>, NoColumns>> + GenerateConstraints<'a, T, StorageDevice>> for StorageDeviceStark { + type PublicInputs = NoColumns; + // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 20b097728..f9d2ce15e 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -17,9 +17,11 @@ use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraint use crate::unstark::NoColumns; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, TapeCommitments>, NoColumns>> + GenerateConstraints<'a, T, TapeCommitments>> for TapeCommitmentsStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index a45c4f04c..e8dd02315 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -14,9 +14,11 @@ use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, T, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T, Columns, NoColumns>> for Unstark +impl<'a, F, T: 'a, const D: usize, Columns, const COLUMNS: usize> + GenerateConstraints<'a, T, Columns> for Unstark { + type PublicInputs = NoColumns; + fn generate_constraints( _vars: &StarkFrameTyped>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 43445e3a8..fdf22eaac 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -31,8 +31,10 @@ const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, XorColumnsView>, NoColumns>> for XorStark + GenerateConstraints<'a, T, XorColumnsView>> for XorStark { + type PublicInputs = NoColumns; + fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { From 5bf479559fe81ff59655b347799d4f440a57375a Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 10 Jun 2024 20:36:53 +0200 Subject: [PATCH 29/84] Move View to a GAT --- circuits/src/bitshift/stark.rs | 5 +++-- circuits/src/cpu/stark.rs | 5 +++-- circuits/src/cpu_skeleton/stark.rs | 5 +++-- circuits/src/expr.rs | 5 +++-- circuits/src/memory/stark.rs | 5 +++-- circuits/src/memory_fullword/stark.rs | 5 +++-- circuits/src/memory_halfword/stark.rs | 5 +++-- circuits/src/memory_zeroinit/stark.rs | 5 +++-- circuits/src/memoryinit/stark.rs | 5 +++-- circuits/src/ops/add/stark.rs | 3 ++- circuits/src/poseidon2/stark.rs | 5 +++-- circuits/src/poseidon2_output_bytes/stark.rs | 5 +++-- circuits/src/poseidon2_sponge/stark.rs | 5 +++-- circuits/src/rangecheck_u8/stark.rs | 5 +++-- circuits/src/register/general/stark.rs | 5 +++-- circuits/src/storage_device/stark.rs | 5 +++-- circuits/src/tape_commitments/stark.rs | 5 +++-- circuits/src/unstark.rs | 3 ++- circuits/src/xor/stark.rs | 5 +++-- 19 files changed, 55 insertions(+), 36 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index f90f8d375..e0071ab86 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -30,10 +30,11 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, BitshiftView>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for BitshiftStark { + type View = BitshiftView; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 5d89a6a46..2b5b3c12b 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -78,9 +78,10 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, CpuState>> for CpuStark +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for CpuStark { + type View = CpuState; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index d47c6853b..a25c72bb7 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -30,10 +30,11 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, CpuSkeleton>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for CpuSkeletonStark { + type View = CpuSkeleton; type PublicInputs = PublicInputs; fn generate_constraints( diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index b0f305b60..e345c5271 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -193,10 +193,11 @@ pub fn build_packed( } } -pub trait GenerateConstraints<'a, T, View> { +pub trait GenerateConstraints<'a, T> { + type View where E: 'a, T: 'a; type PublicInputs where E: 'a, T: 'a; fn generate_constraints( - vars: &StarkFrameTyped>>, + vars: &StarkFrameTyped>, Self::PublicInputs>>, ) -> ConstraintBuilder>; } diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index 9fefcf509..b10d383f1 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -29,9 +29,10 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Memory>> for MemoryStark +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for MemoryStark { + type View = Memory; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index e114662ee..7fa550d5c 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -30,10 +30,11 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, FullWordMemory>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for FullWordMemoryStark { + type View = FullWordMemory; type PublicInputs = NoColumns; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 3bc087e98..d6cfaa388 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -26,10 +26,11 @@ impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, HalfWordMemory>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for HalfWordMemoryStark { + type View = HalfWordMemory; type PublicInputs = NoColumns; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 777c1cd73..80c5bffdc 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -29,10 +29,11 @@ impl HasNamedColumns for MemoryZeroInitStark { const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, MemoryZeroInit>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for MemoryZeroInitStark { + type View = MemoryZeroInit; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 6f1eacade..4de70a922 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -29,10 +29,11 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, MemoryInit>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for MemoryInitStark { + type View = MemoryInit; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index c99980b33..fcf985cad 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -29,9 +29,10 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> GenerateConstraints<'a, T, Add>> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for AddStark { + type View = Add; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 014f9da10..c4ce76460 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -129,10 +129,11 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Poseidon2State>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for Poseidon2_12Stark { + type View = Poseidon2State; type PublicInputs = NoColumns; // NOTE: This one has extra constraints compared to different implementations of diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index 444348a92..f8839dff6 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -30,10 +30,11 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Poseidon2OutputBytes>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for Poseidon2OutputBytesStark { + type View = Poseidon2OutputBytes; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 8ea53708c..8a0c34fe0 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -32,10 +32,11 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: Poseidon2, T: Copy, const D: usize> - GenerateConstraints<'a, T, Poseidon2Sponge>> +impl<'a, F: Poseidon2, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for Poseidon2SpongeStark { + type View = Poseidon2Sponge; type PublicInputs = NoColumns; // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index feaa95e9d..91be5f609 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -29,10 +29,11 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, RangeCheckU8>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for RangeCheckU8Stark { + type View = RangeCheckU8; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 7bb683db7..1ed62d120 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -29,9 +29,10 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, Register>> for RegisterStark +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for RegisterStark { + type View = Register; type PublicInputs = NoColumns; /// Constraints for the [`RegisterStark`]: diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index d59c1fc02..b86a6e703 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -29,10 +29,11 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, StorageDevice>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for StorageDeviceStark { + type View = StorageDevice; type PublicInputs = NoColumns; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index f9d2ce15e..a61a80ff6 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -16,10 +16,11 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, TapeCommitments>> +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for TapeCommitmentsStark { + type View = TapeCommitments; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index e8dd02315..59674a6c7 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,8 +15,9 @@ use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl<'a, F, T: 'a, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T, Columns> for Unstark + GenerateConstraints<'a, T> for Unstark { + type View = Columns; type PublicInputs = NoColumns; fn generate_constraints( diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index fdf22eaac..e0b2742ac 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -30,9 +30,10 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy, const D: usize> - GenerateConstraints<'a, T, XorColumnsView>> for XorStark +impl<'a, F, T: Copy + 'a, const D: usize> + GenerateConstraints<'a, T> for XorStark { + type View = XorColumnsView; type PublicInputs = NoColumns; fn generate_constraints( From 585aa7bdedea0e0cdd749209a86ea11c8a1e1a0b Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 08:34:47 +0200 Subject: [PATCH 30/84] Relax to_typed_starkframe --- expr/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/expr/src/lib.rs b/expr/src/lib.rs index 49d52e2ce..64f22dd99 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -203,8 +203,8 @@ impl ExprBuilder { // We don't actually need the first constraint, but it's useful to make the compiler yell // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to // give direct access to its contents. - View: From<[Expr<'a, T>; N]> + FromIterator>, - PublicInputs: From<[Expr<'a, T>; N2]> + FromIterator>, { + View: FromIterator>, + PublicInputs: FromIterator>, { // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no // need for the reference only access. StarkFrameTyped { From 8a8b500bf935a6ba718c74b3994ed1d12e1f532e Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 08:35:21 +0200 Subject: [PATCH 31/84] Add FromIterator to GenerateConstraint's GATs --- circuits/src/expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index e345c5271..d07f6f237 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -194,8 +194,8 @@ pub fn build_packed( } pub trait GenerateConstraints<'a, T> { - type View where E: 'a, T: 'a; - type PublicInputs where E: 'a, T: 'a; + type View: FromIterator where E: 'a, T: 'a; + type PublicInputs: FromIterator where E: 'a, T: 'a; fn generate_constraints( vars: &StarkFrameTyped>, Self::PublicInputs>>, From 67697a0af4aca5fe7f5f53edb20062656114ded6 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 09:11:02 +0200 Subject: [PATCH 32/84] Fix type of PublicInputs --- expr/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/expr/src/lib.rs b/expr/src/lib.rs index 64f22dd99..20eb40509 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -204,7 +204,7 @@ impl ExprBuilder { // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to // give direct access to its contents. View: FromIterator>, - PublicInputs: FromIterator>, { + PublicInputs: FromIterator>, { // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no // need for the reference only access. StarkFrameTyped { @@ -221,7 +221,7 @@ impl ExprBuilder { public_inputs: vars .get_public_inputs() .iter() - .map(|&v| self.lit(T::from(v))) + .map(|&v| self.lit(U::from(v))) .collect(), } } From 57dc0ea42015b885a2d7fa1d54b9caba2cdfb23f Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 09:24:22 +0200 Subject: [PATCH 33/84] Revert "Fix type of PublicInputs" This reverts commit 67697a0af4aca5fe7f5f53edb20062656114ded6. --- expr/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/expr/src/lib.rs b/expr/src/lib.rs index 20eb40509..64f22dd99 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -204,7 +204,7 @@ impl ExprBuilder { // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to // give direct access to its contents. View: FromIterator>, - PublicInputs: FromIterator>, { + PublicInputs: FromIterator>, { // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no // need for the reference only access. StarkFrameTyped { @@ -221,7 +221,7 @@ impl ExprBuilder { public_inputs: vars .get_public_inputs() .iter() - .map(|&v| self.lit(U::from(v))) + .map(|&v| self.lit(T::from(v))) .collect(), } } From 5250921a664c1494fdd76a2d14c4037ba024599e Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 16:51:29 +0200 Subject: [PATCH 34/84] Add a helper function to skip StarkFrame --- expr/src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/expr/src/lib.rs b/expr/src/lib.rs index 64f22dd99..d45dbde3b 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -225,6 +225,46 @@ impl ExprBuilder { .collect(), } } + + pub fn to_typed_starkframe_<'a, T, U, View, PublicInputs>( + &'a self, + lv: &[T], + nv: &[T], + pis: &[U], + columns: usize, + public_inputs: usize, + ) -> StarkFrameTyped + where + T: Copy + Clone + Default + From + 'a, + U: Copy + Clone + Default, + // We don't actually need the first constraint, but it's useful to make the compiler yell + // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to + // give direct access to its contents. + View: FromIterator>, + PublicInputs: FromIterator>, + // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no + // need for the reference only access. + + { + assert_eq!(lv.len(), columns); + assert_eq!(nv.len(), columns); + assert_eq!(pis.len(), public_inputs); + + StarkFrameTyped { + local_values: lv + .iter() + .map(|&v| self.lit(v)) + .collect(), + next_values: nv + .iter() + .map(|&v| self.lit(v)) + .collect(), + public_inputs: pis + .iter() + .map(|&v| self.lit(T::from(v))) + .collect(), + } + } } /// A helper around `StarkFrame` to add types From 88bb433459a059469085f2577408e9640ee9937a Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 16:54:43 +0200 Subject: [PATCH 35/84] Fix Unstark --- circuits/src/unstark.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 59674a6c7..0d3637049 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -17,11 +17,11 @@ use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl<'a, F, T: 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> for Unstark { - type View = Columns; + type View = NoColumns; type PublicInputs = NoColumns; fn generate_constraints( - _vars: &StarkFrameTyped>>, + _vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { ConstraintBuilder::default() } From 1fe6ac769ce873396789c3e90f3e4d074e4cd0b7 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 16:55:54 +0200 Subject: [PATCH 36/84] Add Debug requirement to GenerateConstraints --- circuits/src/bitshift/stark.rs | 7 ++--- circuits/src/cpu/stark.rs | 7 ++--- circuits/src/cpu_skeleton/stark.rs | 7 ++--- circuits/src/expr.rs | 28 +++++++++++++++++--- circuits/src/memory/stark.rs | 7 ++--- circuits/src/memory_fullword/stark.rs | 7 ++--- circuits/src/memory_halfword/stark.rs | 7 ++--- circuits/src/memory_zeroinit/stark.rs | 7 ++--- circuits/src/memoryinit/stark.rs | 7 ++--- circuits/src/ops/add/stark.rs | 7 ++--- circuits/src/poseidon2/stark.rs | 7 ++--- circuits/src/poseidon2_output_bytes/stark.rs | 7 ++--- circuits/src/poseidon2_sponge/stark.rs | 7 ++--- circuits/src/rangecheck_u8/stark.rs | 7 ++--- circuits/src/register/general/stark.rs | 7 ++--- circuits/src/storage_device/stark.rs | 7 ++--- circuits/src/tape_commitments/stark.rs | 7 ++--- circuits/src/unstark.rs | 7 ++--- circuits/src/xor/stark.rs | 7 ++--- 19 files changed, 97 insertions(+), 57 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index e0071ab86..0c668afd8 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,12 +31,12 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for BitshiftStark { - type View = BitshiftView; - type PublicInputs = NoColumns; + type View = BitshiftView; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 2b5b3c12b..79aaded0c 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -78,11 +79,11 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for CpuStark { - type View = CpuState; - type PublicInputs = NoColumns; + type View = CpuState; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index a25c72bb7..0e00fd2df 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,12 +31,12 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for CpuSkeletonStark { - type View = CpuSkeleton; - type PublicInputs = PublicInputs; + type View = CpuSkeleton; + type PublicInputs = PublicInputs; fn generate_constraints( vars: &StarkFrameTyped>, PublicInputs>>, diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index d07f6f237..e75fa921a 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::panic::Location; pub use expr::PureEvaluator; @@ -160,6 +161,27 @@ pub fn build_ext( } } +pub fn build_debug( + cb: ConstraintBuilder> +) +where + F: RichField, + F: Extendable, + FE: FieldExtension, + P: PackedField, { + let mut evaluator = Cached::from(packed_field_evaluator()); + for constraint in cb.constraints { + let evaluated = evaluator.eval(constraint.term); + + if evaluated.is_zeros() { + log::error!( + "ConstraintConsumer - DEBUG trace (non-zero-constraint): {}", + constraint.location + ) + } + } + } + pub fn build_packed( cb: ConstraintBuilder>, yield_constr: &mut ConstraintConsumer

, @@ -193,9 +215,9 @@ pub fn build_packed( } } -pub trait GenerateConstraints<'a, T> { - type View: FromIterator where E: 'a, T: 'a; - type PublicInputs: FromIterator where E: 'a, T: 'a; +pub trait GenerateConstraints<'a, T: Debug> { + type View: FromIterator where E: 'a + Debug, T: 'a; + type PublicInputs: FromIterator where E: 'a + Debug, T: 'a; fn generate_constraints( vars: &StarkFrameTyped>, Self::PublicInputs>>, diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index b10d383f1..6c26f3e3f 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,11 +30,11 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryStark { - type View = Memory; - type PublicInputs = NoColumns; + type View = Memory; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 7fa550d5c..a64047d75 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,12 +31,12 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for FullWordMemoryStark { - type View = FullWordMemory; - type PublicInputs = NoColumns; + type View = FullWordMemory; + type PublicInputs = NoColumns; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index d6cfaa388..5c8c4d11e 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -26,12 +27,12 @@ impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for HalfWordMemoryStark { - type View = HalfWordMemory; - type PublicInputs = NoColumns; + type View = HalfWordMemory; + type PublicInputs = NoColumns; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 80c5bffdc..bc1fc97c4 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,12 +30,12 @@ impl HasNamedColumns for MemoryZeroInitStark { const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryZeroInitStark { - type View = MemoryZeroInit; - type PublicInputs = NoColumns; + type View = MemoryZeroInit; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 4de70a922..685ff09b6 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,12 +30,12 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryInitStark { - type View = MemoryInit; - type PublicInputs = NoColumns; + type View = MemoryInit; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index fcf985cad..1ff0012b2 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,11 +30,11 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for AddStark { - type View = Add; - type PublicInputs = NoColumns; + type View = Add; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index c4ce76460..b7631a611 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -129,12 +130,12 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2_12Stark { - type View = Poseidon2State; - type PublicInputs = NoColumns; + type View = Poseidon2State; + type PublicInputs = NoColumns; // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index f8839dff6..51dcf4465 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,12 +31,12 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2OutputBytesStark { - type View = Poseidon2OutputBytes; - type PublicInputs = NoColumns; + type View = Poseidon2OutputBytes; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 8a0c34fe0..074406125 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -32,12 +33,12 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: Poseidon2, T: Copy + 'a, const D: usize> +impl<'a, F: Poseidon2, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2SpongeStark { - type View = Poseidon2Sponge; - type PublicInputs = NoColumns; + type View = Poseidon2Sponge; + type PublicInputs = NoColumns; // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 91be5f609..e16074b4c 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,12 +30,12 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for RangeCheckU8Stark { - type View = RangeCheckU8; - type PublicInputs = NoColumns; + type View = RangeCheckU8; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 1ed62d120..9fd16767c 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,11 +30,11 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for RegisterStark { - type View = Register; - type PublicInputs = NoColumns; + type View = Register; + type PublicInputs = NoColumns; /// Constraints for the [`RegisterStark`]: /// diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index b86a6e703..35bdd3368 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -29,12 +30,12 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for StorageDeviceStark { - type View = StorageDevice; - type PublicInputs = NoColumns; + type View = StorageDevice; + type PublicInputs = NoColumns; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index a61a80ff6..9cad89a8f 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -16,12 +17,12 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for TapeCommitmentsStark { - type View = TapeCommitments; - type PublicInputs = NoColumns; + type View = TapeCommitments; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 0d3637049..2902a4115 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, StarkFrameTyped}; @@ -14,11 +15,11 @@ use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, T: 'a, const D: usize, Columns, const COLUMNS: usize> +impl<'a, F, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> for Unstark { - type View = NoColumns; - type PublicInputs = NoColumns; + type View = NoColumns; + type PublicInputs = NoColumns; fn generate_constraints( _vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index e0b2742ac..fbe283883 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +31,11 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for XorStark { - type View = XorColumnsView; - type PublicInputs = NoColumns; + type View = XorColumnsView; + type PublicInputs = NoColumns; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, From 1daef1703ae6aef235806e218999762b3ee51811 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 17:38:51 +0200 Subject: [PATCH 37/84] Add Debug to GenerateConstraints::View --- circuits/src/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index e75fa921a..e08b0185e 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -216,7 +216,7 @@ pub fn build_packed( } pub trait GenerateConstraints<'a, T: Debug> { - type View: FromIterator where E: 'a + Debug, T: 'a; + type View: Debug + FromIterator where E: 'a + Debug, T: 'a; type PublicInputs: FromIterator where E: 'a + Debug, T: 'a; fn generate_constraints( From ec55208277d8a40c365d5bb1dd03c4260a6d8b06 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 17:40:08 +0200 Subject: [PATCH 38/84] Remove debugging code from expr --- circuits/src/expr.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index e08b0185e..16892649a 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -197,15 +197,7 @@ pub fn build_packed( .map(|c| c.map(|constraint| evaluator.eval(constraint))) .collect::>(); - let mozak_stark_debug = std::env::var("MOZAK_STARK_DEBUG").is_ok(); for c in evaluated { - if mozak_stark_debug && !c.term.is_zeros() { - log::error!( - "ConstraintConsumer - DEBUG trace (non-zero-constraint): {}", - c.location - ); - } - (match c.constraint_type { ConstraintType::FirstRow => ConstraintConsumer::constraint_first_row, ConstraintType::Always => ConstraintConsumer::constraint, From d2f8960fc79b0a967487c839c18a6a5f79df78ea Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 17:40:35 +0200 Subject: [PATCH 39/84] Make location and term public --- circuits/src/expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 16892649a..36bcd4c51 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -71,8 +71,8 @@ where #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct Constraint { constraint_type: ConstraintType, - location: &'static Location<'static>, - term: E, + pub location: &'static Location<'static>, + pub term: E, } impl Constraint { From 1450845f233f37547e43be12513b3e5f43e2d0fb Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 17:40:59 +0200 Subject: [PATCH 40/84] Fix build_debug to just evaluate --- circuits/src/expr.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 36bcd4c51..84bfbc3bc 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -163,23 +163,17 @@ pub fn build_ext( pub fn build_debug( cb: ConstraintBuilder> -) +) -> Vec> where F: RichField, F: Extendable, FE: FieldExtension, P: PackedField, { let mut evaluator = Cached::from(packed_field_evaluator()); - for constraint in cb.constraints { - let evaluated = evaluator.eval(constraint.term); - - if evaluated.is_zeros() { - log::error!( - "ConstraintConsumer - DEBUG trace (non-zero-constraint): {}", - constraint.location - ) - } - } + cb.constraints + .into_iter() + .map(|c| c.map(|constraint| evaluator.eval(constraint))) + .collect() } pub fn build_packed( From 4fee3dbea1c620e4d2c4286dc2c40d5ffb77b4a5 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 17:41:12 +0200 Subject: [PATCH 41/84] Finish debug_single_trace --- circuits/src/generation.rs | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 1ceefe405..08100e1a4 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -4,6 +4,7 @@ use std::borrow::Borrow; use std::fmt::{Debug, Display}; +use expr::ExprBuilder; use itertools::{izip, Itertools}; use log::debug; use mozak_runner::elf::Program; @@ -14,15 +15,12 @@ use plonky2::field::polynomial::PolynomialValues; use plonky2::hash::hash_types::RichField; use plonky2::util::timing::TimingTree; use plonky2::util::transpose; -use starky::constraint_consumer::ConstraintConsumer; -use starky::evaluation_frame::StarkEvaluationFrame; use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; -use crate::columns_view::HasNamedColumns; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -// use crate::expr::GenerateConstraints; +use crate::expr::{build_debug, GenerateConstraints}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -218,29 +216,44 @@ pub fn debug_single_trace< 'a, F: RichField + Extendable + Debug, const D: usize, - S: Stark + Display + HasNamedColumns, // + GenerateConstraints<'a, F, S::Columns>, + S: Stark + Display, >( stark: &'a S, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where - S::Columns: FromIterator + Debug, { + for <'b> S: GenerateConstraints<'b, F>, + { + type View<'a, S, F> = >::View; transpose_polys::(trace_rows.to_vec()) .iter() .enumerate() .circular_tuple_windows() .for_each(|((lv_row, lv), (nv_row, nv))| { - let mut consumer = ConstraintConsumer::new_debug_api(lv_row == 0, nv_row == 0); - let vars = - StarkEvaluationFrame::from_values(lv.as_slice(), nv.as_slice(), public_inputs); - stark.eval_packed_generic(&vars, &mut consumer); - if consumer.debug_api_has_constraint_failed() { - let lv: S::Columns = lv.iter().copied().collect(); - let nv: S::Columns = nv.iter().copied().collect(); + let expr_builder = ExprBuilder::default(); + let vars = &expr_builder.to_typed_starkframe_(lv.as_slice(), nv.as_slice(), public_inputs, S::COLUMNS, S::PUBLIC_INPUTS); + let constraints = S::generate_constraints(vars); + let evaluated = build_debug(constraints); + + let failed_locations: Vec<&std::panic::Location<'_>>= + evaluated.into_iter() + .filter(|c| c.term.is_zeros()) + .map(|c| c.location) + .collect(); + + let any_failed = !failed_locations.is_empty(); + + if any_failed { + for loc in failed_locations.into_iter() { + log::error!("debug_single_trace :: (non-zero-constraint): {}", loc) + } + + let lv: View = lv.iter().copied().collect(); + let nv: View = nv.iter().copied().collect(); log::error!("Debug constraints for {stark}"); log::error!("lv-row[{lv_row}] - values: {lv:?}"); log::error!("nv-row[{nv_row}] - values: {nv:?}"); } - assert!(!consumer.debug_api_has_constraint_failed()); + assert!(!any_failed); }); } From 340425bc2bdce4cc481a3ab63533864b3f051641 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 17:47:09 +0200 Subject: [PATCH 42/84] Fix indentation --- circuits/src/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 08100e1a4..57273671f 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -224,7 +224,7 @@ pub fn debug_single_trace< ) where for <'b> S: GenerateConstraints<'b, F>, { - type View<'a, S, F> = >::View; + type View<'a, S, F> = >::View; transpose_polys::(trace_rows.to_vec()) .iter() .enumerate() From cfd544c349d84e35caed9ef9acb45732fa89b80d Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 11 Jun 2024 19:38:18 +0200 Subject: [PATCH 43/84] Whoops --- circuits/src/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 57273671f..cf984548f 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -237,7 +237,7 @@ pub fn debug_single_trace< let failed_locations: Vec<&std::panic::Location<'_>>= evaluated.into_iter() - .filter(|c| c.term.is_zeros()) + .filter(|c| !c.term.is_zeros()) .map(|c| c.location) .collect(); From 681fc125ef80667a6aacc1e8b333ed7a1d77b236 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Wed, 12 Jun 2024 16:01:34 +0200 Subject: [PATCH 44/84] Make ConstraintType public --- circuits/src/expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 84bfbc3bc..91e355a55 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -70,7 +70,7 @@ where #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct Constraint { - constraint_type: ConstraintType, + pub constraint_type: ConstraintType, pub location: &'static Location<'static>, pub term: E, } @@ -88,7 +88,7 @@ impl Constraint { } #[derive(PartialEq, Eq, PartialOrd, Ord, Default, Debug)] -enum ConstraintType { +pub enum ConstraintType { FirstRow, #[default] Always, From c68459cb18a3de4514127a71645563a76774b4dc Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Wed, 12 Jun 2024 16:04:35 +0200 Subject: [PATCH 45/84] Handle FromIterator when Column is empty ArrayVec seems to panic when trying to collect into an ArrayVec of length 0 --- circuits/src/columns_view.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index b2daf2eef..e1616409f 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -173,7 +173,11 @@ macro_rules! columns_view_impl { impl std::iter::FromIterator for $s { fn from_iter>(iter: I) -> Self { const LEN: usize = std::mem::size_of::<$s>(); - let vec: arrayvec::ArrayVec = iter.into_iter().collect(); + let vec: arrayvec::ArrayVec = if LEN == 0 { + arrayvec::ArrayVec::new() + } else { + iter.into_iter().collect() + }; let array = vec.into_inner().expect("iterator of correct length"); Self::from_array(array) } From 7c2296ee78473ceca5d955976d2b5fa269718399 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Wed, 12 Jun 2024 16:12:03 +0200 Subject: [PATCH 46/84] Filter out when constraints are applicable --- circuits/src/generation.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index cf984548f..c9a5c6d45 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, GenerateConstraints}; +use crate::expr::{build_debug, GenerateConstraints, ConstraintType}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -235,17 +235,30 @@ pub fn debug_single_trace< let constraints = S::generate_constraints(vars); let evaluated = build_debug(constraints); - let failed_locations: Vec<&std::panic::Location<'_>>= - evaluated.into_iter() + // Filter out only applicable constraints + let is_first_row = lv_row == 0; + let is_last_row = nv_row == 0; + let applicable = evaluated.into_iter() + .filter( |c| + match c.constraint_type { + ConstraintType::FirstRow => is_first_row, + ConstraintType::Always => true, + ConstraintType::Transition => !is_last_row, + ConstraintType::LastRow => is_last_row, + } + ); + + // Get failed constraints + let failed: Vec<_>= + applicable .filter(|c| !c.term.is_zeros()) - .map(|c| c.location) .collect(); - let any_failed = !failed_locations.is_empty(); + let any_failed = !failed.is_empty(); if any_failed { - for loc in failed_locations.into_iter() { - log::error!("debug_single_trace :: (non-zero-constraint): {}", loc) + for c in failed.into_iter() { + log::error!("debug_single_trace :: non-zero constraint at {} = {}", c.location, c.term) } let lv: View = lv.iter().copied().collect(); @@ -254,6 +267,7 @@ pub fn debug_single_trace< log::error!("lv-row[{lv_row}] - values: {lv:?}"); log::error!("nv-row[{nv_row}] - values: {nv:?}"); } + assert!(!any_failed); }); } From d9f3652f13aa0b7a0191a3ff74316d2a140d163f Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 13 Jun 2024 08:57:37 +0200 Subject: [PATCH 47/84] cargo clippy --- circuits/src/expr.rs | 32 ++++++++++++++++++++++---------- circuits/src/generation.rs | 8 ++++---- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 91e355a55..1bdbd8d06 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -161,20 +161,21 @@ pub fn build_ext( } } +#[must_use] pub fn build_debug( - cb: ConstraintBuilder> + cb: ConstraintBuilder>, ) -> Vec> where F: RichField, F: Extendable, FE: FieldExtension, P: PackedField, { - let mut evaluator = Cached::from(packed_field_evaluator()); - cb.constraints - .into_iter() - .map(|c| c.map(|constraint| evaluator.eval(constraint))) - .collect() - } + let mut evaluator = Cached::from(packed_field_evaluator()); + cb.constraints + .into_iter() + .map(|c| c.map(|constraint| evaluator.eval(constraint))) + .collect() +} pub fn build_packed( cb: ConstraintBuilder>, @@ -201,11 +202,22 @@ pub fn build_packed( } } +type Vars<'a, F, T> = StarkFrameTyped< + >::View>, + >::PublicInputs>, +>; + pub trait GenerateConstraints<'a, T: Debug> { - type View: Debug + FromIterator where E: 'a + Debug, T: 'a; - type PublicInputs: FromIterator where E: 'a + Debug, T: 'a; + type View: Debug + FromIterator + where + E: 'a + Debug, + T: 'a; + type PublicInputs: FromIterator + where + E: 'a + Debug, + T: 'a; fn generate_constraints( - vars: &StarkFrameTyped>, Self::PublicInputs>>, + vars: &Vars<'a, Self, T>, ) -> ConstraintBuilder>; } diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index c9a5c6d45..66e00840f 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -216,13 +216,13 @@ pub fn debug_single_trace< 'a, F: RichField + Extendable + Debug, const D: usize, - S: Stark + Display, + S >( stark: &'a S, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where - for <'b> S: GenerateConstraints<'b, F>, + for <'b> S: Stark + Display + GenerateConstraints<'b, F>, { type View<'a, S, F> = >::View; transpose_polys::(trace_rows.to_vec()) @@ -257,8 +257,8 @@ pub fn debug_single_trace< let any_failed = !failed.is_empty(); if any_failed { - for c in failed.into_iter() { - log::error!("debug_single_trace :: non-zero constraint at {} = {}", c.location, c.term) + for c in failed { + log::error!("debug_single_trace :: non-zero constraint at {} = {}", c.location, c.term); } let lv: View = lv.iter().copied().collect(); From 702a64f4a1bdf0c858c2630195851489c1170e96 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 13 Jun 2024 08:59:08 +0200 Subject: [PATCH 48/84] cargo fmt --- circuits/src/bitshift/stark.rs | 5 +-- circuits/src/cpu/stark.rs | 6 +-- circuits/src/cpu_skeleton/stark.rs | 5 +-- circuits/src/expr.rs | 4 +- circuits/src/generation.rs | 46 ++++++++++---------- circuits/src/memory/stark.rs | 6 +-- circuits/src/memory_fullword/stark.rs | 5 +-- circuits/src/memory_halfword/stark.rs | 5 +-- circuits/src/memory_zeroinit/stark.rs | 5 +-- circuits/src/memoryinit/stark.rs | 5 +-- circuits/src/ops/add/stark.rs | 2 +- circuits/src/poseidon2/stark.rs | 5 +-- circuits/src/poseidon2_output_bytes/stark.rs | 5 +-- circuits/src/poseidon2_sponge/stark.rs | 5 +-- circuits/src/rangecheck_u8/stark.rs | 5 +-- circuits/src/register/general/stark.rs | 6 +-- circuits/src/storage_device/stark.rs | 5 +-- circuits/src/tape_commitments/stark.rs | 5 +-- circuits/src/unstark.rs | 6 +-- circuits/src/xor/stark.rs | 6 +-- 20 files changed, 63 insertions(+), 79 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 0c668afd8..b763a19dd 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -31,12 +31,11 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for BitshiftStark { - type View = BitshiftView; type PublicInputs = NoColumns; + type View = BitshiftView; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 79aaded0c..07f080baa 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -79,11 +79,11 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> for CpuStark +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> + for CpuStark { - type View = CpuState; type PublicInputs = NoColumns; + type View = CpuState; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index 0e00fd2df..d71ef3b73 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -31,12 +31,11 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for CpuSkeletonStark { - type View = CpuSkeleton; type PublicInputs = PublicInputs; + type View = CpuSkeleton; fn generate_constraints( vars: &StarkFrameTyped>, PublicInputs>>, diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 1bdbd8d06..2633544b8 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -217,7 +217,5 @@ pub trait GenerateConstraints<'a, T: Debug> { E: 'a + Debug, T: 'a; - fn generate_constraints( - vars: &Vars<'a, Self, T>, - ) -> ConstraintBuilder>; + fn generate_constraints(vars: &Vars<'a, Self, T>) -> ConstraintBuilder>; } diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 66e00840f..e45e81afc 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, GenerateConstraints, ConstraintType}; +use crate::expr::{build_debug, ConstraintType, GenerateConstraints}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -212,18 +212,12 @@ pub fn debug_traces, const D: usize>( }); } -pub fn debug_single_trace< - 'a, - F: RichField + Extendable + Debug, - const D: usize, - S ->( +pub fn debug_single_trace<'a, F: RichField + Extendable + Debug, const D: usize, S>( stark: &'a S, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where - for <'b> S: Stark + Display + GenerateConstraints<'b, F>, - { + for<'b> S: Stark + Display + GenerateConstraints<'b, F>, { type View<'a, S, F> = >::View; transpose_polys::(trace_rows.to_vec()) .iter() @@ -231,34 +225,38 @@ pub fn debug_single_trace< .circular_tuple_windows() .for_each(|((lv_row, lv), (nv_row, nv))| { let expr_builder = ExprBuilder::default(); - let vars = &expr_builder.to_typed_starkframe_(lv.as_slice(), nv.as_slice(), public_inputs, S::COLUMNS, S::PUBLIC_INPUTS); + let vars = &expr_builder.to_typed_starkframe_( + lv.as_slice(), + nv.as_slice(), + public_inputs, + S::COLUMNS, + S::PUBLIC_INPUTS, + ); let constraints = S::generate_constraints(vars); let evaluated = build_debug(constraints); // Filter out only applicable constraints let is_first_row = lv_row == 0; let is_last_row = nv_row == 0; - let applicable = evaluated.into_iter() - .filter( |c| - match c.constraint_type { - ConstraintType::FirstRow => is_first_row, - ConstraintType::Always => true, - ConstraintType::Transition => !is_last_row, - ConstraintType::LastRow => is_last_row, - } - ); + let applicable = evaluated.into_iter().filter(|c| match c.constraint_type { + ConstraintType::FirstRow => is_first_row, + ConstraintType::Always => true, + ConstraintType::Transition => !is_last_row, + ConstraintType::LastRow => is_last_row, + }); // Get failed constraints - let failed: Vec<_>= - applicable - .filter(|c| !c.term.is_zeros()) - .collect(); + let failed: Vec<_> = applicable.filter(|c| !c.term.is_zeros()).collect(); let any_failed = !failed.is_empty(); if any_failed { for c in failed { - log::error!("debug_single_trace :: non-zero constraint at {} = {}", c.location, c.term); + log::error!( + "debug_single_trace :: non-zero constraint at {} = {}", + c.location, + c.term + ); } let lv: View = lv.iter().copied().collect(); diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index 6c26f3e3f..2a7fe8761 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -30,11 +30,11 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> for MemoryStark +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> + for MemoryStark { - type View = Memory; type PublicInputs = NoColumns; + type View = Memory; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index a64047d75..84d374254 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -31,12 +31,11 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for FullWordMemoryStark { - type View = FullWordMemory; type PublicInputs = NoColumns; + type View = FullWordMemory; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 5c8c4d11e..24535ceea 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -27,12 +27,11 @@ impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for HalfWordMemoryStark { - type View = HalfWordMemory; type PublicInputs = NoColumns; + type View = HalfWordMemory; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index bc1fc97c4..485509b21 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -30,12 +30,11 @@ impl HasNamedColumns for MemoryZeroInitStark { const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryZeroInitStark { - type View = MemoryZeroInit; type PublicInputs = NoColumns; + type View = MemoryZeroInit; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 685ff09b6..1afc2104f 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -30,12 +30,11 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryInitStark { - type View = MemoryInit; type PublicInputs = NoColumns; + type View = MemoryInit; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 1ff0012b2..33b252d46 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -33,8 +33,8 @@ const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for AddStark { - type View = Add; type PublicInputs = NoColumns; + type View = Add; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index b7631a611..4a49d9b0e 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -130,12 +130,11 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2_12Stark { - type View = Poseidon2State; type PublicInputs = NoColumns; + type View = Poseidon2State; // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index 51dcf4465..756fc1bec 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -31,12 +31,11 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2OutputBytesStark { - type View = Poseidon2OutputBytes; type PublicInputs = NoColumns; + type View = Poseidon2OutputBytes; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 074406125..8aa8d53d2 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -33,12 +33,11 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: Poseidon2, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F: Poseidon2, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2SpongeStark { - type View = Poseidon2Sponge; type PublicInputs = NoColumns; + type View = Poseidon2Sponge; // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index e16074b4c..5ca938c28 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -30,12 +30,11 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for RangeCheckU8Stark { - type View = RangeCheckU8; type PublicInputs = NoColumns; + type View = RangeCheckU8; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 9fd16767c..30f777974 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -30,11 +30,11 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> for RegisterStark +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> + for RegisterStark { - type View = Register; type PublicInputs = NoColumns; + type View = Register; /// Constraints for the [`RegisterStark`]: /// diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 35bdd3368..4966a34ed 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -30,12 +30,11 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for StorageDeviceStark { - type View = StorageDevice; type PublicInputs = NoColumns; + type View = StorageDevice; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 9cad89a8f..d7b0b84ea 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -17,12 +17,11 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for TapeCommitmentsStark { - type View = TapeCommitments; type PublicInputs = NoColumns; + type View = TapeCommitments; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 2902a4115..c2dee28fd 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,11 +15,11 @@ use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T> for Unstark +impl<'a, F, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> + for Unstark { - type View = NoColumns; type PublicInputs = NoColumns; + type View = NoColumns; fn generate_constraints( _vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index fbe283883..60db7e8ae 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -31,11 +31,11 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> - GenerateConstraints<'a, T> for XorStark +impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> + for XorStark { - type View = XorColumnsView; type PublicInputs = NoColumns; + type View = XorColumnsView; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, From 88c6b6d702870ba4260a5d623b7df717ee70dd61 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 13 Jun 2024 10:11:32 +0200 Subject: [PATCH 49/84] Fix Display instance for Unstark It will now properly display what kind of stark it is. --- circuits/src/ops/blt_taken/stark.rs | 6 ++- circuits/src/program/stark.rs | 6 ++- circuits/src/program_multiplicities/stark.rs | 6 ++- circuits/src/rangecheck/stark.rs | 6 ++- circuits/src/register/init/stark.rs | 6 ++- circuits/src/register/zero_read/stark.rs | 6 ++- circuits/src/register/zero_write/stark.rs | 6 ++- circuits/src/unstark.rs | 40 ++++++++++++++++---- 8 files changed, 60 insertions(+), 22 deletions(-) diff --git a/circuits/src/ops/blt_taken/stark.rs b/circuits/src/ops/blt_taken/stark.rs index 15f6b8e31..aa07de3c0 100644 --- a/circuits/src/ops/blt_taken/stark.rs +++ b/circuits/src/ops/blt_taken/stark.rs @@ -1,7 +1,9 @@ use super::columns::BltTaken; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, BltTakenStark); #[allow(clippy::module_name_repetitions)] pub type BltTakenStark = - Unstark, { BltTaken::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { BltTaken::<()>::NUMBER_OF_COLUMNS }>; diff --git a/circuits/src/program/stark.rs b/circuits/src/program/stark.rs index 89c4e4396..32cde5f1f 100644 --- a/circuits/src/program/stark.rs +++ b/circuits/src/program/stark.rs @@ -1,7 +1,9 @@ use super::columns::ProgramRom; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, ProgramStark); #[allow(clippy::module_name_repetitions)] pub type ProgramStark = - Unstark, { ProgramRom::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { ProgramRom::<()>::NUMBER_OF_COLUMNS }>; diff --git a/circuits/src/program_multiplicities/stark.rs b/circuits/src/program_multiplicities/stark.rs index 8737e4836..c8a066573 100644 --- a/circuits/src/program_multiplicities/stark.rs +++ b/circuits/src/program_multiplicities/stark.rs @@ -1,7 +1,9 @@ use super::columns::ProgramMult; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, ProgramMultStark); #[allow(clippy::module_name_repetitions)] pub type ProgramMultStark = - Unstark, { ProgramMult::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { ProgramMult::<()>::NUMBER_OF_COLUMNS }>; diff --git a/circuits/src/rangecheck/stark.rs b/circuits/src/rangecheck/stark.rs index da9d1865c..e2caadc79 100644 --- a/circuits/src/rangecheck/stark.rs +++ b/circuits/src/rangecheck/stark.rs @@ -1,10 +1,12 @@ use super::columns::RangeCheckColumnsView; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, RangeCheckStark); #[allow(clippy::module_name_repetitions)] pub type RangeCheckStark = - Unstark, { RangeCheckColumnsView::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { RangeCheckColumnsView::<()>::NUMBER_OF_COLUMNS }>; #[cfg(test)] mod tests { diff --git a/circuits/src/register/init/stark.rs b/circuits/src/register/init/stark.rs index 0f4e497ac..b589b34c3 100644 --- a/circuits/src/register/init/stark.rs +++ b/circuits/src/register/init/stark.rs @@ -1,10 +1,12 @@ use super::columns::RegisterInit; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, RegisterInitStark); /// For sanity check, we can constrain the register address column to be in /// a running sum from 0..=31, but since this fixed table is known to /// both prover and verifier, we do not need to do so here. #[allow(clippy::module_name_repetitions)] pub type RegisterInitStark = - Unstark, { RegisterInit::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { RegisterInit::<()>::NUMBER_OF_COLUMNS }>; diff --git a/circuits/src/register/zero_read/stark.rs b/circuits/src/register/zero_read/stark.rs index b6975b97b..4e734f59f 100644 --- a/circuits/src/register/zero_read/stark.rs +++ b/circuits/src/register/zero_read/stark.rs @@ -1,7 +1,9 @@ use super::columns::RegisterZeroRead; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, RegisterZeroReadStark); #[allow(clippy::module_name_repetitions)] pub type RegisterZeroReadStark = - Unstark, { RegisterZeroRead::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { RegisterZeroRead::<()>::NUMBER_OF_COLUMNS }>; diff --git a/circuits/src/register/zero_write/stark.rs b/circuits/src/register/zero_write/stark.rs index a5dfa90ec..5b04b4744 100644 --- a/circuits/src/register/zero_write/stark.rs +++ b/circuits/src/register/zero_write/stark.rs @@ -1,7 +1,9 @@ use super::columns::RegisterZeroWrite; use crate::columns_view::NumberOfColumns; -use crate::unstark::Unstark; +use crate::unstark::{impl_name, Unstark}; + +impl_name!(N, RegisterZeroWriteStark); #[allow(clippy::module_name_repetitions)] pub type RegisterZeroWriteStark = - Unstark, { RegisterZeroWrite::<()>::NUMBER_OF_COLUMNS }>; + Unstark, { RegisterZeroWrite::<()>::NUMBER_OF_COLUMNS }>; diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index c2dee28fd..92fcf0d8f 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -1,8 +1,8 @@ use core::fmt::Debug; +use std::fmt::Display; use std::marker::PhantomData; use expr::{Expr, StarkFrameTyped}; -use mozak_circuits_derive::StarkNameDisplay; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; @@ -15,8 +15,8 @@ use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> - for Unstark +impl<'a, F, NAME, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> + GenerateConstraints<'a, T> for Unstark { type PublicInputs = NoColumns; type View = NoColumns; @@ -31,15 +31,24 @@ impl<'a, F, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> Genera /// Template for a STARK with zero internal constraints. Use this if the STARK /// itself does not need any built-in constraints, but rely on cross table /// lookups for provability. -#[derive(Copy, Clone, Default, StarkNameDisplay)] +#[derive(Copy, Clone, Default)] #[allow(clippy::module_name_repetitions)] -pub struct Unstark { +pub struct Unstark { pub _f: PhantomData, + pub _name: PhantomData, pub _d: PhantomData, } -impl HasNamedColumns - for Unstark +impl Display + for Unstark +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", NAME::default()) + } +} + +impl HasNamedColumns + for Unstark { type Columns = Columns; } @@ -48,10 +57,11 @@ const PUBLIC_INPUTS: usize = 0; impl< F: RichField + Extendable, + NAME: Sync, const D: usize, Columns: Sync + NumberOfColumns, const COLUMNS: usize, - > Stark for Unstark + > Stark for Unstark { type EvaluationFrame = StarkFrame @@ -81,6 +91,20 @@ impl< fn constraint_degree(&self) -> usize { 3 } } +// Simple marco to create a type holding the name for the Unstark +macro_rules! impl_name { + ($alias:ident, $name:ident) => { + mod name { + #[derive(Default, Debug, Clone, Copy)] + pub struct $name {} + } + + use name::$name as $alias; + } +} + +pub(crate) use impl_name; + #[repr(C)] #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub struct NoColumns { From 0199021ddacdb9d7f25ae856874afd1cb8468f33 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 13 Jun 2024 10:12:47 +0200 Subject: [PATCH 50/84] cargo fmt --- expr/src/lib.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/expr/src/lib.rs b/expr/src/lib.rs index d45dbde3b..a971068d0 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -244,25 +244,15 @@ impl ExprBuilder { PublicInputs: FromIterator>, // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no // need for the reference only access. - { assert_eq!(lv.len(), columns); assert_eq!(nv.len(), columns); assert_eq!(pis.len(), public_inputs); StarkFrameTyped { - local_values: lv - .iter() - .map(|&v| self.lit(v)) - .collect(), - next_values: nv - .iter() - .map(|&v| self.lit(v)) - .collect(), - public_inputs: pis - .iter() - .map(|&v| self.lit(T::from(v))) - .collect(), + local_values: lv.iter().map(|&v| self.lit(v)).collect(), + next_values: nv.iter().map(|&v| self.lit(v)).collect(), + public_inputs: pis.iter().map(|&v| self.lit(T::from(v))).collect(), } } } From 5b83f1139db2dff849f51fa9333803120479d2c8 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 13 Jun 2024 10:15:14 +0200 Subject: [PATCH 51/84] Another cargo fmt --- circuits/src/unstark.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 92fcf0d8f..27fb5e929 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -100,7 +100,7 @@ macro_rules! impl_name { } use name::$name as $alias; - } + }; } pub(crate) use impl_name; From 7deefcd864dde3b3f44a917d6679f9bca50fefcd Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 13 Jun 2024 20:42:32 +0200 Subject: [PATCH 52/84] Simplify instances --- circuits/src/bitshift/stark.rs | 9 +++----- circuits/src/cpu/stark.rs | 9 +++----- circuits/src/cpu_skeleton/stark.rs | 7 +++---- circuits/src/expr.rs | 22 ++++++++------------ circuits/src/generation.rs | 17 ++++++++------- circuits/src/memory/stark.rs | 9 +++----- circuits/src/memory_fullword/stark.rs | 7 +++---- circuits/src/memory_halfword/stark.rs | 7 +++---- circuits/src/memory_zeroinit/stark.rs | 4 ++-- circuits/src/memoryinit/stark.rs | 9 +++----- circuits/src/ops/add/stark.rs | 9 +++----- circuits/src/poseidon2/stark.rs | 7 +++---- circuits/src/poseidon2_output_bytes/stark.rs | 7 +++---- circuits/src/poseidon2_sponge/stark.rs | 7 +++---- circuits/src/rangecheck_u8/stark.rs | 7 +++---- circuits/src/register/general/stark.rs | 9 +++----- circuits/src/storage_device/stark.rs | 7 +++---- circuits/src/tape_commitments/stark.rs | 7 +++---- circuits/src/unstark.rs | 8 +++---- circuits/src/xor/stark.rs | 9 +++----- 20 files changed, 73 insertions(+), 104 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index b763a19dd..ef877fe49 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -31,11 +30,9 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for BitshiftStark -{ - type PublicInputs = NoColumns; - type View = BitshiftView; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for BitshiftStark { + type PublicInputs = NoColumns; + type View = BitshiftView; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 07f080baa..4ae3a57a3 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -79,11 +78,9 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for CpuStark -{ - type PublicInputs = NoColumns; - type View = CpuState; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for CpuStark { + type PublicInputs = NoColumns; + type View = CpuState; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index d71ef3b73..8a75b73b0 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -31,11 +30,11 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for CpuSkeletonStark { - type PublicInputs = PublicInputs; - type View = CpuSkeleton; + type PublicInputs = PublicInputs; + type View = CpuSkeleton; fn generate_constraints( vars: &StarkFrameTyped>, PublicInputs>>, diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 2633544b8..e28fb15eb 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -202,20 +202,16 @@ pub fn build_packed( } } -type Vars<'a, F, T> = StarkFrameTyped< - >::View>, - >::PublicInputs>, ->; +// Helper Types to Access members of GenerateConstraints +pub type PublicInputsOf<'a, S, F, T> = >::PublicInputs; +pub type ViewOf<'a, S, F, T> = >::View; -pub trait GenerateConstraints<'a, T: Debug> { - type View: Debug + FromIterator - where - E: 'a + Debug, - T: 'a; - type PublicInputs: FromIterator - where - E: 'a + Debug, - T: 'a; +type Vars<'a, S, T> = + StarkFrameTyped>, PublicInputsOf<'a, S, T, Expr<'a, T>>>; + +pub trait GenerateConstraints<'a, T: 'a> { + type View; + type PublicInputs; fn generate_constraints(vars: &Vars<'a, Self, T>) -> ConstraintBuilder>; } diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index e45e81afc..5e8957143 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -4,7 +4,7 @@ use std::borrow::Borrow; use std::fmt::{Debug, Display}; -use expr::ExprBuilder; +use expr::{Expr, ExprBuilder}; use itertools::{izip, Itertools}; use log::debug; use mozak_runner::elf::Program; @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, ConstraintType, GenerateConstraints}; +use crate::expr::{build_debug, ConstraintType, GenerateConstraints, PublicInputsOf, ViewOf}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -212,13 +212,16 @@ pub fn debug_traces, const D: usize>( }); } -pub fn debug_single_trace<'a, F: RichField + Extendable + Debug, const D: usize, S>( +pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S>( stark: &'a S, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where - for<'b> S: Stark + Display + GenerateConstraints<'b, F>, { - type View<'a, S, F> = >::View; + for<'b> S: Stark + Display + GenerateConstraints<'b, F>, + for<'b> PublicInputsOf<'b, S, F, Expr<'b, F>>: FromIterator>, + for<'b> ViewOf<'b, S, F, Expr<'b, F>>: FromIterator>, + for<'b> PublicInputsOf<'b, S, F, F>: Debug + FromIterator, + for<'b> ViewOf<'b, S, F, F>: Debug + FromIterator, { transpose_polys::(trace_rows.to_vec()) .iter() .enumerate() @@ -259,8 +262,8 @@ pub fn debug_single_trace<'a, F: RichField + Extendable + Debug, const D: usi ); } - let lv: View = lv.iter().copied().collect(); - let nv: View = nv.iter().copied().collect(); + let lv: ViewOf = lv.iter().copied().collect(); + let nv: ViewOf = nv.iter().copied().collect(); log::error!("Debug constraints for {stark}"); log::error!("lv-row[{lv_row}] - values: {lv:?}"); log::error!("nv-row[{nv_row}] - values: {nv:?}"); diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index 2a7fe8761..17c96e5f1 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +29,9 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for MemoryStark -{ - type PublicInputs = NoColumns; - type View = Memory; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryStark { + type PublicInputs = NoColumns; + type View = Memory; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 84d374254..8f4eadbb4 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -31,11 +30,11 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for FullWordMemoryStark { - type PublicInputs = NoColumns; - type View = FullWordMemory; + type PublicInputs = NoColumns; + type View = FullWordMemory; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 24535ceea..e6629d86b 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -27,11 +26,11 @@ impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for HalfWordMemoryStark { - type PublicInputs = NoColumns; - type View = HalfWordMemory; + type PublicInputs = NoColumns; + type View = HalfWordMemory; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 485509b21..97755e961 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -33,8 +33,8 @@ const PUBLIC_INPUTS: usize = 0; impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryZeroInitStark { - type PublicInputs = NoColumns; - type View = MemoryZeroInit; + type PublicInputs = NoColumns; + type View = MemoryZeroInit; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 1afc2104f..27439c83e 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +29,9 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for MemoryInitStark -{ - type PublicInputs = NoColumns; - type View = MemoryInit; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryInitStark { + type PublicInputs = NoColumns; + type View = MemoryInit; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 33b252d46..2caaf19d5 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +29,9 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for AddStark -{ - type PublicInputs = NoColumns; - type View = Add; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for AddStark { + type PublicInputs = NoColumns; + type View = Add; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 4a49d9b0e..a74a6b4b2 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -130,11 +129,11 @@ const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; fn from_u64(u: u64) -> i64 { GoldilocksField::from_noncanonical_u64(u).to_canonical_i64() } -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2_12Stark { - type PublicInputs = NoColumns; - type View = Poseidon2State; + type PublicInputs = NoColumns; + type View = Poseidon2State; // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index 756fc1bec..d167b873e 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -31,11 +30,11 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2OutputBytesStark { - type PublicInputs = NoColumns; - type View = Poseidon2OutputBytes; + type PublicInputs = NoColumns; + type View = Poseidon2OutputBytes; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 8aa8d53d2..9c2f8358b 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -33,11 +32,11 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: Poseidon2, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F: Poseidon2, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for Poseidon2SpongeStark { - type PublicInputs = NoColumns; - type View = Poseidon2Sponge; + type PublicInputs = NoColumns; + type View = Poseidon2Sponge; // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 5ca938c28..7974ef406 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +29,11 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for RangeCheckU8Stark { - type PublicInputs = NoColumns; - type View = RangeCheckU8; + type PublicInputs = NoColumns; + type View = RangeCheckU8; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 30f777974..5c68831d5 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +29,9 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for RegisterStark -{ - type PublicInputs = NoColumns; - type View = Register; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for RegisterStark { + type PublicInputs = NoColumns; + type View = Register; /// Constraints for the [`RegisterStark`]: /// diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 4966a34ed..4dcffb266 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -30,11 +29,11 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for StorageDeviceStark { - type PublicInputs = NoColumns; - type View = StorageDevice; + type PublicInputs = NoColumns; + type View = StorageDevice; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints( diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index d7b0b84ea..9721b42c1 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -17,11 +16,11 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; use crate::unstark::NoColumns; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for TapeCommitmentsStark { - type PublicInputs = NoColumns; - type View = TapeCommitments; + type PublicInputs = NoColumns; + type View = TapeCommitments; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 27fb5e929..c5a6ee20d 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,11 +15,11 @@ use starky::stark::Stark; use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, NAME, T: Debug + 'a, const D: usize, Columns, const COLUMNS: usize> - GenerateConstraints<'a, T> for Unstark +impl<'a, F, NAME, T: 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> + for Unstark { - type PublicInputs = NoColumns; - type View = NoColumns; + type PublicInputs = NoColumns; + type View = NoColumns; fn generate_constraints( _vars: &StarkFrameTyped>, NoColumns>>, diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 60db7e8ae..418fdbe9a 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -1,4 +1,3 @@ -use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -31,11 +30,9 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for XorStark -{ - type PublicInputs = NoColumns; - type View = XorColumnsView; +impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for XorStark { + type PublicInputs = NoColumns; + type View = XorColumnsView; fn generate_constraints( vars: &StarkFrameTyped>, NoColumns>>, From 40a4d373fa6d0a92f7f427d9a5352c347f19c305 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 18 Jun 2024 14:15:46 +0200 Subject: [PATCH 53/84] Generalise NoColumns to ShadowColumns Adds ShadowColumns which can hold N columns, so that NoColumns is redefined as ShadowColumns. Removes the previous hack on FromIterator. Unfortunately, needed to manually expand columns_view_impl! macro for ShadowColumns. --- circuits/src/columns_view.rs | 6 +- circuits/src/unstark.rs | 183 +++++++++++++++++++++++++++++++++-- 2 files changed, 178 insertions(+), 11 deletions(-) diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index e1616409f..b2daf2eef 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -173,11 +173,7 @@ macro_rules! columns_view_impl { impl std::iter::FromIterator for $s { fn from_iter>(iter: I) -> Self { const LEN: usize = std::mem::size_of::<$s>(); - let vec: arrayvec::ArrayVec = if LEN == 0 { - arrayvec::ArrayVec::new() - } else { - iter.into_iter().collect() - }; + let vec: arrayvec::ArrayVec = iter.into_iter().collect(); let array = vec.into_inner().expect("iterator of correct length"); Self::from_array(array) } diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index c5a6ee20d..e6ebc67be 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -12,17 +12,17 @@ use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsume use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; -use crate::columns_view::{columns_view_impl, HasNamedColumns, NumberOfColumns}; +use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl<'a, F, NAME, T: 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> for Unstark { type PublicInputs = NoColumns; - type View = NoColumns; + type View = ShadowColumns; fn generate_constraints( - _vars: &StarkFrameTyped>, NoColumns>>, + _vars: &StarkFrameTyped, { COLUMNS }>, NoColumns>>, ) -> ConstraintBuilder> { ConstraintBuilder::default() } @@ -105,9 +105,180 @@ macro_rules! impl_name { pub(crate) use impl_name; +pub type NoColumns = ShadowColumns; + #[repr(C)] #[derive(Clone, Copy, Eq, PartialEq, Debug)] -pub struct NoColumns { - _phantom: PhantomData, +pub struct ShadowColumns { + _columns: [T; N], +} + +// Manually implement columns_view_impl! for Columns +impl crate::columns_view::ColumnViewImplHider> { + const fn from_array(value: [T; N]) -> ShadowColumns { + unsafe { crate::columns_view::transmute_without_compile_time_size_checks(value) } + } + + const fn into_array(v: ShadowColumns) -> [T; N] { + unsafe { crate::columns_view::transmute_without_compile_time_size_checks(v) } + } + + const fn from_array_ref(value: &[T; N]) -> &ShadowColumns { + unsafe { crate::columns_view::transmute_ref(value) } + } + + const fn array_ref(v: &ShadowColumns) -> &[T; N] { + unsafe { crate::columns_view::transmute_ref(v) } + } +} + +impl ShadowColumns { + pub const fn from_array(value: [T; N]) -> Self { + crate::columns_view::ColumnViewImplHider::::from_array(value) + } + + #[must_use] + pub const fn into_array(self) -> [T; N] { + crate::columns_view::ColumnViewImplHider::::into_array(self) + } + + pub const fn from_array_ref(value: &[T; N]) -> &Self { + crate::columns_view::ColumnViewImplHider::::from_array_ref(value) + } + + #[must_use] + pub const fn array_ref(&self) -> &[T; N] { + crate::columns_view::ColumnViewImplHider::::array_ref(self) + } + + pub fn iter(&self) -> std::slice::Iter { self.array_ref().into_iter() } + + // At the moment we only use `map` Instruction, + // so it's dead code for the other callers of `columns_view_impl`. + // TODO(Matthias): remove this marker, once we use it for the other structs, + // too. + #[allow(dead_code)] + pub fn map(self, f: F) -> ShadowColumns + where + F: FnMut(T) -> B, { + ShadowColumns::from_array(self.into_array().map(f)) + } +} + +impl crate::columns_view::Zip for ShadowColumns { + fn zip_with(self, other: Self, mut f: F) -> Self + where + F: FnMut(Item, Item) -> Item, { + ShadowColumns::from_array({ + let mut a = self.into_iter(); + let mut b = other.into_iter(); + core::array::from_fn(move |_| f(a.next().unwrap(), b.next().unwrap())) + }) + } +} + +impl crate::columns_view::NumberOfColumns for ShadowColumns { + // `u8` is guaranteed to have a `size_of` of 1. + const NUMBER_OF_COLUMNS: usize = N; +} + +impl From<[T; N]> for ShadowColumns { + fn from(value: [T; N]) -> Self { Self::from_array(value) } +} + +impl From> for [T; N] { + fn from(value: ShadowColumns) -> Self { value.into_array() } +} + +impl<'a, T, const N: usize> From<&'a [T]> for &'a ShadowColumns { + fn from(value: &'a [T]) -> Self { + let value: &[T; N] = + value.try_into().expect("slice of correct length"); + ShadowColumns::from_array_ref(value) + } +} + +impl std::borrow::Borrow<[T]> for ShadowColumns { + fn borrow(&self) -> &[T] { self.array_ref() } +} + +impl std::ops::Index for ShadowColumns +where + [T]: std::ops::Index, +{ + type Output = <[T] as std::ops::Index>::Output; + + fn index(&self, index: I) -> &Self::Output { &self.array_ref()[index] } +} + +impl std::iter::IntoIterator for ShadowColumns { + type IntoIter = std::array::IntoIter; + type Item = T; + + fn into_iter(self) -> Self::IntoIter { self.into_array().into_iter() } +} + +impl<'a, T, const N: usize> std::iter::IntoIterator for &'a ShadowColumns { + type IntoIter = std::slice::Iter<'a, T>; + type Item = &'a T; + + fn into_iter(self) -> Self::IntoIter { self.iter() } +} + +impl std::iter::FromIterator for ShadowColumns { + fn from_iter>(iter: I) -> Self { + let vec: arrayvec::ArrayVec = iter.into_iter().collect(); + let array = vec.into_inner().expect("iterator of correct length"); + Self::from_array(array) + } +} + +impl core::ops::Neg for ShadowColumns { + type Output = Self; + + fn neg(self) -> Self::Output { + self.map(|x| x.checked_neg().expect("negation overflow")) + } +} + +impl core::ops::Add> for ShadowColumns { + type Output = Self; + + fn add(self, other: Self) -> Self::Output { + crate::columns_view::Zip::zip_with(self, other, |a, b| { + a.checked_add(b).expect("addition overflow") + }) + } +} + +impl core::ops::Sub> for ShadowColumns { + type Output = Self; + + fn sub(self, other: Self) -> Self::Output { + crate::columns_view::Zip::zip_with(self, other, |a, b| { + a.checked_sub(b).expect("subtraction overflow") + }) + } +} + +impl core::ops::Mul for ShadowColumns { + type Output = Self; + + fn mul(self, other: i64) -> Self::Output { + self.map(|x| x.checked_mul(other).expect("multiplication overflow")) + } +} + +impl core::iter::Sum> for ShadowColumns { + #[inline] + fn sum>(iter: I) -> Self { + iter.fold(Self::default(), core::ops::Add::add) + } +} + +// Some of our tables have columns that are specifiecd as arrays that are bigger +// than 32 elements. Thus default derivation doesn't work, so we do it manually +// here. +impl Default for ShadowColumns { + fn default() -> Self { ShadowColumns::from_array(core::array::from_fn(|_| Default::default())) } } -columns_view_impl!(NoColumns); From 0800ac36cdd6436911dee51ba9331b5b7f90f661 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 18 Jun 2024 14:17:13 +0200 Subject: [PATCH 54/84] cargo fmt --- circuits/src/unstark.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index e6ebc67be..13abdd1d2 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -192,8 +192,7 @@ impl From> for [T; N] { impl<'a, T, const N: usize> From<&'a [T]> for &'a ShadowColumns { fn from(value: &'a [T]) -> Self { - let value: &[T; N] = - value.try_into().expect("slice of correct length"); + let value: &[T; N] = value.try_into().expect("slice of correct length"); ShadowColumns::from_array_ref(value) } } @@ -236,9 +235,7 @@ impl std::iter::FromIterator for ShadowCo impl core::ops::Neg for ShadowColumns { type Output = Self; - fn neg(self) -> Self::Output { - self.map(|x| x.checked_neg().expect("negation overflow")) - } + fn neg(self) -> Self::Output { self.map(|x| x.checked_neg().expect("negation overflow")) } } impl core::ops::Add> for ShadowColumns { From 68eb2f0194cd96010a42a1f72c19e66c63e775ed Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Tue, 18 Jun 2024 14:32:18 +0200 Subject: [PATCH 55/84] cargo clippy into_iter() called on a reference is the same as iter() --- circuits/src/columns_view.rs | 2 +- circuits/src/unstark.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index b2daf2eef..762830481 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -96,7 +96,7 @@ macro_rules! columns_view_impl { crate::columns_view::ColumnViewImplHider::::array_ref(self) } - pub fn iter(&self) -> std::slice::Iter { self.array_ref().into_iter() } + pub fn iter(&self) -> std::slice::Iter { self.array_ref().iter() } // At the moment we only use `map` Instruction, // so it's dead code for the other callers of `columns_view_impl`. diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 13abdd1d2..34f203e5c 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -151,7 +151,7 @@ impl ShadowColumns { crate::columns_view::ColumnViewImplHider::::array_ref(self) } - pub fn iter(&self) -> std::slice::Iter { self.array_ref().into_iter() } + pub fn iter(&self) -> std::slice::Iter { self.array_ref().iter() } // At the moment we only use `map` Instruction, // so it's dead code for the other callers of `columns_view_impl`. From 05eeaf40484fbf13df4b3faf95084cb38e15f94d Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Wed, 19 Jun 2024 14:49:02 +0800 Subject: [PATCH 56/84] Minimise diff --- circuits/src/columns_view.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index 762830481..b2daf2eef 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -96,7 +96,7 @@ macro_rules! columns_view_impl { crate::columns_view::ColumnViewImplHider::::array_ref(self) } - pub fn iter(&self) -> std::slice::Iter { self.array_ref().iter() } + pub fn iter(&self) -> std::slice::Iter { self.array_ref().into_iter() } // At the moment we only use `map` Instruction, // so it's dead code for the other callers of `columns_view_impl`. From 340c708d44e8e91f74b2604f139a6aa458def0e0 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Wed, 19 Jun 2024 14:54:42 +0800 Subject: [PATCH 57/84] Default to no constraints --- circuits/src/expr.rs | 4 +++- circuits/src/unstark.rs | 9 +-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index e28fb15eb..35f3447be 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -213,5 +213,7 @@ pub trait GenerateConstraints<'a, T: 'a> { type View; type PublicInputs; - fn generate_constraints(vars: &Vars<'a, Self, T>) -> ConstraintBuilder>; + fn generate_constraints(_vars: &Vars<'a, Self, T>) -> ConstraintBuilder> { + ConstraintBuilder::default() + } } diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 34f203e5c..08968a9f1 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -2,7 +2,6 @@ use core::fmt::Debug; use std::fmt::Display; use std::marker::PhantomData; -use expr::{Expr, StarkFrameTyped}; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; @@ -13,19 +12,13 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{ConstraintBuilder, GenerateConstraints}; +use crate::expr::GenerateConstraints; impl<'a, F, NAME, T: 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> for Unstark { type PublicInputs = NoColumns; type View = ShadowColumns; - - fn generate_constraints( - _vars: &StarkFrameTyped, { COLUMNS }>, NoColumns>>, - ) -> ConstraintBuilder> { - ConstraintBuilder::default() - } } /// Template for a STARK with zero internal constraints. Use this if the STARK From a2e0bfff9d9ef91b8edebd36ce8849826c81fdd4 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Wed, 19 Jun 2024 16:50:39 +0200 Subject: [PATCH 58/84] Simplify the implementation of StarkFrameTyped --- circuits/src/expr.rs | 2 +- circuits/src/generation.rs | 16 ++-- expr/src/lib.rs | 145 ++++++++++++++++++++++++------------- 3 files changed, 102 insertions(+), 61 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index e28fb15eb..3ffa177c3 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -206,7 +206,7 @@ pub fn build_packed( pub type PublicInputsOf<'a, S, F, T> = >::PublicInputs; pub type ViewOf<'a, S, F, T> = >::View; -type Vars<'a, S, T> = +pub type Vars<'a, S, T> = StarkFrameTyped>, PublicInputsOf<'a, S, T, Expr<'a, T>>>; pub trait GenerateConstraints<'a, T: 'a> { diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 5e8957143..9a9383fa0 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -4,7 +4,7 @@ use std::borrow::Borrow; use std::fmt::{Debug, Display}; -use expr::{Expr, ExprBuilder}; +use expr::{Expr, ExprBuilder, StarkFrameTyped}; use itertools::{izip, Itertools}; use log::debug; use mozak_runner::elf::Program; @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, ConstraintType, GenerateConstraints, PublicInputsOf, ViewOf}; +use crate::expr::{build_debug, ConstraintType, GenerateConstraints, PublicInputsOf, Vars, ViewOf}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -228,14 +228,10 @@ pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S>( .circular_tuple_windows() .for_each(|((lv_row, lv), (nv_row, nv))| { let expr_builder = ExprBuilder::default(); - let vars = &expr_builder.to_typed_starkframe_( - lv.as_slice(), - nv.as_slice(), - public_inputs, - S::COLUMNS, - S::PUBLIC_INPUTS, - ); - let constraints = S::generate_constraints(vars); + let frame: StarkFrameTyped, Vec> = + StarkFrameTyped::from_values(lv, nv, public_inputs); + let vars: Vars = expr_builder.inject_starkframe(frame); + let constraints = S::generate_constraints(&vars); let evaluated = build_debug(constraints); // Filter out only applicable constraints diff --git a/expr/src/lib.rs b/expr/src/lib.rs index a971068d0..13c7e409a 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -203,68 +203,113 @@ impl ExprBuilder { // We don't actually need the first constraint, but it's useful to make the compiler yell // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to // give direct access to its contents. - View: FromIterator>, - PublicInputs: FromIterator>, { - // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no - // need for the reference only access. - StarkFrameTyped { - local_values: vars - .get_local_values() - .iter() - .map(|&v| self.lit(v)) - .collect(), - next_values: vars - .get_next_values() - .iter() - .map(|&v| self.lit(v)) - .collect(), - public_inputs: vars - .get_public_inputs() - .iter() - .map(|&v| self.lit(T::from(v))) - .collect(), - } + View: From<[Expr<'a, T>; N]> + FromIterator>, + PublicInputs: From<[Expr<'a, T>; N2]> + FromIterator>, { + // NOTE: Rust needs to know all the intermediate types + let frame: StarkFrameTyped, Vec> = StarkFrameTyped::from(vars); + let frame: StarkFrameTyped, Vec> = frame.map_public_inputs(|v| T::from(v)); + self.inject_starkframe(frame) } - pub fn to_typed_starkframe_<'a, T, U, View, PublicInputs>( + /// Inject `StarkFrameTypes` into the `ExprBuilder`. + /// + /// This function will decompose `StarkFrameTyped` using the `IntoIterator` + /// instances of `View` and `PublicInputs` and then recompose them back + /// using `FromIterator` instances of `MappedView` and `MappedPublicInputs` + /// respectively. + pub fn inject_starkframe< + 'a, + T: 'a, + U: 'a, + View, + PublicInputs, + MappedView, + MappedPublicInputs, + >( &'a self, - lv: &[T], - nv: &[T], - pis: &[U], - columns: usize, - public_inputs: usize, - ) -> StarkFrameTyped + frame: StarkFrameTyped, + ) -> StarkFrameTyped where - T: Copy + Clone + Default + From + 'a, - U: Copy + Clone + Default, - // We don't actually need the first constraint, but it's useful to make the compiler yell - // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to - // give direct access to its contents. - View: FromIterator>, - PublicInputs: FromIterator>, - // TODO: Fix `StarkEvaluationFrame` to give direct access to its contents, no - // need for the reference only access. - { - assert_eq!(lv.len(), columns); - assert_eq!(nv.len(), columns); - assert_eq!(pis.len(), public_inputs); - - StarkFrameTyped { - local_values: lv.iter().map(|&v| self.lit(v)).collect(), - next_values: nv.iter().map(|&v| self.lit(v)).collect(), - public_inputs: pis.iter().map(|&v| self.lit(T::from(v))).collect(), - } + View: IntoIterator, + PublicInputs: IntoIterator, + MappedView: FromIterator>, + MappedPublicInputs: FromIterator>, { + frame + .map_view(|v| self.lit(v)) + .map_public_inputs(|v| self.lit(v)) } } /// A helper around `StarkFrame` to add types #[derive(Debug)] -pub struct StarkFrameTyped { - pub local_values: Row, - pub next_values: Row, +pub struct StarkFrameTyped { + pub local_values: View, + pub next_values: View, pub public_inputs: PublicInputs, } +impl StarkFrameTyped { + pub fn from_values(lv: &[T], nv: &[T], pis: &[U]) -> Self + where + T: Copy, + U: Copy, + View: FromIterator, + PublicInputs: FromIterator, { + Self { + local_values: lv.iter().copied().collect(), + next_values: nv.iter().copied().collect(), + public_inputs: pis.iter().copied().collect(), + } + } + + pub fn map_view( + self, + mut f: F, + ) -> StarkFrameTyped + where + View: IntoIterator, + MappedView: FromIterator, + F: FnMut(T) -> B, { + StarkFrameTyped { + local_values: self.local_values.into_iter().map(&mut f).collect(), + next_values: self.next_values.into_iter().map(f).collect(), + public_inputs: self.public_inputs, + } + } + + pub fn map_public_inputs( + self, + f: F, + ) -> StarkFrameTyped + where + PublicInputs: IntoIterator, + MappedPublicInputs: FromIterator, + F: FnMut(U) -> C, { + StarkFrameTyped { + local_values: self.local_values, + next_values: self.next_values, + public_inputs: self.public_inputs.into_iter().map(f).collect(), + } + } +} + +impl<'a, T, U, const N: usize, const N2: usize, View, PublicInputs> + From<&'a StarkFrame> for StarkFrameTyped +where + T: Copy + Default, + U: Copy + Default, + View: From<[T; N]> + FromIterator, + PublicInputs: From<[U; N2]> + FromIterator, +{ + fn from(value: &'a StarkFrame) -> Self { + Self::from_values( + value.get_local_values(), + value.get_next_values(), + value.get_public_inputs(), + ) + } +} + /// Enum for binary operations #[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] pub enum BinOp { From 93ad4c763042c990693e60cd7bb891cd73ccf6e9 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Thu, 20 Jun 2024 16:40:02 +0200 Subject: [PATCH 59/84] [no ci] WIP and BROKEN --- circuits/src/bitshift/stark.rs | 22 +++++----- circuits/src/expr.rs | 73 +++++++++++++++++++++++++++++----- circuits/src/unstark.rs | 15 +++++-- expr/src/lib.rs | 4 +- 4 files changed, 90 insertions(+), 24 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index ef877fe49..6adecfa7e 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -13,7 +13,7 @@ use starky::stark::Stark; use super::columns::BitshiftView; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::unstark::NoColumns; /// Bitshift Trace Constraints @@ -30,11 +30,12 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for BitshiftStark { - type PublicInputs = NoColumns; - type View = BitshiftView; +impl<'a, F: 'a, T: Copy + 'a + std::fmt::Debug, const D: usize> GenerateConstraints<'a, T, { COLUMNS }, { PUBLIC_INPUTS }> for BitshiftStark { + type PublicInputs = NoColumns; + type View = BitshiftView; fn generate_constraints( + self, vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values.executed; @@ -74,6 +75,10 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for Bitshif constraints } + + fn exists(self) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { + as Default>::default() + } } impl, const D: usize> Stark for BitshiftStark { @@ -92,9 +97,8 @@ impl, const D: usize> Stark for BitshiftStark ) where FE: FieldExtension, P: PackedField, { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_packed(constraints, constraint_consumer); + (StarkFrom::{ witness: *self }).eval_packed_generic(vars, constraint_consumer) + } fn constraint_degree(&self) -> usize { 3 } @@ -105,9 +109,7 @@ impl, const D: usize> Stark for BitshiftStark vars: &Self::EvaluationFrameTarget, constraint_consumer: &mut RecursiveConstraintConsumer, ) { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_ext(constraints, circuit_builder, constraint_consumer); + (StarkFrom::{ witness: *self }).eval_ext_circuit(circuit_builder, vars, constraint_consumer) } } diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 3ffa177c3..5c204c2df 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,14 +1,17 @@ use core::fmt::Debug; +use std::marker::PhantomData; use std::panic::Location; pub use expr::PureEvaluator; -use expr::{BinOp, Cached, Evaluator, Expr, StarkFrameTyped, UnaOp}; +use expr::{BinOp, Cached, Evaluator, Expr, ExprBuilder, StarkFrameTyped, UnaOp}; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; use plonky2::iop::ext_target::ExtensionTarget; use plonky2::plonk::circuit_builder::CircuitBuilder; use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; +use starky::evaluation_frame::StarkFrame; +use starky::stark::Stark; struct CircuitBuilderEvaluator<'a, F, const D: usize> where @@ -203,15 +206,67 @@ pub fn build_packed( } // Helper Types to Access members of GenerateConstraints -pub type PublicInputsOf<'a, S, F, T> = >::PublicInputs; -pub type ViewOf<'a, S, F, T> = >::View; +pub type PublicInputsOf<'a, S, F, T, const N: usize, const M: usize> = >::PublicInputs; +pub type ViewOf<'a, S, F, T, const N: usize, const M: usize> = >::View; -pub type Vars<'a, S, T> = - StarkFrameTyped>, PublicInputsOf<'a, S, T, Expr<'a, T>>>; +pub type Vars<'a, S, T, const N: usize, const M: usize> = + StarkFrameTyped, N, M>, PublicInputsOf<'a, S, T, Expr<'a, T>, N, M>>; -pub trait GenerateConstraints<'a, T: 'a> { - type View; - type PublicInputs; +pub trait GenerateConstraints<'a, T: 'a + std::fmt::Debug, const COLUMNS: usize, const PUBLIC_INPUTS: usize> +where Self: 'a +{ + type View: From<[E; COLUMNS]> + FromIterator where Self: 'a; + type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator where Self: 'a; + + fn generate_constraints(self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>) -> ConstraintBuilder>; + + + fn exists(self) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; +} + +// Note: Not sure if D is needed here +#[derive(Default)] +pub struct StarkFrom +{ + pub witness: G, +} + + +impl Stark for StarkFrom +where + for<'b> G: Sync + GenerateConstraints<'b, F, COLUMNS, PUBLIC_INPUTS> + Copy, + F: RichField + Extendable, +{ + type EvaluationFrame = StarkFrame + + where + FE: FieldExtension, + P: PackedField; + type EvaluationFrameTarget = + StarkFrame, ExtensionTarget, { COLUMNS }, { PUBLIC_INPUTS }>; + + fn eval_packed_generic( + &self, + vars: &Self::EvaluationFrame, + constraint_consumer: &mut ConstraintConsumer

, + ) where + FE: FieldExtension, + P: PackedField, { + let expr_builder = ExprBuilder::default(); + let constraints = self.witness.exists().generate_constraints(&expr_builder.to_typed_starkframe(vars)); + build_packed(constraints, constraint_consumer); + } + + fn eval_ext_circuit( + &self, + circuit_builder: &mut CircuitBuilder, + vars: &Self::EvaluationFrameTarget, + constraint_consumer: &mut RecursiveConstraintConsumer, + ) { + let expr_builder = ExprBuilder::default(); + let constraints = self.witness.exists().generate_constraints(&expr_builder.to_typed_starkframe(vars)); + build_ext(constraints, circuit_builder, constraint_consumer); + } - fn generate_constraints(vars: &Vars<'a, Self, T>) -> ConstraintBuilder>; + fn constraint_degree(&self) -> usize { 3 } } diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 34f203e5c..63fdb9c66 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,17 +15,26 @@ use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F, NAME, T: 'a, const D: usize, Columns, const COLUMNS: usize> GenerateConstraints<'a, T> +impl<'a, F: 'a, NAME: 'a, T: 'a + std::fmt::Debug, const D: usize, Columns: 'a, const COLUMNS: usize> GenerateConstraints<'a, T, COLUMNS, PUBLIC_INPUTS> for Unstark { - type PublicInputs = NoColumns; - type View = ShadowColumns; + type PublicInputs = NoColumns; + type View = ShadowColumns; fn generate_constraints( + self, _vars: &StarkFrameTyped, { COLUMNS }>, NoColumns>>, ) -> ConstraintBuilder> { ConstraintBuilder::default() } + + fn exists(self) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { + Unstark:: { + _f: PhantomData::::default(), + _name: self._name, + _d: self._d + } + } } /// Template for a STARK with zero internal constraints. Use this if the STARK diff --git a/expr/src/lib.rs b/expr/src/lib.rs index 13c7e409a..11f26611b 100644 --- a/expr/src/lib.rs +++ b/expr/src/lib.rs @@ -203,8 +203,8 @@ impl ExprBuilder { // We don't actually need the first constraint, but it's useful to make the compiler yell // at us, if we mix things up. See the TODO about fixing `StarkEvaluationFrame` to // give direct access to its contents. - View: From<[Expr<'a, T>; N]> + FromIterator>, - PublicInputs: From<[Expr<'a, T>; N2]> + FromIterator>, { + View: FromIterator>, + PublicInputs: FromIterator>, { // NOTE: Rust needs to know all the intermediate types let frame: StarkFrameTyped, Vec> = StarkFrameTyped::from(vars); let frame: StarkFrameTyped, Vec> = frame.map_public_inputs(|v| T::from(v)); From 9ddbd37389be54dc0de6699269b079927655608f Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 11:47:52 +0800 Subject: [PATCH 60/84] Format --- circuits/src/bitshift/stark.rs | 22 ++++++++---- circuits/src/expr.rs | 61 +++++++++++++++++++++++----------- circuits/src/unstark.rs | 18 +++++++--- 3 files changed, 70 insertions(+), 31 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 6adecfa7e..d295bf892 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -30,7 +30,9 @@ impl HasNamedColumns for BitshiftStark { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: 'a, T: Copy + 'a + std::fmt::Debug, const D: usize> GenerateConstraints<'a, T, { COLUMNS }, { PUBLIC_INPUTS }> for BitshiftStark { +impl<'a, F: 'a, T: Copy + 'a + std::fmt::Debug, const D: usize> + GenerateConstraints<'a, T, { COLUMNS }, { PUBLIC_INPUTS }> for BitshiftStark +{ type PublicInputs = NoColumns; type View = BitshiftView; @@ -75,9 +77,11 @@ impl<'a, F: 'a, T: Copy + 'a + std::fmt::Debug, const D: usize> GenerateConstrai constraints } - - fn exists(self) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { - as Default>::default() + + fn exists( + self, + ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { + as Default>::default() } } @@ -97,8 +101,8 @@ impl, const D: usize> Stark for BitshiftStark ) where FE: FieldExtension, P: PackedField, { - (StarkFrom::{ witness: *self }).eval_packed_generic(vars, constraint_consumer) - + (StarkFrom:: { witness: *self }) + .eval_packed_generic(vars, constraint_consumer) } fn constraint_degree(&self) -> usize { 3 } @@ -109,7 +113,11 @@ impl, const D: usize> Stark for BitshiftStark vars: &Self::EvaluationFrameTarget, constraint_consumer: &mut RecursiveConstraintConsumer, ) { - (StarkFrom::{ witness: *self }).eval_ext_circuit(circuit_builder, vars, constraint_consumer) + (StarkFrom:: { witness: *self }).eval_ext_circuit( + circuit_builder, + vars, + constraint_consumer, + ) } } diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 5c204c2df..3af65d452 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -206,33 +206,48 @@ pub fn build_packed( } // Helper Types to Access members of GenerateConstraints -pub type PublicInputsOf<'a, S, F, T, const N: usize, const M: usize> = >::PublicInputs; -pub type ViewOf<'a, S, F, T, const N: usize, const M: usize> = >::View; - -pub type Vars<'a, S, T, const N: usize, const M: usize> = - StarkFrameTyped, N, M>, PublicInputsOf<'a, S, T, Expr<'a, T>, N, M>>; - -pub trait GenerateConstraints<'a, T: 'a + std::fmt::Debug, const COLUMNS: usize, const PUBLIC_INPUTS: usize> -where Self: 'a -{ - type View: From<[E; COLUMNS]> + FromIterator where Self: 'a; - type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator where Self: 'a; - - fn generate_constraints(self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>) -> ConstraintBuilder>; +pub type PublicInputsOf<'a, S, F, T, const N: usize, const M: usize> = + >::PublicInputs; +pub type ViewOf<'a, S, F, T, const N: usize, const M: usize> = + >::View; + +pub type Vars<'a, S, T, const N: usize, const M: usize> = StarkFrameTyped< + ViewOf<'a, S, T, Expr<'a, T>, N, M>, + PublicInputsOf<'a, S, T, Expr<'a, T>, N, M>, +>; + +pub trait GenerateConstraints< + 'a, + T: 'a + std::fmt::Debug, + const COLUMNS: usize, + const PUBLIC_INPUTS: usize, +> where + Self: 'a, { + type View: From<[E; COLUMNS]> + FromIterator + where + Self: 'a; + type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator + where + Self: 'a; + fn generate_constraints( + self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, + ) -> ConstraintBuilder>; - fn exists(self) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; + fn exists( + self, + ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; } // Note: Not sure if D is needed here #[derive(Default)] -pub struct StarkFrom -{ +pub struct StarkFrom { pub witness: G, } - -impl Stark for StarkFrom +impl Stark + for StarkFrom where for<'b> G: Sync + GenerateConstraints<'b, F, COLUMNS, PUBLIC_INPUTS> + Copy, F: RichField + Extendable, @@ -253,7 +268,10 @@ where FE: FieldExtension, P: PackedField, { let expr_builder = ExprBuilder::default(); - let constraints = self.witness.exists().generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = self + .witness + .exists() + .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -264,7 +282,10 @@ where constraint_consumer: &mut RecursiveConstraintConsumer, ) { let expr_builder = ExprBuilder::default(); - let constraints = self.witness.exists().generate_constraints(&expr_builder.to_typed_starkframe(vars)); + let constraints = self + .witness + .exists() + .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 63fdb9c66..533089981 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -15,7 +15,15 @@ use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints}; -impl<'a, F: 'a, NAME: 'a, T: 'a + std::fmt::Debug, const D: usize, Columns: 'a, const COLUMNS: usize> GenerateConstraints<'a, T, COLUMNS, PUBLIC_INPUTS> +impl< + 'a, + F: 'a, + NAME: 'a, + T: 'a + std::fmt::Debug, + const D: usize, + Columns: 'a, + const COLUMNS: usize, + > GenerateConstraints<'a, T, COLUMNS, PUBLIC_INPUTS> for Unstark { type PublicInputs = NoColumns; @@ -27,12 +35,14 @@ impl<'a, F: 'a, NAME: 'a, T: 'a + std::fmt::Debug, const D: usize, Columns: 'a, ) -> ConstraintBuilder> { ConstraintBuilder::default() } - - fn exists(self) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { + + fn exists( + self, + ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { Unstark:: { _f: PhantomData::::default(), _name: self._name, - _d: self._d + _d: self._d, } } } From 2cfbab1867365d70ea6dc0ce14cb631b0dbe43b2 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 11:52:52 +0800 Subject: [PATCH 61/84] Replace qualified path with 'use' --- circuits/src/expr.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index b189fbaf3..7b043a408 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,5 +1,6 @@ use core::fmt::Debug; -use std::marker::PhantomData; +use std::fmt::Debug; +use std::marker::{Copy, PhantomData}; use std::panic::Location; pub use expr::PureEvaluator; @@ -218,15 +219,15 @@ pub type Vars<'a, S, T, const N: usize, const M: usize> = StarkFrameTyped< pub trait GenerateConstraints< 'a, - T: 'a + std::fmt::Debug, + T: 'a + Debug, const COLUMNS: usize, const PUBLIC_INPUTS: usize, > where Self: 'a, { - type View: From<[E; COLUMNS]> + FromIterator + type View: From<[E; COLUMNS]> + FromIterator where Self: 'a; - type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator + type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator where Self: 'a; @@ -237,7 +238,7 @@ pub trait GenerateConstraints< ConstraintBuilder::default() } - fn exists( + fn exists( self, ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; } From 8f4d44296f9d2304d0f4cb801405354ebc5169b4 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 11:58:26 +0800 Subject: [PATCH 62/84] Revert --- circuits/src/expr.rs | 4 +--- circuits/src/unstark.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 7b043a408..095a27bcc 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -234,9 +234,7 @@ pub trait GenerateConstraints< fn generate_constraints( self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, - ) -> ConstraintBuilder> { - ConstraintBuilder::default() - } + ) -> ConstraintBuilder>; fn exists( self, diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 9c00b3eb2..533089981 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -2,6 +2,7 @@ use core::fmt::Debug; use std::fmt::Display; use std::marker::PhantomData; +use expr::{Expr, StarkFrameTyped}; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; @@ -12,7 +13,7 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::GenerateConstraints; +use crate::expr::{ConstraintBuilder, GenerateConstraints}; impl< 'a, @@ -28,6 +29,13 @@ impl< type PublicInputs = NoColumns; type View = ShadowColumns; + fn generate_constraints( + self, + _vars: &StarkFrameTyped, { COLUMNS }>, NoColumns>>, + ) -> ConstraintBuilder> { + ConstraintBuilder::default() + } + fn exists( self, ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { From 595a85cbe2e347b7c5c4cfbaffca000b6d62bce0 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 12:01:32 +0800 Subject: [PATCH 63/84] Format --- circuits/src/expr.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 095a27bcc..fbbabee09 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,6 +1,5 @@ use core::fmt::Debug; -use std::fmt::Debug; -use std::marker::{Copy, PhantomData}; +use std::marker::Copy; use std::panic::Location; pub use expr::PureEvaluator; @@ -217,12 +216,8 @@ pub type Vars<'a, S, T, const N: usize, const M: usize> = StarkFrameTyped< PublicInputsOf<'a, S, T, Expr<'a, T>, N, M>, >; -pub trait GenerateConstraints< - 'a, - T: 'a + Debug, - const COLUMNS: usize, - const PUBLIC_INPUTS: usize, -> where +pub trait GenerateConstraints<'a, T: 'a + Debug, const COLUMNS: usize, const PUBLIC_INPUTS: usize> +where Self: 'a, { type View: From<[E; COLUMNS]> + FromIterator where From 0c46b6db2edfba72ae410365187ab6a663f99813 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 12:24:39 +0800 Subject: [PATCH 64/84] Simplify --- circuits/src/bitshift/stark.rs | 73 +++++----------------------------- circuits/src/columns_view.rs | 4 ++ circuits/src/expr.rs | 13 ++---- 3 files changed, 19 insertions(+), 71 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index d295bf892..23eaefe73 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -1,37 +1,28 @@ -use std::marker::PhantomData; - -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::{Expr, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::BitshiftView; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints, StarkFrom}; +use crate::columns_view::{HasNamedColumns_, NumberOfColumns}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::unstark::NoColumns; /// Bitshift Trace Constraints #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct BitshiftStark { - pub _f: PhantomData, -} +pub struct BitshiftStark_ {} + +pub type BitshiftStark = + StarkFrom; -impl HasNamedColumns for BitshiftStark { - type Columns = BitshiftView; +impl HasNamedColumns_ for BitshiftStark_ { + type Columns = BitshiftView; } const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: 'a, T: Copy + 'a + std::fmt::Debug, const D: usize> - GenerateConstraints<'a, T, { COLUMNS }, { PUBLIC_INPUTS }> for BitshiftStark +impl<'a, T: Copy + 'a + std::fmt::Debug> GenerateConstraints<'a, T, { COLUMNS }, { PUBLIC_INPUTS }> + for BitshiftStark_ { type PublicInputs = NoColumns; type View = BitshiftView; @@ -77,48 +68,6 @@ impl<'a, F: 'a, T: Copy + 'a + std::fmt::Debug, const D: usize> constraints } - - fn exists( - self, - ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { - as Default>::default() - } -} - -impl, const D: usize> Stark for BitshiftStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - constraint_consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - (StarkFrom:: { witness: *self }) - .eval_packed_generic(vars, constraint_consumer) - } - - fn constraint_degree(&self) -> usize { 3 } - - fn eval_ext_circuit( - &self, - circuit_builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - constraint_consumer: &mut RecursiveConstraintConsumer, - ) { - (StarkFrom:: { witness: *self }).eval_ext_circuit( - circuit_builder, - vars, - constraint_consumer, - ) - } } #[cfg(test)] diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index b2daf2eef..129b2e2cf 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -31,6 +31,10 @@ pub trait HasNamedColumns { type Columns; } +pub trait HasNamedColumns_ { + type Columns; +} + pub trait NumberOfColumns { const NUMBER_OF_COLUMNS: usize; } diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index fbbabee09..63645db9d 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,5 +1,5 @@ use core::fmt::Debug; -use std::marker::Copy; +use std::marker::{Copy, PhantomData}; use std::panic::Location; pub use expr::PureEvaluator; @@ -230,20 +230,17 @@ where self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder>; - - fn exists( - self, - ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; } // Note: Not sure if D is needed here #[derive(Default)] -pub struct StarkFrom { +pub struct StarkFrom { pub witness: G, + pub _f: PhantomData, } impl Stark - for StarkFrom + for StarkFrom where for<'b> G: Sync + GenerateConstraints<'b, F, COLUMNS, PUBLIC_INPUTS> + Copy, F: RichField + Extendable, @@ -266,7 +263,6 @@ where let expr_builder = ExprBuilder::default(); let constraints = self .witness - .exists() .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -280,7 +276,6 @@ where let expr_builder = ExprBuilder::default(); let constraints = self .witness - .exists() .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } From 587beb0a4842e39e4f9aae39c4e06400bbfc226f Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 12:28:15 +0800 Subject: [PATCH 65/84] Restore --- circuits/src/bitshift/stark.rs | 6 ++++++ circuits/src/expr.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 23eaefe73..945790083 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -68,6 +68,12 @@ impl<'a, T: Copy + 'a + std::fmt::Debug> GenerateConstraints<'a, T, { COLUMNS }, constraints } + + fn exists( + self, + ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { + BitshiftStark_ {} + } } #[cfg(test)] diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 63645db9d..cd7bdb744 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -230,6 +230,10 @@ where self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder>; + + fn exists( + self, + ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; } // Note: Not sure if D is needed here @@ -263,6 +267,7 @@ where let expr_builder = ExprBuilder::default(); let constraints = self .witness + .exists() .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -276,6 +281,7 @@ where let expr_builder = ExprBuilder::default(); let constraints = self .witness + .exists() .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } From c0ad81c1e55d58bb2e9951d11d2a243fdd7f629c Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 12:53:25 +0800 Subject: [PATCH 66/84] Simplify --- circuits/src/bitshift/stark.rs | 14 +++------- circuits/src/expr.rs | 50 ++++++++++++---------------------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 945790083..90df12830 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -1,3 +1,5 @@ +use core::fmt::Debug; + use expr::{Expr, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; @@ -21,13 +23,11 @@ impl HasNamedColumns_ for BitshiftStark_ { const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, T: Copy + 'a + std::fmt::Debug> GenerateConstraints<'a, T, { COLUMNS }, { PUBLIC_INPUTS }> - for BitshiftStark_ -{ +impl<'a> GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for BitshiftStark_ { type PublicInputs = NoColumns; type View = BitshiftView; - fn generate_constraints( + fn generate_constraints( self, vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { @@ -68,12 +68,6 @@ impl<'a, T: Copy + 'a + std::fmt::Debug> GenerateConstraints<'a, T, { COLUMNS }, constraints } - - fn exists( - self, - ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { - BitshiftStark_ {} - } } #[cfg(test)] diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index cd7bdb744..7b0138d79 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -206,34 +206,22 @@ pub fn build_packed( } // Helper Types to Access members of GenerateConstraints -pub type PublicInputsOf<'a, S, F, T, const N: usize, const M: usize> = - >::PublicInputs; -pub type ViewOf<'a, S, F, T, const N: usize, const M: usize> = - >::View; +pub type PublicInputsOf<'a, S, T, const N: usize, const M: usize> = + >::PublicInputs; +pub type ViewOf<'a, S, T, const N: usize, const M: usize> = + >::View; -pub type Vars<'a, S, T, const N: usize, const M: usize> = StarkFrameTyped< - ViewOf<'a, S, T, Expr<'a, T>, N, M>, - PublicInputsOf<'a, S, T, Expr<'a, T>, N, M>, ->; +pub type Vars<'a, S, T, const N: usize, const M: usize> = + StarkFrameTyped, N, M>, PublicInputsOf<'a, S, Expr<'a, T>, N, M>>; -pub trait GenerateConstraints<'a, T: 'a + Debug, const COLUMNS: usize, const PUBLIC_INPUTS: usize> -where - Self: 'a, { - type View: From<[E; COLUMNS]> + FromIterator - where - Self: 'a; - type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator - where - Self: 'a; - - fn generate_constraints( - self, - vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, - ) -> ConstraintBuilder>; +pub trait GenerateConstraints { + type View: From<[E; COLUMNS]> + FromIterator; + type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator; - fn exists( - self, - ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS>; + fn generate_constraints<'b, T: Debug>( + &self, + vars: &Vars<'b, Self, T, COLUMNS, PUBLIC_INPUTS>, + ) -> ConstraintBuilder>; } // Note: Not sure if D is needed here @@ -246,8 +234,8 @@ pub struct StarkFrom Stark for StarkFrom where - for<'b> G: Sync + GenerateConstraints<'b, F, COLUMNS, PUBLIC_INPUTS> + Copy, - F: RichField + Extendable, + G: Sync + GenerateConstraints + Copy, + F: RichField + Extendable + Debug, { type EvaluationFrame = StarkFrame @@ -263,12 +251,11 @@ where constraint_consumer: &mut ConstraintConsumer

, ) where FE: FieldExtension, - P: PackedField, { + P: PackedField + Debug + Copy, { let expr_builder = ExprBuilder::default(); - let constraints = self + let constraints: G = self .witness - .exists() - .generate_constraints(&expr_builder.to_typed_starkframe(vars)); + .generate_constraints::<_>(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); } @@ -281,7 +268,6 @@ where let expr_builder = ExprBuilder::default(); let constraints = self .witness - .exists() .generate_constraints(&expr_builder.to_typed_starkframe(vars)); build_ext(constraints, circuit_builder, constraint_consumer); } From 7c78c976654cc016c3790c95cff67e9d5f77f7ff Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 12:54:28 +0800 Subject: [PATCH 67/84] Fix --- circuits/src/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 7b0138d79..1d6f18752 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -253,7 +253,7 @@ where FE: FieldExtension, P: PackedField + Debug + Copy, { let expr_builder = ExprBuilder::default(); - let constraints: G = self + let constraints = self .witness .generate_constraints::<_>(&expr_builder.to_typed_starkframe(vars)); build_packed(constraints, constraint_consumer); From fd2cc3b2d42e65a6892553f0ef7ea06dead4d590 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 12:57:08 +0800 Subject: [PATCH 68/84] Simplify --- circuits/src/bitshift/stark.rs | 14 +++++++------- circuits/src/expr.rs | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 90df12830..bf5d2813b 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -11,23 +11,23 @@ use crate::unstark::NoColumns; /// Bitshift Trace Constraints #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct BitshiftStark_ {} +pub struct BitshiftConstraints {} pub type BitshiftStark = - StarkFrom; + StarkFrom; -impl HasNamedColumns_ for BitshiftStark_ { +impl HasNamedColumns_ for BitshiftConstraints { type Columns = BitshiftView; } const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a> GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for BitshiftStark_ { - type PublicInputs = NoColumns; - type View = BitshiftView; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for BitshiftConstraints { + type PublicInputs = NoColumns; + type View = BitshiftView; - fn generate_constraints( + fn generate_constraints<'a, T: Debug + Copy>( self, vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 1d6f18752..93f4f9ebc 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -218,10 +218,10 @@ pub trait GenerateConstraints type View: From<[E; COLUMNS]> + FromIterator; type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator; - fn generate_constraints<'b, T: Debug>( + fn generate_constraints<'a, T: Debug>( &self, - vars: &Vars<'b, Self, T, COLUMNS, PUBLIC_INPUTS>, - ) -> ConstraintBuilder>; + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, + ) -> ConstraintBuilder>; } // Note: Not sure if D is needed here From cfaeb714de0523bc6000d9d6771f0241e0ab835b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 13:07:34 +0800 Subject: [PATCH 69/84] Convert skeleton CPU --- circuits/src/bitshift/stark.rs | 15 ++++--- circuits/src/columns_view.rs | 4 -- circuits/src/cpu_skeleton/stark.rs | 69 ++++++------------------------ 3 files changed, 20 insertions(+), 68 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index bf5d2813b..67fd2d68c 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -4,7 +4,7 @@ use expr::{Expr, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; use super::columns::BitshiftView; -use crate::columns_view::{HasNamedColumns_, NumberOfColumns}; +use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::unstark::NoColumns; @@ -13,22 +13,23 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct BitshiftConstraints {} +const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + pub type BitshiftStark = StarkFrom; -impl HasNamedColumns_ for BitshiftConstraints { - type Columns = BitshiftView; +// TODO: consider folding this into `GenerateConstraints` trait. +impl HasNamedColumns for BitshiftStark { + type Columns = BitshiftView; } -const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for BitshiftConstraints { type PublicInputs = NoColumns; type View = BitshiftView; fn generate_constraints<'a, T: Debug + Copy>( - self, + &self, vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = vars.local_values.executed; diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index 129b2e2cf..b2daf2eef 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -31,10 +31,6 @@ pub trait HasNamedColumns { type Columns; } -pub trait HasNamedColumns_ { - type Columns; -} - pub trait NumberOfColumns { const NUMBER_OF_COLUMNS: usize; } diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index 8a75b73b0..f7a5d322c 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -1,26 +1,19 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::{Expr, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::CpuSkeleton; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::stark::mozak_stark::PublicInputs; #[derive(Clone, Copy, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct CpuSkeletonStark { - pub _f: PhantomData, -} +pub struct CpuSkeletonConstraints {} + +pub type CpuSkeletonStark = + StarkFrom; impl HasNamedColumns for CpuSkeletonStark { type Columns = CpuSkeleton; @@ -30,13 +23,12 @@ const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for CpuSkeletonStark -{ - type PublicInputs = PublicInputs; - type View = CpuSkeleton; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for CpuSkeletonConstraints { + type PublicInputs = PublicInputs; + type View = CpuSkeleton; - fn generate_constraints( + fn generate_constraints<'a, T: Debug + Copy>( + &self, vars: &StarkFrameTyped>, PublicInputs>>, ) -> ConstraintBuilder> { let lv = vars.local_values; @@ -74,40 +66,3 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> constraints } } - -impl, const D: usize> Stark for CpuSkeletonStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let expr_builder = ExprBuilder::default(); - let vars = expr_builder.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let vars = eb.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} From d16a0303fa068cd78a73a15f127b47c6764d0273 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 13:38:09 +0800 Subject: [PATCH 70/84] Convert CPU --- circuits/src/cpu/stark.rs | 69 ++++++------------------------ circuits/src/cpu_skeleton/stark.rs | 1 + circuits/src/expr.rs | 3 +- 3 files changed, 16 insertions(+), 57 deletions(-) diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 4ae3a57a3..73f027917 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -1,31 +1,24 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::{Expr, StarkFrameTyped}; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::{CpuState, OpSelectors}; use super::{bitwise, branches, div, ecall, jalr, memory, mul, signed_comparison, sub}; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::cpu::shift; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::unstark::NoColumns; +// TODO: fix StarkNameDisplay? +#[derive(Copy, Clone, Default, StarkNameDisplay)] +#[allow(clippy::module_name_repetitions)] +pub struct CpuConstraints {} + /// A Gadget for CPU Instructions /// /// Instructions are either handled directly or through cross table lookup -#[derive(Copy, Clone, Default, StarkNameDisplay)] -#[allow(clippy::module_name_repetitions)] -pub struct CpuStark { - pub _f: PhantomData, -} +pub type CpuStark = StarkFrom; impl HasNamedColumns for CpuStark { type Columns = CpuState; @@ -78,11 +71,12 @@ fn populate_op2_value<'a, P: Copy>( const COLUMNS: usize = CpuState::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for CpuStark { - type PublicInputs = NoColumns; - type View = CpuState; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for CpuConstraints { + type PublicInputs = NoColumns; + type View = CpuState; - fn generate_constraints( + fn generate_constraints<'a, T: Debug + Copy>( + &self, vars: &StarkFrameTyped>, NoColumns>>, ) -> ConstraintBuilder> { let lv = &vars.local_values; @@ -114,41 +108,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for CpuStar } } -impl, const D: usize> Stark for CpuStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - constraint_consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_packed(constraints, constraint_consumer); - } - - fn constraint_degree(&self) -> usize { 3 } - - fn eval_ext_circuit( - &self, - circuit_builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - constraint_consumer: &mut RecursiveConstraintConsumer, - ) { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_ext(constraints, circuit_builder, constraint_consumer); - } -} - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index f7a5d322c..27e696b0f 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -8,6 +8,7 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::stark::mozak_stark::PublicInputs; +// TODO: fix StarkNameDisplay? #[derive(Clone, Copy, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] pub struct CpuSkeletonConstraints {} diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 93f4f9ebc..1cf09c723 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -224,8 +224,7 @@ pub trait GenerateConstraints ) -> ConstraintBuilder>; } -// Note: Not sure if D is needed here -#[derive(Default)] +#[derive(Copy, Clone, Debug, Default)] pub struct StarkFrom { pub witness: G, pub _f: PhantomData, From f40123d8d831ece06851ce93b48f0b2a72f06c6d Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 13:46:18 +0800 Subject: [PATCH 71/84] Convert full word memory --- circuits/src/expr.rs | 9 ++++ circuits/src/memory_fullword/stark.rs | 70 +++++---------------------- 2 files changed, 22 insertions(+), 57 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 1cf09c723..8cda42812 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -1,4 +1,5 @@ use core::fmt::Debug; +use std::fmt::Display; use std::marker::{Copy, PhantomData}; use std::panic::Location; @@ -218,6 +219,8 @@ pub trait GenerateConstraints type View: From<[E; COLUMNS]> + FromIterator; type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator; + // TODO: can we do a default Vars? + fn generate_constraints<'a, T: Debug>( &self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, @@ -230,6 +233,12 @@ pub struct StarkFrom, } +impl Display + for StarkFrom +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.witness.fmt(f) } +} + impl Stark for StarkFrom where diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 8f4eadbb4..cac8a7412 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -1,27 +1,20 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::{Expr, StarkFrameTyped}; use itertools::izip; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; use crate::memory_fullword::columns::{FullWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct FullWordMemoryStark { - pub _f: PhantomData, -} +pub struct FullWordMemoryConstraints {} + +pub type FullWordMemoryStark = + StarkFrom; impl HasNamedColumns for FullWordMemoryStark { type Columns = FullWordMemory; @@ -30,15 +23,14 @@ impl HasNamedColumns for FullWordMemoryStark { const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for FullWordMemoryStark -{ - type PublicInputs = NoColumns; - type View = FullWordMemory; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for FullWordMemoryConstraints { + type PublicInputs = NoColumns; + type View = FullWordMemory; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Debug + Copy>( + &self, + vars: &StarkFrameTyped>, Self::PublicInputs>>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -59,42 +51,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for FullWordMemoryStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use mozak_runner::code; From 7064b4ec3b0c37a8a4d97c0f67d9a725e5aa30c2 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 13:52:58 +0800 Subject: [PATCH 72/84] Simplify --- circuits/src/bitshift/stark.rs | 4 ++-- circuits/src/cpu/stark.rs | 4 ++-- circuits/src/expr.rs | 2 -- circuits/src/memory_fullword/stark.rs | 6 +++--- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 67fd2d68c..975f98658 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -5,7 +5,7 @@ use mozak_circuits_derive::StarkNameDisplay; use super::columns::BitshiftView; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; /// Bitshift Trace Constraints @@ -30,7 +30,7 @@ impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for BitshiftConstraints fn generate_constraints<'a, T: Debug + Copy>( &self, - vars: &StarkFrameTyped>, NoColumns>>, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values.executed; let nv = vars.next_values.executed; diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 73f027917..7fe09538f 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -7,7 +7,7 @@ use super::columns::{CpuState, OpSelectors}; use super::{bitwise, branches, div, ecall, jalr, memory, mul, signed_comparison, sub}; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::cpu::shift; -use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; // TODO: fix StarkNameDisplay? @@ -77,7 +77,7 @@ impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for CpuConstraints { fn generate_constraints<'a, T: Debug + Copy>( &self, - vars: &StarkFrameTyped>, NoColumns>>, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = &vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 8cda42812..46f278366 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -219,8 +219,6 @@ pub trait GenerateConstraints type View: From<[E; COLUMNS]> + FromIterator; type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator; - // TODO: can we do a default Vars? - fn generate_constraints<'a, T: Debug>( &self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index cac8a7412..5bbcbec95 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -1,11 +1,11 @@ use core::fmt::Debug; -use expr::{Expr, StarkFrameTyped}; +use expr::Expr; use itertools::izip; use mozak_circuits_derive::StarkNameDisplay; use crate::columns_view::HasNamedColumns; -use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::memory_fullword::columns::{FullWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; @@ -30,7 +30,7 @@ impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for FullWordMemoryConst // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing fn generate_constraints<'a, T: Debug + Copy>( &self, - vars: &StarkFrameTyped>, Self::PublicInputs>>, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); From 0cb369c4f610024aac125d223c15c93242dc5d81 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Fri, 21 Jun 2024 14:00:11 +0800 Subject: [PATCH 73/84] Convert more --- circuits/src/bitshift/stark.rs | 2 +- circuits/src/cpu/stark.rs | 2 +- circuits/src/cpu_skeleton/stark.rs | 6 +-- circuits/src/memory/stark.rs | 69 +++++---------------------- circuits/src/memory_halfword/stark.rs | 25 +++++----- circuits/src/memory_zeroinit/stark.rs | 59 +++++------------------ 6 files changed, 45 insertions(+), 118 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 975f98658..8489be9d6 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -1,6 +1,6 @@ use core::fmt::Debug; -use expr::{Expr, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::BitshiftView; diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 7fe09538f..8b708e549 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -1,6 +1,6 @@ use core::fmt::Debug; -use expr::{Expr, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::{CpuState, OpSelectors}; diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index 27e696b0f..352ae3b8f 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -1,11 +1,11 @@ use core::fmt::Debug; -use expr::{Expr, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::CpuSkeleton; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::stark::mozak_stark::PublicInputs; // TODO: fix StarkNameDisplay? @@ -30,7 +30,7 @@ impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for CpuSkeletonConstrai fn generate_constraints<'a, T: Debug + Copy>( &self, - vars: &StarkFrameTyped>, PublicInputs>>, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index 17c96e5f1..ae3320f2c 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -1,26 +1,19 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::memory::columns::Memory; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct MemoryStark { - pub _f: PhantomData, -} +pub struct MemoryConstraints {} + +pub type MemoryStark = + StarkFrom; impl HasNamedColumns for MemoryStark { type Columns = Memory; @@ -29,12 +22,13 @@ impl HasNamedColumns for MemoryStark { const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryStark { - type PublicInputs = NoColumns; - type View = Memory; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for MemoryConstraints { + type PublicInputs = NoColumns; + type View = Memory; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Debug + Copy>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; @@ -86,43 +80,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryS } } -impl, const D: usize> Stark for MemoryStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let vars = eb.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); - build_packed(constraints, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let vars = eb.to_typed_starkframe(vars); - let constraints = Self::generate_constraints(&vars); - build_ext(constraints, builder, consumer); - } -} - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index e6629d86b..302418a04 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -1,3 +1,4 @@ +use core::fmt::Debug; use std::marker::PhantomData; use expr::{Expr, ExprBuilder, StarkFrameTyped}; @@ -12,29 +13,31 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ + build_ext, build_packed, ConstraintBuilder, GenerateConstraints, StarkFrom, Vars, +}; use crate::memory_halfword::columns::{HalfWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct HalfWordMemoryStark { - pub _f: PhantomData, -} +pub struct HalfWordMemoryConstraints {} + +pub type HalfWordMemoryStark = + StarkFrom; impl HasNamedColumns for HalfWordMemoryStark { type Columns = HalfWordMemory; } -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for HalfWordMemoryStark -{ - type PublicInputs = NoColumns; - type View = HalfWordMemory; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for HalfWordMemoryConstraints { + type PublicInputs = NoColumns; + type View = HalfWordMemory; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Debug + Copy>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 97755e961..2dcdbcd3d 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -14,14 +14,17 @@ use starky::stark::Stark; use super::columns::MemoryZeroInit; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ + build_ext, build_packed, ConstraintBuilder, GenerateConstraints, StarkFrom, Vars, +}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct MemoryZeroInitStark { - pub _f: PhantomData, -} +pub struct MemoryZeroInitConstraints {} + +pub type MemoryZeroInitStark = + StarkFrom; impl HasNamedColumns for MemoryZeroInitStark { type Columns = MemoryZeroInit; @@ -30,14 +33,13 @@ impl HasNamedColumns for MemoryZeroInitStark { const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> - for MemoryZeroInitStark -{ - type PublicInputs = NoColumns; - type View = MemoryZeroInit; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for MemoryZeroInitConstraints { + type PublicInputs = NoColumns; + type View = MemoryZeroInit; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Debug + Copy>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -48,41 +50,6 @@ impl<'a, F, T: Copy + Debug + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for MemoryZeroInitStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use plonky2::plonk::config::{GenericConfig, Poseidon2GoldilocksConfig}; From 22493b7547d7fa4f48839db299a51a1b8cc01292 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Fri, 21 Jun 2024 10:48:31 +0200 Subject: [PATCH 74/84] Working version --- circuits/src/expr.rs | 18 +++++- circuits/src/generation.rs | 24 ++++--- circuits/src/lib.rs | 1 + circuits/src/memory_halfword/stark.rs | 47 +------------- circuits/src/memory_zeroinit/stark.rs | 15 +---- circuits/src/memoryinit/stark.rs | 67 ++++--------------- circuits/src/ops/add/stark.rs | 68 ++++---------------- circuits/src/poseidon2/stark.rs | 67 ++++--------------- circuits/src/poseidon2_output_bytes/stark.rs | 67 ++++--------------- circuits/src/poseidon2_sponge/stark.rs | 67 ++++--------------- circuits/src/rangecheck_u8/stark.rs | 68 ++++---------------- circuits/src/register/general/stark.rs | 66 ++++--------------- circuits/src/storage_device/stark.rs | 68 ++++---------------- circuits/src/tape_commitments/stark.rs | 68 ++++---------------- circuits/src/unstark.rs | 36 +++-------- circuits/src/xor/stark.rs | 67 ++++--------------- 16 files changed, 173 insertions(+), 641 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 46f278366..aca899c90 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -219,7 +219,7 @@ pub trait GenerateConstraints type View: From<[E; COLUMNS]> + FromIterator; type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator; - fn generate_constraints<'a, T: Debug>( + fn generate_constraints<'a, T: Copy + Debug>( &self, vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder>; @@ -231,6 +231,22 @@ pub struct StarkFrom, } +// Passthrough to G +impl GenerateConstraints for StarkFrom +where G: GenerateConstraints, + { + type View = G::View ; + + type PublicInputs = G::PublicInputs; + + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, + ) -> ConstraintBuilder> { + self.witness.generate_constraints(vars) + } +} + impl Display for StarkFrom { diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 9a9383fa0..a68e3e639 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -4,7 +4,7 @@ use std::borrow::Borrow; use std::fmt::{Debug, Display}; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::{ExprBuilder, StarkFrameTyped}; use itertools::{izip, Itertools}; use log::debug; use mozak_runner::elf::Program; @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, ConstraintType, GenerateConstraints, PublicInputsOf, Vars, ViewOf}; +use crate::expr::{build_debug, ConstraintType, GenerateConstraints, Vars, ViewOf}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -208,20 +208,18 @@ pub fn debug_traces, const D: usize>( .build(); all_starks!(mozak_stark, |stark, kind| { - debug_single_trace::(stark, &traces_poly_values[kind], public_inputs[kind]); + debug_single_trace::(stark, &traces_poly_values[kind], public_inputs[kind]); }); } -pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S>( +pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S, const COLUMNS: usize, const PUBLIC_INPUTS: usize>( stark: &'a S, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where - for<'b> S: Stark + Display + GenerateConstraints<'b, F>, - for<'b> PublicInputsOf<'b, S, F, Expr<'b, F>>: FromIterator>, - for<'b> ViewOf<'b, S, F, Expr<'b, F>>: FromIterator>, - for<'b> PublicInputsOf<'b, S, F, F>: Debug + FromIterator, - for<'b> ViewOf<'b, S, F, F>: Debug + FromIterator, { + S: Stark + Display + GenerateConstraints, + ViewOf<'a, S, F, COLUMNS, PUBLIC_INPUTS>: Debug, + { transpose_polys::(trace_rows.to_vec()) .iter() .enumerate() @@ -230,8 +228,8 @@ pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S>( let expr_builder = ExprBuilder::default(); let frame: StarkFrameTyped, Vec> = StarkFrameTyped::from_values(lv, nv, public_inputs); - let vars: Vars = expr_builder.inject_starkframe(frame); - let constraints = S::generate_constraints(&vars); + let vars: Vars = expr_builder.inject_starkframe(frame); + let constraints = stark.generate_constraints(&vars); let evaluated = build_debug(constraints); // Filter out only applicable constraints @@ -258,8 +256,8 @@ pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S>( ); } - let lv: ViewOf = lv.iter().copied().collect(); - let nv: ViewOf = nv.iter().copied().collect(); + let lv: ViewOf = lv.iter().copied().collect(); + let nv: ViewOf = nv.iter().copied().collect(); log::error!("Debug constraints for {stark}"); log::error!("lv-row[{lv_row}] - values: {lv:?}"); log::error!("nv-row[{nv_row}] - values: {nv:?}"); diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index db07c3cbc..8668a5541 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -5,6 +5,7 @@ #![allow(clippy::missing_panics_doc)] #![allow(clippy::missing_errors_doc)] #![feature(const_trait_impl)] +#![feature(generic_arg_infer)] pub mod bitshift; pub mod columns_view; diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 302418a04..745294dc8 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -1,20 +1,11 @@ use core::fmt::Debug; -use std::marker::PhantomData; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use crate::columns_view::HasNamedColumns; use crate::expr::{ - build_ext, build_packed, ConstraintBuilder, GenerateConstraints, StarkFrom, Vars, + ConstraintBuilder, GenerateConstraints, StarkFrom, Vars, }; use crate::memory_halfword::columns::{HalfWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; @@ -60,40 +51,6 @@ impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for HalfWordMemoryConst const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; -impl, const D: usize> Stark for HalfWordMemoryStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} #[cfg(test)] mod tests { diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 2dcdbcd3d..ac1772f09 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -1,22 +1,11 @@ use core::fmt::Debug; -use std::marker::PhantomData; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::MemoryZeroInit; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{ - build_ext, build_packed, ConstraintBuilder, GenerateConstraints, StarkFrom, Vars, -}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 27439c83e..2d0692c9d 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -1,26 +1,19 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::MemoryInit; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct MemoryInitStark { - pub _f: PhantomData, -} +pub struct MemoryInitConstraints {} + +pub type MemoryInitStark = + StarkFrom; impl HasNamedColumns for MemoryInitStark { type Columns = MemoryInit; @@ -29,12 +22,13 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryInitStark { - type PublicInputs = NoColumns; - type View = MemoryInit; +impl GenerateConstraints<{ COLUMNS}, { PUBLIC_INPUTS}> for MemoryInitConstraints { + type PublicInputs = NoColumns; + type View = MemoryInit; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Debug + Copy>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -45,41 +39,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for MemoryI } } -impl, const D: usize> Stark for MemoryInitStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use plonky2::plonk::config::{GenericConfig, Poseidon2GoldilocksConfig}; diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 2caaf19d5..9a5c7b637 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -1,26 +1,18 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::Add; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct AddStark { - pub _f: PhantomData, -} +pub struct AddConstraints {} + +pub type AddStark = StarkFrom; impl HasNamedColumns for AddStark { type Columns = Add; @@ -29,12 +21,13 @@ impl HasNamedColumns for AddStark { const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for AddStark { - type PublicInputs = NoColumns; - type View = Add; +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for AddConstraints { + type PublicInputs = NoColumns; + type View = Add; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -49,39 +42,4 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for AddStar constraints } -} - -impl, const D: usize> Stark for AddStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - constraint_consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_packed(constraints, constraint_consumer); - } - - fn eval_ext_circuit( - &self, - circuit_builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - constraint_consumer: &mut RecursiveConstraintConsumer, - ) { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_ext(constraints, circuit_builder, constraint_consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} +} \ No newline at end of file diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 4e1b4ed41..f8d56f581 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -1,22 +1,14 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::goldilocks_field::GoldilocksField; -use plonky2::field::packed::PackedField; use plonky2::field::types::{Field, PrimeField64}; -use plonky2::hash::hash_types::RichField; use plonky2::hash::poseidon2::Poseidon2; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::Poseidon2State; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::poseidon2::columns::{NUM_POSEIDON2_COLS, ROUNDS_F, ROUNDS_P, STATE_SIZE}; use crate::unstark::NoColumns; @@ -115,9 +107,10 @@ where #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct Poseidon2_12Stark { - pub _f: PhantomData, -} +pub struct Poseidon2_12Constraints {} + +pub type Poseidon2_12Stark = +StarkFrom; impl HasNamedColumns for Poseidon2_12Stark { type Columns = Poseidon2State; @@ -129,17 +122,18 @@ const PUBLIC_INPUTS: usize = 0; // Compile time assertion that STATE_SIZE equals 12 const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for Poseidon2_12Stark +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> +for Poseidon2_12Constraints { - type PublicInputs = NoColumns; - type View = Poseidon2State; + type PublicInputs = NoColumns; + type View = Poseidon2State; // NOTE: This one has extra constraints compared to different implementations of // `generate_constraints` that were have written so far. It will be something // to take into account when providing a more geneeral API to plonky. - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -202,39 +196,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for Poseidon2_12Stark { - type EvaluationFrame = StarkFrame - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } -} #[cfg(test)] mod tests { diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index d167b873e..d6ad19414 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -1,27 +1,19 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::{FIELDS_COUNT, NUM_POSEIDON2_OUTPUT_BYTES_COLS}; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::poseidon2_output_bytes::columns::Poseidon2OutputBytes; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct Poseidon2OutputBytesStark { - pub _f: PhantomData, -} +pub struct Poseidon2OutputBytesConstraints {} + +pub type Poseidon2OutputBytesStark = StarkFrom; impl HasNamedColumns for Poseidon2OutputBytesStark { type Columns = Poseidon2OutputBytes; @@ -30,14 +22,15 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for Poseidon2OutputBytesStark +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> +for Poseidon2OutputBytesConstraints { - type PublicInputs = NoColumns; - type View = Poseidon2OutputBytes; + type PublicInputs = NoColumns; + type View = Poseidon2OutputBytes; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -58,40 +51,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for Poseidon2OutputBytesStark { - type EvaluationFrame = StarkFrame - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 9c2f8358b..929f95aa2 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -1,30 +1,25 @@ +use core::fmt::Debug; use std::marker::PhantomData; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; use plonky2::hash::hashing::PlonkyPermutation; use plonky2::hash::poseidon2::{Poseidon2, Poseidon2Permutation}; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::NUM_POSEIDON2_SPONGE_COLS; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::poseidon2_sponge::columns::Poseidon2Sponge; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct Poseidon2SpongeStark { - pub _f: PhantomData, +pub struct Poseidon2SpongeConstraints { + _f: PhantomData, } +pub type Poseidon2SpongeStark = StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; + impl HasNamedColumns for Poseidon2SpongeStark { type Columns = Poseidon2Sponge; } @@ -32,15 +27,16 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F: Poseidon2, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for Poseidon2SpongeStark +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS} > + for Poseidon2SpongeConstraints { - type PublicInputs = NoColumns; - type View = Poseidon2Sponge; + type PublicInputs = NoColumns; + type View = Poseidon2Sponge; // For design check https://docs.google.com/presentation/d/10Dv00xL3uggWTPc0L91cgu_dWUzhM7l1EQ5uDEI_cjg/edit?usp=sharing - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let rate = Poseidon2Permutation::::RATE; let state_size = Poseidon2Permutation::::WIDTH; @@ -108,41 +104,6 @@ impl<'a, F: Poseidon2, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for Poseidon2SpongeStark { - type EvaluationFrame = StarkFrame - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - #[allow(clippy::similar_names)] - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 7974ef406..ffc28d670 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -1,26 +1,18 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::RangeCheckU8; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct RangeCheckU8Stark { - pub _f: PhantomData, -} +pub struct RangeCheckU8Constraints {} + +pub type RangeCheckU8Stark = StarkFrom; impl HasNamedColumns for RangeCheckU8Stark { type Columns = RangeCheckU8; @@ -29,14 +21,15 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for RangeCheckU8Stark +impl GenerateConstraints< { COLUMNS }, { PUBLIC_INPUTS }> + for RangeCheckU8Constraints { - type PublicInputs = NoColumns; - type View = RangeCheckU8; + type PublicInputs = NoColumns; + type View = RangeCheckU8; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; @@ -51,41 +44,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for RangeCheckU8Stark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use plonky2::plonk::config::{GenericConfig, Poseidon2GoldilocksConfig}; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 5c68831d5..1b73f44d1 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -1,26 +1,18 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::Register; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct RegisterStark { - pub _f: PhantomData, -} +pub struct RegisterConstraints {} + +pub type RegisterStark = StarkFrom; impl HasNamedColumns for RegisterStark { type Columns = Register; @@ -29,9 +21,9 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for RegisterStark { - type PublicInputs = NoColumns; - type View = Register; +impl GenerateConstraints<{COLUMNS}, {PUBLIC_INPUTS}> for RegisterConstraints { + type PublicInputs = NoColumns; + type View = Register; /// Constraints for the [`RegisterStark`]: /// @@ -48,8 +40,9 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for Registe /// /// For more details, refer to the [Notion /// document](https://www.notion.so/0xmozak/Register-File-STARK-62459d68aea648a0abf4e97aa0093ea2). - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; @@ -91,41 +84,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for Registe } } -impl, const D: usize> Stark for RegisterStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 4dcffb266..52bc65281 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -1,26 +1,18 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use crate::columns_view::HasNamedColumns; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::storage_device::columns::{StorageDevice, NUM_STORAGE_DEVICE_COLS}; use crate::unstark::NoColumns; #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct StorageDeviceStark { - pub _f: PhantomData, -} +pub struct StorageDeviceConstraints {} + +pub type StorageDeviceStark = StarkFrom; impl HasNamedColumns for StorageDeviceStark { type Columns = StorageDevice; @@ -29,15 +21,16 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for StorageDeviceStark +impl GenerateConstraints<{ COLUMNS }, {PUBLIC_INPUTS}> + for StorageDeviceConstraints { - type PublicInputs = NoColumns; - type View = StorageDevice; + type PublicInputs = NoColumns; + type View = StorageDevice; // Design description - https://docs.google.com/presentation/d/1J0BJd49BMQh3UR5TrOhe3k67plHxnohFtFVrMpDJ1oc/edit?usp=sharing - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let nv = vars.next_values; @@ -86,41 +79,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> } } -impl, const D: usize> Stark for StorageDeviceStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } -} - #[cfg(test)] mod tests { use itertools::Itertools; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 9721b42c1..31532cf7c 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -1,29 +1,22 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::TapeCommitments; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> - for TapeCommitmentsStark +impl GenerateConstraints + for TapeCommitmentsConstraints { - type PublicInputs = NoColumns; - type View = TapeCommitments; + type PublicInputs = NoColumns; + type View = TapeCommitments; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv: &TapeCommitments> = &vars.local_values; let mut constraint = ConstraintBuilder::default(); @@ -43,9 +36,9 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> #[derive(Copy, Clone, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct TapeCommitmentsStark { - pub _f: PhantomData, -} +pub struct TapeCommitmentsConstraints {} + +pub type TapeCommitmentsStark = StarkFrom; impl HasNamedColumns for TapeCommitmentsStark { type Columns = TapeCommitments; @@ -54,41 +47,6 @@ impl HasNamedColumns for TapeCommitmentsStark { const COLUMNS: usize = TapeCommitments::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl, const D: usize> Stark for TapeCommitmentsStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } - - fn eval_ext_circuit( - &self, - builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let eb = ExprBuilder::default(); - let constraints = Self::generate_constraints(&eb.to_typed_starkframe(vars)); - build_ext(constraints, builder, consumer); - } -} - #[cfg(test)] mod tests { use itertools::chain; diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 533089981..27ebcfa88 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -2,7 +2,7 @@ use core::fmt::Debug; use std::fmt::Display; use std::marker::PhantomData; -use expr::{Expr, StarkFrameTyped}; +use expr::Expr; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::packed::PackedField; use plonky2::hash::hash_types::RichField; @@ -13,38 +13,20 @@ use starky::evaluation_frame::StarkFrame; use starky::stark::Stark; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, Vars}; -impl< - 'a, - F: 'a, - NAME: 'a, - T: 'a + std::fmt::Debug, - const D: usize, - Columns: 'a, - const COLUMNS: usize, - > GenerateConstraints<'a, T, COLUMNS, PUBLIC_INPUTS> - for Unstark +impl + GenerateConstraints for Unstark { - type PublicInputs = NoColumns; - type View = ShadowColumns; + type PublicInputs = NoColumns; + type View = ShadowColumns; - fn generate_constraints( - self, - _vars: &StarkFrameTyped, { COLUMNS }>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + _vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { ConstraintBuilder::default() } - - fn exists( - self, - ) -> impl GenerateConstraints<'a, U, COLUMNS, PUBLIC_INPUTS> { - Unstark:: { - _f: PhantomData::::default(), - _name: self._name, - _d: self._d, - } - } } /// Template for a STARK with zero internal constraints. Use this if the STARK diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 418fdbe9a..d79acd7a6 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -1,27 +1,19 @@ -use std::marker::PhantomData; +use core::fmt::Debug; -use expr::{Expr, ExprBuilder, StarkFrameTyped}; +use expr::Expr; use itertools::{chain, izip}; use mozak_circuits_derive::StarkNameDisplay; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; use super::columns::XorColumnsView; use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{build_ext, build_packed, ConstraintBuilder, GenerateConstraints}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; #[derive(Clone, Copy, Default, StarkNameDisplay)] #[allow(clippy::module_name_repetitions)] -pub struct XorStark { - pub _f: PhantomData, -} +pub struct XorConstraints {} + +pub type XorStark = StarkFrom; impl HasNamedColumns for XorStark { type Columns = XorColumnsView; @@ -30,12 +22,14 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for XorStark { - type PublicInputs = NoColumns; - type View = XorColumnsView; +impl GenerateConstraints for XorConstraints +{ + type PublicInputs = NoColumns; + type View = XorColumnsView; - fn generate_constraints( - vars: &StarkFrameTyped>, NoColumns>>, + fn generate_constraints<'a, T: Copy + Debug>( + &self, + vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, ) -> ConstraintBuilder> { let lv = vars.local_values; let mut constraints = ConstraintBuilder::default(); @@ -63,41 +57,6 @@ impl<'a, F, T: Copy + 'a, const D: usize> GenerateConstraints<'a, T> for XorStar } } -impl, const D: usize> Stark for XorStark { - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - vars: &Self::EvaluationFrame, - consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_packed(constraints, consumer); - } - - fn constraint_degree(&self) -> usize { 3 } - - fn eval_ext_circuit( - &self, - circuit_builder: &mut CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - consumer: &mut RecursiveConstraintConsumer, - ) { - let expr_builder = ExprBuilder::default(); - let constraints = Self::generate_constraints(&expr_builder.to_typed_starkframe(vars)); - build_ext(constraints, circuit_builder, consumer); - } -} - #[cfg(test)] mod tests { use anyhow::Result; From e3dcd5e28860d28ba830b2553c2304405c358612 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Fri, 21 Jun 2024 10:55:47 +0200 Subject: [PATCH 75/84] cargo fmt --- circuits/src/expr.rs | 11 ++++++----- circuits/src/generation.rs | 12 +++++++++--- circuits/src/memory_halfword/stark.rs | 4 +--- circuits/src/memoryinit/stark.rs | 2 +- circuits/src/ops/add/stark.rs | 5 +++-- circuits/src/poseidon2/stark.rs | 7 ++----- circuits/src/poseidon2_output_bytes/stark.rs | 7 +++---- circuits/src/poseidon2_sponge/stark.rs | 5 +++-- circuits/src/rangecheck_u8/stark.rs | 7 +++---- circuits/src/register/general/stark.rs | 5 +++-- circuits/src/storage_device/stark.rs | 7 +++---- circuits/src/tape_commitments/stark.rs | 7 +++---- circuits/src/xor/stark.rs | 6 +++--- 13 files changed, 43 insertions(+), 42 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index aca899c90..022fbb057 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -232,12 +232,13 @@ pub struct StarkFrom GenerateConstraints for StarkFrom -where G: GenerateConstraints, - { - type View = G::View ; - +impl + GenerateConstraints for StarkFrom +where + G: GenerateConstraints, +{ type PublicInputs = G::PublicInputs; + type View = G::View; fn generate_constraints<'a, T: Copy + Debug>( &self, diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index a68e3e639..67d38175e 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -212,14 +212,20 @@ pub fn debug_traces, const D: usize>( }); } -pub fn debug_single_trace<'a, F: RichField + Extendable, const D: usize, S, const COLUMNS: usize, const PUBLIC_INPUTS: usize>( +pub fn debug_single_trace< + 'a, + F: RichField + Extendable, + const D: usize, + S, + const COLUMNS: usize, + const PUBLIC_INPUTS: usize, +>( stark: &'a S, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where S: Stark + Display + GenerateConstraints, - ViewOf<'a, S, F, COLUMNS, PUBLIC_INPUTS>: Debug, - { + ViewOf<'a, S, F, COLUMNS, PUBLIC_INPUTS>: Debug, { transpose_polys::(trace_rows.to_vec()) .iter() .enumerate() diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 745294dc8..627cd695d 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -4,9 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use crate::columns_view::HasNamedColumns; -use crate::expr::{ - ConstraintBuilder, GenerateConstraints, StarkFrom, Vars, -}; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::memory_halfword::columns::{HalfWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 2d0692c9d..12e6a16c1 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -22,7 +22,7 @@ impl HasNamedColumns for MemoryInitStark { const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints<{ COLUMNS}, { PUBLIC_INPUTS}> for MemoryInitConstraints { +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for MemoryInitConstraints { type PublicInputs = NoColumns; type View = MemoryInit; diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 9a5c7b637..72a184620 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -12,7 +12,8 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct AddConstraints {} -pub type AddStark = StarkFrom; +pub type AddStark = + StarkFrom; impl HasNamedColumns for AddStark { type Columns = Add; @@ -42,4 +43,4 @@ impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for AddConstraints { constraints } -} \ No newline at end of file +} diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index f8d56f581..ca7342c53 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -110,7 +110,7 @@ where pub struct Poseidon2_12Constraints {} pub type Poseidon2_12Stark = -StarkFrom; + StarkFrom; impl HasNamedColumns for Poseidon2_12Stark { type Columns = Poseidon2State; @@ -122,9 +122,7 @@ const PUBLIC_INPUTS: usize = 0; // Compile time assertion that STATE_SIZE equals 12 const _UNUSED_STATE_SIZE_IS_12: [(); STATE_SIZE - 12] = []; -impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> -for Poseidon2_12Constraints -{ +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for Poseidon2_12Constraints { type PublicInputs = NoColumns; type View = Poseidon2State; @@ -196,7 +194,6 @@ for Poseidon2_12Constraints } } - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index d6ad19414..26cbaa62c 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -13,7 +13,8 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct Poseidon2OutputBytesConstraints {} -pub type Poseidon2OutputBytesStark = StarkFrom; +pub type Poseidon2OutputBytesStark = + StarkFrom; impl HasNamedColumns for Poseidon2OutputBytesStark { type Columns = Poseidon2OutputBytes; @@ -22,9 +23,7 @@ impl HasNamedColumns for Poseidon2OutputBytesStark { const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> -for Poseidon2OutputBytesConstraints -{ +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for Poseidon2OutputBytesConstraints { type PublicInputs = NoColumns; type View = Poseidon2OutputBytes; diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 929f95aa2..8120cfeb1 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -18,7 +18,8 @@ pub struct Poseidon2SpongeConstraints { _f: PhantomData, } -pub type Poseidon2SpongeStark = StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; +pub type Poseidon2SpongeStark = + StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; impl HasNamedColumns for Poseidon2SpongeStark { type Columns = Poseidon2Sponge; @@ -27,7 +28,7 @@ impl HasNamedColumns for Poseidon2SpongeStark { const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS} > +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for Poseidon2SpongeConstraints { type PublicInputs = NoColumns; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index ffc28d670..079db4f78 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -12,7 +12,8 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct RangeCheckU8Constraints {} -pub type RangeCheckU8Stark = StarkFrom; +pub type RangeCheckU8Stark = + StarkFrom; impl HasNamedColumns for RangeCheckU8Stark { type Columns = RangeCheckU8; @@ -21,9 +22,7 @@ impl HasNamedColumns for RangeCheckU8Stark { const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints< { COLUMNS }, { PUBLIC_INPUTS }> - for RangeCheckU8Constraints -{ +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for RangeCheckU8Constraints { type PublicInputs = NoColumns; type View = RangeCheckU8; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 1b73f44d1..0f6ea0be7 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -12,7 +12,8 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct RegisterConstraints {} -pub type RegisterStark = StarkFrom; +pub type RegisterStark = + StarkFrom; impl HasNamedColumns for RegisterStark { type Columns = Register; @@ -21,7 +22,7 @@ impl HasNamedColumns for RegisterStark { const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints<{COLUMNS}, {PUBLIC_INPUTS}> for RegisterConstraints { +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for RegisterConstraints { type PublicInputs = NoColumns; type View = Register; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 52bc65281..f642c6262 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -12,7 +12,8 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct StorageDeviceConstraints {} -pub type StorageDeviceStark = StarkFrom; +pub type StorageDeviceStark = + StarkFrom; impl HasNamedColumns for StorageDeviceStark { type Columns = StorageDevice; @@ -21,9 +22,7 @@ impl HasNamedColumns for StorageDeviceStark { const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints<{ COLUMNS }, {PUBLIC_INPUTS}> - for StorageDeviceConstraints -{ +impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for StorageDeviceConstraints { type PublicInputs = NoColumns; type View = StorageDevice; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 31532cf7c..2f31b3410 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -8,9 +8,7 @@ use crate::columns_view::{HasNamedColumns, NumberOfColumns}; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; -impl GenerateConstraints - for TapeCommitmentsConstraints -{ +impl GenerateConstraints for TapeCommitmentsConstraints { type PublicInputs = NoColumns; type View = TapeCommitments; @@ -38,7 +36,8 @@ impl GenerateConstraints #[allow(clippy::module_name_repetitions)] pub struct TapeCommitmentsConstraints {} -pub type TapeCommitmentsStark = StarkFrom; +pub type TapeCommitmentsStark = + StarkFrom; impl HasNamedColumns for TapeCommitmentsStark { type Columns = TapeCommitments; diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index d79acd7a6..39c70c5a7 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -13,7 +13,8 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct XorConstraints {} -pub type XorStark = StarkFrom; +pub type XorStark = + StarkFrom; impl HasNamedColumns for XorStark { type Columns = XorColumnsView; @@ -22,8 +23,7 @@ impl HasNamedColumns for XorStark { const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; -impl GenerateConstraints for XorConstraints -{ +impl GenerateConstraints for XorConstraints { type PublicInputs = NoColumns; type View = XorColumnsView; From bc62799075011b638845edab7fd42a32ffa1c635 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Fri, 21 Jun 2024 11:16:46 +0200 Subject: [PATCH 76/84] Migrate Unstark to a StarkFrom --- circuits/src/unstark.rs | 72 +++++++++-------------------------------- 1 file changed, 15 insertions(+), 57 deletions(-) diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 27ebcfa88..8557fd630 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -3,20 +3,12 @@ use std::fmt::Display; use std::marker::PhantomData; use expr::Expr; -use plonky2::field::extension::{Extendable, FieldExtension}; -use plonky2::field::packed::PackedField; -use plonky2::hash::hash_types::RichField; -use plonky2::iop::ext_target::ExtensionTarget; -use plonky2::plonk::circuit_builder::CircuitBuilder; -use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use starky::evaluation_frame::StarkFrame; -use starky::stark::Stark; - -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; -use crate::expr::{ConstraintBuilder, GenerateConstraints, Vars}; - -impl - GenerateConstraints for Unstark + +use crate::columns_view::HasNamedColumns; +use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; + +impl GenerateConstraints + for UnstarkConstraints { type PublicInputs = NoColumns; type View = ShadowColumns; @@ -29,19 +21,21 @@ impl } } -/// Template for a STARK with zero internal constraints. Use this if the STARK -/// itself does not need any built-in constraints, but rely on cross table -/// lookups for provability. #[derive(Copy, Clone, Default)] #[allow(clippy::module_name_repetitions)] -pub struct Unstark { - pub _f: PhantomData, +pub struct UnstarkConstraints { pub _name: PhantomData, pub _d: PhantomData, } -impl Display - for Unstark +/// Template for a STARK with zero internal constraints. Use this if the STARK +/// itself does not need any built-in constraints, but rely on cross table +/// lookups for provability. +pub type Unstark = + StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; + +impl Display + for UnstarkConstraints { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", NAME::default()) @@ -56,42 +50,6 @@ impl HasNamedColumns const PUBLIC_INPUTS: usize = 0; -impl< - F: RichField + Extendable, - NAME: Sync, - const D: usize, - Columns: Sync + NumberOfColumns, - const COLUMNS: usize, - > Stark for Unstark -{ - type EvaluationFrame = StarkFrame - - where - FE: FieldExtension, - P: PackedField; - type EvaluationFrameTarget = - StarkFrame, ExtensionTarget, COLUMNS, PUBLIC_INPUTS>; - - fn eval_packed_generic( - &self, - _vars: &Self::EvaluationFrame, - _constraint_consumer: &mut ConstraintConsumer

, - ) where - FE: FieldExtension, - P: PackedField, { - } - - fn eval_ext_circuit( - &self, - _builder: &mut CircuitBuilder, - _vars: &Self::EvaluationFrameTarget, - _constraint_consumer: &mut RecursiveConstraintConsumer, - ) { - } - - fn constraint_degree(&self) -> usize { 3 } -} - // Simple marco to create a type holding the name for the Unstark macro_rules! impl_name { ($alias:ident, $name:ident) => { From 07bbbcabaf39061284c3176a4744e40a9cbd25ed Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Fri, 21 Jun 2024 11:32:39 +0200 Subject: [PATCH 77/84] Remove HasNamedColumns --- circuits/src/bitshift/stark.rs | 7 +------ circuits/src/columns_view.rs | 4 ---- circuits/src/cpu/stark.rs | 6 +----- circuits/src/cpu_skeleton/stark.rs | 6 +----- circuits/src/memory/stark.rs | 6 +----- circuits/src/memory_fullword/stark.rs | 5 ----- circuits/src/memory_halfword/stark.rs | 5 ----- circuits/src/memory_zeroinit/stark.rs | 6 +----- circuits/src/memoryinit/stark.rs | 6 +----- circuits/src/ops/add/stark.rs | 6 +----- circuits/src/poseidon2/stark.rs | 5 ----- circuits/src/poseidon2_output_bytes/stark.rs | 5 ----- circuits/src/poseidon2_sponge/stark.rs | 5 ----- circuits/src/rangecheck_u8/stark.rs | 6 +----- circuits/src/register/general/stark.rs | 6 +----- circuits/src/storage_device/stark.rs | 5 ----- circuits/src/tape_commitments/stark.rs | 6 +----- circuits/src/unstark.rs | 7 ------- circuits/src/xor/stark.rs | 6 +----- 19 files changed, 11 insertions(+), 97 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 8489be9d6..90a963f6b 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::BitshiftView; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -19,11 +19,6 @@ const PUBLIC_INPUTS: usize = 0; pub type BitshiftStark = StarkFrom; -// TODO: consider folding this into `GenerateConstraints` trait. -impl HasNamedColumns for BitshiftStark { - type Columns = BitshiftView; -} - impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for BitshiftConstraints { type PublicInputs = NoColumns; type View = BitshiftView; diff --git a/circuits/src/columns_view.rs b/circuits/src/columns_view.rs index b2daf2eef..01a2ae961 100644 --- a/circuits/src/columns_view.rs +++ b/circuits/src/columns_view.rs @@ -27,10 +27,6 @@ pub(crate) const unsafe fn transmute_ref(t: &T) -> &U { &*(std::ptr::from_ref::(t).cast::()) } -pub trait HasNamedColumns { - type Columns; -} - pub trait NumberOfColumns { const NUMBER_OF_COLUMNS: usize; } diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index 8b708e549..fb1eee457 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -5,7 +5,7 @@ use mozak_circuits_derive::StarkNameDisplay; use super::columns::{CpuState, OpSelectors}; use super::{bitwise, branches, div, ecall, jalr, memory, mul, signed_comparison, sub}; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::cpu::shift; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -20,10 +20,6 @@ pub struct CpuConstraints {} /// Instructions are either handled directly or through cross table lookup pub type CpuStark = StarkFrom; -impl HasNamedColumns for CpuStark { - type Columns = CpuState; -} - /// Ensure that if opcode is straight line, then program counter is incremented /// by 4. fn pc_ticks_up<'a, P: Copy>(lv: &CpuState>, cb: &mut ConstraintBuilder>) { diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index 352ae3b8f..d93281124 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::CpuSkeleton; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::stark::mozak_stark::PublicInputs; @@ -16,10 +16,6 @@ pub struct CpuSkeletonConstraints {} pub type CpuSkeletonStark = StarkFrom; -impl HasNamedColumns for CpuSkeletonStark { - type Columns = CpuSkeleton; -} - const COLUMNS: usize = CpuSkeleton::<()>::NUMBER_OF_COLUMNS; // Public inputs: [PC of the first row] const PUBLIC_INPUTS: usize = PublicInputs::<()>::NUMBER_OF_COLUMNS; diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index ae3320f2c..d7404118f 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -3,7 +3,7 @@ use core::fmt::Debug; use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::memory::columns::Memory; use crate::unstark::NoColumns; @@ -15,10 +15,6 @@ pub struct MemoryConstraints {} pub type MemoryStark = StarkFrom; -impl HasNamedColumns for MemoryStark { - type Columns = Memory; -} - const COLUMNS: usize = Memory::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 5bbcbec95..211e3cfe5 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -4,7 +4,6 @@ use expr::Expr; use itertools::izip; use mozak_circuits_derive::StarkNameDisplay; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::memory_fullword::columns::{FullWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; @@ -16,10 +15,6 @@ pub struct FullWordMemoryConstraints {} pub type FullWordMemoryStark = StarkFrom; -impl HasNamedColumns for FullWordMemoryStark { - type Columns = FullWordMemory; -} - const COLUMNS: usize = NUM_HW_MEM_COLS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index 627cd695d..ba73f2d1a 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -3,7 +3,6 @@ use core::fmt::Debug; use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::memory_halfword::columns::{HalfWordMemory, NUM_HW_MEM_COLS}; use crate::unstark::NoColumns; @@ -15,10 +14,6 @@ pub struct HalfWordMemoryConstraints {} pub type HalfWordMemoryStark = StarkFrom; -impl HasNamedColumns for HalfWordMemoryStark { - type Columns = HalfWordMemory; -} - impl GenerateConstraints<{ COLUMNS }, { PUBLIC_INPUTS }> for HalfWordMemoryConstraints { type PublicInputs = NoColumns; type View = HalfWordMemory; diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index ac1772f09..54a5e80b0 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::MemoryZeroInit; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -15,10 +15,6 @@ pub struct MemoryZeroInitConstraints {} pub type MemoryZeroInitStark = StarkFrom; -impl HasNamedColumns for MemoryZeroInitStark { - type Columns = MemoryZeroInit; -} - const COLUMNS: usize = MemoryZeroInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index 12e6a16c1..e48884279 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::MemoryInit; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -15,10 +15,6 @@ pub struct MemoryInitConstraints {} pub type MemoryInitStark = StarkFrom; -impl HasNamedColumns for MemoryInitStark { - type Columns = MemoryInit; -} - const COLUMNS: usize = MemoryInit::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 72a184620..324970c41 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::Add; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -15,10 +15,6 @@ pub struct AddConstraints {} pub type AddStark = StarkFrom; -impl HasNamedColumns for AddStark { - type Columns = Add; -} - const COLUMNS: usize = Add::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index ca7342c53..440459e00 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -7,7 +7,6 @@ use plonky2::field::types::{Field, PrimeField64}; use plonky2::hash::poseidon2::Poseidon2; use super::columns::Poseidon2State; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::poseidon2::columns::{NUM_POSEIDON2_COLS, ROUNDS_F, ROUNDS_P, STATE_SIZE}; use crate::unstark::NoColumns; @@ -112,10 +111,6 @@ pub struct Poseidon2_12Constraints {} pub type Poseidon2_12Stark = StarkFrom; -impl HasNamedColumns for Poseidon2_12Stark { - type Columns = Poseidon2State; -} - const COLUMNS: usize = NUM_POSEIDON2_COLS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index 26cbaa62c..1a9c33032 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -4,7 +4,6 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::{FIELDS_COUNT, NUM_POSEIDON2_OUTPUT_BYTES_COLS}; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::poseidon2_output_bytes::columns::Poseidon2OutputBytes; use crate::unstark::NoColumns; @@ -16,10 +15,6 @@ pub struct Poseidon2OutputBytesConstraints {} pub type Poseidon2OutputBytesStark = StarkFrom; -impl HasNamedColumns for Poseidon2OutputBytesStark { - type Columns = Poseidon2OutputBytes; -} - const COLUMNS: usize = NUM_POSEIDON2_OUTPUT_BYTES_COLS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index 8120cfeb1..ef5f85a16 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -7,7 +7,6 @@ use plonky2::hash::hashing::PlonkyPermutation; use plonky2::hash::poseidon2::{Poseidon2, Poseidon2Permutation}; use super::columns::NUM_POSEIDON2_SPONGE_COLS; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::poseidon2_sponge::columns::Poseidon2Sponge; use crate::unstark::NoColumns; @@ -21,10 +20,6 @@ pub struct Poseidon2SpongeConstraints { pub type Poseidon2SpongeStark = StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; -impl HasNamedColumns for Poseidon2SpongeStark { - type Columns = Poseidon2Sponge; -} - const COLUMNS: usize = NUM_POSEIDON2_SPONGE_COLS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index 079db4f78..f5b30fb84 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::RangeCheckU8; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -15,10 +15,6 @@ pub struct RangeCheckU8Constraints {} pub type RangeCheckU8Stark = StarkFrom; -impl HasNamedColumns for RangeCheckU8Stark { - type Columns = RangeCheckU8; -} - const COLUMNS: usize = RangeCheckU8::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index 0f6ea0be7..fbc761e66 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::Register; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -15,10 +15,6 @@ pub struct RegisterConstraints {} pub type RegisterStark = StarkFrom; -impl HasNamedColumns for RegisterStark { - type Columns = Register; -} - const COLUMNS: usize = Register::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index f642c6262..2227653e4 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -3,7 +3,6 @@ use core::fmt::Debug; use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::storage_device::columns::{StorageDevice, NUM_STORAGE_DEVICE_COLS}; use crate::unstark::NoColumns; @@ -15,10 +14,6 @@ pub struct StorageDeviceConstraints {} pub type StorageDeviceStark = StarkFrom; -impl HasNamedColumns for StorageDeviceStark { - type Columns = StorageDevice; -} - const COLUMNS: usize = NUM_STORAGE_DEVICE_COLS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 2f31b3410..7abf23cef 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -4,7 +4,7 @@ use expr::Expr; use mozak_circuits_derive::StarkNameDisplay; use super::columns::TapeCommitments; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -39,10 +39,6 @@ pub struct TapeCommitmentsConstraints {} pub type TapeCommitmentsStark = StarkFrom; -impl HasNamedColumns for TapeCommitmentsStark { - type Columns = TapeCommitments; -} - const COLUMNS: usize = TapeCommitments::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 8557fd630..a2f5bb2c3 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -4,7 +4,6 @@ use std::marker::PhantomData; use expr::Expr; -use crate::columns_view::HasNamedColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; impl GenerateConstraints @@ -42,12 +41,6 @@ impl Display } } -impl HasNamedColumns - for Unstark -{ - type Columns = Columns; -} - const PUBLIC_INPUTS: usize = 0; // Simple marco to create a type holding the name for the Unstark diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index 39c70c5a7..b6467ed10 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -5,7 +5,7 @@ use itertools::{chain, izip}; use mozak_circuits_derive::StarkNameDisplay; use super::columns::XorColumnsView; -use crate::columns_view::{HasNamedColumns, NumberOfColumns}; +use crate::columns_view::NumberOfColumns; use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; use crate::unstark::NoColumns; @@ -16,10 +16,6 @@ pub struct XorConstraints {} pub type XorStark = StarkFrom; -impl HasNamedColumns for XorStark { - type Columns = XorColumnsView; -} - const COLUMNS: usize = XorColumnsView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; From c2bad474170aca00f62649bc8fa521294f024ed7 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Fri, 21 Jun 2024 11:51:31 +0200 Subject: [PATCH 78/84] Match directly on StarkFrom --- circuits/src/generation.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 67d38175e..888bb4b75 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, ConstraintType, GenerateConstraints, Vars, ViewOf}; +use crate::expr::{build_debug, ConstraintType, GenerateConstraints, Vars, ViewOf, StarkFrom}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; @@ -216,17 +216,17 @@ pub fn debug_single_trace< 'a, F: RichField + Extendable, const D: usize, - S, + G, const COLUMNS: usize, const PUBLIC_INPUTS: usize, >( - stark: &'a S, + stark: &'a StarkFrom, trace_rows: &'a [PolynomialValues], public_inputs: &'a [F], ) where - S: Stark + Display + GenerateConstraints, - ViewOf<'a, S, F, COLUMNS, PUBLIC_INPUTS>: Debug, { - transpose_polys::(trace_rows.to_vec()) + G: Sync + Copy + Display + GenerateConstraints, + ViewOf<'a, G, F, COLUMNS, PUBLIC_INPUTS>: Debug, { + transpose_polys::>(trace_rows.to_vec()) .iter() .enumerate() .circular_tuple_windows() @@ -234,8 +234,8 @@ pub fn debug_single_trace< let expr_builder = ExprBuilder::default(); let frame: StarkFrameTyped, Vec> = StarkFrameTyped::from_values(lv, nv, public_inputs); - let vars: Vars = expr_builder.inject_starkframe(frame); - let constraints = stark.generate_constraints(&vars); + let vars: Vars = expr_builder.inject_starkframe(frame); + let constraints = stark.witness.generate_constraints(&vars); let evaluated = build_debug(constraints); // Filter out only applicable constraints @@ -262,8 +262,8 @@ pub fn debug_single_trace< ); } - let lv: ViewOf = lv.iter().copied().collect(); - let nv: ViewOf = nv.iter().copied().collect(); + let lv: ViewOf = lv.iter().copied().collect(); + let nv: ViewOf = nv.iter().copied().collect(); log::error!("Debug constraints for {stark}"); log::error!("lv-row[{lv_row}] - values: {lv:?}"); log::error!("nv-row[{nv_row}] - values: {nv:?}"); From 715e41dd64d4ab7de7c30ff2bac8561a8e4dc9ff Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Fri, 21 Jun 2024 12:08:00 +0200 Subject: [PATCH 79/84] Remove passthrough to G from StarkFrom --- circuits/src/expr.rs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 022fbb057..7cb5d667c 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -231,23 +231,6 @@ pub struct StarkFrom, } -// Passthrough to G -impl - GenerateConstraints for StarkFrom -where - G: GenerateConstraints, -{ - type PublicInputs = G::PublicInputs; - type View = G::View; - - fn generate_constraints<'a, T: Copy + Debug>( - &self, - vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, - ) -> ConstraintBuilder> { - self.witness.generate_constraints(vars) - } -} - impl Display for StarkFrom { From ebc1c2941d7f799aed629901bc7aa726a28f90bd Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 1 Jul 2024 14:23:59 +0200 Subject: [PATCH 80/84] Inline Unstark --- circuits/src/expr.rs | 6 +++-- circuits/src/generation.rs | 2 +- circuits/src/ops/blt_taken/stark.rs | 23 +++++++++++++++++--- circuits/src/program/stark.rs | 23 +++++++++++++++++--- circuits/src/program_multiplicities/stark.rs | 23 +++++++++++++++++--- circuits/src/rangecheck/stark.rs | 23 +++++++++++++++++--- circuits/src/register/init/stark.rs | 23 +++++++++++++++++--- circuits/src/register/zero_read/stark.rs | 23 +++++++++++++++++--- circuits/src/register/zero_write/stark.rs | 23 +++++++++++++++++--- 9 files changed, 145 insertions(+), 24 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 7cb5d667c..61aaec9ab 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -221,8 +221,10 @@ pub trait GenerateConstraints fn generate_constraints<'a, T: Copy + Debug>( &self, - vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, - ) -> ConstraintBuilder>; + _vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, + ) -> ConstraintBuilder> { + ConstraintBuilder::default() + } } #[derive(Copy, Clone, Debug, Default)] diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 888bb4b75..71a9e2014 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -20,7 +20,7 @@ use starky::stark::Stark; use crate::bitshift::generation::generate_shift_amount_trace; use crate::cpu::generation::{generate_cpu_trace, generate_program_mult_trace}; use crate::cpu_skeleton::generation::generate_cpu_skeleton_trace; -use crate::expr::{build_debug, ConstraintType, GenerateConstraints, Vars, ViewOf, StarkFrom}; +use crate::expr::{build_debug, ConstraintType, GenerateConstraints, StarkFrom, Vars, ViewOf}; use crate::memory::generation::generate_memory_trace; use crate::memory_fullword::generation::generate_fullword_memory_trace; use crate::memory_halfword::generation::generate_halfword_memory_trace; diff --git a/circuits/src/ops/blt_taken/stark.rs b/circuits/src/ops/blt_taken/stark.rs index aa07de3c0..91a733f76 100644 --- a/circuits/src/ops/blt_taken/stark.rs +++ b/circuits/src/ops/blt_taken/stark.rs @@ -1,9 +1,26 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::BltTaken; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct BltTakenConstraints {} + +const COLUMNS: usize = BltTaken::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + +impl GenerateConstraints for BltTakenConstraints { + type PublicInputs = NoColumns; + type View = BltTaken; +} -impl_name!(N, BltTakenStark); +impl Display for BltTakenConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} #[allow(clippy::module_name_repetitions)] pub type BltTakenStark = - Unstark, { BltTaken::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; diff --git a/circuits/src/program/stark.rs b/circuits/src/program/stark.rs index 32cde5f1f..33a085239 100644 --- a/circuits/src/program/stark.rs +++ b/circuits/src/program/stark.rs @@ -1,9 +1,26 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::ProgramRom; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct ProgramConstraints {} + +const COLUMNS: usize = ProgramRom::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + +impl GenerateConstraints for ProgramConstraints { + type PublicInputs = NoColumns; + type View = ProgramRom; +} -impl_name!(N, ProgramStark); +impl Display for ProgramConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} #[allow(clippy::module_name_repetitions)] pub type ProgramStark = - Unstark, { ProgramRom::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; diff --git a/circuits/src/program_multiplicities/stark.rs b/circuits/src/program_multiplicities/stark.rs index c8a066573..2d243530a 100644 --- a/circuits/src/program_multiplicities/stark.rs +++ b/circuits/src/program_multiplicities/stark.rs @@ -1,9 +1,26 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::ProgramMult; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct ProgramMultConstraints {} + +const COLUMNS: usize = ProgramMult::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + +impl GenerateConstraints for ProgramMultConstraints { + type PublicInputs = NoColumns; + type View = ProgramMult; +} -impl_name!(N, ProgramMultStark); +impl Display for ProgramMultConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} #[allow(clippy::module_name_repetitions)] pub type ProgramMultStark = - Unstark, { ProgramMult::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; diff --git a/circuits/src/rangecheck/stark.rs b/circuits/src/rangecheck/stark.rs index e2caadc79..b460a677a 100644 --- a/circuits/src/rangecheck/stark.rs +++ b/circuits/src/rangecheck/stark.rs @@ -1,12 +1,29 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::RangeCheckColumnsView; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct RangeCheckConstraints {} + +const COLUMNS: usize = RangeCheckColumnsView::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; -impl_name!(N, RangeCheckStark); +impl GenerateConstraints for RangeCheckConstraints { + type PublicInputs = NoColumns; + type View = RangeCheckColumnsView; +} + +impl Display for RangeCheckConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} #[allow(clippy::module_name_repetitions)] pub type RangeCheckStark = - Unstark, { RangeCheckColumnsView::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; #[cfg(test)] mod tests { diff --git a/circuits/src/register/init/stark.rs b/circuits/src/register/init/stark.rs index b589b34c3..08d5487ff 100644 --- a/circuits/src/register/init/stark.rs +++ b/circuits/src/register/init/stark.rs @@ -1,12 +1,29 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::RegisterInit; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct RegisterInitConstraints {} + +const COLUMNS: usize = RegisterInit::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + +impl GenerateConstraints for RegisterInitConstraints { + type PublicInputs = NoColumns; + type View = RegisterInit; +} -impl_name!(N, RegisterInitStark); +impl Display for RegisterInitConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} /// For sanity check, we can constrain the register address column to be in /// a running sum from 0..=31, but since this fixed table is known to /// both prover and verifier, we do not need to do so here. #[allow(clippy::module_name_repetitions)] pub type RegisterInitStark = - Unstark, { RegisterInit::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; diff --git a/circuits/src/register/zero_read/stark.rs b/circuits/src/register/zero_read/stark.rs index 4e734f59f..8d790dd24 100644 --- a/circuits/src/register/zero_read/stark.rs +++ b/circuits/src/register/zero_read/stark.rs @@ -1,9 +1,26 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::RegisterZeroRead; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct RegisterZeroReadConstraints {} + +const COLUMNS: usize = RegisterZeroRead::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + +impl GenerateConstraints for RegisterZeroReadConstraints { + type PublicInputs = NoColumns; + type View = RegisterZeroRead; +} -impl_name!(N, RegisterZeroReadStark); +impl Display for RegisterZeroReadConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} #[allow(clippy::module_name_repetitions)] pub type RegisterZeroReadStark = - Unstark, { RegisterZeroRead::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; diff --git a/circuits/src/register/zero_write/stark.rs b/circuits/src/register/zero_write/stark.rs index 5b04b4744..2ab21050a 100644 --- a/circuits/src/register/zero_write/stark.rs +++ b/circuits/src/register/zero_write/stark.rs @@ -1,9 +1,26 @@ +use core::fmt::Debug; +use std::fmt::Display; + use super::columns::RegisterZeroWrite; use crate::columns_view::NumberOfColumns; -use crate::unstark::{impl_name, Unstark}; +use crate::expr::{GenerateConstraints, StarkFrom}; +use crate::unstark::NoColumns; + +#[derive(Default, Clone, Copy, Debug)] +pub struct RegisterZeroWriteConstraints {} + +const COLUMNS: usize = RegisterZeroWrite::<()>::NUMBER_OF_COLUMNS; +const PUBLIC_INPUTS: usize = 0; + +impl GenerateConstraints for RegisterZeroWriteConstraints { + type PublicInputs = NoColumns; + type View = RegisterZeroWrite; +} -impl_name!(N, RegisterZeroWriteStark); +impl Display for RegisterZeroWriteConstraints { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } +} #[allow(clippy::module_name_repetitions)] pub type RegisterZeroWriteStark = - Unstark, { RegisterZeroWrite::<()>::NUMBER_OF_COLUMNS }>; + StarkFrom; From b67f8ee2b5eeb48dcb92f5121c9ea7ed5b7c076f Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 1 Jul 2024 14:34:14 +0200 Subject: [PATCH 81/84] Remove all the unecessary code --- circuits/src/unstark.rs | 232 ++-------------------------------------- 1 file changed, 6 insertions(+), 226 deletions(-) diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index a2f5bb2c3..74fa68b78 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -1,233 +1,13 @@ use core::fmt::Debug; -use std::fmt::Display; use std::marker::PhantomData; -use expr::Expr; - -use crate::expr::{ConstraintBuilder, GenerateConstraints, StarkFrom, Vars}; - -impl GenerateConstraints - for UnstarkConstraints -{ - type PublicInputs = NoColumns; - type View = ShadowColumns; - - fn generate_constraints<'a, T: Copy + Debug>( - &self, - _vars: &Vars<'a, Self, T, COLUMNS, PUBLIC_INPUTS>, - ) -> ConstraintBuilder> { - ConstraintBuilder::default() - } -} - -#[derive(Copy, Clone, Default)] -#[allow(clippy::module_name_repetitions)] -pub struct UnstarkConstraints { - pub _name: PhantomData, - pub _d: PhantomData, -} - -/// Template for a STARK with zero internal constraints. Use this if the STARK -/// itself does not need any built-in constraints, but rely on cross table -/// lookups for provability. -pub type Unstark = - StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; - -impl Display - for UnstarkConstraints -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", NAME::default()) - } -} - -const PUBLIC_INPUTS: usize = 0; - -// Simple marco to create a type holding the name for the Unstark -macro_rules! impl_name { - ($alias:ident, $name:ident) => { - mod name { - #[derive(Default, Debug, Clone, Copy)] - pub struct $name {} - } - - use name::$name as $alias; - }; -} - -pub(crate) use impl_name; - -pub type NoColumns = ShadowColumns; +use crate::columns_view::columns_view_impl; +/// NoColumns is a PhantomData that supports all the traits and interafaces that +/// we are using for Columns. #[repr(C)] #[derive(Clone, Copy, Eq, PartialEq, Debug)] -pub struct ShadowColumns { - _columns: [T; N], -} - -// Manually implement columns_view_impl! for Columns -impl crate::columns_view::ColumnViewImplHider> { - const fn from_array(value: [T; N]) -> ShadowColumns { - unsafe { crate::columns_view::transmute_without_compile_time_size_checks(value) } - } - - const fn into_array(v: ShadowColumns) -> [T; N] { - unsafe { crate::columns_view::transmute_without_compile_time_size_checks(v) } - } - - const fn from_array_ref(value: &[T; N]) -> &ShadowColumns { - unsafe { crate::columns_view::transmute_ref(value) } - } - - const fn array_ref(v: &ShadowColumns) -> &[T; N] { - unsafe { crate::columns_view::transmute_ref(v) } - } -} - -impl ShadowColumns { - pub const fn from_array(value: [T; N]) -> Self { - crate::columns_view::ColumnViewImplHider::::from_array(value) - } - - #[must_use] - pub const fn into_array(self) -> [T; N] { - crate::columns_view::ColumnViewImplHider::::into_array(self) - } - - pub const fn from_array_ref(value: &[T; N]) -> &Self { - crate::columns_view::ColumnViewImplHider::::from_array_ref(value) - } - - #[must_use] - pub const fn array_ref(&self) -> &[T; N] { - crate::columns_view::ColumnViewImplHider::::array_ref(self) - } - - pub fn iter(&self) -> std::slice::Iter { self.array_ref().iter() } - - // At the moment we only use `map` Instruction, - // so it's dead code for the other callers of `columns_view_impl`. - // TODO(Matthias): remove this marker, once we use it for the other structs, - // too. - #[allow(dead_code)] - pub fn map(self, f: F) -> ShadowColumns - where - F: FnMut(T) -> B, { - ShadowColumns::from_array(self.into_array().map(f)) - } -} - -impl crate::columns_view::Zip for ShadowColumns { - fn zip_with(self, other: Self, mut f: F) -> Self - where - F: FnMut(Item, Item) -> Item, { - ShadowColumns::from_array({ - let mut a = self.into_iter(); - let mut b = other.into_iter(); - core::array::from_fn(move |_| f(a.next().unwrap(), b.next().unwrap())) - }) - } -} - -impl crate::columns_view::NumberOfColumns for ShadowColumns { - // `u8` is guaranteed to have a `size_of` of 1. - const NUMBER_OF_COLUMNS: usize = N; -} - -impl From<[T; N]> for ShadowColumns { - fn from(value: [T; N]) -> Self { Self::from_array(value) } -} - -impl From> for [T; N] { - fn from(value: ShadowColumns) -> Self { value.into_array() } -} - -impl<'a, T, const N: usize> From<&'a [T]> for &'a ShadowColumns { - fn from(value: &'a [T]) -> Self { - let value: &[T; N] = value.try_into().expect("slice of correct length"); - ShadowColumns::from_array_ref(value) - } -} - -impl std::borrow::Borrow<[T]> for ShadowColumns { - fn borrow(&self) -> &[T] { self.array_ref() } -} - -impl std::ops::Index for ShadowColumns -where - [T]: std::ops::Index, -{ - type Output = <[T] as std::ops::Index>::Output; - - fn index(&self, index: I) -> &Self::Output { &self.array_ref()[index] } -} - -impl std::iter::IntoIterator for ShadowColumns { - type IntoIter = std::array::IntoIter; - type Item = T; - - fn into_iter(self) -> Self::IntoIter { self.into_array().into_iter() } -} - -impl<'a, T, const N: usize> std::iter::IntoIterator for &'a ShadowColumns { - type IntoIter = std::slice::Iter<'a, T>; - type Item = &'a T; - - fn into_iter(self) -> Self::IntoIter { self.iter() } -} - -impl std::iter::FromIterator for ShadowColumns { - fn from_iter>(iter: I) -> Self { - let vec: arrayvec::ArrayVec = iter.into_iter().collect(); - let array = vec.into_inner().expect("iterator of correct length"); - Self::from_array(array) - } -} - -impl core::ops::Neg for ShadowColumns { - type Output = Self; - - fn neg(self) -> Self::Output { self.map(|x| x.checked_neg().expect("negation overflow")) } -} - -impl core::ops::Add> for ShadowColumns { - type Output = Self; - - fn add(self, other: Self) -> Self::Output { - crate::columns_view::Zip::zip_with(self, other, |a, b| { - a.checked_add(b).expect("addition overflow") - }) - } -} - -impl core::ops::Sub> for ShadowColumns { - type Output = Self; - - fn sub(self, other: Self) -> Self::Output { - crate::columns_view::Zip::zip_with(self, other, |a, b| { - a.checked_sub(b).expect("subtraction overflow") - }) - } -} - -impl core::ops::Mul for ShadowColumns { - type Output = Self; - - fn mul(self, other: i64) -> Self::Output { - self.map(|x| x.checked_mul(other).expect("multiplication overflow")) - } -} - -impl core::iter::Sum> for ShadowColumns { - #[inline] - fn sum>(iter: I) -> Self { - iter.fold(Self::default(), core::ops::Add::add) - } -} - -// Some of our tables have columns that are specifiecd as arrays that are bigger -// than 32 elements. Thus default derivation doesn't work, so we do it manually -// here. -impl Default for ShadowColumns { - fn default() -> Self { ShadowColumns::from_array(core::array::from_fn(|_| Default::default())) } +pub struct NoColumns { + _phantom: PhantomData, } +columns_view_impl!(NoColumns); From 051c9a34712b1d7047c6a501ee013c2a3c1ebf9c Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 1 Jul 2024 15:16:41 +0200 Subject: [PATCH 82/84] Refactor common structure to an unstark macro --- circuits/src/ops/blt_taken/stark.rs | 27 ++------------ circuits/src/program/stark.rs | 27 ++------------ circuits/src/program_multiplicities/stark.rs | 27 ++------------ circuits/src/rangecheck/stark.rs | 27 ++------------ circuits/src/register/init/stark.rs | 33 +++-------------- circuits/src/register/zero_read/stark.rs | 27 ++------------ circuits/src/register/zero_write/stark.rs | 27 ++------------ circuits/src/unstark.rs | 38 ++++++++++++++++++++ 8 files changed, 55 insertions(+), 178 deletions(-) diff --git a/circuits/src/ops/blt_taken/stark.rs b/circuits/src/ops/blt_taken/stark.rs index 91a733f76..247ed8242 100644 --- a/circuits/src/ops/blt_taken/stark.rs +++ b/circuits/src/ops/blt_taken/stark.rs @@ -1,26 +1,3 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::BltTaken; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct BltTakenConstraints {} - -const COLUMNS: usize = BltTaken::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for BltTakenConstraints { - type PublicInputs = NoColumns; - type View = BltTaken; -} - -impl Display for BltTakenConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -#[allow(clippy::module_name_repetitions)] -pub type BltTakenStark = - StarkFrom; +unstark!(BltTakenStark, super::columns::BltTaken); diff --git a/circuits/src/program/stark.rs b/circuits/src/program/stark.rs index 33a085239..ad988f76c 100644 --- a/circuits/src/program/stark.rs +++ b/circuits/src/program/stark.rs @@ -1,26 +1,3 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::ProgramRom; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct ProgramConstraints {} - -const COLUMNS: usize = ProgramRom::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for ProgramConstraints { - type PublicInputs = NoColumns; - type View = ProgramRom; -} - -impl Display for ProgramConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -#[allow(clippy::module_name_repetitions)] -pub type ProgramStark = - StarkFrom; +unstark!(ProgramStark, super::columns::ProgramRom); diff --git a/circuits/src/program_multiplicities/stark.rs b/circuits/src/program_multiplicities/stark.rs index 2d243530a..4686e047a 100644 --- a/circuits/src/program_multiplicities/stark.rs +++ b/circuits/src/program_multiplicities/stark.rs @@ -1,26 +1,3 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::ProgramMult; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct ProgramMultConstraints {} - -const COLUMNS: usize = ProgramMult::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for ProgramMultConstraints { - type PublicInputs = NoColumns; - type View = ProgramMult; -} - -impl Display for ProgramMultConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -#[allow(clippy::module_name_repetitions)] -pub type ProgramMultStark = - StarkFrom; +unstark!(ProgramMultStark, super::columns::ProgramMult); diff --git a/circuits/src/rangecheck/stark.rs b/circuits/src/rangecheck/stark.rs index b460a677a..993fabcf9 100644 --- a/circuits/src/rangecheck/stark.rs +++ b/circuits/src/rangecheck/stark.rs @@ -1,29 +1,6 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::RangeCheckColumnsView; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct RangeCheckConstraints {} - -const COLUMNS: usize = RangeCheckColumnsView::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for RangeCheckConstraints { - type PublicInputs = NoColumns; - type View = RangeCheckColumnsView; -} - -impl Display for RangeCheckConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -#[allow(clippy::module_name_repetitions)] -pub type RangeCheckStark = - StarkFrom; +unstark!(RangeCheckStark, super::columns::RangeCheckColumnsView); #[cfg(test)] mod tests { diff --git a/circuits/src/register/init/stark.rs b/circuits/src/register/init/stark.rs index 08d5487ff..097d884e5 100644 --- a/circuits/src/register/init/stark.rs +++ b/circuits/src/register/init/stark.rs @@ -1,29 +1,6 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::RegisterInit; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct RegisterInitConstraints {} - -const COLUMNS: usize = RegisterInit::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for RegisterInitConstraints { - type PublicInputs = NoColumns; - type View = RegisterInit; -} - -impl Display for RegisterInitConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -/// For sanity check, we can constrain the register address column to be in -/// a running sum from 0..=31, but since this fixed table is known to -/// both prover and verifier, we do not need to do so here. -#[allow(clippy::module_name_repetitions)] -pub type RegisterInitStark = - StarkFrom; +// For sanity check, we can constrain the register address column to be in +// a running sum from 0..=31, but since this fixed table is known to +// both prover and verifier, we do not need to do so here. +unstark!(RegisterInitStark, super::columns::RegisterInit); diff --git a/circuits/src/register/zero_read/stark.rs b/circuits/src/register/zero_read/stark.rs index 8d790dd24..86493861a 100644 --- a/circuits/src/register/zero_read/stark.rs +++ b/circuits/src/register/zero_read/stark.rs @@ -1,26 +1,3 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::RegisterZeroRead; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct RegisterZeroReadConstraints {} - -const COLUMNS: usize = RegisterZeroRead::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for RegisterZeroReadConstraints { - type PublicInputs = NoColumns; - type View = RegisterZeroRead; -} - -impl Display for RegisterZeroReadConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -#[allow(clippy::module_name_repetitions)] -pub type RegisterZeroReadStark = - StarkFrom; +unstark!(RegisterZeroReadStark, super::columns::RegisterZeroRead); diff --git a/circuits/src/register/zero_write/stark.rs b/circuits/src/register/zero_write/stark.rs index 2ab21050a..e968a31e7 100644 --- a/circuits/src/register/zero_write/stark.rs +++ b/circuits/src/register/zero_write/stark.rs @@ -1,26 +1,3 @@ -use core::fmt::Debug; -use std::fmt::Display; +use crate::unstark::unstark; -use super::columns::RegisterZeroWrite; -use crate::columns_view::NumberOfColumns; -use crate::expr::{GenerateConstraints, StarkFrom}; -use crate::unstark::NoColumns; - -#[derive(Default, Clone, Copy, Debug)] -pub struct RegisterZeroWriteConstraints {} - -const COLUMNS: usize = RegisterZeroWrite::<()>::NUMBER_OF_COLUMNS; -const PUBLIC_INPUTS: usize = 0; - -impl GenerateConstraints for RegisterZeroWriteConstraints { - type PublicInputs = NoColumns; - type View = RegisterZeroWrite; -} - -impl Display for RegisterZeroWriteConstraints { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } -} - -#[allow(clippy::module_name_repetitions)] -pub type RegisterZeroWriteStark = - StarkFrom; +unstark!(RegisterZeroWriteStark, super::columns::RegisterZeroWrite); diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index 74fa68b78..e1a349b9f 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -11,3 +11,41 @@ pub struct NoColumns { _phantom: PhantomData, } columns_view_impl!(NoColumns); + +macro_rules! unstark { + ($name:ident, $view:ty) => { + type View = $view; + + mod constraints { + use super::View; + use crate::columns_view::NumberOfColumns; + + #[derive(Default, Clone, Copy, Debug)] + pub struct $name {} + + pub const COLUMNS: usize = View::<()>::NUMBER_OF_COLUMNS; + pub const PUBLIC_INPUTS: usize = 0; + + impl crate::expr::GenerateConstraints for $name { + type PublicInputs = crate::unstark::NoColumns; + type View = View; + } + + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } + } + } + + pub type $name = crate::expr::StarkFrom< + F, + constraints::$name, + { D }, + { constraints::COLUMNS }, + { constraints::PUBLIC_INPUTS }, + >; + }; +} + +pub(crate) use unstark; From ff0652bdb4b255d2e97edda6fb5baa64684f5434 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Mon, 1 Jul 2024 15:24:07 +0200 Subject: [PATCH 83/84] cargo clippy --- circuits/src/bitshift/stark.rs | 1 + circuits/src/cpu/stark.rs | 1 + circuits/src/cpu_skeleton/stark.rs | 1 + circuits/src/memory/stark.rs | 1 + circuits/src/memory_fullword/stark.rs | 1 + circuits/src/memory_halfword/stark.rs | 1 + circuits/src/memory_zeroinit/stark.rs | 1 + circuits/src/memoryinit/stark.rs | 1 + circuits/src/ops/add/stark.rs | 1 + circuits/src/poseidon2/stark.rs | 1 + circuits/src/poseidon2_output_bytes/stark.rs | 1 + circuits/src/poseidon2_sponge/stark.rs | 1 + circuits/src/rangecheck_u8/stark.rs | 1 + circuits/src/register/general/stark.rs | 1 + circuits/src/storage_device/stark.rs | 1 + circuits/src/tape_commitments/stark.rs | 1 + circuits/src/unstark.rs | 4 ++-- circuits/src/xor/stark.rs | 1 + 18 files changed, 19 insertions(+), 2 deletions(-) diff --git a/circuits/src/bitshift/stark.rs b/circuits/src/bitshift/stark.rs index 90a963f6b..0cc13bbc4 100644 --- a/circuits/src/bitshift/stark.rs +++ b/circuits/src/bitshift/stark.rs @@ -16,6 +16,7 @@ pub struct BitshiftConstraints {} const COLUMNS: usize = BitshiftView::<()>::NUMBER_OF_COLUMNS; const PUBLIC_INPUTS: usize = 0; +#[allow(clippy::module_name_repetitions)] pub type BitshiftStark = StarkFrom; diff --git a/circuits/src/cpu/stark.rs b/circuits/src/cpu/stark.rs index fb1eee457..64c54b1d9 100644 --- a/circuits/src/cpu/stark.rs +++ b/circuits/src/cpu/stark.rs @@ -18,6 +18,7 @@ pub struct CpuConstraints {} /// A Gadget for CPU Instructions /// /// Instructions are either handled directly or through cross table lookup +#[allow(clippy::module_name_repetitions)] pub type CpuStark = StarkFrom; /// Ensure that if opcode is straight line, then program counter is incremented diff --git a/circuits/src/cpu_skeleton/stark.rs b/circuits/src/cpu_skeleton/stark.rs index d93281124..5d195fdf1 100644 --- a/circuits/src/cpu_skeleton/stark.rs +++ b/circuits/src/cpu_skeleton/stark.rs @@ -13,6 +13,7 @@ use crate::stark::mozak_stark::PublicInputs; #[allow(clippy::module_name_repetitions)] pub struct CpuSkeletonConstraints {} +#[allow(clippy::module_name_repetitions)] pub type CpuSkeletonStark = StarkFrom; diff --git a/circuits/src/memory/stark.rs b/circuits/src/memory/stark.rs index d7404118f..e266ef33d 100644 --- a/circuits/src/memory/stark.rs +++ b/circuits/src/memory/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct MemoryConstraints {} +#[allow(clippy::module_name_repetitions)] pub type MemoryStark = StarkFrom; diff --git a/circuits/src/memory_fullword/stark.rs b/circuits/src/memory_fullword/stark.rs index 211e3cfe5..858dbba24 100644 --- a/circuits/src/memory_fullword/stark.rs +++ b/circuits/src/memory_fullword/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct FullWordMemoryConstraints {} +#[allow(clippy::module_name_repetitions)] pub type FullWordMemoryStark = StarkFrom; diff --git a/circuits/src/memory_halfword/stark.rs b/circuits/src/memory_halfword/stark.rs index ba73f2d1a..95310803f 100644 --- a/circuits/src/memory_halfword/stark.rs +++ b/circuits/src/memory_halfword/stark.rs @@ -11,6 +11,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct HalfWordMemoryConstraints {} +#[allow(clippy::module_name_repetitions)] pub type HalfWordMemoryStark = StarkFrom; diff --git a/circuits/src/memory_zeroinit/stark.rs b/circuits/src/memory_zeroinit/stark.rs index 54a5e80b0..ff86ba905 100644 --- a/circuits/src/memory_zeroinit/stark.rs +++ b/circuits/src/memory_zeroinit/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct MemoryZeroInitConstraints {} +#[allow(clippy::module_name_repetitions)] pub type MemoryZeroInitStark = StarkFrom; diff --git a/circuits/src/memoryinit/stark.rs b/circuits/src/memoryinit/stark.rs index e48884279..88004ef94 100644 --- a/circuits/src/memoryinit/stark.rs +++ b/circuits/src/memoryinit/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct MemoryInitConstraints {} +#[allow(clippy::module_name_repetitions)] pub type MemoryInitStark = StarkFrom; diff --git a/circuits/src/ops/add/stark.rs b/circuits/src/ops/add/stark.rs index 324970c41..3f7838637 100644 --- a/circuits/src/ops/add/stark.rs +++ b/circuits/src/ops/add/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct AddConstraints {} +#[allow(clippy::module_name_repetitions)] pub type AddStark = StarkFrom; diff --git a/circuits/src/poseidon2/stark.rs b/circuits/src/poseidon2/stark.rs index 440459e00..497f2da5f 100644 --- a/circuits/src/poseidon2/stark.rs +++ b/circuits/src/poseidon2/stark.rs @@ -108,6 +108,7 @@ where #[allow(clippy::module_name_repetitions)] pub struct Poseidon2_12Constraints {} +#[allow(clippy::module_name_repetitions)] pub type Poseidon2_12Stark = StarkFrom; diff --git a/circuits/src/poseidon2_output_bytes/stark.rs b/circuits/src/poseidon2_output_bytes/stark.rs index 1a9c33032..4841061f6 100644 --- a/circuits/src/poseidon2_output_bytes/stark.rs +++ b/circuits/src/poseidon2_output_bytes/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct Poseidon2OutputBytesConstraints {} +#[allow(clippy::module_name_repetitions)] pub type Poseidon2OutputBytesStark = StarkFrom; diff --git a/circuits/src/poseidon2_sponge/stark.rs b/circuits/src/poseidon2_sponge/stark.rs index ef5f85a16..a044504f7 100644 --- a/circuits/src/poseidon2_sponge/stark.rs +++ b/circuits/src/poseidon2_sponge/stark.rs @@ -17,6 +17,7 @@ pub struct Poseidon2SpongeConstraints { _f: PhantomData, } +#[allow(clippy::module_name_repetitions)] pub type Poseidon2SpongeStark = StarkFrom, { D }, { COLUMNS }, { PUBLIC_INPUTS }>; diff --git a/circuits/src/rangecheck_u8/stark.rs b/circuits/src/rangecheck_u8/stark.rs index f5b30fb84..4bcc83c19 100644 --- a/circuits/src/rangecheck_u8/stark.rs +++ b/circuits/src/rangecheck_u8/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct RangeCheckU8Constraints {} +#[allow(clippy::module_name_repetitions)] pub type RangeCheckU8Stark = StarkFrom; diff --git a/circuits/src/register/general/stark.rs b/circuits/src/register/general/stark.rs index fbc761e66..a53495a19 100644 --- a/circuits/src/register/general/stark.rs +++ b/circuits/src/register/general/stark.rs @@ -12,6 +12,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct RegisterConstraints {} +#[allow(clippy::module_name_repetitions)] pub type RegisterStark = StarkFrom; diff --git a/circuits/src/storage_device/stark.rs b/circuits/src/storage_device/stark.rs index 2227653e4..cee6cfccf 100644 --- a/circuits/src/storage_device/stark.rs +++ b/circuits/src/storage_device/stark.rs @@ -11,6 +11,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct StorageDeviceConstraints {} +#[allow(clippy::module_name_repetitions)] pub type StorageDeviceStark = StarkFrom; diff --git a/circuits/src/tape_commitments/stark.rs b/circuits/src/tape_commitments/stark.rs index 7abf23cef..306655848 100644 --- a/circuits/src/tape_commitments/stark.rs +++ b/circuits/src/tape_commitments/stark.rs @@ -36,6 +36,7 @@ impl GenerateConstraints for TapeCommitmentsConstraints #[allow(clippy::module_name_repetitions)] pub struct TapeCommitmentsConstraints {} +#[allow(clippy::module_name_repetitions)] pub type TapeCommitmentsStark = StarkFrom; diff --git a/circuits/src/unstark.rs b/circuits/src/unstark.rs index e1a349b9f..d67067e2e 100644 --- a/circuits/src/unstark.rs +++ b/circuits/src/unstark.rs @@ -3,8 +3,8 @@ use std::marker::PhantomData; use crate::columns_view::columns_view_impl; -/// NoColumns is a PhantomData that supports all the traits and interafaces that -/// we are using for Columns. +/// `NoColumns` is a `PhantomData` that supports all the traits and interafaces +/// that we are using for Columns. #[repr(C)] #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub struct NoColumns { diff --git a/circuits/src/xor/stark.rs b/circuits/src/xor/stark.rs index b6467ed10..64d860f70 100644 --- a/circuits/src/xor/stark.rs +++ b/circuits/src/xor/stark.rs @@ -13,6 +13,7 @@ use crate::unstark::NoColumns; #[allow(clippy::module_name_repetitions)] pub struct XorConstraints {} +#[allow(clippy::module_name_repetitions)] pub type XorStark = StarkFrom; From 0f4c6f26154d15056dd904f5377b9f094469ee56 Mon Sep 17 00:00:00 2001 From: Jakub Zalewski Date: Wed, 3 Jul 2024 10:33:40 +0200 Subject: [PATCH 84/84] Clean up convenience type aliases --- circuits/src/expr.rs | 22 +++++++++++++++------- circuits/src/generation.rs | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 61aaec9ab..55ca8e4af 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -206,15 +206,23 @@ pub fn build_packed( } } -// Helper Types to Access members of GenerateConstraints -pub type PublicInputsOf<'a, S, T, const N: usize, const M: usize> = - >::PublicInputs; -pub type ViewOf<'a, S, T, const N: usize, const M: usize> = - >::View; +/// Convenience alias to `G::PublicInputs`. +pub type PublicInputsOf = + >::PublicInputs; -pub type Vars<'a, S, T, const N: usize, const M: usize> = - StarkFrameTyped, N, M>, PublicInputsOf<'a, S, Expr<'a, T>, N, M>>; +/// Convenience alias to `G::View`. +pub type ViewOf = + >::View; +/// Convenience alias to `StarkFrameTyped` that will be defined over +/// `G::View>` and `G::PublicInputs>` +pub type Vars<'a, G, T, const COLUMNS: usize, const PUBLIC_INPUTS: usize> = StarkFrameTyped< + ViewOf, COLUMNS, PUBLIC_INPUTS>, + PublicInputsOf, COLUMNS, PUBLIC_INPUTS>, +>; + +/// Trait for generating constraints independently from the type of the field +/// and independently from the API of the proving system. pub trait GenerateConstraints { type View: From<[E; COLUMNS]> + FromIterator; type PublicInputs: From<[E; PUBLIC_INPUTS]> + FromIterator; diff --git a/circuits/src/generation.rs b/circuits/src/generation.rs index 71a9e2014..8883dbb85 100644 --- a/circuits/src/generation.rs +++ b/circuits/src/generation.rs @@ -225,7 +225,7 @@ pub fn debug_single_trace< public_inputs: &'a [F], ) where G: Sync + Copy + Display + GenerateConstraints, - ViewOf<'a, G, F, COLUMNS, PUBLIC_INPUTS>: Debug, { + ViewOf: Debug, { transpose_polys::>(trace_rows.to_vec()) .iter() .enumerate()