Skip to content

Commit

Permalink
Merge branch 'main' of github.com:manojkgorle/brainfuckvm
Browse files Browse the repository at this point in the history
  • Loading branch information
m-pandey5 committed Dec 7, 2024
2 parents c1b2ae8 + a47a4a8 commit a725775
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 136 deletions.
1 change: 1 addition & 0 deletions src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ mod test_fri_layer {
}
}
mod test_fri_domain {
#![allow(unused_variables)]
use super::*;
#[test]
fn test_evaluate() {
Expand Down
2 changes: 1 addition & 1 deletion src/multivariate_polynomial/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused_variables)]
use crate::fields::{Field, FieldElement};
use crate::univariate_polynomial::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -123,7 +124,6 @@ impl MPolynomial {

pub fn symbolic_degree_bound(&self, max_degrees: Vec<u128>) -> i128 {
// Check if the polynomial is empty
let field = self.field;
if self.dictionary.is_empty() {
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions src/stark/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused_variables)]
use crate::fields::FieldElement;

pub struct Stark<'a> {
Expand Down
106 changes: 53 additions & 53 deletions src/tables/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl InstructionTable {
}
}

pub fn extend_column(&mut self, randFieldElem: u128, challenges: Vec<FieldElement>) {
let mut ppa = FieldElement::new(randFieldElem, self.table.field);
pub fn extend_column(&mut self, rand_field_elem: u128, challenges: Vec<FieldElement>) {
let mut ppa = FieldElement::new(rand_field_elem, self.table.field);
//take randFieldElement = 1 when not implementing random secret diff constraint
let pea = FieldElement::zero(self.table.field);

Expand Down Expand Up @@ -121,11 +121,11 @@ impl InstructionTable {
Indices::PermutationArg as u128,
Indices::EvaluationArg as u128,
]);
let IP = interpolated[Indices::Address as usize].clone();
let CI = interpolated[Indices::CurrentInstruction as usize].clone();
let NI = interpolated[Indices::NextInstruction as usize].clone();
let PPA = interpolated[Indices::PermutationArg as usize].clone();
let PEA = interpolated[Indices::EvaluationArg as usize].clone();
let ip = interpolated[Indices::Address as usize].clone();
let ci = interpolated[Indices::CurrentInstruction as usize].clone();
let ni = interpolated[Indices::NextInstruction as usize].clone();
let ppa = interpolated[Indices::PermutationArg as usize].clone();
let pea = interpolated[Indices::EvaluationArg as usize].clone();

let next_interpolated = self.table.clone().next_interpolate_columns(vec![
Indices::Address as u128,
Expand All @@ -135,24 +135,24 @@ impl InstructionTable {
Indices::EvaluationArg as u128,
]);

let IP_next = next_interpolated[Indices::Address as usize].clone();
let CI_next = next_interpolated[Indices::CurrentInstruction as usize].clone();
let NI_next = next_interpolated[Indices::NextInstruction as usize].clone();
let PPA_next = next_interpolated[Indices::PermutationArg as usize].clone();
let PEA_next = next_interpolated[Indices::EvaluationArg as usize].clone();
let ip_next = next_interpolated[Indices::Address as usize].clone();
let ci_next = next_interpolated[Indices::CurrentInstruction as usize].clone();
let ni_next = next_interpolated[Indices::NextInstruction as usize].clone();
let ppa_next = next_interpolated[Indices::PermutationArg as usize].clone();
let pea_next = next_interpolated[Indices::EvaluationArg as usize].clone();

let one = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]);
let mut AIR = vec![];
let mut air = vec![];

//Boundary constraint: ip=0
//@todo ppa and pea initial value from extended fn, see once

let boundaryAIR = IP.clone() + PPA.clone()
let boundaryair = ip.clone() + ppa.clone()
- Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)])
+ PEA.clone()
+ pea.clone()
- Polynomial::new_from_coefficients(vec![FieldElement::zero(self.table.field)]);
//@todo check this once!! initial value is not zero and one, set it to req value
AIR.push(boundaryAIR);
air.push(boundaryair);

