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
use super::{GroupAffine, GroupProjective};
use snarkvm_utilities::{
bytes::ToBytes,
io::Cursor,
rand::UniformRand,
serialize::{CanonicalDeserialize, CanonicalSerialize},
to_bytes,
};
use crate::traits::{
pairing_engine::{AffineCurve, ProjectiveCurve},
MontgomeryModelParameters,
TEModelParameters,
};
use snarkvm_fields::{Field, One, PrimeField, Zero};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
pub const ITERATIONS: usize = 10;
pub fn montgomery_conversion_test<P>()
where
P: TEModelParameters,
{
let a = P::BaseField::one().double() * &(P::COEFF_A + &P::COEFF_D) * &(P::COEFF_A - &P::COEFF_D).inverse().unwrap();
let b = P::BaseField::one().double().double() * &(P::COEFF_A - &P::COEFF_D).inverse().unwrap();
assert_eq!(a, P::MontgomeryModelParameters::COEFF_A);
assert_eq!(b, P::MontgomeryModelParameters::COEFF_B);
}
pub fn edwards_test<P: TEModelParameters>()
where
P::BaseField: PrimeField,
{
edwards_curve_serialization_test::<P>();
edwards_from_random_bytes::<P>();
edwards_from_x_and_y_coordinates::<P>();
}
pub fn edwards_curve_serialization_test<P: TEModelParameters>() {
let buf_size = GroupAffine::<P>::zero().serialized_size();
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
for _ in 0..10 {
let a = GroupProjective::<P>::rand(&mut rng);
let a = a.into_affine();
{
let mut serialized = vec![0; buf_size];
let mut cursor = Cursor::new(&mut serialized[..]);
a.serialize(&mut cursor).unwrap();
let mut cursor = Cursor::new(&serialized[..]);
let b = GroupAffine::<P>::deserialize(&mut cursor).unwrap();
assert_eq!(a, b);
}
{
let a = GroupAffine::<P>::zero();
let mut serialized = vec![0; buf_size];
let mut cursor = Cursor::new(&mut serialized[..]);
a.serialize(&mut cursor).unwrap();
let mut cursor = Cursor::new(&serialized[..]);
let b = GroupAffine::<P>::deserialize(&mut cursor).unwrap();
assert_eq!(a, b);
}
{
let a = GroupAffine::<P>::zero();
let mut serialized = vec![0; buf_size - 1];
let mut cursor = Cursor::new(&mut serialized[..]);
a.serialize(&mut cursor).unwrap_err();
}
{
let serialized = vec![0; buf_size - 1];
let mut cursor = Cursor::new(&serialized[..]);
GroupAffine::<P>::deserialize(&mut cursor).unwrap_err();
}
{
let mut serialized = vec![0; a.uncompressed_size()];
let mut cursor = Cursor::new(&mut serialized[..]);
a.serialize_uncompressed(&mut cursor).unwrap();
let mut cursor = Cursor::new(&serialized[..]);
let b = GroupAffine::<P>::deserialize_uncompressed(&mut cursor).unwrap();
assert_eq!(a, b);
}
{
let a = GroupAffine::<P>::zero();
let mut serialized = vec![0; a.uncompressed_size()];
let mut cursor = Cursor::new(&mut serialized[..]);
a.serialize_uncompressed(&mut cursor).unwrap();
let mut cursor = Cursor::new(&serialized[..]);
let b = GroupAffine::<P>::deserialize_uncompressed(&mut cursor).unwrap();
assert_eq!(a, b);
}
}
}
pub fn edwards_from_random_bytes<P: TEModelParameters>()
where
P::BaseField: PrimeField,
{
let buf_size = GroupAffine::<P>::zero().serialized_size();
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
for _ in 0..ITERATIONS {
let a = GroupProjective::<P>::rand(&mut rng);
let a = a.into_affine();
{
let mut serialized = vec![0; buf_size];
let mut cursor = Cursor::new(&mut serialized[..]);
a.serialize(&mut cursor).unwrap();
let mut cursor = Cursor::new(&serialized[..]);
let p1 = GroupAffine::<P>::deserialize(&mut cursor).unwrap();
let p2 = GroupAffine::<P>::from_random_bytes(&serialized).unwrap();
assert_eq!(p1, p2);
}
}
for _ in 0..ITERATIONS {
let biginteger = <<GroupAffine<P> as AffineCurve>::BaseField as PrimeField>::BigInteger::rand(&mut rng);
let mut bytes = to_bytes![biginteger].unwrap();
let mut g = GroupAffine::<P>::from_random_bytes(&bytes);
while g.is_none() {
bytes.iter_mut().for_each(|i| *i = i.wrapping_sub(1));
g = GroupAffine::<P>::from_random_bytes(&bytes);
}
let _g = g.unwrap();
}
}
pub fn edwards_from_x_and_y_coordinates<P: TEModelParameters>()
where
P::BaseField: PrimeField,
{
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
for _ in 0..ITERATIONS {
let a = GroupProjective::<P>::rand(&mut rng);
let a = a.into_affine();
{
let x = a.x;
let a1 = GroupAffine::<P>::from_x_coordinate(x, true).unwrap();
let a2 = GroupAffine::<P>::from_x_coordinate(x, false).unwrap();
assert!(a == a1 || a == a2);
}
{
let y = a.y;
let a1 = GroupAffine::<P>::from_y_coordinate(y, true).unwrap();
let a2 = GroupAffine::<P>::from_y_coordinate(y, false).unwrap();
assert!(a == a1 || a == a2);
}
}
}