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