openvm_algebra_guest/
halo2curves.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use core::ops::{Add, Mul, Sub};

use halo2curves_axiom::ff;

use crate::{field::Field, DivAssignUnsafe, DivUnsafe};

impl<'a, F: ff::Field> DivUnsafe<&'a F> for F {
    type Output = F;

    fn div_unsafe(self, other: &'a F) -> Self::Output {
        self * other.invert().unwrap()
    }
}

impl<'a, F: ff::Field> DivUnsafe<&'a F> for &'a F {
    type Output = F;

    fn div_unsafe(self, other: &'a F) -> Self::Output {
        *self * other.invert().unwrap()
    }
}

impl<F: ff::Field> DivAssignUnsafe for F {
    fn div_assign_unsafe(&mut self, other: Self) {
        *self *= other.invert().unwrap();
    }
}

impl<'a, F: ff::Field> DivAssignUnsafe<&'a F> for F {
    fn div_assign_unsafe(&mut self, other: &'a F) {
        *self *= other.invert().unwrap();
    }
}

impl<F: ff::Field> Field for F
where
    for<'a> &'a F: Add<&'a F, Output = F> + Sub<&'a F, Output = F> + Mul<&'a F, Output = F>,
{
    const ZERO: Self = <F as ff::Field>::ZERO;
    const ONE: Self = <F as ff::Field>::ONE;

    type SelfRef<'a> = &'a F;

    fn double_assign(&mut self) {
        *self += *self;
    }

    fn square_assign(&mut self) {
        *self = self.square();
    }
}

mod bn254 {
    use alloc::vec::Vec;

    use halo2curves_axiom::bn256::{Fq, Fq12, Fq2, Fq6};

    use crate::field::{ComplexConjugate, FieldExtension};

    pub fn bytes_to_bn254_fq(bytes: &[u8]) -> Fq {
        assert_eq!(bytes.len(), 32);
        Fq::from_bytes(&bytes.try_into().unwrap()).unwrap()
    }

    /// FieldExtension for Fq2 with Fq as base field
    impl FieldExtension<Fq> for Fq2 {
        const D: usize = 2;
        type Coeffs = [Fq; 2];

        fn from_coeffs(coeffs: Self::Coeffs) -> Self {
            Fq2 {
                c0: coeffs[0],
                c1: coeffs[1],
            }
        }

        fn from_bytes(bytes: &[u8]) -> Self {
            assert_eq!(bytes.len(), 64);
            Self::from_coeffs([
                bytes_to_bn254_fq(&bytes[0..32]),
                bytes_to_bn254_fq(&bytes[32..64]),
            ])
        }

        fn to_coeffs(self) -> Self::Coeffs {
            [self.c0, self.c1]
        }

        fn to_bytes(&self) -> Vec<u8> {
            let mut bytes = Vec::with_capacity(64);
            bytes.extend_from_slice(&self.c0.to_bytes());
            bytes.extend_from_slice(&self.c1.to_bytes());
            bytes
        }

        fn embed(c0: Fq) -> Self {
            Fq2 { c0, c1: Fq::zero() }
        }

        fn frobenius_map(&self, power: usize) -> Self {
            let mut s = *self;
            Fq2::frobenius_map(&mut s, power);
            s
        }

        fn mul_base(&self, rhs: &Fq) -> Self {
            Fq2 {
                c0: self.c0 * rhs,
                c1: self.c1 * rhs,
            }
        }
    }

    impl ComplexConjugate for Fq2 {
        fn conjugate(self) -> Self {
            let mut s = self;
            Fq2::conjugate(&mut s);
            s
        }

        fn conjugate_assign(&mut self) {
            Fq2::conjugate(self);
        }
    }

    /// FieldExtension for Fq12 with Fq6 as base field since halo2curves does not implement `Field` for Fq6.
    impl FieldExtension<Fq2> for Fq12 {
        const D: usize = 6;
        type Coeffs = [Fq2; 6];

        fn from_coeffs(coeffs: Self::Coeffs) -> Self {
            Fq12 {
                c0: Fq6 {
                    c0: coeffs[0],
                    c1: coeffs[2],
                    c2: coeffs[4],
                },
                c1: Fq6 {
                    c0: coeffs[1],
                    c1: coeffs[3],
                    c2: coeffs[5],
                },
            }
        }

        fn from_bytes(bytes: &[u8]) -> Self {
            assert_eq!(bytes.len(), 384);
            Self::from_coeffs([
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[0..64]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[64..128]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[128..192]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[192..256]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[256..320]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[320..384]),
            ])
        }

        fn to_coeffs(self) -> Self::Coeffs {
            [
                self.c0.c0, self.c1.c0, self.c0.c1, self.c1.c1, self.c0.c2, self.c1.c2,
            ]
        }

        fn to_bytes(&self) -> Vec<u8> {
            let coeffs = self.to_coeffs();
            let mut bytes = Vec::with_capacity(384);
            for coeff in coeffs {
                bytes.extend_from_slice(&coeff.to_bytes());
            }
            bytes
        }

        fn embed(c0: Fq2) -> Self {
            let fq6_pt = Fq6 {
                c0,
                c1: Fq2::zero(),
                c2: Fq2::zero(),
            };
            Fq12 {
                c0: fq6_pt,
                c1: Fq6::zero(),
            }
        }

        fn frobenius_map(&self, power: usize) -> Self {
            let mut s = *self;
            Fq12::frobenius_map(&mut s, power);
            s
        }

