Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustin committed Aug 30, 2023
1 parent 09a4356 commit c4b9d79
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 54 deletions.
9 changes: 4 additions & 5 deletions stark_prover/src/constraints/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::{boundary::BoundaryConstraints, evaluation_table::ConstraintEvaluatio

pub struct CompositionPolynomial<F: IsFFTField, A: AIR> {
boundary_constraints: BoundaryConstraints<F>,
phantom: PhantomData<A>
phantom: PhantomData<A>,
}

impl<F: IsFFTField, A: AIR + AIR<Field = F>> CompositionPolynomial<F, A> {
Expand All @@ -34,7 +34,7 @@ impl<F: IsFFTField, A: AIR + AIR<Field = F>> CompositionPolynomial<F, A> {

Self {
boundary_constraints,
phantom: PhantomData
phantom: PhantomData,
}
}

Expand All @@ -56,14 +56,14 @@ impl<F: IsFFTField, A: AIR + AIR<Field = F>> CompositionPolynomial<F, A> {
air.context().num_transition_constraints() + 1,
&domain.lde_roots_of_unity_coset,
);

let boundary_evaluation = self.compute_boundary_evaluations(
air,
lde_trace,
domain,
alpha_and_beta_transition_coefficients,
alpha_and_beta_boundary_coefficients,
rap_challenges
rap_challenges,
);

let blowup_factor = air.blowup_factor();
Expand Down Expand Up @@ -347,4 +347,3 @@ where
})
.collect()
}

23 changes: 11 additions & 12 deletions stark_prover/src/examples/fibonacci_rap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Div, fmt::Binary};
use std::{fmt::Binary, ops::Div};

use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::{
Expand All @@ -17,8 +17,7 @@ use crate::{
transcript::transcript_to_field,
};

use super::simple_fibonacci::{ConstraintSystem, Permutation, Fibonacci};

use super::simple_fibonacci::{ConstraintSystem, Fibonacci, Permutation};

pub struct FibonacciRAP<F>
where
Expand All @@ -27,7 +26,7 @@ where
context: AirContext,
trace_length: usize,
pub_inputs: FibonacciRAPPublicInputs<F>,
constraint_system: ConstraintSystem<F>
constraint_system: ConstraintSystem<F>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -70,7 +69,7 @@ where
// Columns
vec!["FIBONACCI", "FIBONACCI_PERMUTED", "CUMULATIVE", "FLAG"],
// Challenges
vec!["GAMMA", "ZETA"]
vec!["GAMMA", "ZETA"],
);

// Constraints
Expand All @@ -81,14 +80,14 @@ where
vec!["FIBONACCI".to_string()],
vec!["FIBONACCI_PERMUTED".to_string()],
"GAMMA",
"ZETA"
"ZETA",
);

Self {
context,
trace_length,
pub_inputs: pub_inputs.clone(),
constraint_system: cs
constraint_system: cs,
}
}

Expand Down Expand Up @@ -138,11 +137,11 @@ where
frame.get_row(2).to_vec(),
];

let mut constraints =
vec![
self.constraint_system.constraints[0].evaluate(&real_frame, &vec![]),
self.constraint_system.constraints[2].evaluate(&real_frame, &vec![gamma.clone(), gamma.clone()])
];
let mut constraints = vec![
self.constraint_system.constraints[0].evaluate(&real_frame, &vec![]),
self.constraint_system.constraints[2]
.evaluate(&real_frame, &vec![gamma.clone(), gamma.clone()]),
];

constraints
}
Expand Down
123 changes: 86 additions & 37 deletions stark_prover/src/examples/simple_fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::{element::FieldElement, traits::{IsFFTField, IsField}};
use lambdaworks_math::field::{
element::FieldElement,
traits::{IsFFTField, IsField},
};