//Transition constraints: * == next
//1. (ip-ip*).(ip*-ip-1)
Expand All @@ -164,60 +164,60 @@ impl InstructionTable {
//7. (ip*-ip-1).(pea*-pea)
//8. (ip*-ip).(pea* - pea.eta - (a.ip*+b.ci*+c.ni*))

let transitionAIR = (IP.clone() - IP_next.clone())
* (IP_next.clone() - IP.clone() - one.clone())
+ (IP.clone() - IP_next.clone()) * (NI.clone() - CI_next.clone())
+ (IP_next.clone() - IP.clone() - one.clone()) * (CI_next.clone() - CI.clone())
+ (IP_next.clone() - IP.clone() - one.clone()) * (NI_next.clone() - NI.clone())
+ (IP.clone() + one.clone() - IP_next.clone())
* (PPA_next.clone()
- PPA.clone()
* (IP_next
let transitionair = (ip.clone() - ip_next.clone())
* (ip_next.clone() - ip.clone() - one.clone())
+ (ip.clone() - ip_next.clone()) * (ni.clone() - ci_next.clone())
+ (ip_next.clone() - ip.clone() - one.clone()) * (ci_next.clone() - ci.clone())
+ (ip_next.clone() - ip.clone() - one.clone()) * (ni_next.clone() - ni.clone())
+ (ip.clone() + one.clone() - ip_next.clone())
* (ppa_next.clone()
- ppa.clone()
* (ip_next
.clone()
.scalar_mul(challenges[ChallengeIndices::A as usize])
+ CI_next
+ ci_next
.clone()
.scalar_mul(challenges[ChallengeIndices::B as usize])
+ NI_next
+ ni_next
.clone()
.scalar_mul(challenges[ChallengeIndices::C as usize])
- Polynomial::new_from_coefficients(vec![
challenges[ChallengeIndices::A as usize],
])))
+ (IP.clone() - IP_next.clone()) * (PPA_next.clone() - PPA.clone())
+ (IP_next.clone() - IP.clone() - one) * (PEA_next.clone() - PEA.clone())
+ (IP.clone() - IP_next.clone())
* (PEA_next.clone()
- PEA
+ (ip.clone() - ip_next.clone()) * (ppa_next.clone() - ppa.clone())
+ (ip_next.clone() - ip.clone() - one) * (pea_next.clone() - pea.clone())
+ (ip.clone() - ip_next.clone())
* (pea_next.clone()
- pea
.clone()
.scalar_mul(challenges[ChallengeIndices::Eta as usize])
- (IP_next
- (ip_next
.clone()
.scalar_mul(challenges[ChallengeIndices::A as usize])
+ CI_next
+ ci_next
.clone()
.scalar_mul(challenges[ChallengeIndices::B as usize])
+ NI_next
+ ni_next
.clone()
.scalar_mul(challenges[ChallengeIndices::C as usize])));
AIR.push(transitionAIR);
air.push(transitionair);

//Terminal constraints:
//@todo Tppa = Tipa --> include a constraint for this?
//@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
//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();
//@todo separate Tppa - Tipa term as it will cancel out
AIR.push(terminalAIR);
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();
//@todo separate Tppa - tipa term as it will cancel out
air.push(terminalair);

AIR
air
}

pub fn generate_zerofier(&self) -> Vec<Polynomial> {
Expand All @@ -241,17 +241,17 @@ impl InstructionTable {

let terminal_zerofier = x.clone()
- Polynomial::new_from_coefficients(vec![omicron.clone().pow(self.table.length - 1)]);

zerofiers.push(terminal_zerofier);
zerofiers
}

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

for i in 0..AIR.len() {
quotients.push(AIR[i].clone().q_div(zerofiers[i].clone()).0);
for i in 0..air.len() {
quotients.push(air[i].clone().q_div(zerofiers[i].clone()).0);
}
quotients
}
Expand All @@ -272,7 +272,7 @@ mod test_instruction {
let vm = VirtualMachine::new(field);
let code2 = ">>[++-]<".to_string();
let program = vm.compile(code2);
let (runtime, _, _) = vm.run(&program, "".to_string());
// let (runtime, _, _) = vm.run(&program, "".to_string());
let (processor_matrix, _memory_matrix, instruction_matrix, _input_matrix, _output_matrix) =
vm.simulate(&program, "".to_string());
assert_eq!(
Expand Down
5 changes: 3 additions & 2 deletions src/tables/io.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused_variables)]
use crate::fields::{Field, FieldElement};
use crate::tables::Table;
use crate::tables::{derive_omicron, roundup_npow2};
Expand Down Expand Up @@ -37,8 +38,8 @@ impl IOTable {

pub fn pad(&mut self) {}

pub fn extend_column_ea(&mut self, randFieldElem: u128, challenge: FieldElement) {
let mut ea = FieldElement::new(randFieldElem, self.table.field); // take randFieldElem as zero if no random secret implementation
pub fn extend_column_ea(&mut self, rand_field_elem: u128, challenge: FieldElement) {
let mut ea = FieldElement::new(rand_field_elem, self.table.field); // take rand_field_elem as zero if no random secret implementation
self.table.matrix[0][1] = ea;
for i in 0..self.table.length - 1 {
ea = self.table.matrix[i as usize][1] * challenge
Expand Down
89 changes: 44 additions & 45 deletions src/tables/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ impl Memory {

// processor matrix that goes here is unpadded
pub fn derive_matrix(processor_matrix: &[Vec<FieldElement>]) -> Vec<Vec<FieldElement>> {
let field = processor_matrix[0][0].1;
let zero = FieldElement::zero(field);
let one = FieldElement::one(field);
let mut matrix: Vec<Vec<FieldElement>> = processor_matrix
.iter()
// .filter(|pt| pt[ProcessorIndices::CurrentInstruction as usize] != zero), we need the last processor row, right?
Expand All @@ -108,8 +105,8 @@ impl Memory {
}

//the matrix taken here is padded
pub fn extend_column_ppa(&mut self, randFieldElem: u128, challenges: Vec<FieldElement>) {
let mut ppa = FieldElement::new(randFieldElem, self.table.field);
pub fn extend_column_ppa(&mut self, rand_field_elem: u128, challenges: Vec<FieldElement>) {
let mut ppa = FieldElement::new(rand_field_elem, self.table.field);
self.table.matrix[0][Indices::PermutationArg as usize] = ppa;
for i in 0..self.table.length - 1 {
let weighted_sum = self.table.matrix[i as usize][Indices::Cycle as usize]
Expand All @@ -132,10 +129,10 @@ impl Memory {
Indices::MemoryValue as u128,
Indices::PermutationArg as u128,
]);
let CLK = interpolated[Indices::Cycle as usize].clone();
let MP = interpolated[Indices::MemoryPointer as usize].clone();
let MV = interpolated[Indices::MemoryValue as usize].clone();
let PPA = interpolated[Indices::PermutationArg as usize].clone();
let clk = interpolated[Indices::Cycle as usize].clone();
let mp = interpolated[Indices::MemoryPointer as usize].clone();
let mv = interpolated[Indices::MemoryValue as usize].clone();
let ppa = interpolated[Indices::PermutationArg as usize].clone();

let next_interpolated = self.table.clone().next_interpolate_columns(vec![
Indices::Cycle as u128,
Expand All @@ -144,59 +141,59 @@ impl Memory {
Indices::PermutationArg as u128,
]);

let CLK_next = next_interpolated[Indices::Cycle as usize].clone();
let MP_next = next_interpolated[Indices::MemoryPointer as usize].clone();
let MV_next = next_interpolated[Indices::MemoryValue as usize].clone();
let PPA_next = next_interpolated[Indices::PermutationArg as usize].clone();
let clk_next = next_interpolated[Indices::Cycle as usize].clone();
let mp_next = next_interpolated[Indices::MemoryPointer as usize].clone();
let mv_next = next_interpolated[Indices::MemoryValue as usize].clone();
let ppa_next = next_interpolated[Indices::PermutationArg as usize].clone();
let one = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]);
let mut AIR = vec![];
let mut air = vec![];

//Boundary constraint: clk = mp = mv = 0, ppa = 1 (random secret using diff constr if we use it, not needed rn)
let boundaryAIR = CLK.clone() + MP.clone() + MV.clone() + PPA.clone()
let boundaryair = clk.clone() + mp.clone() + mv.clone() + ppa.clone()
- Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]);
AIR.push(boundaryAIR);
air.push(boundaryair);

//Transition constraints: * == next
//1. (mp+1-mp*).(mp-mp*)
//2. (mp-mp*).mv*
//3. (mp-mp*).(mv-mv*)
//4. (clk - 1 - clk*).(mv*-mv)
//5. ppa.(d.clk + e.mp + f.mv - beta) - ppa*
let transitionAIR = (MP.clone() + one.clone() - MP_next.clone())
* (MP.clone() - MP_next.clone())
+ (MP.clone() - MP_next.clone()) * MV_next.clone()
+ (MP.clone() - MP_next.clone()) * (MV.clone() - MV_next.clone())
+ (CLK.clone() - one.clone() - CLK_next) * (MV_next.clone() - MV.clone())
+ PPA.clone()
* (CLK.scalar_mul(challenges[ChallengeIndices::D as usize])
+ MP.scalar_mul(challenges[ChallengeIndices::E as usize])
+ MV.scalar_mul(challenges[ChallengeIndices::F as usize])
let transitionair = (mp.clone() + one.clone() - mp_next.clone())
* (mp.clone() - mp_next.clone())
+ (mp.clone() - mp_next.clone()) * mv_next.clone()
+ (mp.clone() - mp_next.clone()) * (mv.clone() - mv_next.clone())
+ (clk.clone() - one.clone() - clk_next) * (mv_next.clone() - mv.clone())
+ ppa.clone()
* (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],
]))
- PPA_next;
AIR.push(transitionAIR);
- ppa_next;
air.push(transitionair);

//Terminal constraints:
//@todo Tppa = Tmpa --> include a constraint for this?
//@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
* (CLK.scalar_mul(challenges[ChallengeIndices::D as usize])
+ MP.scalar_mul(challenges[ChallengeIndices::E as usize])
+ MV.scalar_mul(challenges[ChallengeIndices::F as usize])
//@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
* (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
- Tmpa;
//@todo check if Tppa -Tmpa needs to be written separately```````````
AIR.push(terminalAIR);
- tppa.clone()
+ tppa // @todo check this out, this is looking weird.
- tmpa;
//@todo check if Tppa -tmpa needs to be written separately```````````
air.push(terminalair);

AIR
air
}

pub fn generate_zerofier(&self) -> Vec<Polynomial> {
Expand All @@ -221,29 +218,31 @@ impl Memory {
let terminal_zerofier = x.clone()
- Polynomial::new_from_coefficients(vec![omicron.clone().pow(self.table.length - 1)]);

zerofiers.push(terminal_zerofier);
zerofiers
}

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

for i in 0..AIR.len() {
quotients.push(AIR[i].clone().q_div(zerofiers[i].clone()).0);
for i in 0..air.len() {
quotients.push(air[i].clone().q_div(zerofiers[i].clone()).0);
}
quotients
}
}

//@todo test extend column ppa
//@todo test generate AIR
//@todo test generate air
//@todo test generate zerofier
//@todo test generate quotient
//@todo test memory table padding

#[cfg(test)]
mod test_memory_table {
#![allow(unused_variables)]
use super::Memory;
use crate::fields::{Field, FieldElement};
use crate::tables::memory::{ChallengeIndices, Indices};
Expand Down
Loading

0 comments on commit a725775

Please sign in to comment.