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 8, 2024
2 parents d0509b4 + 124b90c commit dfe792d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/stark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use memory::MemoryTable;
use processor::ProcessorTable;
use instruction::InstructionTable;

use crate::channel;
use crate::merkle::*;
use crate::channel::*;
use crate::fri::*;
Expand Down Expand Up @@ -88,6 +89,7 @@ pub fn prove(matrices: Vec<Vec<Vec<FieldElement>>>, inputs: Vec<FieldElement>, f
// }

//@todo could not find a function in channel for fiat shamir, ie sending data as string and then getting random element
// @soumyathakur44, you can use channell.send with .into() to send string data.
//@todo make extend columns function return Terminal value , eg. Tipa, for every table and store it, use it to compare

}
Expand Down
111 changes: 80 additions & 31 deletions src/tables/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ impl ProcessorTable {
mpa *= weighted_sum;
self.table.matrix[(i + 1) as usize][Indices::MemoryPermuation as usize] = mpa;
}
let mut tmpa=mpa*(self.table.matrix[self.table.length as usize][Indices::Cycle as usize]
// @todo what is wrong with tmpa?
let tmpa=mpa*(self.table.matrix[self.table.length as usize][Indices::Cycle as usize]
* challenges[ChallengeIndices::D as usize]
+ self.table.matrix[self.table.length as usize][Indices::MemoryPointer as usize]
* challenges[ChallengeIndices::E as usize]
Expand Down Expand Up @@ -203,8 +204,8 @@ impl ProcessorTable {
self.table.matrix[(i + 1) as usize][Indices::OutputEvaluation as usize] = oea;
}
}
let mut tiea=iea;
let mut toea =oea;
let tiea=iea;
let toea =oea;
let mut terminal:Vec<FieldElement>=Vec::new();
terminal.push(tipa);
terminal.push(tmpa);
Expand Down Expand Up @@ -334,7 +335,7 @@ impl ProcessorTable {

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,tipa,tmpa,tiea,toea);
let air = self.generate_air(challenges,tipa,tmpa,tiea,toea, FieldElement::zero(self.table.field));
let zerofiers = self.generate_zerofier();

for i in 0..air.len() {
Expand Down Expand Up @@ -375,7 +376,7 @@ impl ProcessorTable {
// boundary constraints for the base coloumns
// the values of instructionpermutaion ipa and mpa I am taking as 1
// @todo pa, ea are obtained when the table is extended.
pub fn generate_air(&self, challenges: Vec<FieldElement>,tipa:FieldElement,tmpa:FieldElement,tiea:FieldElement,toea:FieldElement) -> Vec<Polynomial> {
pub fn generate_air(&self, challenges: Vec<FieldElement>,tipa:FieldElement,tmpa:FieldElement,tiea:FieldElement,toea:FieldElement, eval: FieldElement) -> Vec<Polynomial> {
let f =
|x: char| -> FieldElement { FieldElement::new((x as u32) as u128, self.table.field) };
let indices_vec = vec![
Expand Down Expand Up @@ -470,12 +471,13 @@ impl ProcessorTable {
// ip⋆−ip−1
// mp⋆−mp+1
let trasition_i3 = (ip_next.clone() - ip.clone() - poly_one.clone())
+ (mp_next.clone() - mp.clone() + poly_one.clone());
+ (mp_next.clone() - mp.clone() - poly_one.clone());
air.push(trasition_i3);

// ip⋆−ip−1
// mp⋆−mp
// mv⋆−mv−1
// ci = >
let trasition_i4 = (ip_next.clone() - ip.clone() - poly_one.clone())
+ (mp_next.clone() - mp.clone())
+ (mv_next.clone() - mv.clone() - poly_one.clone());
Expand All @@ -484,9 +486,10 @@ impl ProcessorTable {
// ip⋆−ip−1
// mp⋆−mp
// mv⋆−mv+1
// ci = <
let trasition_i5 = (ip_next.clone() - ip.clone() - poly_one.clone())
+ (mp_next.clone() - mp.clone())
+ (mv_next.clone() - mv.clone() - poly_one.clone());
+ (mv_next.clone() - mv.clone() + poly_one.clone());
air.push(trasition_i5);

// ip⋆−ip−1
Expand All @@ -501,12 +504,12 @@ impl ProcessorTable {
+ (mp_next.clone() - mp.clone())
+ (mv_next.clone() - mv.clone());
air.push(trasition_i7);
//clk⋆−clk−1
// clk⋆−clk−1
// inv⋅(1−inv⋅mv)
//ci.(ipa.(a.ip+b.ci+c.ni-alpha)-ipa*) // this constrainst is become redundant becaue we are not using +(ipa*-ipa).deselector
//mpa.(d.clk+e.mp+f.mv-beta)-mpa*
//selector(,)(ci) . (iea.gamma + mv - iea*) + (ci - “,”) . (iea - iea*)
//selector(.)(ci) . (oea.delta + mv - oea*) + (ci - “.”) . (oea - oea*)
// ci.(ipa.(a.ip+b.ci+c.ni-alpha)-ipa*) // this constrainst is become redundant becaue we are not using +(ipa*-ipa).deselector
// pa.(d.clk+e.mp+f.mv-beta)-mpa*
// selector(,)(ci) . (iea.gamma + mv - iea*) + (ci - “,”) . (iea - iea*)
// selector(.)(ci) . (oea.delta + mv - oea*) + (ci - “.”) . (oea - oea*)

// clk⋆−clk−1+inv⋅(1−inv⋅mv)
// ci.(ipa.(a.ip+b.ci+c.ni-alpha)-ipa*)
Expand Down Expand Up @@ -553,29 +556,39 @@ impl ProcessorTable {
// 2.mpa.(d.clk+e.mp+f.mv-beta)-tmpa
// tiea, toea- last element identical to terminal
// 3.iea-tiea 4. oea-toea
let terminal_air = ipa.clone()
let terminal_air1 = ipa.clone()
* (ip
.clone()
.scalar_mul(challenges[ChallengeIndices::A as usize])
+ ci.clone()
.scalar_mul(challenges[ChallengeIndices::B as usize])
+ ni.clone()
.scalar_mul(challenges[ChallengeIndices::C as usize])
- Polynomial::constant(challenges[ChallengeIndices::Beta as usize]))
- Polynomial::constant(tipa)
+ mpa.clone()
- Polynomial::constant(challenges[ChallengeIndices::Alpha as usize]))
- Polynomial::constant(tipa);
let dam = 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]);
println!("mpa eval: {:?}, dam eval:{:?}, {:?}, {:?}", mpa.evaluate(eval), dam.evaluate(eval), mpa.evaluate(eval) * dam.evaluate(eval), mpa.evaluate(eval) * FieldElement::new(10, self.table.field));
let terminal_air2 = 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]))
- Polynomial::constant(tmpa)
+ iea
- Polynomial::constant(tiea)
+ oea
- Polynomial::constant(tmpa);
let terminal_air3 = iea
- Polynomial::constant(tiea);
let terminal_air4 = oea
- Polynomial::constant(toea);
air.push(terminal_air);
air.push(terminal_air1);
air.push(terminal_air2);
air.push(terminal_air3);
air.push(terminal_air4);
air
}
}
Expand Down Expand Up @@ -637,7 +650,6 @@ mod tests_processor_operations {
}
}

// @todo test processor table padding
#[cfg(test)]
mod test_processor {
use std::time::Instant;
Expand Down Expand Up @@ -684,17 +696,21 @@ mod test_processor {
let field = Field::new(18446744069414584321);
let zero = FieldElement::zero(field);
let one = FieldElement::one(field);
let two = one + one;
let vm = VirtualMachine::new(field);
let generator = field.generator();
// let omicron = generator.clone();
let order = 1 << 32;
// let code = "++>+++++[<+>-]++++++++[<++++++>-]<.".to_string();
let code2 = ">>[++-]<".to_string();
let program = vm.compile(code2);
vm.run(&program, "".to_string());
println!("{:?}", program.clone());
let (rt, _, _) = vm.run(&program, "".to_string());
println!("{:?}", rt);
// assert_eq!(program.len(), 2);
let (processor_matrix, memory_matrix, instruction_matrix, input_matrix, output_matrix) =
vm.simulate(&program, "".to_string());
let challenges = vec![one; 11];
let challenges = vec![two; 11];
let mut processor_table = ProcessorTable::new(
field,
processor_matrix.len() as u128,
Expand Down Expand Up @@ -737,19 +753,52 @@ mod test_processor {
input_table.pad();
output_table.pad();

println!("processor table before extending columns");
for row in processor_table.table.matrix.clone() {
println!("{:?}", row);
}
processor_table.extend_columns(challenges.clone());
// println!("processor table before extending columns");
// for row in processor_table.table.matrix.clone() {
// println!("{:?}", row);
// }
let terminal = processor_table.extend_columns(challenges.clone());
println!("processor table after extending columns");
for row in processor_table.table.matrix.clone() {
println!("{:?}", row);
}
// let air = processor_table.generate_air(challenges);
println!("tmpa: {:?}", terminal[1]);
let mut omicron_domain: Vec<FieldElement> = Vec::new();
for i in 0..processor_table.table.height {
omicron_domain.push(processor_table.table.omicron.pow(i));
if i == 4 {
println!("omicron_domain: {:?}", omicron_domain);
}
}
let air = processor_table.generate_air(challenges, terminal[0], terminal[1], terminal[2], terminal[3], omicron_domain[4]);
// println!("air");
// for row in air {
// for row in air.clone() {
// println!("{:?}", row);
// }

// lets evaluate one of air poly.
// start with boundary poly.
// get the fri domain.
let b = air[0].evaluate(omicron_domain[0]);
assert_eq!(b, zero);
// let t_i0 = air[1].evaluate(omicron_domain[0]);
for v in 0..rt-1 {
let t_all = air[9].evaluate(omicron_domain[v as usize]);
// println!("{:?}", t_all);
assert_eq!(t_all, zero);
}
// why is the t_air not evaluating to zero?
for v in omicron_domain.clone() {
let t_air1 = air[10].evaluate(v);
let t_air2 = air[11].evaluate(v);
let t_air3 = air[12].evaluate(v);
let t_air4 = air[13].evaluate(v);
println!("v: {:?}, t_air1: {:?}, t_air2: {:?}, t_air3: {:?}, t_air4: {:?}, sum: {:?}", v, t_air1, t_air2, t_air3, t_air4, t_air1+t_air2+t_air3+t_air4);
// assert_eq!(t_air, zero);

}
assert_eq!(air[5].evaluate(omicron_domain[1]), zero);
assert_eq!(air[5].evaluate(omicron_domain[0]), zero);
assert!(air[5].evaluate(omicron_domain[2]) != zero);
}
}

0 comments on commit dfe792d

Please sign in to comment.