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
#![allow(clippy::op_ref)]
use ff::Field;
pub type AffinePoint<Fe> = (Fe, Fe);
pub type ProjectivePoint<Fe> = (Fe, Fe, Fe);
#[inline(always)]
pub fn add<Fe>(
(ax, ay, az): ProjectivePoint<Fe>,
(bx, by, bz): ProjectivePoint<Fe>,
curve_equation_b: Fe,
) -> ProjectivePoint<Fe>
where
Fe: Field,
{
let xx = ax * bx;
let yy = ay * by;
let zz = az * bz;
let xy_pairs = ((ax + ay) * &(bx + by)) - &(xx + &yy);
let yz_pairs = ((ay + az) * &(by + bz)) - &(yy + &zz);
let xz_pairs = ((ax + az) * &(bx + bz)) - &(xx + &zz);
let bzz_part = xz_pairs - &(curve_equation_b * &zz);
let bzz3_part = bzz_part.double() + &bzz_part;
let yy_m_bzz3 = yy - &bzz3_part;
let yy_p_bzz3 = yy + &bzz3_part;
let zz3 = zz.double() + &zz;
let bxz_part = (curve_equation_b * &xz_pairs) - &(zz3 + &xx);
let bxz3_part = bxz_part.double() + &bxz_part;
let xx3_m_zz3 = xx.double() + &xx - &zz3;
(
(yy_p_bzz3 * &xy_pairs) - &(yz_pairs * &bxz3_part),
(yy_p_bzz3 * &yy_m_bzz3) + &(xx3_m_zz3 * &bxz3_part),
(yy_m_bzz3 * &yz_pairs) + &(xy_pairs * &xx3_m_zz3),
)
}
#[inline(always)]
pub fn add_mixed<Fe>(
(ax, ay, az): ProjectivePoint<Fe>,
(bx, by): AffinePoint<Fe>,
curve_equation_b: Fe,
) -> ProjectivePoint<Fe>
where
Fe: Field,
{
let xx = ax * &bx;
let yy = ay * &by;
let xy_pairs = ((ax + &ay) * &(bx + &by)) - &(xx + &yy);
let yz_pairs = (by * &az) + &ay;
let xz_pairs = (bx * &az) + &ax;
let bz_part = xz_pairs - &(curve_equation_b * &az);
let bz3_part = bz_part.double() + &bz_part;
let yy_m_bzz3 = yy - &bz3_part;
let yy_p_bzz3 = yy + &bz3_part;
let z3 = az.double() + &az;
let bxz_part = (curve_equation_b * &xz_pairs) - &(z3 + &xx);
let bxz3_part = bxz_part.double() + &bxz_part;
let xx3_m_zz3 = xx.double() + &xx - &z3;
(
(yy_p_bzz3 * &xy_pairs) - &(yz_pairs * &bxz3_part),
(yy_p_bzz3 * &yy_m_bzz3) + &(xx3_m_zz3 * &bxz3_part),
(yy_m_bzz3 * &yz_pairs) + &(xy_pairs * &xx3_m_zz3),
)
}
#[inline(always)]
pub fn double<Fe>((x, y, z): ProjectivePoint<Fe>, curve_equation_b: Fe) -> ProjectivePoint<Fe>
where
Fe: Field,
{
let xx = x.square();
let yy = y.square();
let zz = z.square();
let xy2 = (x * &y).double();
let xz2 = (x * &z).double();
let bzz_part = (curve_equation_b * &zz) - &xz2;
let bzz3_part = bzz_part.double() + &bzz_part;
let yy_m_bzz3 = yy - &bzz3_part;
let yy_p_bzz3 = yy + &bzz3_part;
let y_frag = yy_p_bzz3 * &yy_m_bzz3;
let x_frag = yy_m_bzz3 * &xy2;
let zz3 = zz.double() + &zz;
let bxz2_part = (curve_equation_b * &xz2) - &(zz3 + &xx);
let bxz6_part = bxz2_part.double() + &bxz2_part;
let xx3_m_zz3 = xx.double() + &xx - &zz3;
let dy = y_frag + &(xx3_m_zz3 * &bxz6_part);
let yz2 = (y * &z).double();
let dx = x_frag - &(bxz6_part * &yz2);
let dz = (yz2 * &yy).double().double();
(dx, dy, dz)
}