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 14, 2024
2 parents 0bbd3fe + 4980b5c commit 2fc9aee
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 115 deletions.
30 changes: 15 additions & 15 deletions src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::univariate_polynomial::*;
//boundary_q includes boundary constraints for all tables together, similarly for others

pub fn combination_polynomial(
boundary_q: Vec<Polynomial>,
transition_q: Vec<Polynomial>,
terminal_q: Vec<Polynomial>,
processor_q: Vec<Polynomial>,
memory_q: Vec<Polynomial>,
instruction_q: Vec<Polynomial>,
challenges: Vec<FieldElement>,
height: usize,
field: Field,
Expand All @@ -29,28 +29,28 @@ pub fn combination_polynomial(
//@todo what should be degree here since processor and instruction table can have different heights
//@todo we can also pass a single vector of all quotient

for i in 0..boundary_q.clone().len() {
let d = degree - boundary_q[i].clone().degree();
combination += Polynomial::new_from_coefficients(vec![alpha]) * boundary_q[i].clone()
for i in 0..processor_q.clone().len() {
let d = degree - processor_q[i].clone().degree();
combination += Polynomial::new_from_coefficients(vec![alpha]) * processor_q[i].clone()
+ Polynomial::new_from_coefficients(vec![beta])
* x.clone().pow(d as u128)
* boundary_q[i].clone();
* processor_q[i].clone();
}

for i in 0..transition_q.clone().len() {
let d = degree - transition_q[i].clone().degree();
combination += Polynomial::new_from_coefficients(vec![alpha]) * transition_q[i].clone()
for i in 0..memory_q.clone().len() {
let d = degree - memory_q[i].clone().degree();
combination += Polynomial::new_from_coefficients(vec![alpha]) * memory_q[i].clone()
+ Polynomial::new_from_coefficients(vec![beta])
* x.clone().pow(d as u128)
* transition_q[i].clone();
* memory_q[i].clone();
}

for i in 0..terminal_q.clone().len() {
let d = degree - terminal_q[i].clone().degree();
combination += Polynomial::new_from_coefficients(vec![alpha]) * terminal_q[i].clone()
for i in 0..instruction_q.clone().len() {
let d = degree - instruction_q[i].clone().degree();
combination += Polynomial::new_from_coefficients(vec![alpha]) * instruction_q[i].clone()
+ Polynomial::new_from_coefficients(vec![beta])
* x.clone().pow(d as u128)
* terminal_q[i].clone();
* instruction_q[i].clone();
}

combination
Expand Down
31 changes: 25 additions & 6 deletions src/stark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,33 @@ pub fn prove(matrices: Vec<Vec<Vec<FieldElement>>>, inputs: Vec<FieldElement>, f
channel.send(x.to_bytes());
challenges_combination.push(channel.receive_random_field_element(field));

// let processor_AIR = processor_table.generate_air(challenges_extension, Terminal_processor[0], Terminal_processor[1], Terminal_processor[2], Terminal_processor[3]);
// let memory_AIR= memory_table.generate_air(challenges_extension, Terminal_memory[0]);
// let instruction_AIR = instruction_table.generate_air(challenges_extension, Terminal_instruction[0], Terminal_instruction[1]);
let eval = FieldElement::zero(field);

let processor_zerofier = processor_table.generate_zerofier();
let memory_zerofier= memory_table.generate_zerofier();
let instruction_zerofier = instruction_table.generate_zerofier();

let processor_quotients = processor_table.generate_quotients(challenges_extension.clone(), Terminal_processor[0], Terminal_processor[1], Terminal_processor[2], Terminal_processor[3]);
let memory_quotients = memory_table.generate_quotients(challenges_extension.clone(), Terminal_memory[0]);
let instruction_quotients = instruction_table.generate_quotients(challenges_extension, Terminal_instruction[0], Terminal_instruction[1]);

//for inter table arguments constraints
assert_eq!(Terminal_processor[0], Terminal_instruction[0]); //Tipa = Tppa
assert_eq!(Terminal_processor[1], Terminal_memory[0]); //Tmpa = Tppa
assert_eq!(Terminal_processor[2], Terminal_input[0]); //Tipa = Tea input
assert_eq!(Terminal_processor[3], Terminal_output[0]); //Tipa = Tea output
//let this be for now:- assert_eq!(Terminal_instruction[1], Tpea); //Tpea = program evaluation

//form combination polynomial
let combination= combination_polynomial(processor_quotients, memory_quotients, instruction_quotients, challenges_combination, instruction_table.table.height as usize, field);
let combination_codeword = domain.evaluate(combination.clone());

let merkle_combination = MerkleTree::new(&combination_codeword);
channel.send(merkle_combination.inner.root().unwrap().to_vec());

let (fri_polys, fri_domains, fri_layers, fri_merkles) = fri_commit(combination.clone(), domain, combination_codeword, merkle_combination, &mut channel);

let no_of_queries = 5;
decommit_fri(no_of_queries, expansion_f, 1<<64-1<<32+1, vec![&data1, &data2], vec![&merkle1, &merkle2], &fri_layers, &fri_merkles, &mut channel);

//print channel proof, proofsize, time taken for running prover, space taken etc etc.

}

Expand Down
178 changes: 97 additions & 81 deletions src/tables/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{derive_omicron, roundup_npow2, Table};
use crate::fields::{Field, FieldElement};
use crate::univariate_polynomial::*;
pub struct InstructionTable {
pub struct InstructionTable {
pub table: Table,
}

Expand Down Expand Up @@ -85,41 +85,35 @@ impl InstructionTable {
#[warn(unused_variables)]
pub fn extend_column(&mut self, rand_field_elem: u128, challenges: Vec<FieldElement>) ->Vec<FieldElement>{
let mut terminal:Vec<FieldElement>=Vec::new();
let mut ppa = self.table.matrix[0 as usize][Indices::Address as usize]
let field = self.table.field;
let mut ppa = FieldElement::one(field);
let mut pea = self.table.matrix[(0) as usize][Indices::Address as usize]
* challenges[ChallengeIndices::A as usize]
+ self.table.matrix[0 as usize][Indices::CurrentInstruction as usize]
* challenges[ChallengeIndices::B as usize]
+ self.table.matrix[0 as usize][Indices::NextInstruction as usize]
* challenges[ChallengeIndices::C as usize]
- challenges[ChallengeIndices::Alpha as usize];

let pea = self.table.matrix[0 as usize][Indices::Address as usize]
* challenges[ChallengeIndices::A as usize]
+ self.table.matrix[0 as usize][Indices::CurrentInstruction as usize]
+ self.table.matrix[(0) as usize][Indices::CurrentInstruction as usize]
* challenges[ChallengeIndices::B as usize]
+ self.table.matrix[0 as usize][Indices::NextInstruction as usize]
+ self.table.matrix[(0) as usize][Indices::NextInstruction as usize]
* challenges[ChallengeIndices::C as usize];

self.table.matrix[0_usize][Indices::PermutationArg as usize] = ppa;
self.table.matrix[0_usize][Indices::EvaluationArg as usize] = pea;
self.table.matrix[(0) as usize][Indices::PermutationArg as usize] = ppa;
self.table.matrix[(0) as usize][Indices::EvaluationArg as usize] = pea;

for i in 0..self.table.length - 1 {
let weighted_sum = self.table.matrix[(i + 1) as usize][Indices::Address as usize]
for i in 0..self.table.length-1 {
let weighted_sum = self.table.matrix[(i+1) as usize][Indices::Address as usize]
* challenges[ChallengeIndices::A as usize]
+ self.table.matrix[(i + 1) as usize][Indices::CurrentInstruction as usize]
+ self.table.matrix[(i+1) as usize][Indices::CurrentInstruction as usize]
* challenges[ChallengeIndices::B as usize]
+ self.table.matrix[(i + 1) as usize][Indices::NextInstruction as usize]
+ self.table.matrix[(i+1) as usize][Indices::NextInstruction as usize]
* challenges[ChallengeIndices::C as usize];
if self.table.matrix[(i + 1) as usize][Indices::Address as usize]
if self.table.matrix[(i+1) as usize][Indices::Address as usize]
== self.table.matrix[i as usize][Indices::Address as usize]
{
ppa *= weighted_sum - challenges[ChallengeIndices::Alpha as usize];
self.table.matrix[(i + 1) as usize][Indices::PermutationArg as usize] = ppa;
self.table.matrix[(i + 1) as usize][Indices::EvaluationArg as usize] = pea;
} else {
self.table.matrix[(i + 1) as usize][Indices::PermutationArg as usize] = ppa;
self.table.matrix[(i + 1) as usize][Indices::EvaluationArg as usize] =
pea * challenges[ChallengeIndices::Eta as usize] + weighted_sum;
pea = pea * challenges[ChallengeIndices::Eta as usize] + weighted_sum;
self.table.matrix[(i + 1) as usize][Indices::EvaluationArg as usize] = pea;
}

}
Expand Down Expand Up @@ -166,7 +160,12 @@ impl InstructionTable {
let boundaryair = ip.clone() + ppa.clone()
- Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)])
+ pea.clone()
- Polynomial::new_from_coefficients(vec![FieldElement::zero(self.table.field)]);
- Polynomial::constant((self.table.matrix[(0) as usize][Indices::Address as usize]
* challenges[ChallengeIndices::A as usize]
+ self.table.matrix[(0) as usize][Indices::CurrentInstruction as usize]
* challenges[ChallengeIndices::B as usize]
+ self.table.matrix[(0) as usize][Indices::NextInstruction as usize]
* challenges[ChallengeIndices::C as usize]));
//@todo check this once!! initial value is not zero and one, set it to req value
air.push(boundaryair);

Expand Down Expand Up @@ -277,9 +276,10 @@ mod test_instruction {
use crate::fields::Field;
use crate::fields::FieldElement;
use crate::vm::VirtualMachine;
use super::InstructionTable;
use crate::tables::Table;
use crate::tables::instruction::ChallengeIndices;
use crate::tables::processor::ProcessorTable;
use crate::tables::instruction::InstructionTable;
use crate::tables::io::IOTable;
use crate::tables::memory::MemoryTable;


#[test]
Expand Down Expand Up @@ -320,18 +320,19 @@ mod test_instruction {
FieldElement::zero(field)
);
}

#[test]
fn test_generate_air(){
let field = Field::new((1<<64)-(1<<32)+1);
fn test_air() {
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 = FieldElement::new(1753635133440165772, field);
// let omicron = generator.clone();
let generator = field.generator().pow((1<<32)-1);
let omicron = generator.clone();
let order = 1 << 32;
// let code = "++>+++++[<+>-]++++++++[<++++++>-]<.".to_string();
let code2 = ">>[++-]<".to_string();
let code2 = ">>[++-]+".to_string();
let program = vm.compile(code2);
println!("{:?}", program.clone());
let (rt, _, _) = vm.run(&program, "".to_string());
Expand All @@ -340,76 +341,91 @@ mod test_instruction {
let (processor_matrix, memory_matrix, instruction_matrix, input_matrix, output_matrix) =
vm.simulate(&program, "".to_string());
let challenges = vec![two; 11];
// let mut processor_table = ProcessorTable::new(
// field,
// processor_matrix.len() as u128,
// generator,
// order,
// processor_matrix,
// );
// let mut memory_table = MemoryTable::new(
// field,
// memory_matrix.len() as u128,
// generator,
// order,
// memory_matrix,
// );
let mut processor_table = ProcessorTable::new(
field,
processor_matrix.len() as u128,
generator,
order,
processor_matrix,
);
let mut memory_table = MemoryTable::new(
field,
memory_matrix.len() as u128,
generator,
order,
memory_matrix,
);
let mut instruction_table = InstructionTable::new(
field,
instruction_matrix.len() as u128,
generator,
order,
instruction_matrix,
);
// let mut input_table = IOTable::new(
// field,
// input_matrix.len() as u128,
// generator,
// order,
// input_matrix,
// );
// let mut output_table = IOTable::new(
// field,
// output_matrix.len() as u128,
// generator,
// order,
// output_matrix,
// );

// processor_table.pad();
// memory_table.pad();
let mut input_table = IOTable::new(
field,
input_matrix.len() as u128,
generator,
order,
input_matrix,
);
let mut output_table = IOTable::new(
field,
output_matrix.len() as u128,
generator,
order,
output_matrix,
);

processor_table.pad();
memory_table.pad();
instruction_table.pad();
// input_table.pad();
// output_table.pad();
let terminal = instruction_table.extend_column(1,challenges.clone());
input_table.pad();
output_table.pad();

// println!("instruction table before extending columns");
// for row in instruction_table.table.matrix.clone() {
// println!("{:?}", row);
// }
let terminal = instruction_table.extend_column(0, challenges.clone());
let terminal2 = processor_table.extend_columns(challenges.clone());
println!("instruction table after extending columns");
for row in instruction_table.table.matrix.clone() {
println!("{:?}", row);
}
println!("tppa: {:?}", terminal[0]);
let mut omicron_domain: Vec<FieldElement> = Vec::new();
println!("{:?}",instruction_table.table.length);
// println!("processor table after extending columns");
// for row in processor_table.table.matrix.clone() {
// println!("{:?}", row);
// }
println!("tppa: {:?}", terminal[0]);
println!("tpea: {:?}", terminal[1]);
println!("tipa: {:?}", terminal2[0]);

let mut omicron_domain: Vec<FieldElement> = Vec::new();
for i in 0..instruction_table.table.height {
omicron_domain.push(instruction_table.table.omicron.pow(i));
if i == 4 {
println!("omicron_domain: {:?}", omicron_domain);
}
}

let air = instruction_table.generate_air(challenges, terminal[0],terminal[1]);

let air = instruction_table.generate_air(challenges, terminal[0], terminal[1]);
// println!("air");
// for row in air.clone() {
// println!("{:?}", row);
// }

let b = air[0].evaluate(omicron_domain[0]);

assert_eq!(b, zero);
// for v in 0..rt-1 {
// let t_all = air[1].evaluate(omicron_domain[v as usize]);
// assert_eq!(t_all, zero);
// }

// let v =omicron_domain[4];

// let t_air1 = air[2].evaluate(v);

// assert_eq!(t_air1, zero);
}
for v in 0..instruction_table.table.length-1 {
let transition = air[1].evaluate(omicron_domain[v as usize]);
//println!("{:?}: {}", transition, v);
assert_eq!(transition, zero);
}

let terminal = air[2].evaluate(omicron_domain[(instruction_table.table.length-1) as usize]);
assert_eq!(terminal, zero);

}

}
7 changes: 4 additions & 3 deletions src/tables/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,14 @@ mod test_memory_table {
input_table.pad();
output_table.pad();
let terminal = memory_table.extend_column_ppa(1,challenges.clone());
let terminal2 = processor_table.extend_columns(challenges.clone());
println!("memory_table after extending columns");
for row in memory_table.table.matrix.clone() {
println!("{:?}", row);
}
println!("tppa: {:?}", terminal[0]);
let mut omicron_domain: Vec<FieldElement> = Vec::new();
println!("{:?}",memory_table.table.length);
println!("tppa: {:?}", terminal[0]);
println!("tmpa: {:?}", terminal2[1]);
let mut omicron_domain: Vec<FieldElement> = Vec::new();
for i in 0..memory_table.table.height {
omicron_domain.push(memory_table.table.omicron.pow(i));
if i == 4 {
Expand Down
Loading

0 comments on commit 2fc9aee

Please sign in to comment.