forked from mrtuborg/libSTARK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAcspWitnessChecker_UTEST.cpp
814 lines (689 loc) · 27.1 KB
/
AcspWitnessChecker_UTEST.cpp
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
/**
* @file AcspWitnessChecker_UTEST.cpp
* @brief Unit test for AcspWitnessChecker
*
* @author Michael Riabzev, RiabzevMichael@gmail.com
* =====================================================================================
*/
#include "lightCircLib/lightCircPoly.hpp"
#include <languages/Acsp/AcspWitnessChecker.hpp>
#include <common/Algebra/LinearSpace.hpp>
#include <common/Infrastructure/Infrastructure.hpp>
#include <algebraLib/UnivariatePolynomialGeneral.hpp>
#include <gtest/gtest.h>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <vector>
#include <set>
namespace{
using libstark::AcspInstance;
using libstark::AcspWitness;
using libstark::AcspWitnessChecker;
using libstark::lightCircPoly;
using libstark::Sequence;
using Algebra::FieldElement;
using Algebra::zero;
using Algebra::one;
using Algebra::generateRandom;
using Algebra::elementsSet_t;
using Algebra::PolynomialDegree;
using Algebra::UnivariatePolynomialInterface;
using Algebra::UnivariatePolynomialGeneral;
using Algebra::DivisorPolynomial;
using Algebra::FiniteSetInterface;
using Algebra::FieldElementPredicate;
using Algebra::LinearSpace;
using Algebra::getStandartOrderedBasis;
using Infrastructure::Log2;
using std::pair;
using std::vector;
using std::set;
using std::unique_ptr;
using std::move;
#ifdef __GNUC__
// TODO: work around until gcc adds make_unique, remove on upgrade
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#else // #ifdef __GNUC__
using std::make_unique;
#endif // #ifdef __GNUC__
/**
* @brief Generates a random univariate polynomial
* @param maxDegree maximal degree for polynomial
* @return the polynomial
*/
unique_ptr< UnivariatePolynomialGeneral> generateRandomPoly(unsigned int maxDegree) {
unique_ptr< UnivariatePolynomialGeneral> poly(new UnivariatePolynomialGeneral); //polynomial for test
for(unsigned int i=0; i<=maxDegree; i++){
FieldElement coeff = generateRandom();
poly->setCoefficient(i,coeff);
}
return move(poly);
}
/**
* @class randomSequence
* @brief A mapping of \f$\mathbb{N}\f$ into the field
* such that the first \f$len\f$ integers are mapped into random field elements
* and the rest are mapped to zeros
*/
class randomSequence : public Sequence<FieldElement> {
public:
/**
* @brief The constructor
* @param n boundary of indexes that can be mapped to non-zero elements (namely \f$len\f$)
*/
randomSequence(Sequence<FieldElement>::index_t n):order(n){
for (unsigned int i=0; i<order.size(); i++){
order[i] = generateRandom();
}
}
/**
* @brief The mapping of integers to the field
* @param index some integer
* @return its mapping
*/
FieldElement getElementByIndex(Sequence<FieldElement>::index_t index)const {
if (index < order.size()) return order[index];
else return zero();
}
private:
/**
* The mapping is represented using a vector of field elements,
* if an integer is in the domain of the vector coordinates
* it is mapped to the vectors value in that coordinate,
* otherwise it is mapped to zero.
*/
vector< FieldElement> order;
};
/**
* @class specificSet
* @brief A set of field elements
* defined by explicit insertion of elements into it
*/
class specificSet : public FiniteSetInterface {
public:
/**
* @brief Default construction
*/
specificSet() {
vanishingPoly_ = UnivariatePolynomialGeneral(elements_);
}
/**
* @brief Adds an element to the set
* @param element element to add
*/
void addElement(const FieldElement& element){
elements_.insert(element);
vanishingPoly_ = UnivariatePolynomialGeneral(elements_);
}
/**
* @brief return the set of elements
* @return the elements set
*/
const elementsSet_t& getElements()const {
return elements_;
}
/**
* @brief Checks if an elements that satisfies a predicate exist in the set
* @param pred the predicate
* @return True iff such an element exists
*/
bool exist(const unique_ptr<const FieldElementPredicate>& pred)const {
for(elementsSet_t::iterator z=elements_.begin(); z!=elements_.end(); z++){
if (pred->test(*z) == true){
return true;
}
}
return false;
}
size_t size()const {return elements_.size();}
virtual const UnivariatePolynomialGeneral& vanishingPoly()const{
return vanishingPoly_;
}
/**
* Returns whether a field element is a member of the set
*/
bool contains(const FieldElement& e)const{
return count(elements_.begin(),elements_.end(),e)>0;
}
private:
/**
* The set of elements
*/
elementsSet_t elements_;
UnivariatePolynomialGeneral vanishingPoly_;
};
namespace Acsp_UTEST {
/**
* @brief Generates a random pair of AcspInstance and AcspWitness
* such that the witness satisfies the instance for vanishing validation check
* @return The random pair
*/
static pair<AcspInstance,AcspWitness> vanishing_generate_valid_pair(){
unsigned int neighbours_amount = 1+ rand() % 10; //neighbours_amount = number of variables of P, cant be 0
unsigned int vanishing_set_size = rand() % 100;
unsigned int maxDegree = rand() % 100;
/**
* Construct witness
*/
AcspWitness witness(generateRandomPoly(maxDegree));
/**
* Construct instance
*/
/**
* Construct vanishing set
*/
unique_ptr<specificSet> vanishingSet =
unique_ptr<specificSet>(new specificSet);
for (unsigned long i=0 ; i<vanishing_set_size; i++){
FieldElement root = generateRandom();
vanishingSet->addElement(root);
}
/**
* Construct neighbours
*/
AcspInstance::polynomialsVec neighborPolys;
for (unsigned int i=0; i<neighbours_amount; i++){
neighborPolys.push_back(generateRandomPoly(maxDegree));
}
/**
* Construct constraint polynomial in such a way
* that the Witness will satisfy the instance.
* We construct the constraints polynomial as
* a 'distinct product' of univariate polynomials:
* \f$P(x_0,x_1,x_2,\dots,x_n) = 1\cdot q_1(x_1)\cdot q_2(x_2)\cdots q_n(x_n)\f$
* so in this case:
* \f$(P\circ (x \vert A\circ \vec{N}))(z) = \displaystyle{\prod_{i=0}^{n} (q_i\circ A\circ N_i)(z)}\f$
* so we build \f$P\f$ in such a way that for each \f$z \in H\f$ exist an index
* \f$i\f$ such that \f$ ( x - A(N_i(z))) \vert q_i(x) \f$.
*
* This is done by dividing (randomly) the elements of the vanishing set
* to \f$n\f$ subset, where \f$n\f$ is the number of variables of \f$P\f$,
* name them \f$S_1,S_2,\dots,S_n\f$.
* We define \f$q_i (x) = \displaystyle{\prod_{z\in S_i } (x - A(N_i(z)))}\f$
*/
/** Generate roots sets vector */
elementsSet_t cloningSource;
vector<elementsSet_t> rootsSets(neighbours_amount,cloningSource);
const elementsSet_t& vanishingElements = vanishingSet->getElements();
for (elementsSet_t::iterator z = vanishingElements.begin(); z!=vanishingElements.end(); z++){
unsigned int index = rand() % neighbours_amount;
FieldElement neighb_eval = neighborPolys[index]->eval(*z);
FieldElement root = witness.assignmentPoly().eval(neighb_eval);
rootsSets[index].insert(root);
}
/** construct P */
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(UnivariatePolynomialGeneral(one())));
for (vector<elementsSet_t>::iterator roots=rootsSets.begin();roots!=rootsSets.end();roots++ ) {
UnivariatePolynomialGeneral q(*roots);
lightCircPoly qq(q);
ConstraintsPolynomial->multiplyDistinct(qq);
}
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a random pair of AcspInstance and AcspWitness
* such that the witness does not satisfy the instance for vanishing validation check
* @return The random pair
*/
static pair<AcspInstance,AcspWitness> vanishing_generate_invalid_pair(){
unsigned int neighbours_amount = 1+ rand() % 10; //neighbours_amount = number of variables of P, cant be 0
unsigned int vanishing_set_size = 1+ rand() % 100; //if there are no elements that have too be, no restriction checked
unsigned int maxDegree = rand() % 65;
/**
* Construct witness
*/
AcspWitness witness(generateRandomPoly(maxDegree));
/**
* Construct instance
*/
/**
* Construct vanishing set
*/
unique_ptr<specificSet> vanishingSet =
unique_ptr<specificSet>(new specificSet);
for (unsigned long i=0 ; i<vanishing_set_size; i++){
FieldElement root = generateRandom();
vanishingSet->addElement(root);
}
/**
* Construct neighbours
*/
AcspInstance::polynomialsVec neighborPolys;
for (unsigned int i=0; i<neighbours_amount; i++){
neighborPolys.push_back(generateRandomPoly(maxDegree));
}
/**
* Similarly to the completeness check,
* we generate P as a product of univariate polynomials
* defined by their roots.
* In this case first of all roots are selected randomly, but a
* special root (to be explained now).
* We choose some \f$ z\f$ from the vanishing set, and for each
* \f$ q_i(x)\f$ we set \f$A(N_i(z))\f$ to be a root.
* Now we know that for each index \f$ i\f$, \f$ z\f$ is a root
* of \f$ q_i(A(N_i(x)))\f$. This tells us that \f$z\f$ is not
* a root of \f$ q_i(A(N_i(x))) + 1\f$ for any index \f$i\f$.
* We define \f$ P(x) = \displaystyle{\prod_i (q_i(x) + 1)}\f$,
* by this we make sure \f$ z\f$ is not a root of \f$ P\circ A\circ\vec{N}\f$,
* hence the witness does not satisfy the instance.
*/
/** Generate roots sets vector */
elementsSet_t cloningSource;
vector< elementsSet_t > rootsSets(neighbours_amount,cloningSource);
for (vector<elementsSet_t>::iterator roots=rootsSets.begin();roots!=rootsSets.end();roots++ ) {
for (unsigned int i=0; i<maxDegree; i++){
FieldElement root = generateRandom();
roots->insert(root);
}
}
/** choosing the specific element \f$ z\f$ and the needed root to each polynomial */
FieldElement z = *(vanishingSet->getElements().begin());//There must be at least one element
for(unsigned int i=0; i< neighbours_amount; i++){
FieldElement neighb_eval = neighborPolys[i]->eval(z);
FieldElement root = witness.assignmentPoly().eval(neighb_eval);
rootsSets[i].insert(root);
}
/** construct P */
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(UnivariatePolynomialGeneral(one())));
UnivariatePolynomialGeneral one_P( one());
for (vector<elementsSet_t>::iterator roots=rootsSets.begin();roots!=rootsSets.end();roots++ ) {
UnivariatePolynomialGeneral q(*roots);
q.add(one_P);
lightCircPoly qq(q);
ConstraintsPolynomial->multiplyDistinct(qq);
}
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a random pair of AcspInstance and AcspWitness
* such that the witness satisfies the instance for witness degree validation check,
* meaning deg A <= AcspInstance.witnessDegreeBound
* @return The random pair
*/
static pair<AcspInstance,AcspWitness> witness_degree_generate_valid_pair(){
unsigned int witnessDegree = rand() % 100;
unsigned int vanishingSetSize = witnessDegree +1 + rand()%100;
/**
* Construct witness
*/
AcspWitness witness(generateRandomPoly(witnessDegree));
/**
* Construct instance
*/
/**
* Construct neighbours (we don't care about it, empty)
*/
AcspInstance::polynomialsVec neighborPolys;
/**
* Construct constraints polynomial (we don't care about it, this is the univariate identity polynomial)
*/
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(UnivariatePolynomialGeneral(one())));
/**
* Construct vanishing set (empty, because we don't care about it)
*/
unique_ptr<specificSet> vanishingSet =
unique_ptr<specificSet>(new specificSet);
while (vanishingSet->size() < vanishingSetSize){
FieldElement root = generateRandom();
vanishingSet->addElement(root);
}
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(),AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a random pair of AcspInstance and AcspWitness
* such that the witness does not satisfy the instance for
* witness degree validation check,
* meaning deg A > AcspInstance.witnessDegreeBound
* @return The random pair
*/
static pair<AcspInstance,AcspWitness> witness_degree_generate_invalid_pair(){
unsigned int witnessDegree = 1 + rand() % 10;
unsigned int vanishingSetSize = rand()%witnessDegree;
/**
* Construct witness
*/
AcspWitness witness(generateRandomPoly(witnessDegree));
/**
* Construct instance
*/
/**
* Construct neighbours (we don't care about it, empty)
*/
AcspInstance::polynomialsVec neighborPolys;
/**
* Construct constraints polynomial (we don't care about it, this is the univariate identity polynomial)
*/
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(UnivariatePolynomialGeneral(one())));
/**
* Construct vanishing set
*/
unique_ptr<specificSet> vanishingSet =
unique_ptr<specificSet>(new specificSet);
while (vanishingSet->size() < vanishingSetSize){
FieldElement root = generateRandom();
vanishingSet->addElement(root);
}
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),PolynomialDegree(PolynomialDegree::integral_t(witness.assignmentPoly().getDegree())-1), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a random pair of AcspInstance and AcspWitness
* such that the witness satisfies the instance for boundary constraints validation check
* @return The random pair
*/
static pair<AcspInstance,AcspWitness> boundary_generate_valid_pair(){
int inputLen = rand() % 100;
/**
* Construct witness
*/
AcspWitness witness(generateRandomPoly(0));
/**
* Construct instance
*/
/**
* Construct neighbours (we don't care about it, empty)
*/
AcspInstance::polynomialsVec neighborPolys;
/**
* Construct constraints polynomial (we don't care about it, this is the univariate identity polynomial)
*/
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(UnivariatePolynomialGeneral(one())));
/**
* Construct vanishing set (empty, because we don't care about it)
*/
unique_ptr<specificSet> vanishingSet =
unique_ptr<specificSet>(new specificSet);
/** Construct boundary constraints parameters */
AcspInstance::boundaryConstraints_t boundaryConstraints;
for(int i=0; i<inputLen; i++){
const FieldElement x = generateRandom();
boundaryConstraints[x] = witness.assignmentPoly().eval(x);
}
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(),boundaryConstraints);
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a pair (AcspInstance,AcspWitness)
* that does not satisfy the boundary constraints
* @return The pair
*/
static pair<AcspInstance,AcspWitness> boundary_generate_invalid_pair(){
unsigned int expectedInputLen = 1 + rand() % 100;
/**
* Construct witness
*/
AcspWitness witness(generateRandomPoly(0));
/**
* Construct instance
*/
/**
* Construct neighbours (we don't care about it, empty)
*/
AcspInstance::polynomialsVec neighborPolys;
/**
* Construct constraints polynomial (we don't care about it, this is the univariate identity polynomial)
*/
unique_ptr<lightCircPoly> ConstraintsPolynomial (new lightCircPoly(UnivariatePolynomialGeneral(one())));
/**
* Construct vanishing set (empty, because we don't care about it)
*/
unique_ptr<specificSet> vanishingSet =
unique_ptr<specificSet>(new specificSet);
/** Construct boundary constraints parameters */
AcspInstance::boundaryConstraints_t boundaryConstraints;
for(unsigned int i=0; i<expectedInputLen; i++){
const FieldElement x = generateRandom();
const FieldElement y = witness.assignmentPoly().eval(x);
boundaryConstraints[x] = y + one();
}
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(),boundaryConstraints);
/** Return Acsp pair */
pair<AcspInstance,AcspWitness> AcspPair = move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
return move(AcspPair);
}
/**
* @brief Generates a valid pair (AcspInstance,AcspWitness)
* such a pair can be proven and verified using PCP
* First the field and the vanishing set (which is an affine subspace) is defined
* We define the polynomial vanishing on the set to be \f$Z(x)\f$
* the witness is defined to be the polynomial \f$ \frac{Z(x)}{x}\f$, the constraint poly is \f$P(x,y) = x \cdot y \f$
* and \f$ \vec{N} = (x) \f$
* no input is given
* @return The pair
*/
static pair<AcspInstance,AcspWitness> generate_valid_pair_most_in_witness(){
const size_t vanishingSpaceDegree = 3;
/**
* Construct vanishing set - affine
*/
unique_ptr<LinearSpace> vanishingSet(new LinearSpace(Algebra::getStandartOrderedBasis(vanishingSpaceDegree)));
/**
* Construct neighbours
*/
AcspInstance::polynomialsVec neighborPolys;
UnivariatePolynomialGeneral* idPoly = new UnivariatePolynomialGeneral;
idPoly->setCoefficient(1, one());
neighborPolys.push_back(unique_ptr<UnivariatePolynomialInterface>(idPoly));
/**
* Construct constraints polynomial
*/
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(*idPoly));
ConstraintsPolynomial->multiplyDistinct(lightCircPoly(*idPoly));
/**
* Construct witness
*/
const UnivariatePolynomialInterface& vanishingPoly = vanishingSet->vanishingPoly();
unique_ptr<UnivariatePolynomialInterface> A(idPoly->divideByMe(vanishingPoly));
AcspWitness witness(move(A));
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
delete idPoly;
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a valid pair (AcspInstance,AcspWitness)
* such a pair can be proven and verified using PCP
* First the field and the vanishing set (which is an affine subspace) is defined
* We define the polynomial vanishing on the set to be \f$Z(x)\f$
* the witness is defined to be the identity polynomial \f$x\f$, \f$P(x) = Z(x) \f$
* and \f$ \vec{N} = () \f$
* no input is given
* @return The pair
*/
static pair<AcspInstance,AcspWitness> generate_valid_pair_all_in_constraintsPoly(){
const size_t vanishingSpaceDegree = 3;
/**
* Construct vanishing set - affine
*/
const vector<FieldElement> vanishingSpaceBasis = getStandartOrderedBasis(vanishingSpaceDegree);
unique_ptr<LinearSpace> vanishingSet(new LinearSpace(vanishingSpaceBasis));
/**
* Construct neighbours
*/
AcspInstance::polynomialsVec neighborPolys;
/**
* Construct constraints polynomial
*/
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(vanishingSet->vanishingPoly()));
/**
* Construct witness
*/
unique_ptr<UnivariatePolynomialGeneral> idPoly(new UnivariatePolynomialGeneral);
idPoly->setCoefficient(1, one());
AcspWitness witness(move(idPoly));
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
/**
* @brief Generates a valid pair (AcspInstance,AcspWitness)
* such a pair can be proven and verified using PCP
* First the field and the vanishing set (which is an affine subspace) is defined
* We define the polynomial vanishing on the set to be \f$Z(x)\f$
* the witness is defined to be the identity polynomial \f$x\f$, \f$P(x,y) = y \f$
* and \f$ \vec{N} = (Z(x)) \f$
* no input is given
* @return The pair
*/
static pair<AcspInstance,AcspWitness> generate_valid_pair_all_in_neighbor(){
const size_t vanishingSpaceDegree = 3;
/**
* Construct vanishing set - affine
*/
const vector<FieldElement> vanishingSpaceBasis = getStandartOrderedBasis(vanishingSpaceDegree);
unique_ptr<LinearSpace> vanishingSet(new LinearSpace(vanishingSpaceBasis));
/**
* Construct neighbours
*/
AcspInstance::polynomialsVec neighborPolys;
neighborPolys.push_back(unique_ptr<UnivariatePolynomialInterface>(new UnivariatePolynomialGeneral(vanishingSet->vanishingPoly())));
/**
* Construct constraints polynomial
*/
UnivariatePolynomialGeneral* idPoly = new UnivariatePolynomialGeneral;
idPoly->setCoefficient(1, one());
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(UnivariatePolynomialGeneral(zero())));
ConstraintsPolynomial->addDistinct(lightCircPoly(*idPoly));
/**
* Construct witness
*/
AcspWitness witness(move(unique_ptr<UnivariatePolynomialGeneral>(idPoly)));
/** Construct the instance data */
AcspInstance instance(move(vanishingSet),move(neighborPolys),move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance,AcspWitness>(move(instance),move(witness)));
}
//added by Ariel
//In this test the constraint polynomial is x+y. There is one neighbour that's the identity, and the constraint poly is Z_H, i.e.,
//the vanishing space subspace poly. The witness poly is the identity x
static pair<AcspInstance, AcspWitness> generate_valid_pair_all_in_constraintsPoly2(){
const size_t vanishingSpaceDegree = 3;
/**
* Construct vanishing set - affine
*/
const vector<FieldElement> vanishingSpaceBasis = getStandartOrderedBasis(vanishingSpaceDegree);
unique_ptr<LinearSpace> vanishingSet(new LinearSpace(vanishingSpaceBasis));
/**
* Construct neighbours
*/
unique_ptr<UnivariatePolynomialGeneral> idPoly(new UnivariatePolynomialGeneral);
idPoly->setCoefficient(1, one());
AcspInstance::polynomialsVec neighborPolys;
neighborPolys.push_back(move(idPoly));
/**
* Construct constraints polynomial Z_H + Z_H (the add distinct method creates a new variable)
*/
idPoly = unique_ptr<UnivariatePolynomialGeneral>(new UnivariatePolynomialGeneral());
idPoly->setCoefficient(1, one());
unique_ptr<lightCircPoly> ConstraintsPolynomial(new lightCircPoly(vanishingSet->vanishingPoly()));
ConstraintsPolynomial->addDistinct(lightCircPoly(vanishingSet->vanishingPoly()));
/**
* Construct witness
*/
idPoly = unique_ptr<UnivariatePolynomialGeneral>(new UnivariatePolynomialGeneral());
idPoly->setCoefficient(1, one());
AcspWitness witness(move(idPoly));
/** Construct the instance data */
AcspInstance instance(move(vanishingSet), move(neighborPolys), move(ConstraintsPolynomial),witness.assignmentPoly().getDegree(), AcspInstance::boundaryConstraints_t());
/** Return Acsp pair */
return move(pair<AcspInstance, AcspWitness>(move(instance), move(witness)));
}
} //namespace Acsp_UTEST
/**
* @brief GTEST function to test completeness of AcspWitnessChecker::verify for a full witness
* such that the witness is the identity polynomial, the constraint polynomial is P(x,y)=y
* and the single neighbor is the vanishing polynomial
*/
TEST(AcspWitnessChecker,verify_all_in_neighbor){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::generate_valid_pair_all_in_neighbor();
EXPECT_TRUE(AcspWitnessChecker::verify(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test completeness of AcspWitnessChecker::verify for a full witness
* such that the witness is the identity polynomial, the constraint polynomial is the vanishing univariate polynomial,
* and there are no neighbors
*/
TEST(AcspWitnessChecker,verify_all_in_constraintsPoly){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::generate_valid_pair_all_in_constraintsPoly();
EXPECT_TRUE(AcspWitnessChecker::verify(validPair.first,validPair.second));
}
/** added by ARIEL
* @brief GTEST function to test completeness of AcspWitnessChecker::verify for a full witness
* such that the witness is the identity polynomial, the constraint polynomial is Z_H(x)+Z_H(y) where H is the vanishing space,
* there is one neighbor which is the identity
*/
TEST(AcspWitnessChecker, verify_all_in_constraintsPoly2){
pair<AcspInstance, AcspWitness> validPair = Acsp_UTEST::generate_valid_pair_all_in_constraintsPoly2();
EXPECT_TRUE(AcspWitnessChecker::verify(validPair.first, validPair.second));
}
/**
* @brief GTEST function to test completeness of AcspWitnessChecker::verify for a full witness
* such that the witness is the vanishing polynomial and the instance is trivial
*/
TEST(AcspWitnessChecker,DISABLED_verify_most_in_witness){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::generate_valid_pair_most_in_witness();
EXPECT_TRUE(AcspWitnessChecker::verify(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test completeness of AcspWitnessChecker::verify_vanishing
*/
TEST(AcspWitnessChecker,verify_vanishing_completness){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::vanishing_generate_valid_pair();
EXPECT_TRUE(AcspWitnessChecker::verify_vanishing(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test soundness of AcspWitnessChecker::verify_vanishing
*/
TEST(AcspWitnessChecker,verify_vanishing_soundness){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::vanishing_generate_invalid_pair();
EXPECT_FALSE(AcspWitnessChecker::verify_vanishing(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test completeness of AcspWitnessChecker::verify_witness_degree
*/
TEST(AcspWitnessChecker,verify_witness_degree_completness){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::witness_degree_generate_valid_pair();
EXPECT_TRUE(AcspWitnessChecker::verify_witness_degree(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test soundness of AcspWitnessChecker::verify_witness_degree
*/
TEST(AcspWitnessChecker,verify_witness_degree_soundness){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::witness_degree_generate_invalid_pair();
EXPECT_FALSE(AcspWitnessChecker::verify_witness_degree(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test completeness of AcspWitnessChecker::verify_boundary
*/
TEST(AcspWitnessChecker,verify_boundary_complitness){
pair<AcspInstance,AcspWitness> validPair = Acsp_UTEST::boundary_generate_valid_pair();
EXPECT_TRUE(AcspWitnessChecker::verify_boundary(validPair.first,validPair.second));
}
/**
* @brief GTEST function to test soundness of AcspWitnessChecker::verify_boundary
*/
TEST(AcspWitnessChecker,verify_boundary_soundness){
pair<AcspInstance,AcspWitness> AcspPair = Acsp_UTEST::boundary_generate_invalid_pair();
EXPECT_FALSE(AcspWitnessChecker::verify_boundary(AcspPair.first, AcspPair.second));
}
}