forked from arkworks-rs/poly-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
1386 lines (1270 loc) · 49.6 KB
/
lib.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! A crate for polynomial commitment schemes.
#![deny(unused_import_braces, unused_qualifications, trivial_casts)]
#![deny(trivial_numeric_casts, private_in_public, variant_size_differences)]
#![deny(stable_features, unreachable_pub, non_shorthand_field_patterns)]
#![deny(unused_attributes, unused_mut)]
#![deny(missing_docs)]
#![deny(unused_imports)]
#![deny(renamed_and_removed_lints, stable_features, unused_allocation)]
#![deny(unused_comparisons, bare_trait_objects, unused_must_use, const_err)]
#![forbid(unsafe_code)]
#![allow(
clippy::upper_case_acronyms,
clippy::too_many_arguments,
clippy::type_complexity,
clippy::try_err,
clippy::map_collect_result_unit,
clippy::not_unsafe_ptr_arg_deref,
clippy::suspicious_op_assign_impl,
clippy::assertions_on_constants
)]
#[macro_use]
extern crate derivative;
#[macro_use]
extern crate bench_utils;
pub use algebra::fft::DensePolynomial as Polynomial;
use algebra::{serialize::*, Field, SemanticallyValid};
use rand_core::RngCore;
/// Implements a Fiat-Shamir based Rng that allows one to incrementally update
/// the seed based on new messages in the proof transcript.
pub mod rng;
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Debug,
iter::FromIterator,
rc::Rc,
string::{String, ToString},
vec::Vec,
};
/// Data structures used by a polynomial commitment scheme.
pub mod data_structures;
pub use data_structures::*;
/// Errors pertaining to query sets.
pub mod error;
pub use error::*;
use crate::rng::FiatShamirRng;
/// A random number generator that bypasses some limitations of the Rust borrow
/// checker.
pub mod optional_rng;
/// A polynomial commitment scheme based on the hardness of the
/// discrete logarithm problem in prime-order groups.
/// The construction is detailed in [[BCMS20]][pcdas].
///
/// [pcdas]: https://eprint.iacr.org/2020/499
pub mod ipa_pc;
/// `QuerySet` is the set of queries that are to be made to a set of labeled polynomials/equations
/// `p` that have previously been committed to. Each element of a `QuerySet` is a pair of
/// `(label, (point_label, point))`, where `label` is the label of a polynomial in `p`,
/// `point_label` is the label for the point (e.g., "beta"), and and `point` is the field element
/// that `p[label]` is to be queried at.
pub type QuerySet<'a, F> = BTreeSet<(String, (String, F))>;
/// `Evaluations` is the result of querying a set of labeled polynomials or equations
/// `p` at a `QuerySet` `Q`. It maps each element of `Q` to the resulting evaluation.
/// That is, if `(label, query)` is an element of `Q`, then `evaluation.get((label, query))`
/// should equal `p[label].evaluate(query)`.
pub type Evaluations<'a, F> = BTreeMap<(String, F), F>;
/// A proof of satisfaction of linear combinations.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct BatchLCProof<F: Field, PC: PolynomialCommitment<F>> {
/// Evaluation proof.
pub proof: PC::BatchProof,
/// Evaluations required to verify the proof.
pub evals: Option<Vec<F>>,
}
impl<F: Field, PC: PolynomialCommitment<F>> SemanticallyValid for BatchLCProof<F, PC> {
fn is_valid(&self) -> bool {
self.proof.is_valid()
&& if self.evals.is_some() {
self.evals.as_ref().unwrap().is_valid()
} else {
true
}
}
}
/// Describes the interface for a polynomial commitment scheme that allows
/// a sender to commit to multiple polynomials and later provide a succinct proof
/// of evaluation for the corresponding commitments at a query set `Q`, while
/// enforcing per-polynomial degree bounds.
pub trait PolynomialCommitment<F: Field>: Sized {
/// The universal parameters for the commitment scheme. These are "trimmed"
/// down to `Self::CommitterKey` and `Self::VerifierKey` by `Self::trim`.
type UniversalParams: PCUniversalParams;
/// The committer key for the scheme; used to commit to a polynomial and then
/// open the commitment to produce an evaluation proof.
type CommitterKey: PCCommitterKey;
/// The verifier key for the scheme; used to check an evaluation proof.
type VerifierKey: PCVerifierKey;
/// The prepared verifier key for the scheme; used to check an evaluation proof.
type PreparedVerifierKey: PCPreparedVerifierKey<Self::VerifierKey> + Clone;
/// The commitment to a polynomial.
type Commitment: PCCommitment + Default + Debug + Eq + PartialEq;
/// The prepared commitment to a polynomial.
type PreparedCommitment: PCPreparedCommitment<Self::Commitment>;
/// The commitment randomness.
type Randomness: PCRandomness;
/// The evaluation proof for a single point.
type Proof: Clone + Debug + Eq + PartialEq + CanonicalSerialize + CanonicalDeserialize;
/// The evaluation proof for a query set.
type BatchProof: Clone
+ Debug
+ Eq
+ PartialEq
+ CanonicalSerialize
+ CanonicalDeserialize
+ SemanticallyValid;
/// The error type for the scheme.
type Error: std::error::Error + From<Error>;
/// Source of random data
type RandomOracle: FiatShamirRng<Error = Self::Error>;
/// Constructs public parameters when given as input the maximum degree `degree`
/// for the polynomial commitment scheme.
fn setup(max_degree: usize) -> Result<Self::UniversalParams, Self::Error>;
/// Constructs public parameters when given as input the maximum degree `degree`
/// for the polynomial commitment scheme from given seed
fn setup_from_seed(
max_degree: usize,
seed: &[u8],
) -> Result<Self::UniversalParams, Self::Error>;
/// Specializes the public parameters for polynomials up to the given `supported_degree`
/// and for enforcing degree bounds in the range `1..=supported_degree`.
fn trim(
pp: &Self::UniversalParams,
supported_degree: usize,
) -> Result<(Self::CommitterKey, Self::VerifierKey), Self::Error>;
/// Outputs a commitments to `polynomials`. If `polynomials[i].is_hiding()`,
/// then the `i`-th commitment is hiding up to `polynomials.hiding_bound()` queries.
/// `rng` should not be `None` if `polynomials[i].is_hiding() == true` for any `i`.
///
/// If for some `i`, `polynomials[i].is_hiding() == false`, then the
/// corresponding randomness is `Self::Randomness::empty()`.
///
/// If for some `i`, `polynomials[i].degree_bound().is_some()`, then that
/// polynomial will have the corresponding degree bound enforced.
fn commit<'a>(
ck: &Self::CommitterKey,
polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
rng: Option<&mut dyn RngCore>,
) -> Result<
(
Vec<LabeledCommitment<Self::Commitment>>,
Vec<LabeledRandomness<Self::Randomness>>,
),
Self::Error,
>;
/// Single point multi poly open:
/// On input a list of labeled polynomials and a query point, `open` outputs a proof of evaluation
/// of the polynomials at the query point.
/// For now it is just a wrapper for the low-level function `open_individual_opening_challenges()`
/// and hence assumes that the statement of the opening proof (i.e. the commitments, the query point,
/// and the evaluations) are bound to the state of the Fiat-Shamir rng.
fn open<'a>(
ck: &Self::CommitterKey,
labeled_polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: F,
fs_rng: &mut Self::RandomOracle,
rands: impl IntoIterator<Item = &'a LabeledRandomness<Self::Randomness>>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
Self::Randomness: 'a,
Self::Commitment: 'a,
{
Self::open_individual_opening_challenges(
ck,
labeled_polynomials,
commitments,
point,
fs_rng,
rands,
rng,
)
}
/// Multi point multi poly open:
/// On input a list of labeled polynomials and a query set, `open` outputs a proof of evaluation
/// of the polynomials at the points in the query set.
/// For now it is just a wrapper for the low-level function `open_individual_opening_challenges()`
/// and hence assumes that the statement of the opening proof (i.e. the commitments, the query set,
/// and the evaluations) are bound to the state of the Fiat-Shamir rng.
/// TODO: rename this function
fn batch_open<'a>(
ck: &Self::CommitterKey,
labeled_polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
fs_rng: &mut Self::RandomOracle,
rands: impl IntoIterator<Item = &'a LabeledRandomness<Self::Randomness>>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::BatchProof, Self::Error>
where
Self::Randomness: 'a,
Self::Commitment: 'a,
{
Self::batch_open_individual_opening_challenges(
ck,
labeled_polynomials,
commitments,
query_set,
fs_rng,
rands,
rng,
)
}
/// Single point multi poly verify:
/// Verifies that `values` are the evaluations at `point` of the polynomials
/// committed inside `commitments`.
fn check<'a>(
vk: &Self::VerifierKey,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: F,
values: impl IntoIterator<Item = F>,
proof: &Self::Proof,
fs_rng: &mut Self::RandomOracle,
) -> Result<bool, Self::Error>
where
Self::Commitment: 'a,
{
// as in open(), setup Fiat-Shamir rng, etc.
Self::check_individual_opening_challenges(vk, commitments, point, values, proof, fs_rng)
}
/// Multi point multi poly verify:
/// Checks that `values` are the true evaluations at `query_set` of the polynomials
/// committed in `labeled_commitments`.
/// TODO: rename this function
fn batch_check<'a>(
vk: &Self::VerifierKey,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
evaluations: &Evaluations<F>,
proof: &Self::BatchProof,
fs_rng: &mut Self::RandomOracle,
) -> Result<bool, Self::Error>
where
Self::Commitment: 'a,
{
Self::batch_check_individual_opening_challenges(
vk,
commitments,
query_set,
evaluations,
proof,
fs_rng,
)
}
/// Multi point multi LC open:
/// On input a list of polynomials, linear combinations of those polynomials,
/// and a query set, `open_combination` outputs a proof of evaluation of
/// the combinations at the points in the query set.
/// For now it is just a wrapper for the low-level function `open_combinations_individual_opening_challenges()`
/// and hence assumes that the statement of the opening proof (i.e. the LCs, the query set,
/// and the evaluations) are bound to the state of the Fiat-Shamir rng.
fn open_combinations<'a>(
ck: &Self::CommitterKey,
linear_combinations: impl IntoIterator<Item = &'a LinearCombination<F>>,
polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
fs_rng: &mut Self::RandomOracle,
rands: impl IntoIterator<Item = &'a LabeledRandomness<Self::Randomness>>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<F, Self>, Self::Error>
where
Self::Randomness: 'a,
Self::Commitment: 'a,
{
Self::open_combinations_individual_opening_challenges(
ck,
linear_combinations,
polynomials,
commitments,
query_set,
fs_rng,
rands,
rng,
)
}
/// Multi point multi LC verify.
/// Checks that `evaluations` are the true evaluations at `query_set` of the
/// linear combinations of polynomials committed in `commitments`.
fn check_combinations<'a>(
vk: &Self::VerifierKey,
linear_combinations: impl IntoIterator<Item = &'a LinearCombination<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
eqn_query_set: &QuerySet<F>,
eqn_evaluations: &Evaluations<F>,
proof: &BatchLCProof<F, Self>,
fs_rng: &mut Self::RandomOracle,
) -> Result<bool, Self::Error>
where
Self::Commitment: 'a,
{
Self::check_combinations_individual_opening_challenges(
vk,
linear_combinations,
commitments,
eqn_query_set,
eqn_evaluations,
proof,
fs_rng,
)
}
/// Single point multi poly open, allowing the random oracle to be passed from
/// 'outside' to the function.
/// CAUTION: This is a low-level function to be handled carefully, typically
/// presuming that commitments and query_set is already bound to the internal
/// state of the Fiat-Shamir rng.
/// TODO: rename this function
fn open_individual_opening_challenges<'a>(
ck: &Self::CommitterKey,
labeled_polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: F,
fs_rng: &mut Self::RandomOracle,
rands: impl IntoIterator<Item = &'a LabeledRandomness<Self::Randomness>>,
// `rng` if needed for blinding
rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
Self::Randomness: 'a,
Self::Commitment: 'a;
/// Multi point multi poly open, allowing the random oracle to be passed from
/// 'outside' to the function.
/// CAUTION: This is a low-level function to be handled carefully, typically
/// presuming that commitments and query_set is already bound to the internal
/// state of the Fiat-Shamir rng.
/// TODO: rename this function
fn batch_open_individual_opening_challenges<'a>(
ck: &Self::CommitterKey,
labeled_polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
fs_rng: &mut Self::RandomOracle,
rands: impl IntoIterator<Item = &'a LabeledRandomness<Self::Randomness>>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::BatchProof, Self::Error>
where
Self::Randomness: 'a,
Self::Commitment: 'a;
/// Single point multi poly verify, with random oracle passed from 'outside'.
/// CAUTION: This is a low-level function to be handled carefully, typically
/// presuming that commitments and query_set is already bound to the internal
/// state of the Fiat-Shamir rng.
/// TODO: rename this function
fn check_individual_opening_challenges<'a>(
vk: &Self::VerifierKey,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: F,
values: impl IntoIterator<Item = F>,
proof: &Self::Proof,
fs_rng: &mut Self::RandomOracle,
) -> Result<bool, Self::Error>
where
Self::Commitment: 'a;
/// Multi point multi poly verify, with random oracle passed from 'outside'.
/// CAUTION: This is a low-level function to be handled carefully, typically
/// presuming that commitments and query_set is already bound to the internal
/// state of the Fiat-Shamir rng.
/// TODO: rename this function
fn batch_check_individual_opening_challenges<'a>(
vk: &Self::VerifierKey,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
evaluations: &Evaluations<F>,
proof: &Self::BatchProof,
fs_rng: &mut Self::RandomOracle,
) -> Result<bool, Self::Error>
where
Self::Commitment: 'a;
/// Default implementation of Multi point multi LC open, with random oracle passed from 'outside'.
/// Evaluates each of the (non-trivial) LC-polynomials at each of the query point the LC is queried.
/// CAUTION: This is a low-level function to be handled with carefully, presuming that
/// 1) the commitments
/// 2) their LC's, and
/// 3) the query set as well as the evaluations
/// are already bound to the internal state of the Fiat-Shamir rng.
// TODO: rename this function
fn open_combinations_individual_opening_challenges<'a>(
ck: &Self::CommitterKey,
linear_combinations: impl IntoIterator<Item = &'a LinearCombination<F>>,
polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
fs_rng: &mut Self::RandomOracle,
rands: impl IntoIterator<Item = &'a LabeledRandomness<Self::Randomness>>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<F, Self>, Self::Error>
where
Self::Randomness: 'a,
Self::Commitment: 'a,
{
let linear_combinations: Vec<_> = linear_combinations.into_iter().collect();
let polynomials: Vec<_> = polynomials.into_iter().collect();
let poly_query_set =
lc_query_set_to_poly_query_set(linear_combinations.iter().copied(), query_set);
let poly_evals = evaluate_query_set(polynomials.iter().copied(), &poly_query_set);
let proof = Self::batch_open_individual_opening_challenges(
ck,
polynomials,
commitments,
&poly_query_set,
fs_rng,
rands,
rng,
)?;
Ok(BatchLCProof {
proof,
evals: Some(poly_evals.values().copied().collect()),
})
}
/// Default implementation of Multi point multi LC verify, with random oracle passed from 'outside'.
/// Evaluates each of the (non-trivial) LC-polynomials at the query point.
/// CAUTION: This is a low-level function to be handled carefully, typically
/// presuming that commitments and query_set is already bound to the internal
/// state of the Fiat-Shamir rng.
/// TODO: rename this function
fn check_combinations_individual_opening_challenges<'a>(
vk: &Self::VerifierKey,
linear_combinations: impl IntoIterator<Item = &'a LinearCombination<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
eqn_query_set: &QuerySet<F>,
eqn_evaluations: &Evaluations<F>,
proof: &BatchLCProof<F, Self>,
fs_rng: &mut Self::RandomOracle,
) -> Result<bool, Self::Error>
where
Self::Commitment: 'a,
{
let BatchLCProof { proof, evals } = proof;
if !evals.is_some() {
return Err(Error::IncorrectProof.into())
}
let lc_s = BTreeMap::from_iter(linear_combinations.into_iter().map(|lc| (lc.label(), lc)));
let poly_query_set = lc_query_set_to_poly_query_set(lc_s.values().copied(), eqn_query_set);
let poly_evals = Evaluations::from_iter(
poly_query_set
.iter()
.map(|(_, point)| point)
.cloned()
.zip(evals.clone().unwrap()),
);
for &(ref lc_label, (_, point)) in eqn_query_set {
if let Some(lc) = lc_s.get(lc_label) {
let claimed_rhs = *eqn_evaluations.get(&(lc_label.clone(), point)).ok_or(
Error::MissingEvaluation {
label: lc_label.to_string(),
},
)?;
let mut actual_rhs = F::zero();
for (coeff, label) in lc.iter() {
let eval = match label {
LCTerm::One => F::one(),
LCTerm::PolyLabel(l) => *poly_evals
.get(&(l.clone(), point))
.ok_or(Error::MissingEvaluation { label: l.clone() })?,
};
actual_rhs += &(*coeff * eval);
}
if claimed_rhs != actual_rhs {
eprintln!("Claimed evaluation of {} is incorrect", lc.label());
return Ok(false);
}
}
}
let pc_result = Self::batch_check_individual_opening_challenges(
vk,
commitments,
&poly_query_set,
&poly_evals,
proof,
fs_rng,
)?;
if !pc_result {
eprintln!("Evaluation proofs failed to verify");
return Ok(false);
}
Ok(true)
}
}
/// Evaluate the given polynomials at `query_set`.
pub fn evaluate_query_set<'a, F: Field>(
polys: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
query_set: &QuerySet<'a, F>,
) -> Evaluations<'a, F> {
let polys = BTreeMap::from_iter(polys.into_iter().map(|p| (p.label(), p)));
let mut evaluations = Evaluations::new();
for (label, (_, point)) in query_set {
let poly = polys
.get(label)
.expect("polynomial in evaluated lc is not found");
let eval = poly.evaluate(*point);
evaluations.insert((label.clone(), *point), eval);
}
evaluations
}
/// Evaluate the given polynomials at `query_set` and returns a Vec<((poly_label, point_label), eval)>)
pub fn evaluate_query_set_to_vec<'a, F: Field>(
polys: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
query_set: &QuerySet<'a, F>,
) -> Vec<((String, String), F)> {
// use std::{
// collections::BTreeMap,
// iter::FromIterator,
// };
let polys = BTreeMap::from_iter(polys.into_iter().map(|p| (p.label(), p)));
let mut v = Vec::new();
for (label, (point_label, point)) in query_set {
let poly = polys
.get(label)
.expect("polynomial in evaluated lc is not found");
let eval = poly.evaluate(*point);
v.push(((label.clone(), point_label.clone()), eval));
}
v
}
/// Generic conversion of an LC query set into a poly query set, by
/// considering every non-trivial poly of an LC to be evaluated at each
/// point the LC is queried.
fn lc_query_set_to_poly_query_set<'a, F: 'a + Field>(
linear_combinations: impl IntoIterator<Item = &'a LinearCombination<F>>,
query_set: &QuerySet<F>,
) -> QuerySet<'a, F> {
let mut poly_query_set = QuerySet::new();
let lc_s = linear_combinations.into_iter().map(|lc| (lc.label(), lc));
let linear_combinations = BTreeMap::from_iter(lc_s);
for (lc_label, (point_label, point)) in query_set {
if let Some(lc) = linear_combinations.get(lc_label) {
// select the non-trivial polynomials in the LC
for (_, poly_label) in lc.iter().filter(|(_, l)| !l.is_one()) {
// add the poly to be queried at the point
if let LCTerm::PolyLabel(l) = poly_label {
poly_query_set.insert((l.into(), (point_label.clone(), *point)));
}
}
}
}
poly_query_set
}
#[cfg(test)]
pub mod tests {
use crate::rng::FiatShamirRngSeed;
use crate::*;
use algebra::{serialize::test_canonical_serialize_deserialize, Field, SemanticallyValid};
use rand::{distributions::Distribution, thread_rng, Rng};
fn setup_test_fs_rng<F, PC>() -> PC::RandomOracle
where
F: Field,
PC: PolynomialCommitment<F>,
{
let mut fs_rng_seed_builder = <PC::RandomOracle as FiatShamirRng>::Seed::new();
fs_rng_seed_builder.add_bytes(b"TEST_SEED").unwrap();
let fs_rng_seed = fs_rng_seed_builder.finalize();
PC::RandomOracle::from_seed(fs_rng_seed)
}
#[derive(Copy, Clone, PartialEq)]
pub enum NegativeType {
Values,
Commitments,
CommitterKey,
VerifierKey,
}
#[derive(Copy, Clone, Default)]
struct TestInfo {
num_iters: usize,
/// Max segment size
max_degree: Option<usize>,
/// Segment size
supported_degree: Option<usize>,
num_polynomials: usize,
enforce_degree_bounds: bool,
max_num_queries: usize,
num_equations: Option<usize>,
segmented: bool,
negative_type: Option<NegativeType>,
}
pub fn bad_degree_bound_test<F, PC>() -> Result<(), PC::Error>
where
F: Field,
PC: PolynomialCommitment<F>,
{
let rng = &mut thread_rng();
let max_degree = 100;
let pp = PC::setup(max_degree)?;
test_canonical_serialize_deserialize(true, &pp);
for _ in 0..10 {
let supported_degree = rand::distributions::Uniform::from(1..=max_degree).sample(rng);
assert!(
max_degree >= supported_degree,
"max_degree < supported_degree"
);
let mut labels = Vec::new();
let mut polynomials = Vec::new();
let mut degree_bounds = Vec::new();
for i in 0..10 {
let label = format!("Test{}", i);
labels.push(label.clone());
let poly = Polynomial::rand(supported_degree, rng);
let degree_bound = 1usize;
let hiding_bound = Some(1);
degree_bounds.push(degree_bound);
polynomials.push(LabeledPolynomial::new(
label,
poly,
Some(degree_bound),
hiding_bound,
))
}
let supported_hiding_bound = polynomials
.iter()
.map(|p| p.hiding_bound().unwrap_or(0))
.max()
.unwrap_or(0);
println!("supported degree: {:?}", supported_degree);
println!("supported hiding bound: {:?}", supported_hiding_bound);
let (ck, vk) = PC::trim(&pp, supported_degree)?;
assert!(ck.is_valid());
assert!(vk.is_valid());
println!("Trimmed");
test_canonical_serialize_deserialize(true, &ck);
test_canonical_serialize_deserialize(true, &vk);
let (comms, rands) = PC::commit(&ck, &polynomials, Some(rng))?;
assert!(comms.is_valid());
let mut query_set = QuerySet::new();
let mut values = Evaluations::new();
let point = F::rand(rng);
for (i, label) in labels.iter().enumerate() {
query_set.insert((label.clone(), (format!("{}", i), point)));
let value = polynomials[i].evaluate(point);
values.insert((label.clone(), point), value);
}
println!("Generated query set");
let mut fs_rng = setup_test_fs_rng::<F, PC>();
let proof = PC::batch_open(
&ck,
&polynomials,
&comms,
&query_set,
&mut fs_rng,
&rands,
Some(rng),
)?;
assert!(proof.is_valid());
test_canonical_serialize_deserialize(true, &proof);
let mut fs_rng = setup_test_fs_rng::<F, PC>();
let result = PC::batch_check(&vk, &comms, &query_set, &values, &proof, &mut fs_rng)?;
assert!(result, "proof was incorrect, Query set: {:#?}", query_set);
}
Ok(())
}
fn test_template<F, PC>(info: TestInfo) -> Result<(), PC::Error>
where
F: Field,
PC: PolynomialCommitment<F>,
{
for _ in 0..info.num_iters {
let TestInfo {
max_degree,
supported_degree,
num_polynomials,
enforce_degree_bounds,
max_num_queries,
segmented,
negative_type,
..
} = info;
let rng = &mut thread_rng();
// sample random max_degree from 2 up to 64.
let max_degree =
max_degree.unwrap_or(rand::distributions::Uniform::from(2..=64).sample(rng));
let pp = PC::setup(max_degree)?;
test_canonical_serialize_deserialize(true, &pp);
// sample supported_degree if not defined
let supported_degree = match supported_degree {
Some(0) => 0,
Some(d) => d,
None => rand::distributions::Uniform::from(1..=max_degree).sample(rng),
};
assert!(
max_degree >= supported_degree,
"max_degree < supported_degree"
);
let mut polynomials = Vec::new();
let mut degree_bounds = if enforce_degree_bounds {
Some(Vec::new())
} else {
None
};
// sample the maximum number of segments from 5 up to 15.
let seg_mul = rand::distributions::Uniform::from(5..=15).sample(rng);
let mut labels = Vec::new();
println!("Sampled supported degree");
// sample `max_num_queries` query points
let num_points_in_query_set =
rand::distributions::Uniform::from(1..=max_num_queries).sample(rng);
for i in 0..num_polynomials {
let label = format!("Test{}", i);
labels.push(label.clone());
// sample polynomial of random degree
let degree;
if segmented {
// sample degree from 5*`supported_degree` up to `seg_mul`*`supported_degree`
degree = if supported_degree > 0 {
rand::distributions::Uniform::from(1..=supported_degree).sample(rng)
} else {
0
} * seg_mul;
} else {
// sample degree from 1 up to `supported_degree`
degree = if supported_degree > 0 {
rand::distributions::Uniform::from(1..=supported_degree).sample(rng)
} else {
0
}
}
let poly = Polynomial::rand(degree, rng);
println!("Poly {} degree: {}", i, degree);
// If specified, we sample any degree bound larger than the degree of the
// polynomial.
let degree_bound = if let Some(degree_bounds) = &mut degree_bounds {
let segment_size = (supported_degree + 1).next_power_of_two();
let num_segments = (degree + 1) / segment_size
+ if (degree + 1) % segment_size == 0 {
0
} else {
1
};
let range =
rand::distributions::Uniform::from(degree..num_segments * segment_size);
let degree_bound = range.sample(rng);
degree_bounds.push(degree_bound);
Some(degree_bound)
} else {
None
};
// Sample hiding bound. The concrete value > 0 does not matter.
// TODO: Beyond `Some` or `None`, the hiding_bound is not used by the dlog PC,
// as randomization up to a certain number of queries is outsourced.
// We should think about how to treat that in future.
let hiding_bound = if num_points_in_query_set >= degree {
Some(degree)
} else {
Some(num_points_in_query_set)
};
println!("Hiding bound: {:?}", hiding_bound);
polynomials.push(LabeledPolynomial::new(
label,
poly,
degree_bound,
hiding_bound,
))
}
let supported_hiding_bound = polynomials
.iter()
.map(|p| p.hiding_bound().unwrap_or(0))
.max()
.unwrap_or(0);
println!("supported degree: {:?}", supported_degree);
println!("supported hiding bound: {:?}", supported_hiding_bound);
println!("num_points_in_query_set: {:?}", num_points_in_query_set);
let (mut ck, mut vk) = PC::trim(&pp, supported_degree)?;
if negative_type.is_some() && negative_type.unwrap() == NegativeType::CommitterKey {
ck.randomize();
}
if negative_type.is_some() && negative_type.unwrap() == NegativeType::VerifierKey {
vk.randomize();
}
assert!(ck.is_valid());
assert!(vk.is_valid());
println!("Trimmed");
test_canonical_serialize_deserialize(true, &ck);
test_canonical_serialize_deserialize(true, &vk);
let (mut comms, rands) = PC::commit(&ck, &polynomials, Some(rng))?;
if negative_type.is_some() && negative_type.unwrap() == NegativeType::Commitments {
for comm in comms.iter_mut() {
comm.randomize();
}
}
assert!(comms.is_valid());
// Construct "symmetric" query set, over which every polynomial
// is to be queried
let mut query_set = QuerySet::new();
let mut values = Evaluations::new();
// let mut point = F::one();
for _ in 0..num_points_in_query_set {
let point = F::rand(rng);
for (i, label) in labels.iter().enumerate() {
query_set.insert((label.clone(), (format!("{}", i), point)));
let value = polynomials[i].evaluate(point);
if negative_type.is_some() && negative_type.unwrap() == NegativeType::Values {
values.insert((label.clone(), point), F::rand(rng));
} else {
values.insert((label.clone(), point), value);
}
}
}
println!("Generated query set");
let mut fs_rng = setup_test_fs_rng::<F, PC>();
let proof = PC::batch_open(
&ck,
&polynomials,
&comms,
&query_set,
&mut fs_rng,
&rands,
Some(rng),
)?;
assert!(proof.is_valid());
test_canonical_serialize_deserialize(true, &proof);
// Assert success using the same key
let mut fs_rng = setup_test_fs_rng::<F, PC>();
let result = PC::batch_check(&vk, &comms, &query_set, &values, &proof, &mut fs_rng)?;
if !result {
println!(
"Failed with {} polynomials, num_points_in_query_set: {:?}",
num_polynomials, num_points_in_query_set
);
println!("Degree of polynomials:",);
for poly in polynomials {
println!("Degree: {:?}", poly.degree());
}
return Err(Error::IncorrectProof.into());
}
// Assert success using a bigger key
let bigger_degree = max_degree * 2;
let pp = PC::setup(bigger_degree)?;
let (_, vk) = PC::trim(&pp, bigger_degree)?;
let mut fs_rng = setup_test_fs_rng::<F, PC>();
assert!(PC::batch_check(
&vk,
&comms,
&query_set,
&values,
&proof,
&mut fs_rng
)?);
}
Ok(())
}
fn equation_test_template<F, PC>(info: TestInfo) -> Result<(), PC::Error>
where
F: Field,
PC: PolynomialCommitment<F>,
{
let TestInfo {
num_iters,
max_degree,
supported_degree,
num_polynomials,
enforce_degree_bounds,
max_num_queries,
num_equations,
..
} = info;
let rng = &mut thread_rng();
let max_degree =
max_degree.unwrap_or(rand::distributions::Uniform::from(2..=64).sample(rng));
let pp = PC::setup(max_degree)?;
for _ in 0..num_iters {
let supported_degree = supported_degree
.unwrap_or(rand::distributions::Uniform::from(1..=max_degree).sample(rng));
assert!(
max_degree >= supported_degree,
"max_degree < supported_degree"
);
let mut polynomials = Vec::new();
let mut degree_bounds = if enforce_degree_bounds {
Some(Vec::new())
} else {
None
};
let mut labels = Vec::new();
println!("Sampled supported degree");
// Generate polynomials
let num_points_in_query_set =
rand::distributions::Uniform::from(1..=max_num_queries).sample(rng);
for i in 0..num_polynomials {
let label = format!("Test{}", i);
labels.push(label.clone());
let degree = rand::distributions::Uniform::from(1..=supported_degree).sample(rng);
let poly = Polynomial::rand(degree, rng);
let degree_bound = if let Some(degree_bounds) = &mut degree_bounds {
if rng.gen() {
let range = rand::distributions::Uniform::from(degree..=supported_degree);
let degree_bound = range.sample(rng);
degree_bounds.push(degree_bound);
Some(degree_bound)
} else {
None
}
} else {
None
};
let hiding_bound = if num_points_in_query_set >= degree {
Some(degree)
} else {
Some(num_points_in_query_set)
};
println!("Hiding bound: {:?}", hiding_bound);
polynomials.push(LabeledPolynomial::new(
label,