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
use crate::constants::A_PLUS_TWO_OVER_FOUR;
use crate::curve::edwards::extended::ExtendedPoint;
use crate::field::{FieldElement, Scalar};
use std::fmt;
use std::ops::Mul;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
const LOW_A: MontgomeryPoint = MontgomeryPoint([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
const LOW_B: MontgomeryPoint = MontgomeryPoint([
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
const LOW_C: MontgomeryPoint = MontgomeryPoint([
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
]);
#[derive(Copy, Clone)]
#[cfg_attr(feature = "zeroize", derive(Zeroize))]
pub struct MontgomeryPoint(pub [u8; 56]);
impl fmt::Debug for MontgomeryPoint {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.0[..].fmt(formatter)
}
}
impl ConstantTimeEq for MontgomeryPoint {
fn ct_eq(&self, other: &MontgomeryPoint) -> Choice {
self.0.ct_eq(&other.0)
}
}
impl PartialEq for MontgomeryPoint {
fn eq(&self, other: &MontgomeryPoint) -> bool {
self.ct_eq(other).into()
}
}
impl Eq for MontgomeryPoint {}
#[derive(Copy, Clone)]
pub struct ProjectiveMontgomeryPoint {
U: FieldElement,
W: FieldElement,
}
impl Mul<&Scalar> for &MontgomeryPoint {
type Output = MontgomeryPoint;
fn mul(self, scalar: &Scalar) -> MontgomeryPoint {
let affine_u = FieldElement::from_bytes(&self.0);
let mut x0 = ProjectiveMontgomeryPoint::identity();
let mut x1 = ProjectiveMontgomeryPoint {
U: affine_u,
W: FieldElement::one(),
};
let bits = scalar.bits();
let mut swap = 0;
for s in (0..448).rev() {
let bit = bits[s] as u8;
let choice: u8 = (swap ^ bit) as u8;
ProjectiveMontgomeryPoint::conditional_swap(&mut x0, &mut x1, Choice::from(choice));
differential_add_and_double(&mut x0, &mut x1, &affine_u);
swap = bit;
}
x0.to_affine()
}
}
impl Mul<&MontgomeryPoint> for &Scalar {
type Output = MontgomeryPoint;
fn mul(self, point: &MontgomeryPoint) -> MontgomeryPoint {
point * self
}
}
impl MontgomeryPoint {
pub fn to_edwards(&self, sign: u8) -> Option<ExtendedPoint> {
todo!()
}
pub fn is_low_order(&self) -> bool {
(*self == LOW_A) || (*self == LOW_B) || (*self == LOW_C)
}
pub fn as_bytes(&self) -> &[u8; 56] {
&self.0
}
pub const fn generator() -> MontgomeryPoint {
MontgomeryPoint([
0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
])
}
pub fn to_projective(&self) -> ProjectiveMontgomeryPoint {
ProjectiveMontgomeryPoint {
U: FieldElement::from_bytes(&self.0),
W: FieldElement::one(),
}
}
}
impl ConditionallySelectable for ProjectiveMontgomeryPoint {
fn conditional_select(
a: &ProjectiveMontgomeryPoint,
b: &ProjectiveMontgomeryPoint,
choice: Choice,
) -> ProjectiveMontgomeryPoint {
ProjectiveMontgomeryPoint {
U: FieldElement::conditional_select(&a.U, &b.U, choice),
W: FieldElement::conditional_select(&a.W, &b.W, choice),
}
}
}
fn differential_add_and_double(
P: &mut ProjectiveMontgomeryPoint,
Q: &mut ProjectiveMontgomeryPoint,
affine_PmQ: &FieldElement,
) {
let t0 = P.U + P.W;
let t1 = P.U - P.W;
let t2 = Q.U + Q.W;
let t3 = Q.U - Q.W;
let t4 = t0.square();
let t5 = t1.square();
let t6 = t4 - t5;
let t7 = t0 * t3;
let t8 = t1 * t2;
let t9 = t7 + t8;
let t10 = t7 - t8;
let t11 = t9.square();
let t12 = t10.square();
let t13 = A_PLUS_TWO_OVER_FOUR * t6;
let t14 = t4 * t5;
let t15 = t13 + t5;
let t16 = t6 * t15;
let t17 = *affine_PmQ * t12;
let t18 = t11;
P.U = t14;
P.W = t16;
Q.U = t18;
Q.W = t17;
}
impl ProjectiveMontgomeryPoint {
pub fn identity() -> ProjectiveMontgomeryPoint {
ProjectiveMontgomeryPoint {
U: FieldElement::one(),
W: FieldElement::zero(),
}
}
pub fn to_affine(&self) -> MontgomeryPoint {
let x = self.U * self.W.invert();
MontgomeryPoint(x.to_bytes())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_montgomery_edwards() {
let scalar = Scalar::from(200);
use crate::constants::GOLDILOCKS_BASE_POINT as bp;
let montgomery_bp = bp.to_montgomery();
let montgomery_res = &montgomery_bp * &scalar;
let goldilocks_point = bp.scalar_mul(&scalar);
assert_eq!(goldilocks_point.to_montgomery(), montgomery_res);
}
}