openvm_pairing_guest/halo2curves_shims/bn254/
mod.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
mod curve;
mod final_exp;
mod line;
mod miller_loop;

pub use curve::*;
pub use line::*;

#[cfg(test)]
pub mod tests;

use halo2curves_axiom::bn256::{Fq, Fq12, Fq2, G1Affine, G2Affine};
use openvm_algebra_guest::field::{Field, FieldExtension};
use rand::Rng;

use crate::{
    affine_point::AffineCoords,
    pairing::{Evaluatable, EvaluatedLine, FromLineDType, UnevaluatedLine},
};

impl FromLineDType<Fq2> for Fq12 {
    fn from_evaluated_line_d_type(line: EvaluatedLine<Fq2>) -> Fq12 {
        FieldExtension::<Fq2>::from_coeffs([
            Fq2::one(),
            line.b,
            Fq2::zero(),
            line.c,
            Fq2::zero(),
            Fq2::zero(),
        ])
    }
}

impl Evaluatable<Fq, Fq2> for UnevaluatedLine<Fq2> {
    fn evaluate(&self, xy_frac: &(Fq, Fq)) -> EvaluatedLine<Fq2> {
        let (x_over_y, y_inv) = xy_frac;
        EvaluatedLine {
            b: self.b.mul_base(x_over_y),
            c: self.c.mul_base(y_inv),
        }
    }
}

impl AffineCoords<Fq> for G1Affine {
    fn new(x: Fq, y: Fq) -> Self {
        G1Affine { x, y }
    }

    fn x(&self) -> Fq {
        self.x
    }

    fn y(&self) -> Fq {
        self.y
    }

    fn neg(&self) -> Self {
        let mut pt = *self;
        pt.y = -pt.y;
        pt
    }

    fn random(rng: &mut impl Rng) -> Self {
        G1Affine::random(rng)
    }

    fn generator() -> Self {
        G1Affine::generator()
    }

    fn is_infinity(&self) -> bool {
        self.x == Fq::ZERO && self.y == Fq::ZERO
    }
}

impl AffineCoords<Fq2> for G2Affine {
    fn new(x: Fq2, y: Fq2) -> Self {
        G2Affine { x, y }
    }

    fn x(&self) -> Fq2 {
        self.x
    }

    fn y(&self) -> Fq2 {
        self.y
    }

    fn neg(&self) -> Self {
        let mut pt = *self;
        pt.y = -pt.y;
        pt
    }

    fn random(rng: &mut impl Rng) -> Self {
        G2Affine::random(rng)
    }

    fn generator() -> Self {
        G2Affine::generator()
    }

    fn is_infinity(&self) -> bool {
        self.x == Fq2::ZERO && self.y == Fq2::ZERO
    }
}