Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
manojkgorle committed Dec 7, 2024
2 parents 89f3a52 + 3311ebc commit d6999ea
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 67 deletions.
57 changes: 32 additions & 25 deletions src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,31 +365,38 @@ impl FriDomain {
#[cfg(test)]
//@todo write test for fri layer, in stark 101, eval domain was a vector, here it is FriDomain struct, thus some code and values will change accordingly

// mod test_fri_layer{
// use super::*;
// //use crate::{field::Field, utils::*};
// #[test]
// fn test_fri() {
// let field = Field::new(1<<64-1<<32+1);
// let poly = Polynomial::new_from_coefficients(vec![
// FieldElement(2, field),
// FieldElement(3, field),
// FieldElement(0, field),
// FieldElement(1, field),
// ]);
// let domain = FriDomain { offset: (FieldElement::one(field)), omega: (field.primitive_nth_root(4)), length: (4) };
// let beta = FieldElement(3, field);

// let (next_poly, next_eval_domain, next_evaluations) = next_fri_layer(poly, beta, domain);
// assert_eq!(next_poly.coefficients.len(), 2);
// assert_eq!(next_poly.coefficients[0].0, 4);
// assert_eq!(next_poly.coefficients[1].0, 3);
// assert_eq!(next_eval_domain.length, 1);
// assert_eq!(next_eval_domain[0].0, 4);
// assert_eq!(next_evaluations.len(), 1);
// assert_eq!(next_evaluations[0].0, 2);
// }
// }
mod test_fri_layer {
use super::*;
//use crate::{field::Field, utils::*};
#[test]
fn test_fri() {
let field = Field::new(17);
let poly = Polynomial::new_from_coefficients(vec![
FieldElement(2, field),
FieldElement(3, field),
FieldElement(0, field),
FieldElement(1, field),
]);
let domain = FriDomain {
offset: (FieldElement::one(field)),
omega: (FieldElement(4, field)),
length: (4),
};

let beta = FieldElement(3, field);

let (next_poly, next_eval_domain, next_evaluations) = next_fri_layer(poly, beta, domain);

assert_eq!(next_poly.coefficients.len(), 2);
assert_eq!(next_poly.coefficients[0].0, 11);
assert_eq!(next_poly.coefficients[1].0, 3);
assert_eq!(next_eval_domain.length, 2);
assert_eq!(next_eval_domain.omega, FieldElement::new(16, field));
assert_eq!(next_evaluations.len(), 2);
assert_eq!(next_evaluations[0].0, 14);
assert_eq!(next_evaluations[1].0, 8);
}
}
mod test_fri_domain {
#![allow(unused_variables)]
use super::*;
Expand Down
13 changes: 5 additions & 8 deletions src/tables/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl InstructionTable {
}
}

pub fn generate_air(&self, challenges: Vec<FieldElement>) -> Vec<Polynomial> {
pub fn generate_air(&self, challenges: Vec<FieldElement>,tppa:FieldElement,tpea:FieldElement) -> Vec<Polynomial> {
let interpolated = self.table.clone().interpolate_columns(vec![
Indices::Address as u128,
Indices::CurrentInstruction as u128,
Expand Down Expand Up @@ -203,17 +203,14 @@ impl InstructionTable {
air.push(transitionair);

//Terminal constraints:
//@todo Tppa = tipa --> include a constraint for this?

//ppa - Tppa
//pea - tpea
//@todo Tppa and tipa given by prover, for now just taking it as empty polynomials to write constraint without error
//@todo tpea is computed locally by verifier, taking empty polynomial for now

let tppa = Polynomial::new_from_coefficients(vec![]);
let tipa = Polynomial::new_from_coefficients(vec![]);
let tpea = Polynomial::new_from_coefficients(vec![]);
let terminalair =
ppa.clone() - tppa.clone() + pea.clone() - tpea.clone() + tppa.clone() - tipa.clone();
ppa.clone() -Polynomial::constant(tppa) + pea.clone() - Polynomial::constant(tpea) ;
//@todo separate Tppa - tipa term as it will cancel out
air.push(terminalair);

Expand Down Expand Up @@ -245,9 +242,9 @@ impl InstructionTable {
zerofiers
}

pub fn generate_quotients(&self, challenges: Vec<FieldElement>) -> Vec<Polynomial> {
pub fn generate_quotients(&self, challenges: Vec<FieldElement>,tppa:FieldElement,tpea:FieldElement) -> Vec<Polynomial> {
let mut quotients = vec![];
let air = self.generate_air(challenges);
let air = self.generate_air(challenges,tppa,tpea);
let zerofiers = self.generate_zerofier();

for i in 0..air.len() {
Expand Down
20 changes: 8 additions & 12 deletions src/tables/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Memory {
}

//this is after padding and extension
pub fn generate_air(&self, challenges: Vec<FieldElement>) -> Vec<Polynomial> {
pub fn generate_air(&self, challenges: Vec<FieldElement>,tppa:FieldElement) -> Vec<Polynomial> {
let interpolated = self.table.clone().interpolate_columns(vec![
Indices::Cycle as u128,
Indices::MemoryPointer as u128,
Expand Down Expand Up @@ -175,22 +175,18 @@ impl Memory {
air.push(transitionair);

//Terminal constraints:
//@todo Tppa = tmpa --> include a constraint for this?

//ppa.(d.clk+e.mp+f.mv-beta)-Tppa
//@todo Tppa and tmpa given by prover, for now just taking it as empty polynomials to write constraint without error
let tppa = Polynomial::new_from_coefficients(vec![]);
let tmpa = Polynomial::new_from_coefficients(vec![]);
let terminalair = ppa
// Tppa given by prover, for now just taking it as empty polynomials to write constraint without error
let terminalair = ppa
* (clk.scalar_mul(challenges[ChallengeIndices::D as usize])
+ mp.scalar_mul(challenges[ChallengeIndices::E as usize])
+ mv.scalar_mul(challenges[ChallengeIndices::F as usize])
- Polynomial::new_from_coefficients(vec![
challenges[ChallengeIndices::Beta as usize],
]))
- tppa.clone()
+ tppa // @todo check this out, this is looking weird.
- tmpa;
//@todo check if Tppa -tmpa needs to be written separately```````````
- Polynomial::constant(tppa);

air.push(terminalair);

air
Expand Down Expand Up @@ -222,9 +218,9 @@ impl Memory {
zerofiers
}

pub fn generate_quotients(&self, challenges: Vec<FieldElement>) -> Vec<Polynomial> {
pub fn generate_quotients(&self, challenges: Vec<FieldElement>,tppa:FieldElement) -> Vec<Polynomial> {
let mut quotients = vec![];
let air = self.generate_air(challenges);
let air = self.generate_air(challenges,tppa);
let zerofiers = self.generate_zerofier();

for i in 0..air.len() {
Expand Down
83 changes: 61 additions & 22 deletions src/tables/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ impl ProcessorTable {
zerofiers
}

pub fn generate_quotients(&self, challenges: Vec<FieldElement>) -> Vec<Polynomial> {
pub fn generate_quotients(&self, challenges: Vec<FieldElement>,tipa:FieldElement,tmpa:FieldElement,tiea:FieldElement,toea:FieldElement) -> Vec<Polynomial> {
let mut quotients = vec![];
let air = self.generate_air(challenges);
let air = self.generate_air(challenges,tipa,tmpa,tiea,toea);
let zerofiers = self.generate_zerofier();

for i in 0..air.len() {
Expand All @@ -333,10 +333,26 @@ impl ProcessorTable {
}
acc
}

// boundary constraints for the base coloumns
//@ I am not using this function because it because universal selector is redundant
// define a selector polynomial for a valid set if instruction from this set then it should be zero
pub fn universal_selector(ci: Polynomial, field: Field) ->Polynomial{
let f = |x: char| -> FieldElement { FieldElement::new((x as u32) as u128, field) };
// let mut deselectors = Vec::new();
let mut acc = Polynomial::constant(FieldElement::new(1, field));

// for target_char in "[]<>,.+-".chars() {
let target_char = ['[',']','<','>',',','.','+','-'];

for c in target_char.iter() {

acc *= ci.clone() - Polynomial::constant(FieldElement::new(f(*c).0, field));

} acc
}

//boundary constraints for the base coloumns
// the values of instructionpermutaion ipa and mpa I am taking as 1
pub fn generate_air(&self, challenges: Vec<FieldElement>) -> Vec<Polynomial> {
pub fn generate_air(&self, challenges: Vec<FieldElement>,tipa:FieldElement,tmpa:FieldElement,tiea:FieldElement,toea:FieldElement) -> Vec<Polynomial> {
let f =
|x: char| -> FieldElement { FieldElement::new((x as u32) as u128, self.table.field) };
let interpolated = self.table.clone().interpolate_columns(vec![
Expand Down Expand Up @@ -378,16 +394,13 @@ impl ProcessorTable {
]);
let clk_next = next_interpolated[Indices::Cycle as usize].clone();
let ip_next = next_interpolated[Indices::InstructionPointer as usize].clone();
let ci_next = next_interpolated[Indices::CurrentInstruction as usize].clone();
let ni_next = next_interpolated[Indices::NextInstruction as usize].clone();
let mp_next = next_interpolated[Indices::MemoryPointer as usize].clone();
let mv_next = next_interpolated[Indices::MemoryValue as usize].clone();
let inv_mv_next = next_interpolated[Indices::MemoryValueInverse as usize].clone();
let ipa_next = next_interpolated[Indices::InstructionPermutaion as usize].clone();
let mpa_next = next_interpolated[Indices::MemoryPermuation as usize].clone();
let iea_next = next_interpolated[Indices::InputEvaluation as usize].clone();
let oea_next = next_interpolated[Indices::OutputEvaluation as usize].clone();
let one = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]);

let mut air = vec![];
//Boundary contsraints :clk=mp=mv=inv=ip=0
//iea=oea=1 (we are using 1 instead of any random number)
Expand Down Expand Up @@ -517,16 +530,17 @@ impl ProcessorTable {
- oea_next.clone())
+ (ci.clone() - Polynomial::constant(f('.'))) * (oea.clone() - oea_next.clone());
air.push(trasition_all);
//@todo have to seperate the terminal
// Terminal constraints
// tipa, tmpa- last row not accumulated so:
//1.ipa.(a.ip+ b.ci+c.ni-alpha)-tipa
//2.mpa.(d.clk+e.mp+f.mv-beta)-tmpa
// tiea, toea- last element identical to terminal
//3.iea-tiea 4. oea-toea
let tipa = Polynomial::new_from_coefficients(vec![]);
let tmpa = Polynomial::new_from_coefficients(vec![]);
let tiea = Polynomial::new_from_coefficients(vec![]);
let toea = Polynomial::new_from_coefficients(vec![]);
// let tipa = Polynomial::new_from_coefficients(vec![]);
// let tmpa = Polynomial::new_from_coefficients(vec![]);
// let tiea = Polynomial::new_from_coefficients(vec![]);
// let toea = Polynomial::new_from_coefficients(vec![]);
let terminal_air = ipa.clone()
* (ip
.clone()
Expand All @@ -536,19 +550,19 @@ impl ProcessorTable {
+ ni.clone()
.scalar_mul(challenges[ChallengeIndices::C as usize])
- Polynomial::constant(challenges[ChallengeIndices::Beta as usize]))
- tipa.clone()
- Polynomial::constant(tipa)
+ mpa.clone()
* (clk.scalar_mul(challenges[ChallengeIndices::D as usize])
+ mp.clone()
.scalar_mul(challenges[ChallengeIndices::E as usize])
+ mv.clone()
.scalar_mul(challenges[ChallengeIndices::F as usize])
- Polynomial::constant(challenges[ChallengeIndices::Beta as usize]))
- tmpa
- Polynomial::constant(tmpa)
+ iea
- tiea
- Polynomial::constant(tiea)
+ oea
- toea;
- Polynomial::constant(toea);
air.push(terminal_air);
air
}
Expand Down Expand Up @@ -583,6 +597,31 @@ mod tests_processor_operations {
ci_array[i as usize]
);
}
}
#[test]
fn test_universal_selector(){
let field = Field::new((1 << 64) - (1 << 32) + 1);
let f = |x: char| -> FieldElement { FieldElement::new((x as u32) as u128, field) };
let ci_array = vec!['[', ']', '<', '>', '-', '.', '+', ','];
let mut values_array: Vec<FieldElement> = Vec::new();
for c in ci_array.clone() {
println!("{:?}", f(c));
values_array.push(f(c));
}
let generator = FieldElement::new(1753635133440165772, field);
let order = 1 << 32;
let target_order = 8;
//println!("generator ={:?}", generator);
let omicron = derive_omicron(generator, order, target_order);
//println!("omicron ={:?}", omicron);
let domain = FriDomain::new(FieldElement::new(1, field), omicron, target_order);
let ci = domain.interpolate(values_array);
let poly = ProcessorTable::universal_selector(ci, field);
let values=poly.evaluate(omicron.pow(1));
assert_eq!(values,FieldElement::zero(field));



}
}

Expand Down Expand Up @@ -695,10 +734,10 @@ mod test_processor {
for row in processor_table.table.matrix.clone() {
println!("{:?}", row);
}
let air = processor_table.generate_air(challenges);
println!("air");
for row in air {
println!("{:?}", row);
}
// let air = processor_table.generate_air(challenges);
// println!("air");
// for row in air {
// println!("{:?}", row);
// }
}
}

0 comments on commit d6999ea

Please sign in to comment.