openvm_pairing_guest/pairing/
line.rs

1/// A line function on Fp12 x Fp12 in a sparse representation.
2/// Let `Fp12 = Fp2[w] / (w^6 - \xi)`. Then the line function is
3/// `L(x,y) = 1 + b (x/y) w' + c (1/y) w'^3`
4/// where `w' = w` for D-type and `w' = w^{-1}` for M-type twists
5#[repr(C)]
6#[derive(Clone, Copy, Debug)]
7pub struct UnevaluatedLine<Fp2> {
8    pub b: Fp2,
9    pub c: Fp2,
10}
11
12/// The output of a line function on Fp12 x Fp12 (see `UnevaluatedLine`).
13/// Represents 1 + b w' + c w'^3 where w' = w for D-type and w' = w^-1 for M-type twists.
14#[derive(Clone, Copy, Debug)]
15pub struct EvaluatedLine<Fp2> {
16    pub b: Fp2,
17    pub c: Fp2,
18}
19
20pub trait Evaluatable<Fp, Fp2> {
21    // xy_frac is (x/y, 1/y)
22    fn evaluate(&self, xy_frac: &(Fp, Fp)) -> EvaluatedLine<Fp2>;
23}
24
25impl<Fp2> IntoIterator for EvaluatedLine<Fp2> {
26    type Item = Fp2;
27    type IntoIter = core::array::IntoIter<Fp2, 2>;
28    fn into_iter(self) -> Self::IntoIter {
29        [self.b, self.c].into_iter()
30    }
31}
32
33/// Convert M-type lines into Fp12 elements
34pub trait FromLineMType<Fp2> {
35    fn from_evaluated_line_m_type(line: EvaluatedLine<Fp2>) -> Self;
36}
37
38/// Trait definition for line multiplication opcodes for M-type lines
39pub trait LineMulMType<Fp2, Fp12> {
40    /// Multiplies two lines in 023-form to get an element in 02345-form
41    fn mul_023_by_023(l0: &EvaluatedLine<Fp2>, l1: &EvaluatedLine<Fp2>) -> [Fp2; 5];
42
43    /// Multiplies a line in 023-form with a Fp12 element to get an Fp12 element
44    fn mul_by_023(f: &Fp12, l: &EvaluatedLine<Fp2>) -> Fp12;
45
46    /// Multiplies a line in 02345-form with a Fp12 element to get an Fp12 element
47    fn mul_by_02345(f: &Fp12, x: &[Fp2; 5]) -> Fp12;
48}
49
50/// Convert D-type lines into Fp12 elements
51pub trait FromLineDType<Fp2> {
52    fn from_evaluated_line_d_type(line: EvaluatedLine<Fp2>) -> Self;
53}
54
55/// Trait definition for line multiplication opcodes for D-type lines
56pub trait LineMulDType<Fp2, Fp12> {
57    /// Multiplies two lines in 013-form to get an element in 01234-form
58    fn mul_013_by_013(l0: &EvaluatedLine<Fp2>, l1: &EvaluatedLine<Fp2>) -> [Fp2; 5];
59
60    /// Multiplies a line in 013-form with a Fp12 element to get an Fp12 element
61    fn mul_by_013(f: &Fp12, l: &EvaluatedLine<Fp2>) -> Fp12;
62
63    /// Multiplies a line in 01234-form with a Fp12 element to get an Fp12 element
64    fn mul_by_01234(f: &Fp12, x: &[Fp2; 5]) -> Fp12;
65}