Skip to content

Commit

Permalink
prover
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyathakur44 committed Dec 14, 2024
1 parent 1d28879 commit 4980b5c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 60 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
24 changes: 23 additions & 1 deletion src/stark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,31 @@ pub fn prove(matrices: Vec<Vec<Vec<FieldElement>>>, inputs: Vec<FieldElement>, f

let eval = FieldElement::zero(field);


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 =
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
66 changes: 33 additions & 33 deletions src/tables/instruction.rs
Original file line number Diff line number Diff line change
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 @@ -333,15 +332,15 @@ mod test_instruction {
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());
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 @@ -394,10 +393,10 @@ mod test_instruction {
for row in instruction_table.table.matrix.clone() {
println!("{:?}", row);
}
println!("processor table after extending columns");
for row in processor_table.table.matrix.clone() {
println!("{:?}", row);
}
// 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]);
Expand All @@ -418,13 +417,14 @@ mod test_instruction {
let b = air[0].evaluate(omicron_domain[0]);
assert_eq!(b, zero);

// for v in 0..rt-1 {
// let transition = air[1].evaluate(omicron_domain[v as usize]);
// // println!("{:?}", t_all);
// assert_eq!(transition, 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]);
let terminal = air[2].evaluate(omicron_domain[(instruction_table.table.length-1) as usize]);
assert_eq!(terminal, zero);

}

Expand Down
6 changes: 4 additions & 2 deletions src/tables/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +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!("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
17 changes: 9 additions & 8 deletions src/tables/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ impl ProcessorTable {
ipa *= weighted_sum;
self.table.matrix[(i + 1) as usize][Indices::InstructionPermutaion as usize] = ipa;
}
let tipa=ipa*(self.table.matrix[(self.table.length-1) as usize][Indices::InstructionPointer as usize]
* challenges[ChallengeIndices::A as usize]
+ self.table.matrix[(self.table.length-1) as usize][Indices::CurrentInstruction as usize]
* challenges[ChallengeIndices::B as usize]
+ self.table.matrix[(self.table.length-1) as usize][Indices::NextInstruction as usize]
* challenges[ChallengeIndices::C as usize]
- challenges[ChallengeIndices::Alpha as usize]);
// let tipa=ipa*(self.table.matrix[(self.table.length-1) as usize][Indices::InstructionPointer as usize]
// * challenges[ChallengeIndices::A as usize]
// + self.table.matrix[(self.table.length-1) as usize][Indices::CurrentInstruction as usize]
// * challenges[ChallengeIndices::B as usize]
// + self.table.matrix[(self.table.length-1) as usize][Indices::NextInstruction as usize]
// * challenges[ChallengeIndices::C as usize]
// - challenges[ChallengeIndices::Alpha as usize]);
let tipa = ipa;

for i in 0..self.table.length - 1 {
let weighted_sum = self.table.matrix[i as usize][Indices::Cycle as usize]
Expand All @@ -173,7 +174,7 @@ impl ProcessorTable {
self.table.matrix[(i + 1) as usize][Indices::MemoryPermuation as usize] = mpa;
}

let tmpa=mpa*(self.table.matrix[(self.table.length-1) as usize][Indices::Cycle as usize]
let tmpa=mpa*(self.table.matrix[(self.table.length-1) as usize][Indices::Cycle as usize]
* challenges[ChallengeIndices::D as usize]
+ self.table.matrix[(self.table.length-1) as usize][Indices::MemoryPointer as usize]
* challenges[ChallengeIndices::E as usize]
Expand Down
1 change: 0 additions & 1 deletion src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ impl VirtualMachine {
stack.pop();
}
}

program
}

Expand Down

0 comments on commit 4980b5c

Please sign in to comment.