Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
proto_utils.tcc
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_SERIALIZATION_PROTO_UTILS_TCC__
6 #define __ZETH_SERIALIZATION_PROTO_UTILS_TCC__
7 
8 #include "libzeth/core/field_element_utils.hpp"
9 #include "libzeth/serialization/proto_utils.hpp"
10 
11 #include <cassert>
12 
13 namespace libzeth
14 {
15 
16 template<typename ppT>
17 zeth_proto::Group1Point point_g1_affine_to_proto(const libff::G1<ppT> &point)
18 {
19  assert(!point.is_zero());
20  libff::G1<ppT> aff = point;
21  aff.to_affine_coordinates();
22 
23  zeth_proto::Group1Point res;
24  res.set_x_coord(field_element_to_json(aff.X));
25  res.set_y_coord(field_element_to_json(aff.Y));
26  return res;
27 }
28 
29 template<typename ppT>
30 libff::G1<ppT> point_g1_affine_from_proto(const zeth_proto::Group1Point &point)
31 {
32  using Fq = libff::Fq<ppT>;
33  Fq x_coordinate = field_element_from_json<Fq>(point.x_coord());
34  Fq y_coordinate = field_element_from_json<Fq>(point.y_coord());
35  return libff::G1<ppT>(x_coordinate, y_coordinate, Fq::one());
36 }
37 
38 template<typename ppT>
39 zeth_proto::Group2Point point_g2_affine_to_proto(const libff::G2<ppT> &point)
40 {
41  assert(!point.is_zero());
42  libff::G2<ppT> aff = point;
43  aff.to_affine_coordinates();
44 
45  zeth_proto::Group2Point res;
46  res.set_x_coord(field_element_to_json(aff.X));
47  res.set_y_coord(field_element_to_json(aff.Y));
48  return res;
49 }
50 
51 template<typename ppT>
52 libff::G2<ppT> point_g2_affine_from_proto(const zeth_proto::Group2Point &point)
53 {
54  using TwistField = typename libff::G2<ppT>::twist_field;
55  const TwistField X = field_element_from_json<TwistField>(point.x_coord());
56  const TwistField Y = field_element_from_json<TwistField>(point.y_coord());
57  return libff::G2<ppT>(X, Y, TwistField::one());
58 }
59 
60 template<typename FieldT, size_t TreeDepth>
61 joinsplit_input<FieldT, TreeDepth> joinsplit_input_from_proto(
62  const zeth_proto::JoinsplitInput &input)
63 {
64  if (TreeDepth != input.merkle_path_size()) {
65  throw std::invalid_argument("Invalid merkle path length");
66  }
67 
68  std::vector<FieldT> input_merkle_path;
69  for (size_t i = 0; i < TreeDepth; i++) {
70  FieldT mk_node =
71  base_field_element_from_hex<FieldT>(input.merkle_path(i));
72  input_merkle_path.push_back(mk_node);
73  }
74 
75  return joinsplit_input<FieldT, TreeDepth>(
76  std::move(input_merkle_path),
77  bits_addr<TreeDepth>::from_size_t(input.address()),
78  zeth_note_from_proto(input.note()),
79  bits256::from_hex(input.spending_ask()),
80  bits256::from_hex(input.nullifier()));
81 }
82 
83 template<typename ppT>
84 void pairing_parameters_to_proto(zeth_proto::PairingParameters &pp_proto)
85 {
86  pp_proto.set_name(pp_name<ppT>());
87  pp_proto.set_r(libff::bigint_to_hex(libff::Fr<ppT>::mod));
88  pp_proto.set_q(libff::bigint_to_hex(libff::Fq<ppT>::mod));
89  *pp_proto.mutable_generator_g1() =
90  point_g1_affine_to_proto<ppT>(libff::G1<ppT>::one());
91  *pp_proto.mutable_generator_g2() =
92  point_g2_affine_to_proto<ppT>(libff::G2<ppT>::one());
93 }
94 
95 } // namespace libzeth
96 
97 #endif // __ZETH_SERIALIZATION_PROTO_UTILS_TCC__