use crate::{
constraints::boundary::{BoundaryConstraint, BoundaryConstraints},
Expand All @@ -17,7 +20,7 @@ where
context: AirContext,
trace_length: usize,
pub_inputs: FibonacciPublicInputs<F>,
constraint_system: ConstraintSystem<F>
constraint_system: ConstraintSystem<F>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -52,14 +55,17 @@ where
num_transition_exemptions: 1,
};

let mut cs = ConstraintSystem::new(vec!["Fibonacci", "Disordered_Fibonacci", "Z"], vec!["Alpha"]);
let mut cs = ConstraintSystem::new(
vec!["Fibonacci", "Disordered_Fibonacci", "Z"],
vec!["Alpha"],
);
Fibonacci::add_to(&mut cs, "Fibonacci");

Self {
pub_inputs: pub_inputs.clone(),
context,
trace_length,
constraint_system: cs
constraint_system: cs,
}
}

Expand Down Expand Up @@ -88,7 +94,7 @@ where
frame.get_row(1).to_vec(),
frame.get_row(2).to_vec(),
],
&Vec::new()
&Vec::new(),
);

vec![res]
Expand Down Expand Up @@ -137,8 +143,12 @@ pub fn fibonacci_trace<F: IsFFTField>(
TraceTable::new_from_cols(&[ret])
}

