From 370c9dab6951ccf86c3a3d6b003fd48c32aba8fa Mon Sep 17 00:00:00 2001 From: soumyathakur44 Date: Tue, 17 Dec 2024 00:20:22 +0530 Subject: [PATCH] prover run --- src/fri/mod.rs | 11 ++- src/stark/mod.rs | 147 ++++++++++++++++++++++--------- src/tables/instruction.rs | 2 +- src/tables/memory.rs | 2 +- src/tables/processor.rs | 18 ++-- src/univariate_polynomial/mod.rs | 6 ++ 6 files changed, 133 insertions(+), 53 deletions(-) diff --git a/src/fri/mod.rs b/src/fri/mod.rs index 113ed2f..d995975 100644 --- a/src/fri/mod.rs +++ b/src/fri/mod.rs @@ -30,27 +30,36 @@ pub fn combination_polynomial( //@todo we can also pass a single vector of all quotient for i in 0..processor_q.clone().len() { - let d = degree - processor_q[i].clone().degree(); + if processor_q[i].degree()>>, inputs: String, field: Field let generator = field.generator().pow((1 << 32) - 1); let order = 1 << 32; log::info!("Generating tables"); + println!("Generating tables..."); let mut processor_table = ProcessorTable::new( field, @@ -119,13 +120,15 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field order, matrices[4].clone(), ); - log::info!("padding all tables"); + log::info!("Padding all tables"); + println!("Padding all tables..."); processor_table.pad(); memory_table.pad(); instruction_table.pad(); input_table.pad(); output_table.pad(); log::info!("Interpolating all tables"); + println!("Interpolating all tables..."); let processor_interpol_columns = processor_table .table @@ -140,10 +143,11 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field .clone() .interpolate_columns(vec![0, 1, 2]); - let initial_length = instruction_table.table.clone().height; + let initial_length = roundup_npow2(9*instruction_table.table.clone().height); //all codewords are evaluated on this expanded domain that has length expanded_length let expanded_length = initial_length * (expansion_f as u128); log::info!("Extending the domain"); + println!("Extending the domain..."); let domain = FriDomain::new( offset, @@ -158,7 +162,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field // memory: clk, mp, mv // instruction: ip, ci, ni // input and output tables are public, we dont commit to those, we only check their terminal extensions after extending - log::info!("evaluating on the extended domain"); + log::info!("Evaluating on the extended domain"); + println!("Evaluating on the extended domain..."); for i in 0..processor_interpol_columns.clone().len() { basecodewords.push(domain.evaluate(processor_interpol_columns[i].clone())); @@ -175,7 +180,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field //we are zipping all the base codewords (for each index in order) using concatenation let mut basecodeword: Vec = Vec::new(); - log::info!("zipping all the codewords on the extended domain"); + log::info!("Zipping all the codewords on the extended domain"); + println!("Zipping all the codewords on the extended domain..."); for i in 0..expanded_length as usize { let mut x: Vec = vec![]; @@ -212,7 +218,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field // get 11 challenges array from fiat shamir let mut channel = Channel::new(); - log::info!("commiting the base codewords"); + log::info!("Commiting the base codewords"); + println!("Commiting the base codewords..."); let merkle1 = MerkleTree::new(&basecodeword); channel.send(merkle1.inner.root().unwrap().to_vec()); @@ -226,7 +233,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field challenges_extension.push(channel.receive_random_field_element(field)); // use extend column function on tables -> extends the base columns to extension columns - log::info!("generating the extension coloumn using the fiat-shamir challenges"); + log::info!("Generating the extension column using the fiat-shamir challenges"); + println!("Generating the extension column using the fiat-shamir challenges..."); let Terminal_processor = processor_table.extend_columns(challenges_extension.clone()); let Terminal_memory = memory_table.extend_column_ppa(1, challenges_extension.clone()); let Terminal_instruction = instruction_table.extend_column(1, challenges_extension.clone()); @@ -238,7 +246,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field .clone(); //These contain polynomials for interpolation of extension columns - log::info!("interpolating the extension columns"); + log::info!("Interpolating the extension columns"); + println!("Interpolating the extension columns..."); let processor_interpol_columns_2 = processor_table .table .clone() @@ -256,7 +265,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field // memory: ppa // instruction: ppa, pea // input and output tables are public, we dont commit to those, we only check their terminal extensions after extending - log::info!("evaluating the extendion columns on the extended domain"); + log::info!("Evaluating the extension columns on the extended domain"); + println!("Evaluating the extension columns on the extended domain..."); for i in 0..processor_interpol_columns_2.clone().len() { extension_codewords.push(domain.evaluate(processor_interpol_columns_2[i].clone())); @@ -271,7 +281,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field } let mut extension_codeword: Vec = Vec::new(); - log::info!("zipping all the extension codewords"); + log::info!("Zipping all the extension codewords"); + println!("Zipping all the extension codewords..."); for i in 0..expanded_length as usize { let mut x: Vec = vec![]; @@ -301,54 +312,104 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field // data2.push(FieldElement::from_bytes(array)); // } - log::info!("commiting the extension codewords"); + log::info!("Commiting the extension codewords"); + println!("Commiting the extension codewords..."); let merkle2 = MerkleTree::new(&extension_codeword); channel.send(merkle2.inner.root().unwrap().to_vec()); let mut challenges_combination = vec![]; let x = channel.receive_random_field_element(field); challenges_combination.push(x); + // channel.send(x.to_bytes()); challenges_combination.push(channel.receive_random_field_element(field)); - 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 = instruction_table.generate_quotients( - challenges_extension, - Terminal_instruction[0], - Terminal_instruction[1], - ); + // let processor_quotients = processor_table.generate_quotients( + // challenges_extension.clone(), + // Terminal_processor[0], + // Terminal_processor[1], + // Terminal_processor[2], + // Terminal_processor[3], + // ); + let processor_air = processor_table.generate_air(challenges_extension.clone(), Terminal_processor[0], Terminal_processor[1], Terminal_processor[2], Terminal_processor[3], eval); + println!("\nproc air degree: "); + for i in 0..processor_air.len(){ + print!("{} ", processor_air[i].degree()); + } - //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 + // let memory_quotients = + // memory_table.generate_quotients(challenges_extension.clone(), Terminal_memory[0]); + + let memory_air = memory_table.generate_air(challenges_extension.clone(), Terminal_memory[0]); + println!("\nmemory air degree: "); + for i in 0..memory_air.len(){ + print!("{} ", memory_air[i].degree()); + } + + // let instruction_quotients = instruction_table.generate_quotients( + // challenges_extension, + // Terminal_instruction[0], + // Terminal_instruction[1], + // ); + + let instruction_air = instruction_table.generate_air(challenges_extension.clone(), Terminal_instruction[0], Terminal_instruction[1]); + println!("\ninstruction air degree: "); + for i in 0..instruction_air.len(){ + print!("{} ", instruction_air[i].degree()); + } + + // form zerofiers + let processor_zerofiers = processor_table.generate_zerofier(); + println!("\nproc zerofier degree: "); + for i in 0..processor_zerofiers.len(){ + print!("{} ", processor_zerofiers[i].degree()); + } + let memory_zerofiers = memory_table.generate_zerofier(); + println!("\nmemory zerofier degree: "); + for i in 0..memory_zerofiers.len(){ + print!("{} ", memory_zerofiers[i].degree()); + } + let instruction_zerofiers = instruction_table.generate_zerofier(); + println!("\ninstruction zerofier degree: "); + for i in 0..instruction_zerofiers.len(){ + print!("{} ", instruction_zerofiers[i].degree()); + } + + let mut processor_q = vec![]; + println!("\nproc q degree: "); + for i in 0..processor_zerofiers.len(){ + processor_q.push((processor_air[i].clone().q_div(processor_zerofiers[i].clone())).0); + print!("{} ", processor_q[i].degree()); + } + let mut memory_q = vec![]; + println!("\nmemory q degree: "); + for i in 0..memory_zerofiers.len(){ + memory_q.push((memory_air[i].clone().q_div(memory_zerofiers[i].clone())).0); + print!("{} ", memory_q[i].degree()); + } + let mut instruction_q = vec![]; + println!("\ninstruction q degree: "); + for i in 0..instruction_zerofiers.len(){ + instruction_q.push((instruction_air[i].clone().q_div(instruction_zerofiers[i].clone())).0); + print!("{} ", instruction_q[i].degree()); + } - //form combination polynomial + println!("\ninstruc table height: {}", instruction_table.table.height); + // form combination polynomial + // 9 is the maximum factor in AIR degree + let degree_bound = roundup_npow2(9*instruction_table.table.height)-1; let combination = combination_polynomial( - processor_quotients, - memory_quotients, - instruction_quotients, + processor_q, + memory_q, + instruction_q, challenges_combination, - instruction_table.table.height as usize, + degree_bound 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, @@ -368,6 +429,7 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field &fri_merkles, &mut channel, ); + println!("decommit done"); for i in 0..channel.compressed_proof.len(){ for j in 0..channel.compressed_proof[0].len(){ @@ -376,8 +438,9 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field println!("{}: " ,i); } let x = channel.compressed_proof; + println!("compressed proof printed above!"); x - + //print channel proof, proofsize, time taken for running prover, space taken etc etc. } @@ -410,6 +473,8 @@ pub fn prove(matrices: Vec>>, inputs: String, field: Field //pub fn verify proof{collect all betas to check for fri layer} +//@todo Terminal values compare + pub fn verify_proof( num_of_queries: usize, maximum_random_int: u64, @@ -669,12 +734,12 @@ mod stark_test { fn test_proving() { let field = Field(18446744069414584321); let vm = VirtualMachine::new(field); - let code = "++++".to_string(); + let code = "++>+-[+--].".to_string(); let program = vm.compile(code); let (running_time, input_symbols, output_symbols) = vm.run(&program, "".to_string()); let (processor_matrix, memory_matrix, instruction_matrix, input_matrix, output_matrix) = vm.simulate(&program, "".to_string()); - //assert_eq!(running_time as usize, processor_matrix.len()); + assert_eq!(running_time as usize, processor_matrix.len()); let offset = FieldElement::one(field); let expansion_f =1; diff --git a/src/tables/instruction.rs b/src/tables/instruction.rs index f862193..9eaf569 100644 --- a/src/tables/instruction.rs +++ b/src/tables/instruction.rs @@ -254,7 +254,7 @@ impl InstructionTable { x.clone() - Polynomial::new_from_coefficients(vec![omicron.clone().pow(0)]); zerofiers.push(boundary_zerofier); - let mut transition_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { transition_zerofier *= x.clone() - Polynomial::new_from_coefficients(vec![omicron.clone().pow(i)]); diff --git a/src/tables/memory.rs b/src/tables/memory.rs index 9cb6f4e..18924d4 100644 --- a/src/tables/memory.rs +++ b/src/tables/memory.rs @@ -228,7 +228,7 @@ impl MemoryTable { x.clone() - Polynomial::new_from_coefficients(vec![omicron.clone().pow(0)]); zerofiers.push(boundary_zerofier); - let mut transition_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { transition_zerofier *= x.clone() - Polynomial::new_from_coefficients(vec![omicron.clone().pow(i)]); diff --git a/src/tables/processor.rs b/src/tables/processor.rs index 1df4248..b64e222 100644 --- a/src/tables/processor.rs +++ b/src/tables/processor.rs @@ -235,7 +235,7 @@ impl ProcessorTable { zerofiers.push(boundary_zerofier); //i0 - let mut transition_i0_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i0_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f('[') == ci { @@ -246,7 +246,7 @@ impl ProcessorTable { zerofiers.push(transition_i0_zerofier); //i1 - let mut transition_i1_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i1_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f(']') == ci { @@ -257,7 +257,7 @@ impl ProcessorTable { zerofiers.push(transition_i1_zerofier); //i2 - let mut transition_i2_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i2_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f('<') == ci { @@ -268,7 +268,7 @@ impl ProcessorTable { zerofiers.push(transition_i2_zerofier); //i3 - let mut transition_i3_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i3_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f('>') == ci { @@ -279,7 +279,7 @@ impl ProcessorTable { zerofiers.push(transition_i3_zerofier); //i4 - let mut transition_i4_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i4_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f('+') == ci { @@ -290,7 +290,7 @@ impl ProcessorTable { zerofiers.push(transition_i4_zerofier); //i5 - let mut transition_i5_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i5_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f('-') == ci { @@ -301,7 +301,7 @@ impl ProcessorTable { zerofiers.push(transition_i5_zerofier); //i6 - let mut transition_i6_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i6_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f(',') == ci { @@ -312,7 +312,7 @@ impl ProcessorTable { zerofiers.push(transition_i6_zerofier); //i7 - let mut transition_i7_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_i7_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { let ci = self.table.matrix[i as usize][Indices::CurrentInstruction as usize]; if f('.') == ci { @@ -323,7 +323,7 @@ impl ProcessorTable { zerofiers.push(transition_i7_zerofier); //all - let mut transition_all_zerofier = Polynomial::new_from_coefficients(vec![]); + let mut transition_all_zerofier = Polynomial::new_from_coefficients(vec![FieldElement::one(self.table.field)]); for i in 0..self.table.length - 1 { transition_all_zerofier *= x.clone() - Polynomial::new_from_coefficients(vec![omicron.clone().pow(i)]); diff --git a/src/univariate_polynomial/mod.rs b/src/univariate_polynomial/mod.rs index cbe14c9..8a28b6b 100644 --- a/src/univariate_polynomial/mod.rs +++ b/src/univariate_polynomial/mod.rs @@ -90,6 +90,12 @@ impl Polynomial { let field = Field::new(self.coefficients[0].modulus()); let n = self.coefficients.len(); let m = poly2.coefficients.len(); + if( m==0){ + return (self, Polynomial::zero(field)) + } + if( n==0){ + return (self, Polynomial::zero(field)) + } if n < m { return ( Polynomial::new_from_coefficients(vec![FieldElement::new(0, field)]),