Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
phase2.hpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 //
3 // SPDX-License-Identifier: LGPL-3.0+
4 
5 #ifndef __ZETH_MPC_GROTH16_PHASE2_HPP__
6 #define __ZETH_MPC_GROTH16_PHASE2_HPP__
7 
10 
11 // Structures and operations related to the "Phase 2" MPC described in
12 // [BoweGM17]. Parts of the implementation use techniques from the
13 // "Phase2" library from "zk-SNARK MPCs, made easy".
14 //
15 // References:
16 //
17 // \[BoweGM17]
18 // "Scalable Multi-party Computation for zk-SNARK Parameters in the Random
19 // Beacon Model"
20 // Sean Bowe and Ariel Gabizon and Ian Miers,
21 // IACR Cryptology ePrint Archive 2017,
22 // <http://eprint.iacr.org/2017/1050>
23 //
24 // "Phase2" (From "zk-SNARK MPCs, made easy" library
25 // https://github.com/ebfull/phase2)
26 //
27 // "Sapling MPC" ("Multi-party computation for Zcash's Sapling zk-SNARK public
28 // parameters" https://github.com/zcash-hackworks/sapling-mpc)
29 
30 namespace libzeth
31 {
32 
33 template<typename ppT> class srs_powersoftau;
34 template<typename ppT> class srs_mpc_layer_L1;
35 
41 template<typename ppT> class srs_mpc_phase2_accumulator
42 {
43 public:
44  // Hash of the initial state before any contributions are made. Kept
45  // constant over the MPC, and used to check that challenges and responses
46  // are part of the same MPC.
48 
49  libff::G1<ppT> delta_g1;
50 
51  libff::G2<ppT> delta_g2;
52 
53  // { H_i } = { [ t(x) . x^i / delta ]_1 } i \in [0..n-2] (n-1 entries)
54  libff::G1_vector<ppT> H_g1;
55 
56  // { L_j } = { [ ABC_j / delta ]_1 } j \in [num_inputs + 1..num_variables]
57  libff::G1_vector<ppT> L_g1;
58 
60  const mpc_hash_t cs_hash,
61  const libff::G1<ppT> &delta_g1,
62  const libff::G2<ppT> &delta_g2,
63  libff::G1_vector<ppT> &&H_g1,
64  libff::G1_vector<ppT> &&L_g1);
65 
66  bool operator==(const srs_mpc_phase2_accumulator<ppT> &other) const;
67  bool is_well_formed() const;
68  void write(std::ostream &out) const;
69  void write_compressed(std::ostream &out) const;
70  static srs_mpc_phase2_accumulator<ppT> read(std::istream &in);
71  static srs_mpc_phase2_accumulator<ppT> read_compressed(std::istream &in);
72 };
73 
85 template<typename ppT> class srs_mpc_phase2_publickey
86 {
87 public:
89  libff::G1<ppT> new_delta_g1;
90  libff::G1<ppT> s_g1;
91  libff::G1<ppT> s_delta_j_g1;
92  libff::G2<ppT> r_delta_j_g2;
93 
96  const libff::G1<ppT> &new_delta_g1,
97  const libff::G1<ppT> &s_g1,
98  const libff::G1<ppT> &s_delta_j_g1,
99  const libff::G2<ppT> &r_delta_j_g2);
100 
101  bool operator==(const srs_mpc_phase2_publickey<ppT> &other) const;
102  bool is_well_formed() const;
103  void write(std::ostream &out) const;
104  static srs_mpc_phase2_publickey<ppT> read(std::istream &in);
105  void compute_digest(mpc_hash_t out_digest) const;
106 };
107 
110 template<typename ppT> class srs_mpc_phase2_challenge
111 {
112 public:
115 
119 
120  bool operator==(const srs_mpc_phase2_challenge<ppT> &other) const;
121  bool is_well_formed() const;
122  void write(std::ostream &out) const;
123  static srs_mpc_phase2_challenge<ppT> read(std::istream &in);
124 };
125 
128 template<typename ppT> class srs_mpc_phase2_response
129 {
130 public:
133 
137 
138  bool operator==(const srs_mpc_phase2_response<ppT> &other) const;
139  bool is_well_formed() const;
140  void write(std::ostream &out) const;
141  static srs_mpc_phase2_response<ppT> read(std::istream &in);
142 };
143 
144 // Phase2 functions
145 
146 template<mp_size_t n, const libff::bigint<n> &modulus>
148  const mpc_hash_t transcript_digest, libff::Fp_model<n, modulus> &out_fr);
149 
150 template<typename ppT>
151 libff::G2<ppT> srs_mpc_digest_to_g2(const mpc_hash_t digest);
152 
156 template<typename ppT>
158  const mpc_hash_t cs_hash,
159  const srs_mpc_layer_L1<ppT> &layer_L1,
160  size_t num_inputs);
161 
164 template<typename ppT>
166  const mpc_hash_t transcript_digest,
167  const libff::G1<ppT> &last_delta,
168  const libff::Fr<ppT> &secret);
169 
174 template<typename ppT>
176  const libff::G1<ppT> last_delta_g1,
177  const srs_mpc_phase2_publickey<ppT> &publickey);
178 
182 template<typename ppT>
184  const srs_mpc_phase2_accumulator<ppT> &last_accum,
185  const libff::Fr<ppT> &delta_j);
186 
195 template<typename ppT>
198  const srs_mpc_phase2_accumulator<ppT> &updated);
199 
206 template<typename ppT>
209  const srs_mpc_phase2_accumulator<ppT> &updated,
210  const srs_mpc_phase2_publickey<ppT> &publickey);
211 
214 template<typename ppT>
216  srs_mpc_phase2_accumulator<ppT> &&initial_accumulator);
217 
222 template<typename ppT>
224  const srs_mpc_phase2_challenge<ppT> &challenge,
225  const libff::Fr<ppT> &delta_j);
226 
230 template<typename ppT>
232  const srs_mpc_phase2_challenge<ppT> &challenge,
233  const srs_mpc_phase2_response<ppT> &response);
234 
239 template<typename ppT>
241  srs_mpc_phase2_response<ppT> &&response);
242 
258 template<typename ppT, bool enable_contribution_check = true>
260  const mpc_hash_t initial_transcript_digest,
261  const libff::G1<ppT> &initial_delta,
262  const mpc_hash_t check_for_contribution,
263  std::istream &transcript_stream,
264  libff::G1<ppT> &out_final_delta,
265  mpc_hash_t out_final_transcript_digest,
266  bool &out_contribution_found);
267 
270 template<typename ppT>
272  const mpc_hash_t initial_transcript_digest,
273  const libff::G1<ppT> &initial_delta,
274  std::istream &transcript_stream,
275  libff::G1<ppT> &out_final_delta,
276  mpc_hash_t out_final_transcript_digest);
277 
282 template<typename ppT>
284  const srs_mpc_layer_L1<ppT> &layer1,
285  const libff::Fr<ppT> &delta,
286  size_t num_inputs);
287 
294 template<
295  typename ppT,
296  libff::multi_exp_base_form BaseForm = libff::multi_exp_base_form_normal>
297 libsnark::r1cs_gg_ppzksnark_keypair<ppT> mpc_create_key_pair(
298  srs_powersoftau<ppT> &&pot,
299  srs_mpc_layer_L1<ppT> &&layer1,
301  libsnark::r1cs_constraint_system<libff::Fr<ppT>> &&cs,
302  const libsnark::qap_instance<libff::Fr<ppT>> &qap);
303 
304 } // namespace libzeth
305 
307 
308 #endif // __ZETH_MPC_GROTH16_PHASE2_HPP__
libzeth::srs_mpc_phase2_accumulator::delta_g2
libff::G2< ppT > delta_g2
Definition: phase2.hpp:51
libzeth::srs_mpc_phase2_begin
srs_mpc_phase2_accumulator< ppT > srs_mpc_phase2_begin(const mpc_hash_t cs_hash, const srs_mpc_layer_L1< ppT > &layer_L1, size_t num_inputs)
libzeth::srs_mpc_phase2_response::read
static srs_mpc_phase2_response< ppT > read(std::istream &in)
libzeth::srs_mpc_phase2_response::operator==
bool operator==(const srs_mpc_phase2_response< ppT > &other) const
groth16_snark.hpp
libzeth::srs_mpc_phase2_response::write
void write(std::ostream &out) const
libzeth::srs_mpc_phase2_compute_public_key
srs_mpc_phase2_publickey< ppT > srs_mpc_phase2_compute_public_key(const mpc_hash_t transcript_digest, const libff::G1< ppT > &last_delta, const libff::Fr< ppT > &secret)
libzeth::srs_mpc_phase2_accumulator::write_compressed
void write_compressed(std::ostream &out) const
mpc_hash.hpp
libzeth::srs_mpc_phase2_verify_publickey
bool srs_mpc_phase2_verify_publickey(const libff::G1< ppT > last_delta_g1, const srs_mpc_phase2_publickey< ppT > &publickey)
libzeth::srs_mpc_phase2_challenge::accumulator
srs_mpc_phase2_accumulator< ppT > accumulator
Definition: phase2.hpp:114
libzeth::mpc_create_key_pair
libsnark::r1cs_gg_ppzksnark_keypair< ppT > mpc_create_key_pair(srs_powersoftau< ppT > &&pot, srs_mpc_layer_L1< ppT > &&layer1, srs_mpc_phase2_accumulator< ppT > &&layer2, libsnark::r1cs_constraint_system< libff::Fr< ppT >> &&cs, const libsnark::qap_instance< libff::Fr< ppT >> &qap)
libzeth::srs_mpc_phase2_accumulator::H_g1
libff::G1_vector< ppT > H_g1
Definition: phase2.hpp:54
libzeth::srs_mpc_phase2_accumulator::write
void write(std::ostream &out) const
libzeth::srs_mpc_phase2_verify_transcript
bool srs_mpc_phase2_verify_transcript(const mpc_hash_t initial_transcript_digest, const libff::G1< ppT > &initial_delta, const mpc_hash_t check_for_contribution, std::istream &transcript_stream, libff::G1< ppT > &out_final_delta, mpc_hash_t out_final_transcript_digest, bool &out_contribution_found)
libzeth
Definition: binary_operation.hpp:15
libzeth::srs_mpc_phase2_publickey::s_delta_j_g1
libff::G1< ppT > s_delta_j_g1
Definition: phase2.hpp:91
libzeth::srs_mpc_phase2_challenge
Definition: phase2.hpp:110
libzeth::srs_mpc_phase2_accumulator::L_g1
libff::G1_vector< ppT > L_g1
Definition: phase2.hpp:57
libzeth::srs_mpc_phase2_accumulator::is_well_formed
bool is_well_formed() const
libzeth::srs_mpc_digest_to_fp
void srs_mpc_digest_to_fp(const mpc_hash_t transcript_digest, libff::Fp_model< n, modulus > &out_fr)
libzeth::srs_mpc_phase2_publickey::transcript_digest
mpc_hash_t transcript_digest
Definition: phase2.hpp:88
libzeth::srs_mpc_phase2_accumulator::delta_g1
libff::G1< ppT > delta_g1
Definition: phase2.hpp:49
libzeth::srs_mpc_phase2_publickey::is_well_formed
bool is_well_formed() const
libzeth::srs_mpc_layer_L1
Definition: mpc_utils.hpp:37
libzeth::srs_mpc_digest_to_g2
libff::G2< ppT > srs_mpc_digest_to_g2(const mpc_hash_t digest)
libzeth::srs_mpc_phase2_update_accumulator
srs_mpc_phase2_accumulator< ppT > srs_mpc_phase2_update_accumulator(const srs_mpc_phase2_accumulator< ppT > &last_accum, const libff::Fr< ppT > &delta_j)
libzeth::srs_mpc_phase2_challenge::read
static srs_mpc_phase2_challenge< ppT > read(std::istream &in)
libzeth::srs_mpc_phase2_accumulator::srs_mpc_phase2_accumulator
srs_mpc_phase2_accumulator(const mpc_hash_t cs_hash, const libff::G1< ppT > &delta_g1, const libff::G2< ppT > &delta_g2, libff::G1_vector< ppT > &&H_g1, libff::G1_vector< ppT > &&L_g1)
libzeth::srs_mpc_phase2_accumulator::cs_hash
mpc_hash_t cs_hash
Definition: phase2.hpp:47
libzeth::srs_mpc_phase2_response::is_well_formed
bool is_well_formed() const
libzeth::srs_mpc_phase2_accumulator::read
static srs_mpc_phase2_accumulator< ppT > read(std::istream &in)
libzeth::srs_mpc_phase2_accumulator::read_compressed
static srs_mpc_phase2_accumulator< ppT > read_compressed(std::istream &in)
libzeth::srs_mpc_phase2_publickey::operator==
bool operator==(const srs_mpc_phase2_publickey< ppT > &other) const
libzeth::srs_mpc_phase2_verify_update
bool srs_mpc_phase2_verify_update(const srs_mpc_phase2_accumulator< ppT > &last, const srs_mpc_phase2_accumulator< ppT > &updated, const srs_mpc_phase2_publickey< ppT > &publickey)
libzeth::srs_mpc_phase2_challenge::is_well_formed
bool is_well_formed() const
libzeth::srs_mpc_phase2_accumulator::operator==
bool operator==(const srs_mpc_phase2_accumulator< ppT > &other) const
libzeth::mpc_hash_t
size_t[MPC_HASH_ARRAY_LENGTH] mpc_hash_t
Definition: mpc_hash.hpp:21
libzeth::srs_mpc_phase2_compute_response
srs_mpc_phase2_response< ppT > srs_mpc_phase2_compute_response(const srs_mpc_phase2_challenge< ppT > &challenge, const libff::Fr< ppT > &delta_j)
libzeth::srs_mpc_phase2_verify_response
bool srs_mpc_phase2_verify_response(const srs_mpc_phase2_challenge< ppT > &challenge, const srs_mpc_phase2_response< ppT > &response)
libzeth::srs_powersoftau
Definition: mpc_utils.hpp:31
libzeth::srs_mpc_phase2_publickey::s_g1
libff::G1< ppT > s_g1
Definition: phase2.hpp:90
libzeth::srs_mpc_phase2_publickey::srs_mpc_phase2_publickey
srs_mpc_phase2_publickey(const mpc_hash_t transcript_digest, const libff::G1< ppT > &new_delta_g1, const libff::G1< ppT > &s_g1, const libff::G1< ppT > &s_delta_j_g1, const libff::G2< ppT > &r_delta_j_g2)
libzeth::srs_mpc_phase2_publickey
Definition: phase2.hpp:85
libzeth::srs_mpc_phase2_publickey::read
static srs_mpc_phase2_publickey< ppT > read(std::istream &in)
libzeth::srs_mpc_phase2_publickey::write
void write(std::ostream &out) const
libzeth::srs_mpc_phase2_response
Definition: phase2.hpp:128
libzeth::srs_mpc_phase2_response::new_accumulator
srs_mpc_phase2_accumulator< ppT > new_accumulator
Definition: phase2.hpp:131
libzeth::srs_mpc_phase2_accumulator
Definition: phase2.hpp:41
libzeth::srs_mpc_phase2_challenge::transcript_digest
mpc_hash_t transcript_digest
Definition: phase2.hpp:113
libzeth::srs_mpc_phase2_initial_challenge
srs_mpc_phase2_challenge< ppT > srs_mpc_phase2_initial_challenge(srs_mpc_phase2_accumulator< ppT > &&initial_accumulator)
libzeth::srs_mpc_phase2_compute_challenge
srs_mpc_phase2_challenge< ppT > srs_mpc_phase2_compute_challenge(srs_mpc_phase2_response< ppT > &&response)
phase2.tcc
libzeth::srs_mpc_phase2_response::publickey
srs_mpc_phase2_publickey< ppT > publickey
Definition: phase2.hpp:132
libzeth::srs_mpc_dummy_phase2
srs_mpc_phase2_challenge< ppT > srs_mpc_dummy_phase2(const srs_mpc_layer_L1< ppT > &layer1, const libff::Fr< ppT > &delta, size_t num_inputs)
libzeth::srs_mpc_phase2_publickey::new_delta_g1
libff::G1< ppT > new_delta_g1
Definition: phase2.hpp:89
libzeth::srs_mpc_phase2_response::srs_mpc_phase2_response
srs_mpc_phase2_response(srs_mpc_phase2_accumulator< ppT > &&new_accumulator, srs_mpc_phase2_publickey< ppT > &&publickey)
libzeth::srs_mpc_phase2_challenge::srs_mpc_phase2_challenge
srs_mpc_phase2_challenge(const mpc_hash_t transcript_digest, srs_mpc_phase2_accumulator< ppT > &&accumulator)
libzeth::srs_mpc_phase2_challenge::write
void write(std::ostream &out) const
libzeth::srs_mpc_phase2_update_is_consistent
bool srs_mpc_phase2_update_is_consistent(const srs_mpc_phase2_accumulator< ppT > &last, const srs_mpc_phase2_accumulator< ppT > &updated)
libzeth::srs_mpc_phase2_publickey::r_delta_j_g2
libff::G2< ppT > r_delta_j_g2
Definition: phase2.hpp:92
libzeth::srs_mpc_phase2_challenge::operator==
bool operator==(const srs_mpc_phase2_challenge< ppT > &other) const
libzeth::srs_mpc_phase2_publickey::compute_digest
void compute_digest(mpc_hash_t out_digest) const