pub trait Constraint<F: IsField> {
fn evaluate(&self, frame: &Vec<Vec<FieldElement<F>>>, challenges: &Vec<FieldElement<F>>) -> FieldElement<F>;
pub trait Constraint<F: IsField> {
fn evaluate(
&self,
frame: &Vec<Vec<FieldElement<F>>>,
challenges: &Vec<FieldElement<F>>,
) -> FieldElement<F>;
}

pub struct Binary {
Expand All @@ -147,34 +157,44 @@ pub struct Binary {

impl Binary {
pub fn add_to<F: IsField>(cs: &mut ConstraintSystem<F>, binary_column: &str) {
cs.constraints.push(Box::new(
Self { binary_column: cs.index(&binary_column.to_string()) }
));
cs.constraints.push(Box::new(Self {
binary_column: cs.index(&binary_column.to_string()),
}));
}
}

impl<F: IsField> Constraint<F> for Binary {
fn evaluate(&self, frame: &Vec<Vec<FieldElement<F>>>, challenges: &Vec<FieldElement<F>>) -> FieldElement<F> {
fn evaluate(
&self,
frame: &Vec<Vec<FieldElement<F>>>,
challenges: &Vec<FieldElement<F>>,
) -> FieldElement<F> {
(&frame[0][self.binary_column] - FieldElement::one()) * &frame[0][self.binary_column]
}
}

#[derive(Clone)]
pub struct Fibonacci {
fibonacci_column: usize
fibonacci_column: usize,
}

impl Fibonacci {
pub fn add_to<F: IsField>(cs: &mut ConstraintSystem<F>, column_name: &str) {
cs.constraints.push(Box::new(
Self { fibonacci_column: cs.index(&column_name.to_string()) }
));
cs.constraints.push(Box::new(Self {
fibonacci_column: cs.index(&column_name.to_string()),
}));
}
}

impl<F: IsField> Constraint<F> for Fibonacci {
fn evaluate(&self, frame: &Vec<Vec<FieldElement<F>>>, challenges: &Vec<FieldElement<F>>) -> FieldElement<F> {
&frame[2][self.fibonacci_column] - &frame[1][self.fibonacci_column] - &frame[0][self.fibonacci_column]
fn evaluate(
&self,
frame: &Vec<Vec<FieldElement<F>>>,
challenges: &Vec<FieldElement<F>>,
) -> FieldElement<F> {
&frame[2][self.fibonacci_column]
- &frame[1][self.fibonacci_column]
- &frame[0][self.fibonacci_column]
}
}

Expand All @@ -183,25 +203,34 @@ pub struct Permutation {
columns_a: Vec<usize>,
columns_b: Vec<usize>,
challenge_z: usize,
challenge_gamma: usize
challenge_gamma: usize,
}

impl Permutation {
pub fn add_to<F: IsField>(cs: &mut ConstraintSystem<F>, cumulative: &str, columns_a: Vec<String>, columns_b: Vec<String>, challenge_z: &str, challenge_gamma: &str) {
cs.constraints.push(Box::new(
Self {
cumulative: cs.index(&cumulative.to_string()),
columns_a: columns_a.iter().map(|x| cs.index(&x)).collect(),
columns_b: columns_b.iter().map(|x| cs.index(&x)).collect(),
challenge_gamma: cs.challenge_index(&challenge_gamma.to_string()),
challenge_z: cs.challenge_index(&challenge_z.to_string()),
}
));
pub fn add_to<F: IsField>(
cs: &mut ConstraintSystem<F>,
cumulative: &str,
columns_a: Vec<String>,
columns_b: Vec<String>,
challenge_z: &str,
challenge_gamma: &str,
) {
cs.constraints.push(Box::new(Self {
cumulative: cs.index(&cumulative.to_string()),
columns_a: columns_a.iter().map(|x| cs.index(&x)).collect(),
columns_b: columns_b.iter().map(|x| cs.index(&x)).collect(),
challenge_gamma: cs.challenge_index(&challenge_gamma.to_string()),
challenge_z: cs.challenge_index(&challenge_z.to_string()),
}));
}
}

impl<F: IsField> Constraint<F> for Permutation {
fn evaluate(&self, frame: &Vec<Vec<FieldElement<F>>>, challenges: &Vec<FieldElement<F>>) -> FieldElement<F> {
fn evaluate(
&self,
frame: &Vec<Vec<FieldElement<F>>>,
challenges: &Vec<FieldElement<F>>,
) -> FieldElement<F> {
let z = &challenges[self.challenge_z];
let gamma = challenges[self.challenge_gamma].clone();

Expand All @@ -215,35 +244,55 @@ impl<F: IsField> Constraint<F> for Permutation {
}
}

pub struct ConstraintSystem<F: IsField>{
pub struct ConstraintSystem<F: IsField> {
column_names: Vec<String>,
challenges: Vec<String>,
pub constraints: Vec<Box<dyn Constraint<F>>>
pub constraints: Vec<Box<dyn Constraint<F>>>,
}

impl<F: IsField> ConstraintSystem<F> {
pub fn new(column_names: Vec<&str>, challenges: Vec<&str>) -> Self {
Self {
column_names: column_names.iter().map(|x| x.to_string()).collect(),
challenges: challenges.iter().map(|x| x.to_string()).collect(),
constraints: Vec::new()
constraints: Vec::new(),
}
}

fn index(&self, column_name: &String) -> usize {
self.column_names.iter().position(|c|c == column_name).unwrap()
self.column_names
.iter()
.position(|c| c == column_name)
.unwrap()
}

fn challenge_index(&self, column_name: &String) -> usize {
self.challenges.iter().position(|c|c == column_name).unwrap()
self.challenges
.iter()
.position(|c| c == column_name)
.unwrap()
}

pub fn add_fibonacci_constraint(&mut self, column_name: &str) {
Fibonacci::add_to(self, column_name)
}

pub fn add_permutation_constraint(&mut self, cumulative: &str, columns_a: Vec<String>, columns_b: Vec<String>, challenge_z: &str, challenge_gamma: &str) {
Permutation::add_to(self, cumulative, columns_a, columns_b, challenge_z, challenge_gamma)
pub fn add_permutation_constraint(
&mut self,
cumulative: &str,
columns_a: Vec<String>,
columns_b: Vec<String>,
challenge_z: &str,
challenge_gamma: &str,
) {
Permutation::add_to(
self,
cumulative,
columns_a,
columns_b,
challenge_z,
challenge_gamma,
)
}

pub fn add_binary_constraint(&mut self, binary_column: &str) {
Expand Down

0 comments on commit c4b9d79

Please sign in to comment.