        fn mul_base(&self, rhs: &Fq2) -> Self {
            let fq6_pt = Fq6 {
                c0: *rhs,
                c1: Fq2::zero(),
                c2: Fq2::zero(),
            };
            Fq12 {
                c0: self.c0 * fq6_pt,
                c1: self.c1 * fq6_pt,
            }
        }
    }

    /// This is complex conjugation of Fq12 over Fq6
    impl ComplexConjugate for Fq12 {
        fn conjugate(self) -> Self {
            let mut s = self;
            Fq12::conjugate(&mut s);
            s
        }

        fn conjugate_assign(&mut self) {
            Fq12::conjugate(self);
        }
    }
}

mod bls12_381 {
    use alloc::vec::Vec;

    use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2, Fq6};

    use crate::field::{ComplexConjugate, FieldExtension};

    pub fn bytes_to_bls12_381_fq(bytes: &[u8]) -> Fq {
        assert_eq!(bytes.len(), 48);
        Fq::from_bytes(&bytes.try_into().unwrap()).unwrap()
    }

    /// FieldExtension for Fq2 with Fq as base field
    impl FieldExtension<Fq> for Fq2 {
        const D: usize = 2;
        type Coeffs = [Fq; 2];

        fn from_coeffs(coeffs: [Fq; 2]) -> Self {
            Fq2 {
                c0: coeffs[0],
                c1: coeffs[1],
            }
        }

        fn from_bytes(bytes: &[u8]) -> Self {
            assert_eq!(bytes.len(), 96);
            Self::from_coeffs([
                bytes_to_bls12_381_fq(&bytes[0..48]),
                bytes_to_bls12_381_fq(&bytes[48..96]),
            ])
        }

        fn to_coeffs(self) -> Self::Coeffs {
            [self.c0, self.c1]
        }

        fn to_bytes(&self) -> Vec<u8> {
            let mut bytes = Vec::with_capacity(96);
            bytes.extend_from_slice(&self.c0.to_bytes());
            bytes.extend_from_slice(&self.c1.to_bytes());
            bytes
        }

        fn embed(c0: Fq) -> Self {
            Fq2 { c0, c1: Fq::zero() }
        }

        fn frobenius_map(&self, power: usize) -> Self {
            if power % 2 == 0 {
                *self
            } else {
                // note: Fq2::frobenius_map is same as Fq2::conjugate
                Fq2::frobenius_map(self)
            }
        }

        fn mul_base(&self, rhs: &Fq) -> Self {
            Fq2 {
                c0: self.c0 * rhs,
                c1: self.c1 * rhs,
            }
        }
    }

    impl ComplexConjugate for Fq2 {
        fn conjugate(self) -> Self {
            Fq2::conjugate(&self)
        }

        fn conjugate_assign(&mut self) {
            *self = Fq2::conjugate(self);
        }
    }

    /// Note that halo2curves does not implement `Field` for Fq6, so we need to implement the intermediate points manually.
    ///
    /// FieldExtension for Fq12 with Fq2 as base field since halo2curves does not implement `Field` for Fq6.
    impl FieldExtension<Fq2> for Fq12 {
        const D: usize = 6;
        type Coeffs = [Fq2; 6];

        fn from_coeffs(coeffs: [Fq2; 6]) -> Self {
            Fq12 {
                c0: Fq6 {
                    c0: coeffs[0],
                    c1: coeffs[2],
                    c2: coeffs[4],
                },
                c1: Fq6 {
                    c0: coeffs[1],
                    c1: coeffs[3],
                    c2: coeffs[5],
                },
            }
        }

        fn from_bytes(bytes: &[u8]) -> Self {
            assert_eq!(bytes.len(), 576);
            Self::from_coeffs([
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[0..96]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[96..192]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[192..288]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[288..384]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[384..480]),
                <Fq2 as FieldExtension<Fq>>::from_bytes(&bytes[480..576]),
            ])
        }

        fn to_coeffs(self) -> Self::Coeffs {
            [
                self.c0.c0, self.c1.c0, self.c0.c1, self.c1.c1, self.c0.c2, self.c1.c2,
            ]
        }

        fn to_bytes(&self) -> Vec<u8> {
            let coeffs = self.to_coeffs();
            let mut bytes = Vec::with_capacity(576);
            for coeff in coeffs {
                bytes.extend_from_slice(&coeff.to_bytes());
            }
            bytes
        }

        fn embed(base_elem: Fq2) -> Self {
            let fq6_pt = Fq6 {
                c0: base_elem,
                c1: Fq2::zero(),
                c2: Fq2::zero(),
            };
            Fq12 {
                c0: fq6_pt,
                c1: Fq6::zero(),
            }
        }

        /// Raises this element to p^power, where p is prime characteristic of `Self`.
        fn frobenius_map(&self, power: usize) -> Self {
            let mut x = *self;
            for _ in 0..power % 12 {
                x = Fq12::frobenius_map(&x);
            }
            x
        }

        fn mul_base(&self, rhs: &Fq2) -> Self {
            let fq6_pt = Fq6 {
                c0: *rhs,
                c1: Fq2::zero(),
                c2: Fq2::zero(),
            };
            Fq12 {
                c0: self.c0 * fq6_pt,
                c1: self.c1 * fq6_pt,
            }
        }
    }

    /// This is complex conjugation of Fq12 over Fq6
    impl ComplexConjugate for Fq12 {
        fn conjugate(self) -> Self {
            Fq12::conjugate(&self)
        }

        fn conjugate_assign(&mut self) {
            *self = Fq12::conjugate(self);
        }
    }
}