openvm_snark_verifier/
traits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use core::{
    fmt::{self, Debug},
    marker::PhantomData,
    ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};

use halo2_proofs::halo2curves::{
    bn256::{Fq as Halo2Fp, Fr as Halo2Fr, G1Affine},
    ff::PrimeField,
    CurveAffine,
};
use openvm_ecc_guest::algebra::{ExpBytes, Field, IntMod};
use openvm_pairing_guest::bn254::{Bn254G1Affine as EcPoint, Fp, Scalar as Fr};
use serde::{Deserialize, Serialize};
use snark_verifier_sdk::snark_verifier::{
    halo2_base::halo2_proofs,
    loader::{LoadedEcPoint, LoadedScalar},
    util::arithmetic::FieldOps,
};

use super::loader::{OpenVmLoader, LOADER};

#[derive(Clone, Serialize, Deserialize)]
#[repr(transparent)]
#[serde(bound(serialize = "F2: Serialize", deserialize = "F2: Deserialize<'de>"))]
pub struct OpenVmScalar<F, F2>(pub F2, PhantomData<F>);

impl<F, F2> OpenVmScalar<F, F2> {
    pub fn new(value: F2) -> Self {
        Self(value, PhantomData)
    }
}

#[derive(Clone, Serialize, Deserialize)]
#[repr(transparent)]
#[serde(bound(serialize = "C: Serialize", deserialize = "C: Deserialize<'de>"))]
pub struct OpenVmEcPoint<CA, C>(pub C, PhantomData<CA>);

impl<CA, C> OpenVmEcPoint<CA, C> {
    pub fn new(point: C) -> Self {
        Self(point, PhantomData)
    }
}

impl<F: PrimeField, F2: Field> PartialEq for OpenVmScalar<F, F2> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl LoadedScalar<Halo2Fp> for OpenVmScalar<Halo2Fp, Fp> {
    type Loader = OpenVmLoader;

    fn loader(&self) -> &Self::Loader {
        &LOADER
    }

    fn pow_var(&self, exp: &Self, _: usize) -> Self {
        OpenVmScalar(self.0.exp_bytes(true, &exp.0.to_be_bytes()), PhantomData)
    }
}

impl LoadedScalar<Halo2Fr> for OpenVmScalar<Halo2Fr, Fr> {
    type Loader = OpenVmLoader;

    fn loader(&self) -> &Self::Loader {
        &LOADER
    }

    fn pow_var(&self, exp: &Self, _: usize) -> Self {
        OpenVmScalar(self.0.exp_bytes(true, &exp.0.to_be_bytes()), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> Debug for OpenVmScalar<F, F2> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Scalar")
            .field("value", &self.0.clone())
            .finish()
    }
}

impl<F: PrimeField, F2: Field> Add for OpenVmScalar<F, F2> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        OpenVmScalar(self.0.clone() + rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> Sub for OpenVmScalar<F, F2> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        OpenVmScalar(self.0.clone() - rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> Mul for OpenVmScalar<F, F2> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        OpenVmScalar(self.0.clone() * rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> Neg for OpenVmScalar<F, F2> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        OpenVmScalar(-self.0.clone(), PhantomData)
    }
}

impl<'b, F: PrimeField, F2: Field> Add<&'b Self> for OpenVmScalar<F, F2> {
    type Output = Self;

    fn add(self, rhs: &'b Self) -> Self::Output {
        OpenVmScalar(self.0.clone() + rhs.0.clone(), PhantomData)
    }
}

impl<'b, F: PrimeField, F2: Field> Sub<&'b Self> for OpenVmScalar<F, F2> {
    type Output = Self;

    fn sub(self, rhs: &'b Self) -> Self::Output {
        OpenVmScalar(self.0.clone() - rhs.0.clone(), PhantomData)
    }
}

impl<'b, F: PrimeField, F2: Field> Mul<&'b Self> for OpenVmScalar<F, F2> {
    type Output = Self;

    fn mul(self, rhs: &'b Self) -> Self::Output {
        OpenVmScalar(self.0.clone() * rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> AddAssign for OpenVmScalar<F, F2> {
    fn add_assign(&mut self, rhs: Self) {
        *self = OpenVmScalar(self.0.clone() + rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> SubAssign for OpenVmScalar<F, F2> {
    fn sub_assign(&mut self, rhs: Self) {
        *self = OpenVmScalar(self.0.clone() - rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> MulAssign for OpenVmScalar<F, F2> {
    fn mul_assign(&mut self, rhs: Self) {
        *self = OpenVmScalar(self.0.clone() * rhs.0.clone(), PhantomData)
    }
}

impl<'b, F: PrimeField, F2: Field> AddAssign<&'b Self> for OpenVmScalar<F, F2> {
    fn add_assign(&mut self, rhs: &'b Self) {
        *self = OpenVmScalar(self.0.clone() + rhs.0.clone(), PhantomData)
    }
}

impl<'b, F: PrimeField, F2: Field> SubAssign<&'b Self> for OpenVmScalar<F, F2> {
    fn sub_assign(&mut self, rhs: &'b Self) {
        *self = OpenVmScalar(self.0.clone() - rhs.0.clone(), PhantomData)
    }
}

impl<'b, F: PrimeField, F2: Field> MulAssign<&'b Self> for OpenVmScalar<F, F2> {
    fn mul_assign(&mut self, rhs: &'b Self) {
        *self = OpenVmScalar(self.0.clone() * rhs.0.clone(), PhantomData)
    }
}

impl<F: PrimeField, F2: Field> FieldOps for OpenVmScalar<F, F2> {
    fn invert(&self) -> Option<Self> {
        (self.0 != F2::ZERO).then(|| OpenVmScalar::new(self.0.invert()))
    }
}

impl<CA: CurveAffine> PartialEq for OpenVmEcPoint<CA, EcPoint> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl LoadedEcPoint<G1Affine> for OpenVmEcPoint<G1Affine, EcPoint> {
    type Loader = OpenVmLoader;

    fn loader(&self) -> &Self::Loader {
        &LOADER
    }
}

impl<CA: CurveAffine> Debug for OpenVmEcPoint<CA, EcPoint> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EcPoint")
            .field("value", &self.0.clone())
            .finish()
    }
}