Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Precompute twiddle factors #145

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ rust-version = "1.66"

[dependencies]
rand = "0.8.5"
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks", rev = "a21d2c5" }
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks", rev = "a21d2c5" }
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks", branch = "fft_with_twiddles_param" }
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks", branch = "fft_with_twiddles_param" }
thiserror = "1.0.38"
log = "0.4.17"
bincode = { version = "2.0.0-rc.2", tag = "v2.0.0-rc.2", git = "https://github.com/bincode-org/bincode.git" }
Expand Down
295 changes: 145 additions & 150 deletions src/cairo/air.rs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/cairo/decode/instruction_flags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{cairo::errors::InstructionDecodingError, FE};
use lambdaworks_math::traits::ByteConversion;

// Constants for instructions decoding
const DST_REG_MASK: u64 = 0x0001;
Expand Down
35 changes: 17 additions & 18 deletions src/cairo/execution_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn get_memory_holes(sorted_addrs: &[FE], codelen: usize) -> Vec<FE> {

while hole_addr.representative() < addr.representative() {
if hole_addr.representative() > (codelen as u64).into() {
memory_holes.push(hole_addr.clone());
memory_holes.push(hole_addr);
}
hole_addr += FE::one();
}
Expand Down Expand Up @@ -246,7 +246,7 @@ fn fill_memory_holes(trace: &mut TraceTable<Stark252PrimeField>, memory_holes: &
// columns.
addr_cols.iter().for_each(|a_col| {
if let Some(hole) = memory_holes_iter.next() {
padding_row[*a_col] = hole.clone();
padding_row[*a_col] = *hole;
}
});

Expand Down Expand Up @@ -303,15 +303,15 @@ pub fn build_cairo_execution_trace(
let instructions: Vec<FE> = raw_trace
.rows
.iter()
.map(|t| memory.get(&t.pc).unwrap().clone())
.map(|t| *memory.get(&t.pc).unwrap())
.collect();

// t0, t1 and mul derived values are constructed. For details refer to
// section 9.1 of the Cairo whitepaper
let t0: Vec<FE> = trace_repr_flags
.iter()
.zip(&dsts)
.map(|(repr_flags, dst)| repr_flags[9].clone() * dst)
.map(|(repr_flags, dst)| repr_flags[9] * dst)
.collect();
let t1: Vec<FE> = t0.iter().zip(&res).map(|(t, r)| t * r).collect();
let mul: Vec<FE> = op0s.iter().zip(&op1s).map(|(op0, op1)| op0 * op1).collect();
Expand Down Expand Up @@ -372,8 +372,7 @@ fn add_rc_builtin_columns(
trace_cols.push(column.to_vec())
});

let mut rc_values_dereferenced: Vec<FE> =
range_checked_values.iter().map(|&x| x.clone()).collect();
let mut rc_values_dereferenced: Vec<FE> = range_checked_values.iter().map(|&&x| x).collect();
rc_values_dereferenced.resize(trace_cols[0].len(), FE::zero());

trace_cols.push(rc_values_dereferenced);
Expand Down Expand Up @@ -417,7 +416,7 @@ fn compute_res(flags: &[CairoInstructionFlags], op0s: &[FE], op1s: &[FE], dsts:
// values later on.
// See section 9.5 of the Cairo whitepaper, page 53.
if dst == &FE::zero() {
dst.clone()
*dst
} else {
dst.inv()
}
Expand All @@ -428,7 +427,7 @@ fn compute_res(flags: &[CairoInstructionFlags], op0s: &[FE], op1s: &[FE], dsts:
}
}
PcUpdate::Regular | PcUpdate::Jump | PcUpdate::JumpRel => match f.res_logic {
ResLogic::Op1 => op1.clone(),
ResLogic::Op1 => *op1,
ResLogic::Add => op0 + op1,
ResLogic::Mul => op0 * op1,
ResLogic::Unconstrained => {
Expand Down Expand Up @@ -464,11 +463,11 @@ fn compute_dst(
.map(|((f, o), t)| match f.dst_reg {
DstReg::AP => {
let addr = t.ap.checked_add_signed(o.off_dst.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
DstReg::FP => {
let addr = t.fp.checked_add_signed(o.off_dst.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
})
.unzip()
Expand Down Expand Up @@ -498,11 +497,11 @@ fn compute_op0(
.map(|((f, o), t)| match f.op0_reg {
Op0Reg::AP => {
let addr = t.ap.checked_add_signed(o.off_op0.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
Op0Reg::FP => {
let addr = t.fp.checked_add_signed(o.off_op0.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
})
.unzip()
Expand Down Expand Up @@ -547,22 +546,22 @@ fn compute_op1(
let addr = aux_get_last_nim_of_field_element(op0)
.checked_add_signed(offset.off_op1.into())
.unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
Op1Src::Imm => {
let pc = trace_state.pc;
let addr = pc.checked_add_signed(offset.off_op1.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
Op1Src::AP => {
let ap = trace_state.ap;
let addr = ap.checked_add_signed(offset.off_op1.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
Op1Src::FP => {
let fp = trace_state.fp;
let addr = fp.checked_add_signed(offset.off_op1.into()).unwrap();
(FE::from(addr), memory.get(&addr).unwrap().clone())
(FE::from(addr), memory.get(&addr).unwrap())
}
})
.unzip()
Expand All @@ -587,7 +586,7 @@ fn update_values(
op0s[i] = (register_states.rows[i].pc + instruction_size).into();
dst[i] = register_states.rows[i].fp.into();
} else if f.opcode == CairoOpcode::AssertEq {
res[i] = dst[i].clone();
res[i] = dst[i];
}
}
}
Expand All @@ -601,7 +600,7 @@ fn rows_to_cols<const N: usize>(rows: &[[FE; N]]) -> Vec<Vec<FE>> {
for col_idx in 0..n_cols {
let mut col = Vec::new();
for row in rows {
col.push(row[col_idx].clone());
col.push(row[col_idx]);
}
cols.push(col);
}
Expand Down
8 changes: 4 additions & 4 deletions src/starks/constraints/boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ mod test {
// * a0 = 1
// * a1 = 1
// * a7 = 32
let a0 = BoundaryConstraint::new_simple(0, one.clone());
let a1 = BoundaryConstraint::new_simple(1, one.clone());
let a0 = BoundaryConstraint::new_simple(0, one);
let a1 = BoundaryConstraint::new_simple(1, one);
let result = BoundaryConstraint::new_simple(7, FieldElement::<PrimeField>::from(32));

let constraints = BoundaryConstraints::from_constraints(vec![a0, a1, result]);

let primitive_root = PrimeField::get_primitive_root_of_unity(3).unwrap();

// P_0(x) = (x - 1)
let a0_zerofier = Polynomial::new(&[-one.clone(), one.clone()]);
let a0_zerofier = Polynomial::new(&[-one, one]);
// P_1(x) = (x - w^1)
let a1_zerofier = Polynomial::new(&[-primitive_root.pow(1u32), one.clone()]);
let a1_zerofier = Polynomial::new(&[-primitive_root.pow(1u32), one]);
// P_res(x) = (x - w^7)
let res_zerofier = Polynomial::new(&[-primitive_root.pow(7u32), one]);

Expand Down
14 changes: 8 additions & 6 deletions src/starks/constraints/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use lambdaworks_math::{

#[cfg(debug_assertions)]
use crate::starks::debug::check_boundary_polys_divisibility;
use crate::starks::domain::Domain;
use crate::starks::frame::Frame;
use crate::starks::prover::evaluate_polynomial_on_lde_domain;
use crate::starks::trace::TraceTable;
use crate::starks::traits::AIR;
use crate::starks::{domain::Domain, prover::evaluate_polynomial_on_lde_domain_with_twiddles};

use super::{boundary::BoundaryConstraints, evaluation_table::ConstraintEvaluationTable};
use std::iter::zip;
Expand Down Expand Up @@ -54,7 +53,7 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
// The + 1 is for the boundary constraints column
let mut evaluation_table = ConstraintEvaluationTable::new(
self.air.context().num_transition_constraints() + 1,
&domain.lde_roots_of_unity_coset,
&domain.twiddles,
);
let n_trace_colums = self.trace_polys.len();
let boundary_constraints = &self.boundary_constraints;
Expand All @@ -76,11 +75,12 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
#[cfg(debug_assertions)]
boundary_polys.push(boundary_poly.clone());

evaluate_polynomial_on_lde_domain(
evaluate_polynomial_on_lde_domain_with_twiddles(
&boundary_poly,
domain.blowup_factor,
domain.interpolation_domain_size,
&domain.coset_offset,
&domain.twiddles,
)
.unwrap()
})
Expand All @@ -98,11 +98,12 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
#[cfg(debug_assertions)]
boundary_zerofiers.push(zerofier.clone());

let mut evals = evaluate_polynomial_on_lde_domain(
let mut evals = evaluate_polynomial_on_lde_domain_with_twiddles(
&zerofier,
domain.blowup_factor,
domain.interpolation_domain_size,
&domain.coset_offset,
&domain.twiddles,
)
.unwrap();
FieldElement::inplace_batch_inverse(&mut evals);
Expand All @@ -126,11 +127,12 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
let transition_exemptions_evaluations: Vec<_> = transition_exemptions
.iter()
.map(|exemption| {
evaluate_polynomial_on_lde_domain(
evaluate_polynomial_on_lde_domain_with_twiddles(
exemption,
domain.blowup_factor,
domain.interpolation_domain_size,
&domain.coset_offset,
&domain.twiddles,
)
.unwrap()
})
Expand Down
21 changes: 15 additions & 6 deletions src/starks/domain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use lambdaworks_math::{
fft::cpu::roots_of_unity::get_powers_of_primitive_root_coset,
field::{element::FieldElement, traits::IsFFTField},
fft::cpu::roots_of_unity::{get_powers_of_primitive_root, get_powers_of_primitive_root_coset},
field::{
element::FieldElement,
traits::{IsFFTField, RootsConfig},
},
};

use super::traits::AIR;
Expand All @@ -14,6 +17,7 @@ pub struct Domain<F: IsFFTField> {
pub(crate) coset_offset: FieldElement<F>,
pub(crate) blowup_factor: usize,
pub(crate) interpolation_domain_size: usize,
pub(crate) twiddles: Vec<FieldElement<F>>,
}

impl<F: IsFFTField> Domain<F> {
Expand All @@ -35,11 +39,15 @@ impl<F: IsFFTField> Domain<F> {
)
.unwrap();

let lde_root_order = (air.trace_length() * blowup_factor).trailing_zeros();
let lde_roots_of_unity_coset = get_powers_of_primitive_root_coset(
let lde_size = air.trace_length() * blowup_factor;
let lde_root_order = lde_size.trailing_zeros();
let lde_roots_of_unity_coset =
get_powers_of_primitive_root_coset(lde_root_order as u64, lde_size, &coset_offset)
.unwrap();
let twiddles = get_powers_of_primitive_root(
lde_root_order as u64,
air.trace_length() * blowup_factor,
&coset_offset,
lde_size / 2,
RootsConfig::BitReverse,
)
.unwrap();

Expand All @@ -52,6 +60,7 @@ impl<F: IsFFTField> Domain<F> {
blowup_factor,
coset_offset,
interpolation_domain_size,
twiddles,
}
}
}
4 changes: 2 additions & 2 deletions src/starks/example/dummy_air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ impl AIR for DummyAIR {
let second_row = frame.get_row(1);
let third_row = frame.get_row(2);

let f_constraint = &first_row[0] * (&first_row[0] - FieldElement::one());
let f_constraint = first_row[0] * (first_row[0] - FieldElement::one());

let fib_constraint = &third_row[1] - &second_row[1] - &first_row[1];
let fib_constraint = third_row[1] - second_row[1] - first_row[1];

vec![f_constraint, fib_constraint]
}
Expand Down
4 changes: 2 additions & 2 deletions src/starks/example/fibonacci_2_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl AIR for Fibonacci2ColsAIR {
// constraints of Fibonacci sequence (2 terms per step):
// s_{0, i+1} = s_{0, i} + s_{1, i}
// s_{1, i+1} = s_{1, i} + s_{0, i+1}
let first_transition = &second_row[0] - &first_row[0] - &first_row[1];
let second_transition = &second_row[1] - &first_row[1] - &second_row[0];
let first_transition = second_row[0] - first_row[0] - first_row[1];
let second_transition = second_row[1] - first_row[1] - second_row[0];

vec![first_transition, second_transition]
}
Expand Down
7 changes: 3 additions & 4 deletions src/starks/example/fibonacci_rap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl AIR for FibonacciRAP {
aux_col.push(FieldElement::<Self::Field>::one());
} else {
let z_i = &aux_col[i - 1];
let n_p_term = not_perm[i - 1].clone() + gamma;
let p_term = &perm[i - 1] + gamma;
let n_p_term = not_perm[i - 1] + gamma;
let p_term = perm[i - 1] + gamma;

aux_col.push(z_i * n_p_term.div(p_term));
}
Expand All @@ -83,8 +83,7 @@ impl AIR for FibonacciRAP {
let second_row = frame.get_row(1);
let third_row = frame.get_row(2);

let mut constraints =
vec![third_row[0].clone() - second_row[0].clone() - first_row[0].clone()];
let mut constraints = vec![third_row[0] - second_row[0] - first_row[0]];

// Auxiliary constraints
let z_i = &frame.get_row(0)[2];
Expand Down
2 changes: 1 addition & 1 deletion src/starks/example/quadratic_air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl AIR for QuadraticAIR {
let first_row = frame.get_row(0);
let second_row = frame.get_row(1);

vec![&second_row[0] - &first_row[0] * &first_row[0]]
vec![second_row[0] - first_row[0] * first_row[0]]
}

fn number_auxiliary_rap_columns(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/starks/example/simple_fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl AIR for FibonacciAIR {
let second_row = frame.get_row(1);
let third_row = frame.get_row(2);

vec![third_row[0].clone() - second_row[0].clone() - first_row[0].clone()]
vec![third_row[0] - second_row[0] - first_row[0]]
}

fn boundary_constraints(
Expand Down
Loading