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
use hacspec_lib::*;
public_nat_mod!(
type_name: FieldElement,
type_of_canvas: FieldCanvas,
bit_size_of_field: 256,
modulo_value: "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
);
public_nat_mod!(
type_name: Scalar,
type_of_canvas: ScalarCanvas,
bit_size_of_field: 256,
modulo_value: "8000000000000000000000000000000000000000000000000000000000000000"
);
type Point = (FieldElement, FieldElement);
bytes!(SerializedPoint, 32);
bytes!(SerializedScalar, 32);
fn mask_scalar(s: SerializedScalar) -> SerializedScalar {
let mut k = s;
k[0] &= U8(248);
k[31] &= U8(127);
k[31] |= U8(64);
k
}
fn decode_scalar(s: SerializedScalar) -> Scalar {
let k = mask_scalar(s);
Scalar::from_byte_seq_le(k)
}
fn decode_point(u: SerializedPoint) -> Point {
let mut u_ = u;
u_[31] &= U8(127);
(
FieldElement::from_byte_seq_le(u_),
FieldElement::from_literal(1),
)
}
fn encode_point(p: Point) -> SerializedPoint {
let (x, y) = p;
let b = x * y.inv();
SerializedPoint::new().update_start(&b.to_byte_seq_le())
}
fn point_add_and_double(q: Point, (nq, nqp1): (Point, Point)) -> (Point, Point) {
let (x_1, _z_1) = q;
let (x_2, z_2) = nq;
let (x_3, z_3) = nqp1;
let a = x_2 + z_2;
let aa = a.pow(2);
let b = x_2 - z_2;
let bb = b * b;
let e = aa - bb;
let c = x_3 + z_3;
let d = x_3 - z_3;
let da = d * a;
let cb = c * b;
let x_3 = (da + cb).pow(2);
let z_3 = x_1 * ((da - cb).pow(2));
let x_2 = aa * bb;
let e121665 = FieldElement::from_literal(121_665);
let z_2 = e * (aa + (e121665 * e));
((x_2, z_2), (x_3, z_3))
}
fn swap(x: (Point, Point)) -> (Point, Point) {
(x.1, x.0)
}
fn montgomery_ladder(k: Scalar, init: Point) -> Point {
let inf = (FieldElement::from_literal(1), FieldElement::from_literal(0));
let mut acc: (Point, Point) = (inf, init);
for i in 0..256 {
if k.bit(255 - i) {
acc = swap(acc);
acc = point_add_and_double(init, acc);
acc = swap(acc);
} else {
acc = point_add_and_double(init, acc);
}
}
acc.0
}
pub fn scalarmult(s: SerializedScalar, p: SerializedPoint) -> SerializedPoint {
let s_ = decode_scalar(s);
let p_ = decode_point(p);
let r = montgomery_ladder(s_, p_);
encode_point(r)
}
pub fn secret_to_public(s: SerializedScalar) -> SerializedPoint {
let base = SerializedPoint::from_hex("09");
scalarmult(s, base)
}
#[test]
fn test_encode_decode_scalar() {
let s = SerializedScalar::from_public_slice(&[
0xa5, 0x46, 0xe3, 0x6b, 0xf0, 0x52, 0x7c, 0x9d, 0x3b, 0x16, 0x15, 0x4b, 0x82, 0x46, 0x5e,
0xdd, 0x62, 0x14, 0x4c, 0x0a, 0xc1, 0xfc, 0x5a, 0x18, 0x50, 0x6a, 0x22, 0x44, 0xba, 0x44,
0x9a, 0xc4,
]);
let s_expected =
Scalar::from_hex("449a44ba44226a50185afcc10a4c1462dd5e46824b15163b9d7c52f06be346a0");
let s_ = decode_scalar(s);
assert_eq!(s_expected, s_);
let u = SerializedPoint::from_public_slice(&[
0xe6, 0xdb, 0x68, 0x67, 0x58, 0x30, 0x30, 0xdb, 0x35, 0x94, 0xc1, 0xa4, 0x24, 0xb1, 0x5f,
0x7c, 0x72, 0x66, 0x24, 0xec, 0x26, 0xb3, 0x35, 0x3b, 0x10, 0xa9, 0x03, 0xa6, 0xd0, 0xab,
0x1c, 0x4c,
]);
let u_expected = (
FieldElement::from_hex("4c1cabd0a603a9103b35b326ec2466727c5fb124a4c19435db3030586768dbe6"),
FieldElement::from_literal(1),
);
let u_ = decode_point(u);
assert_eq!(u_expected, u_);
let u_encoded = encode_point(u_);
assert!(u.declassify_eq(&u_encoded));
}