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