sonobe/solidity-verifiers/src/utils/encoding.rs
solidity-verifiers/src/utils/encoding.rsRaw
1/// Defines encodings of G1 and G2 elements for use in Solidity templates.
2use ark_bn254::{Fq, G1Affine, G2Affine};
3use std::fmt::{self, Display};
4
5#[derive(Debug, Default)]
6pub struct FqWrapper(pub Fq);
7
8impl Display for FqWrapper {
9 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10 write!(f, "{}", self.0)
11 }
12}
13
14#[derive(Debug, Default)]
15pub struct G1Repr(pub [FqWrapper; 2]);
16
17impl Display for G1Repr {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 write!(f, "{:#?}", self.0)
20 }
21}
22
23/// Converts a G1 element to a representation that can be used in Solidity templates.
24pub fn g1_to_fq_repr(g1: G1Affine) -> G1Repr {
25 G1Repr([FqWrapper(g1.x), FqWrapper(g1.y)])
26}
27
28#[derive(Debug, Default)]
29pub struct G2Repr(pub [[FqWrapper; 2]; 2]);
30
31impl Display for G2Repr {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(f, "{:#?}", self.0)
34 }
35}
36
37/// Converts a G2 element to a representation that can be used in Solidity templates.
38pub fn g2_to_fq_repr(g2: G2Affine) -> G2Repr {
39 G2Repr([
40 [FqWrapper(g2.x.c0), FqWrapper(g2.x.c1)],
41 [FqWrapper(g2.y.c0), FqWrapper(g2.y.c1)],
42 ])
43}