-
Notifications
You must be signed in to change notification settings - Fork 178
/
mod.rs
656 lines (563 loc) · 22.5 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//! Contains STARK proof struct and associated components.
use alloc::vec::Vec;
use core::cmp;
use crypto::{Hasher, MerkleTree};
use fri::FriProof;
use math::FieldElement;
use utils::{ByteReader, Deserializable, DeserializationError, Serializable, SliceReader};
use crate::{ProofOptions, TraceInfo};
mod context;
pub use context::Context;
mod commitments;
pub use commitments::Commitments;
mod queries;
pub use queries::Queries;
mod ood_frame;
pub use ood_frame::{OodFrame, TraceOodFrame};
mod table;
pub use table::Table;
#[cfg(test)]
mod tests;
// CONSTANTS
// ================================================================================================
const GRINDING_CONTRIBUTION_FLOOR: u32 = 80;
const MAX_PROXIMITY_PARAMETER: u64 = 1000;
// PROOF
// ================================================================================================
/// A proof generated by Winterfell prover.
///
/// A STARK proof contains information proving that a computation was executed correctly. A proof
/// also contains basic metadata for the computation, but neither the definition of the computation
/// itself, nor public inputs consumed by the computation are contained in a proof.
///
/// Optionally, it may contain a GKR proof. The GKR proof object gives the possibility to prove some
/// auxiliary trace constraints using GKR, as described in [Improving logarithmic derivative lookups
/// using GKR](https://eprint.iacr.org/2023/1284.pdf).
///
/// A proof can be serialized into a sequence of bytes using [to_bytes()](Proof::to_bytes) function,
/// and deserialized from a sequence of bytes using [from_bytes()](Proof::from_bytes) function.
///
/// To estimate soundness of a proof (in bits), [security_level()](Proof::security_level) function
/// can be used.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Proof {
/// Basic metadata about the execution of the computation described by this proof.
pub context: Context,
/// Number of unique queries made by the verifier. This will be different from the
/// context.options.num_queries if the same position in the domain was queried more than once.
pub num_unique_queries: u8,
/// Commitments made by the prover during the commit phase of the protocol.
pub commitments: Commitments,
/// Decommitments of extended execution trace values (for all trace segments) at position
/// queried by the verifier.
pub trace_queries: Vec<Queries>,
/// Decommitments of constraint composition polynomial evaluations at positions queried by
/// the verifier.
pub constraint_queries: Queries,
/// Trace and constraint polynomial evaluations at an out-of-domain point.
pub ood_frame: OodFrame,
/// Low-degree proof for a DEEP composition polynomial.
pub fri_proof: FriProof,
/// Proof-of-work nonce for query seed grinding.
pub pow_nonce: u64,
/// Optionally, an auxiliary (non-STARK) proof that was generated during auxiliary trace generation.
pub gkr_proof: Option<Vec<u8>>,
}
impl Proof {
/// Returns STARK protocol parameters used to generate this proof.
pub fn options(&self) -> &ProofOptions {
self.context.options()
}
/// Returns trace info for the computation described by this proof.
pub fn trace_info(&self) -> &TraceInfo {
self.context.trace_info()
}
/// Returns the size of the LDE domain for the computation described by this proof.
pub fn lde_domain_size(&self) -> usize {
self.context.lde_domain_size()
}
// SECURITY LEVEL
// --------------------------------------------------------------------------------------------
/// Returns security level of this proof (in bits).
///
/// When `conjectured` is true, conjectured security level is returned; otherwise, provable
/// security level is returned. Usually, the number of queries needed for provable security is
/// 2x - 3x higher than the number of queries needed for conjectured security at the same
/// security level.
pub fn security_level<H: Hasher>(&self, conjectured: bool) -> u32 {
if conjectured {
get_conjectured_security(
self.context.options(),
self.context.num_modulus_bits(),
self.trace_info().length(),
H::COLLISION_RESISTANCE,
)
} else {
get_proven_security(
self.context.options(),
self.context.num_modulus_bits(),
self.trace_info().length(),
H::COLLISION_RESISTANCE,
)
}
}
// SERIALIZATION / DESERIALIZATION
// --------------------------------------------------------------------------------------------
/// Serializes this proof into a vector of bytes.
pub fn to_bytes(&self) -> Vec<u8> {
Serializable::to_bytes(self)
}
/// Returns a STARK proof read from the specified `source`.
///
/// # Errors
/// Returns an error of a valid STARK proof could not be read from the specified `source`.
pub fn from_bytes(source: &[u8]) -> Result<Self, DeserializationError> {
Deserializable::read_from_bytes(source)
}
/// Creates a dummy `Proof` for use in tests.
pub fn new_dummy() -> Self {
use crypto::{hashers::Blake3_192 as DummyHasher, BatchMerkleProof};
use math::fields::f64::BaseElement as DummyField;
use crate::FieldExtension;
Self {
context: Context::new::<DummyField>(
TraceInfo::new(1, 8),
ProofOptions::new(1, 2, 2, FieldExtension::None, 8, 1),
),
num_unique_queries: 0,
commitments: Commitments::default(),
trace_queries: Vec::new(),
constraint_queries: Queries::new::<DummyHasher<DummyField>, DummyField, MerkleTree<_>>(
BatchMerkleProof::<DummyHasher<DummyField>> { nodes: Vec::new(), depth: 0 },
vec![vec![DummyField::ONE]],
),
ood_frame: OodFrame::default(),
fri_proof: FriProof::new_dummy(),
pow_nonce: 0,
gkr_proof: None,
}
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for Proof {
fn write_into<W: utils::ByteWriter>(&self, target: &mut W) {
self.context.write_into(target);
target.write_u8(self.num_unique_queries);
self.commitments.write_into(target);
target.write_many(&self.trace_queries);
self.constraint_queries.write_into(target);
self.ood_frame.write_into(target);
self.fri_proof.write_into(target);
self.pow_nonce.write_into(target);
self.gkr_proof.write_into(target);
}
}
impl Deserializable for Proof {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let context = Context::read_from(source)?;
let num_unique_queries = source.read_u8()?;
let commitments = Commitments::read_from(source)?;
let num_trace_segments = context.trace_info().num_segments();
let mut trace_queries = Vec::with_capacity(num_trace_segments);
for _ in 0..num_trace_segments {
trace_queries.push(Queries::read_from(source)?);
}
let proof = Proof {
context,
num_unique_queries,
commitments,
trace_queries,
constraint_queries: Queries::read_from(source)?,
ood_frame: OodFrame::read_from(source)?,
fri_proof: FriProof::read_from(source)?,
pow_nonce: source.read_u64()?,
gkr_proof: Option::<Vec<u8>>::read_from(source)?,
};
Ok(proof)
}
}
// HELPER FUNCTIONS
// ================================================================================================
/// Computes conjectured security level for the specified proof parameters.
fn get_conjectured_security(
options: &ProofOptions,
base_field_bits: u32,
trace_domain_size: usize,
collision_resistance: u32,
) -> u32 {
// compute max security we can get for a given field size
let field_size = base_field_bits * options.field_extension().degree();
let field_security = field_size - (trace_domain_size * options.blowup_factor()).ilog2();
// compute security we get by executing multiple query rounds
let security_per_query = options.blowup_factor().ilog2();
let mut query_security = security_per_query * options.num_queries() as u32;
// include grinding factor contributions only for proofs adequate security
if query_security >= GRINDING_CONTRIBUTION_FLOOR {
query_security += options.grinding_factor();
}
cmp::min(cmp::min(field_security, query_security) - 1, collision_resistance)
}
/// Estimates proven security level for the specified proof parameters.
fn get_proven_security(
options: &ProofOptions,
base_field_bits: u32,
trace_domain_size: usize,
collision_resistance: u32,
) -> u32 {
let m_min: usize = 3;
let m_max = compute_upper_m(trace_domain_size);
let m_optimal = (m_min as u32..m_max as u32)
.max_by_key(|&a| {
proven_security_protocol_for_m(
options,
base_field_bits,
trace_domain_size,
a as usize,
)
})
.expect(
"Should not fail since m_max is larger than m_min for all trace sizes of length greater than 4",
);
cmp::min(
proven_security_protocol_for_m(
options,
base_field_bits,
trace_domain_size,
m_optimal as usize,
),
collision_resistance as u64,
) as u32
}
/// Computes proven security level for the specified proof parameters for a fixed
/// value of the proximity parameter m in the list-decoding regime.
fn proven_security_protocol_for_m(
options: &ProofOptions,
base_field_bits: u32,
trace_domain_size: usize,
m: usize,
) -> u64 {
let extension_field_bits = (base_field_bits * options.field_extension().degree()) as f64;
let num_fri_queries = options.num_queries() as f64;
let m = m as f64;
let rho = 1.0 / options.blowup_factor() as f64;
let alpha = (1.0 + 0.5 / m) * sqrt(rho);
let max_deg = options.blowup_factor() as f64 + 1.0;
// To apply Theorem 8 in https://eprint.iacr.org/2022/1216.pdf, we need to apply FRI with
// a slightly larger agreement parameter alpha.
// More concretely, we need alpha > rho_plus.sqrt() where rho_plus is the rate in function field
// F(Z) and defined as (trace_domain_size + 2.0) / lde_domain_size .
// This means that the range of m needs to be restricted in order to ensure that
// alpha := 1 - theta := rho.sqrt() * (1 + 1/2m) is greater than rho_plus.sqrt().
// Determining the range of m is the responsibility of the calling function.
// Now, once m is fixed, we need to make sure that we choose an m_plus such that
// alpha <= rho_plus.sqrt() * (1 + 1/2m_plus). This m_plus will be used to define
// the list-decoding list size in F(Z).
// Modified rate in function field F(Z)
let lde_domain_size = (trace_domain_size * options.blowup_factor()) as f64;
let trace_domain_size = trace_domain_size as f64;
let num_openings = 2.0;
let rho_plus = (trace_domain_size + num_openings) / lde_domain_size;
// New proximity parameter m_plus, corresponding to rho_plus, needed to make sure that
// alpha < rho_plus.sqrt() * (1 + 1 / (2 * m_plus))
let m_plus = ceil(1.0 / (2.0 * (alpha / sqrt(rho_plus) - 1.0)));
let alpha_plus = (1.0 + 0.5 / m_plus) * sqrt(rho_plus);
let theta_plus = 1.0 - alpha_plus;
// Computes FRI commit-phase (i.e., pre-query) soundness error.
// This considers only the first term given in eq. 7 in https://eprint.iacr.org/2022/1216.pdf,
// i.e. 0.5 * (m + 0.5)^7 * n^2 / (rho^1.5.q) as all other terms are negligible in comparison.
let fri_commit_err_bits = extension_field_bits
- log2((0.5 * powf(m + 0.5, 7.0) / powf(rho, 1.5)) * powf(lde_domain_size, 2.0));
// Compute FRI query-phase soundness error
let fri_queries_err_bits =
options.grinding_factor() as f64 - log2(powf(1.0 - theta_plus, num_fri_queries));
// Combined error for FRI
let fri_err_bits = cmp::min(fri_commit_err_bits as u64, fri_queries_err_bits as u64);
if fri_err_bits < 1 {
return 0;
}
let fri_err_bits = fri_err_bits - 1;
// List size
let l_plus = (2.0 * m_plus + 1.0) / (2.0 * sqrt(rho_plus));
// ALI related soundness error. Note that C here is equal to 1 because of our use of
// linear batching.
let ali_err_bits = -log2(l_plus) + extension_field_bits;
// DEEP related soundness error. Note that this uses that the denominator |F| - |D ∪ H|
// can be approximated by |F| for all practical domain sizes. We also use the blow-up factor
// as an upper bound for the maximal constraint degree.
let deep_err_bits = -log2(
l_plus * (max_deg * (trace_domain_size + num_openings - 1.0) + (trace_domain_size - 1.0)),
) + extension_field_bits;
let min = cmp::min(cmp::min(fri_err_bits, ali_err_bits as u64), deep_err_bits as u64);
if min < 1 {
return 0;
}
min - 1
}
// HELPER FUNCTIONS
// ================================================================================================
/// Computes the largest proximity parameter m needed for Theorem 8
/// in <https://eprint.iacr.org/2022/1216.pdf> to work.
fn compute_upper_m(h: usize) -> f64 {
let h = h as f64;
let m_max = ceil(0.25 * h * (1.0 + sqrt(1.0 + 2.0 / h)));
// We cap the range to 1000 as the optimal m value will be in the lower range of [m_min, m_max]
// since increasing m too much will lead to a deterioration in the FRI commit soundness making
// any benefit gained in the FRI query soundess mute.
cmp::min(m_max as u64, MAX_PROXIMITY_PARAMETER) as f64
}
#[cfg(feature = "std")]
pub fn log2(value: f64) -> f64 {
value.log2()
}
#[cfg(not(feature = "std"))]
pub fn log2(value: f64) -> f64 {
libm::log2(value)
}
#[cfg(feature = "std")]
pub fn sqrt(value: f64) -> f64 {
value.sqrt()
}
#[cfg(not(feature = "std"))]
pub fn sqrt(value: f64) -> f64 {
libm::sqrt(value)
}
#[cfg(feature = "std")]
pub fn powf(value: f64, exp: f64) -> f64 {
value.powf(exp)
}
#[cfg(not(feature = "std"))]
pub fn powf(value: f64, exp: f64) -> f64 {
libm::pow(value, exp)
}
#[cfg(feature = "std")]
pub fn ceil(value: f64) -> f64 {
value.ceil()
}
#[cfg(not(feature = "std"))]
pub fn ceil(value: f64) -> f64 {
libm::ceil(value)
}
#[cfg(test)]
mod prove_security_tests {
use math::{fields::f64::BaseElement, StarkField};
use super::ProofOptions;
use crate::{proof::get_proven_security, FieldExtension};
#[test]
fn get_96_bits_security() {
let field_extension = FieldExtension::Cubic;
let base_field_bits = BaseElement::MODULUS_BITS;
let fri_folding_factor = 8;
let fri_remainder_max_degree = 127;
let grinding_factor = 20;
let blowup_factor = 4;
let num_queries = 80;
let collision_resistance = 128;
let trace_length = 2_usize.pow(18);
let mut options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_1 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert_eq!(security_1, 97);
// increasing the blowup factor should increase the bits of security gained per query
let blowup_factor = 8;
let num_queries = 53;
options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_2 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert_eq!(security_2, 97);
}
#[test]
fn get_128_bits_security() {
let field_extension = FieldExtension::Cubic;
let base_field_bits = BaseElement::MODULUS_BITS;
let fri_folding_factor = 8;
let fri_remainder_max_degree = 127;
let grinding_factor = 20;
let blowup_factor = 8;
let num_queries = 85;
let collision_resistance = 128;
let trace_length = 2_usize.pow(18);
let mut options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_1 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert_eq!(security_1, 128);
// increasing the blowup factor should increase the bits of security gained per query
let blowup_factor = 16;
let num_queries = 65;
options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_2 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert_eq!(security_2, 128);
}
#[test]
fn extension_degree() {
let field_extension = FieldExtension::Quadratic;
let base_field_bits = BaseElement::MODULUS_BITS;
let fri_folding_factor = 8;
let fri_remainder_max_degree = 127;
let grinding_factor = 20;
let blowup_factor = 8;
let num_queries = 85;
let collision_resistance = 128;
let trace_length = 2_usize.pow(18);
let mut options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_1 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert_eq!(security_1, 67);
// increasing the extension degree improves the FRI commit phase soundness error and permits
// reaching 128 bits security
let field_extension = FieldExtension::Cubic;
options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_2 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert_eq!(security_2, 128);
}
#[test]
fn trace_length() {
let field_extension = FieldExtension::Cubic;
let base_field_bits = BaseElement::MODULUS_BITS;
let fri_folding_factor = 8;
let fri_remainder_max_degree = 127;
let grinding_factor = 20;
let blowup_factor = 8;
let num_queries = 80;
let collision_resistance = 128;
let trace_length = 2_usize.pow(20);
let mut options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_1 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
let trace_length = 2_usize.pow(16);
options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_2 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert!(security_1 < security_2);
}
#[test]
fn num_fri_queries() {
let field_extension = FieldExtension::Cubic;
let base_field_bits = BaseElement::MODULUS_BITS;
let fri_folding_factor = 8;
let fri_remainder_max_degree = 127;
let grinding_factor = 20;
let blowup_factor = 8;
let num_queries = 60;
let collision_resistance = 128;
let trace_length = 2_usize.pow(20);
let mut options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_1 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
let num_queries = 80;
options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_2 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert!(security_1 < security_2);
}
#[test]
fn blowup_factor() {
let field_extension = FieldExtension::Cubic;
let base_field_bits = BaseElement::MODULUS_BITS;
let fri_folding_factor = 8;
let fri_remainder_max_degree = 127;
let grinding_factor = 20;
let blowup_factor = 8;
let num_queries = 30;
let collision_resistance = 128;
let trace_length = 2_usize.pow(20);
let mut options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_1 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
let blowup_factor = 16;
options = ProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor as usize,
fri_remainder_max_degree as usize,
);
let security_2 =
get_proven_security(&options, base_field_bits, trace_length, collision_resistance);
assert!(security_1 < security_2);
}
}