ark-curves-cherry-picked/ed_on_bn254/src/fields/tests.rs
ed_on_bn254/src/fields/tests.rsRaw
1use ark_algebra_test_templates::*;
2use ark_ff::{
3 biginteger::BigInteger256 as BigInteger,
4 fields::{Field, LegendreSymbol::*},
5 AdditiveGroup, One, Zero,
6};
7use ark_std::str::FromStr;
8
9use crate::{Fq, Fr};
10
11test_field!(fr; Fr; mont_prime_field);
12test_field!(fq; Fq; mont_prime_field);
13
14#[test]
15fn test_fq_add() {
16 let f1 = Fq::from_str(
17 "18386742314266644595564329008376577163854043021652781768352795308532764650733",
18 )
19 .unwrap();
20 let f2 = Fq::from_str(
21 "39786307610986038981023499868190793548353538256264351797285876981647142458383",
22 )
23 .unwrap();
24 let f3 = Fq::from_str(
25 "14396564181574133132095017386052820535110852477085064878242263917028290117882",
26 )
27 .unwrap();
28 assert!(!f1.is_zero());
29 assert!(!f2.is_zero());
30 assert!(!f3.is_zero());
31 assert_eq!(f1 + &f2, f3);
32}
33
34#[test]
35fn test_fq_add_one() {
36 let f1 = Fq::from_str(
37 "4946875394261337176810256604189376311946643975348516311606738923340201185904",
38 )
39 .unwrap();
40 let f2 = Fq::from_str(
41 "4946875394261337176810256604189376311946643975348516311606738923340201185905",
42 )
43 .unwrap();
44 assert!(!f1.is_zero());
45 assert!(!f2.is_zero());
46 assert_eq!(f1 + &Fq::one(), f2);
47}
48
49#[test]
50fn test_fq_mul() {
51 let f1 = Fq::from_str(
52 "24703123148064348394273033316595937198355721297494556079070134653139656190956",
53 )
54 .unwrap();
55 let f2 = Fq::from_str(
56 "38196797080882758914424853878212529985425118523754343117256179679117054302131",
57 )
58 .unwrap();
59 let f3 = Fq::from_str(
60 "1321267396236123309645330145349353750536542060403774171357889269349508194307",
61 )
62 .unwrap();
63 assert!(!f1.is_zero());
64 assert!(!f2.is_zero());
65 assert!(!f3.is_zero());
66 assert_eq!(f1 * &f2, f3);
67}
68
69#[test]
70fn test_fq_triple_mul() {
71 let f1 = Fq::from_str(
72 "23834398828139479510988224171342199299644042568628082836691700490363123893905",
73 )
74 .unwrap();
75 let f2 = Fq::from_str(
76 "48343809612844640454129919255697536258606705076971130519928764925719046689317",
77 )
78 .unwrap();
79 let f3 = Fq::from_str(
80 "22704845471524346880579660022678666462201713488283356385810726260959369106033",
81 )
82 .unwrap();
83 let f4 = Fq::from_str(
84 "7747776931431194635550680695131420638163057297019399136408144301550822179875",
85 )
86 .unwrap();
87 assert!(!f1.is_zero());
88 assert!(!f2.is_zero());
89 assert!(!f3.is_zero());
90 assert_eq!(f1 * &f2 * &f3, f4);
91}
92
93#[test]
94fn test_fq_div() {
95 let f1 = Fq::from_str(
96 "31892744363926593013886463524057935370302352424137349660481695792871889573091",
97 )
98 .unwrap();
99 let f2 = Fq::from_str(
100 "47695868328933459965610498875668250916462767196500056002116961816137113470902",
101 )
102 .unwrap();
103 let f3 = Fq::from_str(
104 "7301086967624450577859019086314322648061398679982346993011603220910508457334",
105 )
106 .unwrap();
107 assert!(!f1.is_zero());
108 assert!(!f2.is_zero());
109 assert!(!f3.is_zero());
110 assert_eq!(f1 / &f2, f3);
111}
112
113#[test]
114fn test_fq_sub() {
115 let f1 = Fq::from_str(
116 "18695869713129401390241150743745601908470616448391638969502807001833388904079",
117 )
118 .unwrap();
119 let f2 = Fq::from_str(
120 "10105476028534616828778879109836101003805485072436929139123765141153277007373",
121 )
122 .unwrap();
123 let f3 = Fq::from_str(
124 "8590393684594784561462271633909500904665131375954709830379041860680111896706",
125 )
126 .unwrap();
127 assert!(!f1.is_zero());
128 assert!(!f2.is_zero());
129 assert!(!f3.is_zero());
130 assert_eq!(f1 - &f2, f3);
131}
132
133#[test]
134fn test_fq_double_in_place_thrice() {
135 let mut f1 = Fq::from_str(
136 "32768907806651393940832831055386272949401004221411141755415956893066040832473",
137 )
138 .unwrap();
139 let f3 = Fq::from_str(
140 "21380590862979124081952185245260157621176025366712756262647409092194433207997",
141 )
142 .unwrap();
143 assert!(!f1.is_zero());
144 assert!(!f3.is_zero());
145 f1.double_in_place();
146 f1.double_in_place();
147 f1.double_in_place();
148 assert_eq!(f1, f3);
149}
150
151#[test]
152fn test_fq_generate_random_ed_on_bn254_point() {
153 let a = Fq::from_str("168700").unwrap();
154
155 let d = Fq::from_str("168696").unwrap();
156 let y = Fq::from_str(
157 "19987327827845206670850937090314462639017692512983955920885166014935289314257",
158 )
159 .unwrap();
160 let x2 = Fq::from_str(
161 "2144239075372598103060889495211040948751593385312551803225522963913923559328",
162 )
163 .unwrap();
164
165 let computed_y2 = y.square();
166 let y2 = Fq::from_str(
167 "11134206686211572308995578277928848431421308813024790181507137950838333998633",
168 )
169 .unwrap();
170 assert_eq!(y2, computed_y2);
171
172 let computed_dy2 = d * &computed_y2;
173 let dy2 =
174 Fq::from_str("345576003677591687256955722467813448317229128849323754147891993737799010947")
175 .unwrap();
176 assert_eq!(dy2, computed_dy2);
177
178 let computed_divisor = computed_dy2 - a;
179 let divisor =
180 Fq::from_str("345576003677591687256955722467813448317229128849323754147891993737798842247")
181 .unwrap();
182 assert_eq!(divisor, computed_divisor);
183
184 let computed_x2 = (computed_y2 - &Fq::one()) / &computed_divisor;
185 assert_eq!(x2, computed_x2);
186
187 let x = Fq::from_str(
188 "4801447892755635304907919953550459075619191823587157449340656925102682829025",
189 )
190 .unwrap();
191 let computed_x = computed_x2.sqrt().unwrap();
192 assert_eq!(computed_x.square(), x2);
193 assert_eq!(x, computed_x);
194
195 fn add<'a>(curr: (Fq, Fq), other: &'a (Fq, Fq)) -> (Fq, Fq) {
196 let y1y2 = curr.1 * &other.1;
197 let x1x2 = curr.0 * &other.0;
198 let a = Fq::from_str("168700").unwrap();
199 let d = Fq::from_str("168696").unwrap();
200 let dx1x2y1y2 = d * &y1y2 * &x1x2;
201
202 let d1 = Fq::one() + &dx1x2y1y2;
203 let d2 = Fq::one() - &dx1x2y1y2;
204
205 let x1y2 = curr.0 * &other.1;
206 let y1x2 = curr.1 * &other.0;
207
208 let x = (x1y2 + &y1x2) / &d1;
209 let y = (y1y2 - a * &x1x2) / &d2;
210
211 (x, y)
212 }
213
214 let result = add((x, y), &(x, y));
215 let result = add(result, &result);
216 let result = add(result, &result);
217
218 let point_x =
219 Fq::from_str("380676173762867192861894055350059333852732198308367125138259398265363727587")
220 .unwrap();
221 let point_y = Fq::from_str(
222 "8435074244857818446059206728316702149733931432112984450960434710303841866985",
223 )
224 .unwrap();
225 assert_eq!((point_x, point_y), result);
226}
227
228#[test]
229fn test_fq_square_in_place() {
230 let mut f1 = Fq::from_str(
231 "6060110850233386730847324622937480088943976359504617699731744947670229990461",
232 )
233 .unwrap();
234 let f3 = Fq::from_str(
235 "17018926051730832095053393285350575966874590491719897015583930476179087429684",
236 )
237 .unwrap();
238 assert!(!f1.is_zero());
239 assert!(!f3.is_zero());
240 f1.square_in_place();
241 assert_eq!(f1, f3);
242}
243
244#[test]
245fn test_fq_from_str() {
246 let f1_from_repr = Fq::from(BigInteger::new([
247 0xab8a2535947d1a77,
248 0x9ba74cbfda0bbcda,
249 0xe928b59724d60baf,
250 0x1cccaaeb9bb1680a,
251 ]));
252 let f1 = Fq::from_str(
253 "13026376210409056429264774981357153555336288129100724591327877625017068755575",
254 )
255 .unwrap();
256 let f2_from_repr = Fq::from(BigInteger::new([
257 0x97e9103775d2f35c,
258 0xbe6756b6c587544b,
259 0x6ee38c3afd88ef4b,
260 0x2bacd150f540c677,
261 ]));
262 let f2 = Fq::from_str(
263 "19754794831832707859764530223239420866832328728734160755396495950822165902172",
264 )
265 .unwrap();
266 assert_eq!(f1_from_repr, f1);
267 assert_eq!(f2_from_repr, f2);
268}
269
270#[test]
271fn test_fq_legendre() {
272 assert_eq!(QuadraticResidue, Fq::one().legendre());
273 assert_eq!(Zero, Fq::zero().legendre());
274
275 let e = BigInteger::new([
276 0x2e8de1a676c03be8,
277 0x73350d34fe25a560,
278 0x7ea085919029688e,
279 0x1d0868cb993cf28,
280 ]);
281 assert_eq!(QuadraticResidue, Fq::from(e).legendre());
282 let e = BigInteger::new([
283 0x891d8cc23c8d0706,
284 0xe91800e007db2698,
285 0xfff380321e9ac7a7,
286 0x2659e28bd17eab6,
287 ]);
288 assert_eq!(QuadraticNonResidue, Fq::from(e).legendre());
289}
290
291#[test]
292fn test_fr_add() {
293 let f1 = Fr::from(BigInteger::new([
294 0xccfc9a195e0f5c46,
295 0xaed4874d13fb1285,
296 0x27368f86ca2848eb,
297 0x4f8adcfeb44fccc,
298 ]));
299 let f2 = Fr::from(BigInteger::new([
300 0x661ff05bf8570851,
301 0x1b171f4c59be97ef,
302 0x5d2ce7f9b4d701f3,
303 0x1e0e794623e0f68,
304 ]));
305 let f3 = Fr::from(BigInteger::new([
306 0xcba9f2991d453da6,
307 0x1eacb8e13498bc6a,
308 0x4d596ec9aecf1fd3,
309 0xcd0b95f15cd82f,
310 ]));
311 assert_eq!(f1 + &f2, f3);
312}
313
314#[test]
315fn test_fr_mul() {
316 let f1 = Fr::from(BigInteger::new([
317 0xc2964d2dd5fb980f,
318 0xbab64d599c57e496,
319 0x39cae13e7d1d4f78,
320 0x1aa995aa4de205c,
321 ]));
322 let f2 = Fr::from(BigInteger::new([
323 0xc256e720cd43533b,
324 0x3bfbadf6247e13bb,
325 0x94c3d63a53714f63,
326 0x10f8a7bf74efd57,
327 ]));
328 let f3 = Fr::from(BigInteger::new([
329 0x5eac88be41e0e1fd,
330 0x57aab36675b11e24,
331 0x835582d896b4d13f,
332 0x4808736e213036e,
333 ]));
334 assert_eq!(f1 * &f2, f3);
335}
336
337#[test]
338fn test_fr_from_str() {
339 let f100_from_repr = Fr::from(BigInteger::new([0x64, 0, 0, 0]));
340 let f100 = Fr::from_str("100").unwrap();
341 assert_eq!(f100_from_repr, f100);
342}