Skip to content

Commit 5c58571

Browse files
committed
added_comments
1 parent da2bdb9 commit 5c58571

3 files changed

Lines changed: 94 additions & 44 deletions

File tree

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code, unused_imports)]
22
struct ConsoleLogger;
33
use chrono::Local;
4-
use log::{Level,Metadata, Record};
4+
use log::{Level, Metadata, Record};
55
use pprof::protos::Message;
66
use pprof::ProfilerGuard;
77
use std::fs::File;

src/stark/mod.rs

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ pub enum ChallengeIndices {
4646
}
4747

4848
// prove:
49-
// matrices -> processor, memory, instruction, input, output -> in this order
49+
//give matrices created by vm in this order-> processor, memory, instruction, input, output
50+
//1. create table from the func given in their respective module.
5051

5152
pub fn prove(
5253
matrices: &[&[Vec<FieldElement>]],
@@ -106,8 +107,12 @@ pub fn prove(
106107
order,
107108
matrices[4],
108109
);
109-
log::info!("Generating tables took: {:?}ms", (Local::now() - t).num_milliseconds());
110+
log::info!(
111+
"Generating tables took: {:?}ms",
112+
(Local::now() - t).num_milliseconds()
113+
);
110114
t = Local::now();
115+
//2.pad all tables
111116
processor_table.pad();
112117
memory_table.pad();
113118
instruction_table.pad();
@@ -121,7 +126,7 @@ pub fn prove(
121126
"Padding all tables took: {:?}ms",
122127
(Local::now() - t).num_milliseconds()
123128
);
124-
129+
//3.interpolate all tables
125130
t = Local::now();
126131
let processor_interpol_columns: Vec<Polynomial> = processor_table
127132
.table
@@ -138,10 +143,10 @@ pub fn prove(
138143
.clone()
139144
.interpolate_columns(vec![0, 1, 2]);
140145

141-
log::info!(
142-
"Interpolating memory table took: {:?}ms",
143-
(Local::now() - t).num_milliseconds()
144-
);
146+
log::info!(
147+
"Interpolating memory table took: {:?}ms",
148+
(Local::now() - t).num_milliseconds()
149+
);
145150
t = Local::now();
146151
let instruction_interpol_columns: Vec<Polynomial> = instruction_table
147152
.table
@@ -181,6 +186,7 @@ pub fn prove(
181186
);
182187

183188
// @todo make these functions rust native, by using iter.
189+
//4.Evaluating all tables on the extended domain
184190
for i in 0..processor_interpol_columns.clone().len() {
185191
basecodewords.push(domain.evaluate(processor_interpol_columns[i].clone()));
186192
}
@@ -199,7 +205,7 @@ pub fn prove(
199205
);
200206
t = Local::now();
201207

202-
// zipping all the base codewords (for each index in order) by taking their merkle root
208+
// 5.zipping all the base codewords (for each index in order) by taking their merkle root
203209
let mut basecodeword: Vec<FieldElement> = Vec::with_capacity(expanded_length as usize);
204210

205211
for i in 0..expanded_length as usize {
@@ -224,23 +230,27 @@ pub fn prove(
224230
);
225231
t = Local::now();
226232

227-
// get 11 challenges array from fiat shamir
233+
//6.commitng the base codewords
228234
let mut channel = Channel::new();
229235
let merkle1 = MerkleTree::new(&basecodeword);
236+
//Sending merkle root to channel i.e. verifier
237+
// channel acts as verifier, simulates verifier, in proof generation of non-interactive proving setup. and vice versa for proof verification
238+
// to convert interactive proving to non interactive proving, fiat shamir heuristic is used.
239+
// with fiat shamir heuristic, the channel is deterministic for both prover and verifier.
230240
channel.send(merkle1.inner.root().unwrap().to_vec());
231241
log::info!(
232242
"Commiting the base codewords took: {:?}ms",
233243
(Local::now() - t).num_milliseconds()
234244
);
235245

236246
let mut challenges_extension = vec![];
237-
247+
//7.receiving the challenges from the channel i.e. verifier to compute extension columns
238248
for _ in 0..=10 {
239249
let x = channel.receive_random_field_element(field);
240250
challenges_extension.push(x);
241251
}
242252

243-
// use extend column function on tables -> extends the base columns to extension columns
253+
//8.use extend column function on tables to extend the base columns to extension columns
244254
t = Local::now();
245255
let terminal_processor = processor_table.extend_columns(challenges_extension.clone());
246256
let terminal_memory = memory_table.extend_column_ppa(1, challenges_extension.clone());
@@ -257,7 +267,7 @@ pub fn prove(
257267
);
258268
t = Local::now();
259269

260-
//These contain polynomials for interpolation of extension columns
270+
//9. interpolate the extension columns
261271
let processor_interpol_columns_2 = processor_table
262272
.table
263273
.clone()
@@ -284,6 +294,7 @@ pub fn prove(
284294
// memory: ppa
285295
// instruction: ppa, pea
286296
// input and output tables are public, we dont commit to those, we only check their terminal extensions after extending
297+
//10.Evaluating all extended columns on the extended domain
287298
for i in 0..processor_interpol_columns_2.clone().len() {
288299
extension_codewords.push(domain.evaluate(processor_interpol_columns_2[i].clone()));
289300
}
@@ -301,7 +312,7 @@ pub fn prove(
301312
(Local::now() - t).num_milliseconds()
302313
);
303314
t = Local::now();
304-
315+
//11. zipping all the extension codewords
305316
let mut extension_codeword: Vec<FieldElement> = Vec::with_capacity(expanded_length as usize);
306317
for i in 0..expanded_length as usize {
307318
let mut x: Vec<FieldElement> = vec![];
@@ -324,16 +335,17 @@ pub fn prove(
324335
(Local::now() - t).num_milliseconds()
325336
);
326337
t = Local::now();
327-
338+
//12.commiting the extension codewords
328339
let merkle2 = MerkleTree::new(&extension_codeword);
340+
//Sending merkle root to channel i.e. verifier
329341
channel.send(merkle2.inner.root().unwrap().to_vec());
330342

331343
log::info!(
332344
"Commiting the extension codewords took: {:?}ms",
333345
(Local::now() - t).num_milliseconds()
334346
);
335347
t = Local::now();
336-
348+
//13.receveing challenges from channel (verifier) for computing the combination polynomial
337349
let mut challenges_combination = vec![];
338350
let x = channel.receive_random_field_element(field);
339351
challenges_combination.push(x);
@@ -346,7 +358,7 @@ pub fn prove(
346358
(Local::now() - t).num_milliseconds()
347359
);
348360
t = Local::now();
349-
361+
//14. generating all the AIRs for different tables
350362
let processor_air = processor_table.generate_air(
351363
challenges_extension.clone(),
352364
terminal_processor[0],
@@ -379,9 +391,7 @@ pub fn prove(
379391
(Local::now() - t).num_milliseconds()
380392
);
381393
t = Local::now();
382-
383-
// Generate zerofiers
384-
394+
//15. generating the zerofier for boundary,transition and terminal constraints for each table
385395
let processor_zerofiers = processor_table.generate_zerofier();
386396
log::info!(
387397
"generating the processor zerofiers took: {:?}ms",
@@ -403,6 +413,7 @@ pub fn prove(
403413
);
404414
t = Local::now();
405415

416+
//16.generating the quotient polynomial for each table
406417
let zero = FieldElement::zero(field);
407418
let processor_q = (0..processor_zerofiers.len())
408419
.into_par_iter()
@@ -441,8 +452,8 @@ pub fn prove(
441452
);
442453
t = Local::now();
443454

444-
// form combination polynomial
445-
// 9 is the maximum factor in AIR degree
455+
// 17. generating the combination polynomial from quotient polynomials of diff tables
456+
// the maximum degree bound will be 9*height
446457
let degree_bound = roundup_npow2(9 * (instruction_table.table.height - 1)) - 1;
447458

448459
//@todo optimize this
@@ -458,20 +469,58 @@ pub fn prove(
458469
"generating the combination polynomial took: {:?}ms",
459470
(Local::now() - t).num_milliseconds()
460471
);
472+
//18. evaluating the combination polynomial on the extended domain
461473
t = Local::now();
462474
let combination_codeword = domain.evaluate(combination.clone());
463475
log::info!(
464476
"evaluating the combination polynomial took: {:?}ms",
465477
(Local::now() - t).num_milliseconds()
466478
);
467479
t = Local::now();
480+
//19. commiting the combnation codewords
468481
let merkle_combination = MerkleTree::new(&combination_codeword);
469482
channel.send(merkle_combination.inner.root().unwrap().to_vec());
470483
log::info!(
471484
"commiting the combination codewords took: {:?}ms",
472485
(Local::now() - t).num_milliseconds()
473486
);
474487
t = Local::now();
488+
// Now we have commited to the combination polynomial.
489+
// Composition polynomial will be a polynomial only if all the contraints are satisfied.
490+
// Using F.R.I, we will show composition polynomial is close to a polynomial of low degree.
491+
// A function is close to a polynomial if its distance to the polynomial is small.
492+
// distance of a function and polynomial is measured as, f:Domain->F, Distance(f, p) => for every d in Domain, f(d) != p(d)
493+
// How can we prove that a composition polynomial is close to polynomial of low degree?
494+
// Here comes F.R.I, Fast Reed solomon Interactive oracle proofs of proximity.
495+
// FRI is a folding scheme, similar to what we see in FFT for breaking down the polynomial into 2 polynomials of even degree(s).
496+
// Prover tries to convence the verifier that, the commitment of composition polynomial is close to a low degree polynomial.
497+
// F.R.I Protocol:
498+
// 1. Receive random element `beta` from verifier.
499+
// 2. Apply the FRI folding scheme or FRI operator to the composition polynomial.
500+
// 3. Commit to the new polynomial obtained after applying FRI operator.
501+
// 4. Send the new commitment to the verifier.
502+
// 5. Repeat step 1-4, until the polynomial degree is less than accepted degree in terms of security. in this case repeat till degree is 0.
503+
// 6. Prover sends the result to the verifier.
504+
505+
// F.R.I Operator or folding scheme:
506+
// from proving: function is close to a polynomial of degree < D
507+
// to proving: new function is close to a new polynomial of degree < D/2, where new function has half the domain size of old polynomial.
508+
// Example: To prove: A function is close to a polynomial of a degree < 1024, with function domain size = 8192
509+
// After applying the FRI operator we need to prove the new polynomial degree < 512 with new function domain size = 4096
510+
// split ot even and odd powers.
511+
// P_0(x) = g(x^2) + x h(x^2)
512+
// Get random element beta from verifier
513+
// P_1(y) = g(y) + beta * h(y)
514+
515+
// For this example, repeat steps 1-4 till degree of polynomial < 1, when domain size is 8.
516+
517+
// The new evaluation domain, will be half of the old evaluation domain.
518+
// and new evaluation domain is first half of the old evaluation domain squared.
519+
// eval domain: w, w.h, w.h2, .... w.h^8091
520+
// new eval domain: w^2, (w.h)^2, ... (w.h^4095)^2
521+
// square of the first half of the old eval domian, is equal to square of second half of old eval domain. This is a cyclic group property.
522+
523+
//20.generate fri layers and commit to the fri layers.
475524
let (_fri_polys, fri_domains, fri_layers, fri_merkles) = fri_commit(
476525
combination.clone(),
477526
domain,
@@ -484,6 +533,14 @@ pub fn prove(
484533
(Local::now() - t).num_milliseconds()
485534
);
486535
t = Local::now();
536+
537+
// Proof or provers work contains generating commitments and decommiting them, to convence the verifier over the integrity of the computation.
538+
// i) Commitment ✅
539+
// ii) Decommitment -> query phase
540+
// Decommitment involves verifier sending random elements from evaluation domain to prover. and prover responding with decommitments to the evaluations, which involve sending merkle paths along with evaluations.
541+
542+
// with each successful query and valid decommitment, verifiers confidence in the proof increases.
543+
487544
let no_of_queries = num_queries;
488545
decommit_fri(
489546
no_of_queries,
@@ -524,22 +581,17 @@ pub fn prove(
524581
)
525582
}
526583

527-
// verifier knows -
528-
// constraints (therefore AIR)
529-
// combination polynomial equation
530-
// challenges of extension columns
531-
// challenges of combination polynomial
532-
533584
// prover sends to verifier -
534-
// height (whose correctness is indirectly verified through fri and degree bound)
535-
// base codewords merkle root, extension codewords merkle root
536-
// for each query (index) of verifier, prover sends respective evaluation and merkle authentication path of evaluation
537-
// written in fri decommit_on_query
585+
//-> height (whose correctness is indirectly verified through fri and degree bound)
586+
//-> base codewords merkle root, extension codewords merkle root
587+
// ->for each query (index) of verifier, prover sends respective evaluation and merkle authentication path of evaluation
588+
589+
//panic for invalid proof
538590

539591
pub fn verify_proof(
540592
num_of_queries: usize,
541593
maximum_random_int: u64,
542-
blow_up_factor: usize, //expansion_factor
594+
blow_up_factor: usize,
543595
field: Field,
544596
fri_domains: &[Vec<FieldElement>],
545597
compressed_proof: &[Vec<u8>],
@@ -553,7 +605,7 @@ pub fn verify_proof(
553605
log::info!("verifying proof");
554606
let start = Local::now();
555607
let mut channel = Channel::new();
556-
// merkle root of the zipped base codewords
608+
//merkle root of the zipped base codewords
557609
let base_merkle_root = compressed_proof[0].clone();
558610

559611
channel.send(base_merkle_root.clone());
@@ -566,11 +618,12 @@ pub fn verify_proof(
566618
challenges_extension.push(x);
567619
}
568620
challenges_extension.push(channel.receive_random_field_element(field));
569-
621+
// merkle root of the zipped exten codewords
570622
let exten_merkle_root = compressed_proof[1].clone();
571623

572624
channel.send(exten_merkle_root.clone());
573625

626+
// get challenges for the combination polynomial
574627
let mut challenges_combination = vec![];
575628
let x = channel.receive_random_field_element(field);
576629
challenges_combination.push(x);
@@ -582,9 +635,6 @@ pub fn verify_proof(
582635
channel.send(combination_merkle_root);
583636

584637
//commit to fri
585-
586-
// height should be power of 2
587-
// for fri_layer degree of the combination polynomial should be less then the height and the domain size will be the height*expansion_fact
588638
// fri.layer.len = 1+ log(height)/log2
589639

590640
let number = degree_bound + 1_usize;
@@ -604,7 +654,7 @@ pub fn verify_proof(
604654
// last term of the constant polynomial
605655

606656
channel.send(last_layer_free_term.clone());
607-
// base_idx will be the point where the end of the compressed_proof indices for thr fri-layer_root commitment after this we have added the element and there authentication path
657+
// base_idx will be the point where the end of the compressed_proof indices for thr fri-layer_root commitment after this we have added the element and there authentication path
608658
let mut base_idx = 3_usize + fri_layer_length;
609659
for i in 0..num_of_queries {
610660
let idx = channel.receive_random_int(0, maximum_random_int, true) as usize;
@@ -709,16 +759,16 @@ pub fn verify_queries(
709759
));
710760
//for inter table arguments constraints
711761
//Tipa = Tppa
712-
assert_eq!(terminal_processor[0], terminal_instruction[0]);
762+
assert_eq!(terminal_processor[0], terminal_instruction[0]);
713763
//Tmpa = Tppa
714-
assert_eq!(terminal_processor[1], terminal_memory[0]);
764+
assert_eq!(terminal_processor[1], terminal_memory[0]);
715765
//Tiea = Tea input
716766
if !terminal_input.is_empty() {
717-
assert_eq!(terminal_processor[2], terminal_input[0]);
767+
assert_eq!(terminal_processor[2], terminal_input[0]);
718768
}
719769
//Toea = Tea output
720770
if !terminal_output.is_empty() {
721-
assert_eq!(terminal_processor[3], terminal_output[0]);
771+
assert_eq!(terminal_processor[3], terminal_output[0]);
722772
}
723773

724774
verify_fri_layers(

src/tables/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl IOTable {
4444
challenge: FieldElement,
4545
) -> Vec<FieldElement> {
4646
// take rand_field_elem as zero if no random secret implementation
47-
let mut ea = FieldElement::new(rand_field_elem, self.table.field);
47+
let mut ea = FieldElement::new(rand_field_elem, self.table.field);
4848
let mut terminal: Vec<FieldElement> = Vec::with_capacity(self.table.length as usize);
4949
if !self.table.matrix.is_empty() {
5050
self.table.matrix[0][1] = ea;

0 commit comments

Comments
 (0)