Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
kzg10_verifier_gadget.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3  * @author This file is part of libff, developed by Clearmatics Ltd
4  * (originally developed by SCIPR Lab) and contributors
5  * (see AUTHORS).
6  * @copyright MIT license (see LICENSE file)
7  *****************************************************************************/
8 
9 #ifndef LIBSNARK_GADGETLIB1_GADGETS_VERIFIERS_KZG10_VERIFIER_GADGET_TCC_
10 #define LIBSNARK_GADGETLIB1_GADGETS_VERIFIERS_KZG10_VERIFIER_GADGET_TCC_
11 
12 #include "libsnark/gadgetlib1/gadgets/verifiers/kzg10_verifier_gadget.hpp"
13 
14 namespace libsnark
15 {
16 
17 template<typename ppT>
18 kzg10_srs_variable<ppT>::kzg10_srs_variable(
19  protoboard<libff::Fr<ppT>> &pb,
20  const size_t max_degree,
21  const std::string &annotation_prefix)
22  : alpha_g2(pb, FMT(annotation_prefix, " alpha_g2"))
23 
24 {
25  alpha_powers_g1.reserve(max_degree + 1);
26  for (size_t i = 0; i < max_degree + 1; ++i) {
27  alpha_powers_g1.emplace_back(
28  pb, FMT(annotation_prefix, " alpha_powers_g1[%zu]", i));
29  }
30 }
31 
32 template<typename ppT>
33 void kzg10_srs_variable<ppT>::generate_r1cs_witness(
34  const typename kzg10<npp>::srs &srs)
35 {
36  assert(srs.alpha_powers_g1.size() == alpha_powers_g1.size());
37 
38  for (size_t i = 0; i < srs.alpha_powers_g1.size(); ++i) {
39  alpha_powers_g1[i].generate_r1cs_witness(srs.alpha_powers_g1[i]);
40  }
41  alpha_g2.generate_r1cs_witness(srs.alpha_g2);
42 }
43 
44 template<typename ppT>
45 kzg10_verifier_gadget<ppT>::kzg10_verifier_gadget(
46  protoboard<libff::Fr<ppT>> &pb,
47  const kzg10_srs_variable<ppT> &srs,
48  const kzg10_commitment_variable<ppT> &commitment,
49  pb_linear_combination<libff::Fr<ppT>> i,
50  pb_linear_combination<libff::Fr<ppT>> poly_eval,
51  const kzg10_witness_variable<ppT> &witness,
52  pb_variable<libff::Fr<ppT>> result,
53  const std::string &annotation_prefix)
54  : gadget<libff::Fr<ppT>>(pb, annotation_prefix)
55 
56  , i_in_G2(pb, FMT(annotation_prefix, " i_in_G2"))
57  , compute_i_in_G2(
58  pb,
59  i,
60  G2_variable<ppT>(
61  pb,
62  libff::G2<other_curve<ppT>>::one(),
63  FMT(annotation_prefix, " one_G2")),
64  i_in_G2,
65  FMT(annotation_prefix, " compute_i_in_G2"))
66 
67  , B(pb, FMT(annotation_prefix, " B"))
68  , compute_B(
69  pb,
70  srs.alpha_g2,
71  -i_in_G2.value,
72  B,
73  FMT(annotation_prefix, " compute_B"))
74 
75  , poly_eval_in_G1(pb, FMT(annotation_prefix, " poly_eval_in_G1"))
76  , compute_poly_eval_in_G1(
77  pb,
78  poly_eval,
79  G1_variable<ppT>(
80  pb,
81  libff::G1<other_curve<ppT>>::one(),
82  FMT(annotation_prefix, " one_G1")),
83  poly_eval_in_G1,
84  FMT(annotation_prefix, " compute_poly_eval_in_G1"))
85 
86  , C(pb, FMT(annotation_prefix, " C"))
87  , compute_C(
88  pb,
89  commitment,
90  -poly_eval_in_G1.value,
91  C,
92  FMT(annotation_prefix, " compute_C"))
93 
94  , A_precomp()
95  , compute_A_precomp(
96  pb, witness, A_precomp, FMT(annotation_prefix, " compute_A_precomp"))
97  , B_precomp()
98  , compute_B_precomp(
99  pb, B, B_precomp, FMT(annotation_prefix, " compute_B_precomp"))
100  , C_precomp()
101  , compute_C_precomp(
102  pb, C, C_precomp, FMT(annotation_prefix, " compute_C_precomp"))
103  , D_precomp(
104  pb,
105  libff::G2<other_curve<ppT>>::one(),
106  FMT(annotation_prefix, " D_precomp"))
107 
108  , check_result(pb_variable_allocate<FieldT>(
109  pb, FMT(annotation_prefix, " check_result")))
110  , check_pairing_equality(
111  pb,
112  A_precomp,
113  B_precomp,
114  C_precomp,
115  D_precomp,
116  check_result,
117  FMT(annotation_prefix, " check_pairing_equality"))
118 
119  , group_elements_non_zero(pb_variable_allocate<FieldT>(
120  pb, FMT(annotation_prefix, " group_elements_non_zero")))
121  , result(result)
122 {
123 }
124 
125 template<typename ppT>
126 void kzg10_verifier_gadget<ppT>::generate_r1cs_constraints()
127 {
128  compute_i_in_G2.generate_r1cs_constraints();
129  compute_B.generate_r1cs_constraints();
130  compute_poly_eval_in_G1.generate_r1cs_constraints();
131  compute_C.generate_r1cs_constraints();
132  compute_A_precomp.generate_r1cs_constraints();
133  compute_B_precomp.generate_r1cs_constraints();
134  compute_C_precomp.generate_r1cs_constraints();
135  check_pairing_equality.generate_r1cs_constraints();
136 
137  // group_elements_non_zero =
138  // (1 - i_in_G2.is_identity) * (1 - poly_eval_in_G1.is_identity)
139  this->pb.add_r1cs_constraint(
140  r1cs_constraint<FieldT>(
141  FieldT::one() - i_in_G2.is_identity,
142  FieldT::one() - poly_eval_in_G1.is_identity,
143  group_elements_non_zero),
144  FMT(this->annotation_prefix, " compute_group_elements_non_zero"));
145 
146  // result = group_elements_non_zero * check_result
147  this->pb.add_r1cs_constraint(
148  r1cs_constraint<FieldT>(group_elements_non_zero, check_result, result),
149  FMT(this->annotation_prefix, " compute_result"));
150 }
151 
152 template<typename ppT> void kzg10_verifier_gadget<ppT>::generate_r1cs_witness()
153 {
154  compute_i_in_G2.generate_r1cs_witness();
155  // compute_B.B = -i_in_G2.value. Evaluate the result of the negation.
156  compute_B.B.Y->evaluate();
157  compute_B.generate_r1cs_witness();
158  compute_poly_eval_in_G1.generate_r1cs_witness();
159  // compute_C.B = -poly_eval_in_G1.value. Evaluate the result of negation.
160  compute_C.B.Y.evaluate(this->pb);
161  compute_C.generate_r1cs_witness();
162  compute_A_precomp.generate_r1cs_witness();
163  compute_B_precomp.generate_r1cs_witness();
164  compute_C_precomp.generate_r1cs_witness();
165  check_pairing_equality.generate_r1cs_witness();
166 
167  const FieldT group_elements_non_zero_val =
168  (FieldT::one() - this->pb.lc_val(i_in_G2.is_identity)) *
169  (FieldT::one() - this->pb.lc_val(poly_eval_in_G1.is_identity));
170  const FieldT result_val =
171  group_elements_non_zero_val * this->pb.val(check_result);
172 
173  this->pb.val(group_elements_non_zero) = group_elements_non_zero_val;
174  this->pb.val(result) = result_val;
175 }
176 
177 } // namespace libsnark
178 
179 #endif // LIBSNARK_GADGETLIB1_GADGETS_VERIFIERS_KZG10_VERIFIER_GADGET_TCC_