1 // Copyright (c) 2015-2022 Clearmatics Technologies Ltd
3 // SPDX-License-Identifier: LGPL-3.0+
5 #ifndef __ZETH_SERIALIZATION_PROTO_UTILS_TCC__
6 #define __ZETH_SERIALIZATION_PROTO_UTILS_TCC__
8 #include "libzeth/core/field_element_utils.hpp"
9 #include "libzeth/serialization/proto_utils.hpp"
16 template<typename ppT>
17 zeth_proto::Group1Point point_g1_affine_to_proto(const libff::G1<ppT> &point)
19 assert(!point.is_zero());
20 libff::G1<ppT> aff = point;
21 aff.to_affine_coordinates();
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));
29 template<typename ppT>
30 libff::G1<ppT> point_g1_affine_from_proto(const zeth_proto::Group1Point &point)
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());
38 template<typename ppT>
39 zeth_proto::Group2Point point_g2_affine_to_proto(const libff::G2<ppT> &point)
41 assert(!point.is_zero());
42 libff::G2<ppT> aff = point;
43 aff.to_affine_coordinates();
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));
51 template<typename ppT>
52 libff::G2<ppT> point_g2_affine_from_proto(const zeth_proto::Group2Point &point)
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());
60 template<typename FieldT, size_t TreeDepth>
61 joinsplit_input<FieldT, TreeDepth> joinsplit_input_from_proto(
62 const zeth_proto::JoinsplitInput &input)
64 if (TreeDepth != input.merkle_path_size()) {
65 throw std::invalid_argument("Invalid merkle path length");
68 std::vector<FieldT> input_merkle_path;
69 for (size_t i = 0; i < TreeDepth; i++) {
71 base_field_element_from_hex<FieldT>(input.merkle_path(i));
72 input_merkle_path.push_back(mk_node);
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()));
83 template<typename ppT>
84 void pairing_parameters_to_proto(zeth_proto::PairingParameters &pp_proto)
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());
95 } // namespace libzeth
97 #endif // __ZETH_SERIALIZATION_PROTO_UTILS_TCC__