arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
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_ec::AdditiveGroup;
  4use ark_ff::{
  5    biginteger::BigInteger256 as BigInteger,
  6    fields::{Field, LegendreSymbol::*},
  7    One, Zero,
  8};
  9use ark_std::str::FromStr;
 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        "5737174750126493097140088368381404874517028777389495743035013590241325924603",
 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        "38057113854472161555556064369220825628027487067886761874351491955834635348140",
 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        "18897508522635316277030308074760673440128491438505204942623624791502972539393",
 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        "29049672724678710659792141917402891276693777283079976086581207190825261000580",
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        "52407761752706389608871686410346320244445823769178582752913020344774001921732",
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_bls12_381_point() {
153    let d = Fq::from_str(
154        "19257038036680949359750312669786877991949435402254120286184196891950884077233",
155    )
156    .unwrap();
157    let y = Fq::from_str(
158        "20269054604167148422407276086932743904275456233139568486008667107872965128512",
159    )
160    .unwrap();
161    let x2 = Fq::from_str(
162        "35041048504708632193693740149219726446678304552734087046982753200179718192840",
163    )
164    .unwrap();
165
166    let computed_y2 = y.square();
167    let y2 = Fq::from_str(
168        "22730681238307918419349440108285755984465605552827817317611903495170775437833",
169    )
170    .unwrap();
171    assert_eq!(y2, computed_y2);
172
173    let computed_dy2 = d * &computed_y2;
174    let dy2 = Fq::from_str(
175        "24720347560552809545835752815204882739669031262711919770503096707526812943411",
176    )
177    .unwrap();
178    assert_eq!(dy2, computed_dy2);
179
180    let computed_divisor = computed_dy2 + &Fq::one();
181    let divisor = Fq::from_str(
182        "24720347560552809545835752815204882739669031262711919770503096707526812943412",
183    )
184    .unwrap();
185    assert_eq!(divisor, computed_divisor);
186
187    let computed_x2 = (computed_y2 - &Fq::one()) / &computed_divisor;
188    assert_eq!(x2, computed_x2);
189
190    let x = Fq::from_str(
191        "15337652609730546173818014678723269532482775720866471265774032070871608223361",
192    )
193    .unwrap();
194    let computed_x = computed_x2.sqrt().unwrap();
195    assert_eq!(computed_x.square(), x2);
196    assert_eq!(x, computed_x);
197
198    fn add<'a>(curr: (Fq, Fq), other: &'a (Fq, Fq)) -> (Fq, Fq) {
199        let y1y2 = curr.1 * &other.1;
200        let x1x2 = curr.0 * &other.0;
201        let d = Fq::from_str(
202            "19257038036680949359750312669786877991949435402254120286184196891950884077233",
203        )
204        .unwrap();
205        let dx1x2y1y2 = d * &y1y2 * &x1x2;
206
207        let d1 = Fq::one() + &dx1x2y1y2;
208        let d2 = Fq::one() - &dx1x2y1y2;
209
210        let x1y2 = curr.0 * &other.1;
211        let y1x2 = curr.1 * &other.0;
212
213        let x = (x1y2 + &y1x2) / &d1;
214        let y = (y1y2 + &x1x2) / &d2;
215
216        (x, y)
217    }
218
219    let result = add((x, y), &(x, y));
220    let result = add(result, &result);
221    let result = add(result, &result);
222
223    let point_x = Fq::from_str(
224        "47259664076168047050113154262636619161204477920503059672059915868534495873964",
225    )
226    .unwrap();
227    let point_y = Fq::from_str(
228        "19016409245280491801573912449420132838852726543024859389273314249842195919690",
229    )
230    .unwrap();
231    assert_eq!((point_x, point_y), result);
232}
233
234#[test]
235fn test_fq_square_in_place() {
236    let mut f1 = Fq::from_str(
237        "34864651240005695523200639428464570946052769938774601449735727714436878540682",
238    )
239    .unwrap();
240    let f3 =
241        Fq::from_str("213133100629336594719108316042277780359104840987226496279264105585804377948")
242            .unwrap();
243    assert!(!f1.is_zero());
244    assert!(!f3.is_zero());
245    f1.square_in_place();
246    assert_eq!(f1, f3);
247}
248
249#[test]
250fn test_fq_from_str() {
251    let f1_from_repr = Fq::from(BigInteger::new([
252        0xab8a2535947d1a77,
253        0x9ba74cbfda0bbcda,
254        0xe928b59724d60baf,
255        0x1cccaaeb9bb1680a,
256    ]));
257    let f1 = Fq::from_str(
258        "13026376210409056429264774981357153555336288129100724591327877625017068755575",
259    )
260    .unwrap();
261    let f2_from_repr = Fq::from(BigInteger::new([
262        0x97e9103775d2f35c,
263        0xbe6756b6c587544b,
264        0x6ee38c3afd88ef4b,
265        0x2bacd150f540c677,
266    ]));
267    let f2 = Fq::from_str(
268        "19754794831832707859764530223239420866832328728734160755396495950822165902172",
269    )
270    .unwrap();
271    assert_eq!(f1_from_repr, f1);
272    assert_eq!(f2_from_repr, f2);
273}
274
275#[test]
276fn test_fq_legendre() {
277    assert_eq!(QuadraticResidue, Fq::one().legendre());
278    assert_eq!(Zero, Fq::zero().legendre());
279
280    let e = BigInteger::new([
281        0x0dbc5349cd5664da,
282        0x8ac5b6296e3ae29d,
283        0x127cb819feceaa3b,
284        0x3a6b21fb03867191,
285    ]);
286    assert_eq!(QuadraticResidue, Fq::from(e).legendre());
287    let e = BigInteger::new([
288        0x96341aefd047c045,
289        0x9b5f4254500a4d65,
290        0x1ee08223b68ac240,
291        0x31d9cd545c0ec7c6,
292    ]);
293    assert_eq!(QuadraticNonResidue, Fq::from(e).legendre());
294}
295
296#[test]
297fn test_fr_add() {
298    let f1 = Fr::from(BigInteger::new([
299        0xc81265fb4130fe0c,
300        0xb308836c14e22279,
301        0x699e887f96bff372,
302        0x84ecc7e76c11ad,
303    ]));
304    let f2 = Fr::from(BigInteger::new([
305        0x71875719b422efb8,
306        0x43658e68a93612,
307        0x9fa756be2011e833,
308        0xaa2b2cb08dac497,
309    ]));
310    let f3 = Fr::from(BigInteger::new([
311        0x3999bd14f553edc4,
312        0xb34be8fa7d8b588c,
313        0x945df3db6d1dba5,
314        0xb279f92f046d645,
315    ]));
316    assert_eq!(f1 + &f2, f3);
317}
318
319#[test]
320fn test_fr_mul() {
321    let f1 = Fr::from(BigInteger::new([
322        0xc81265fb4130fe0c,
323        0xb308836c14e22279,
324        0x699e887f96bff372,
325        0x84ecc7e76c11ad,
326    ]));
327    let f2 = Fr::from(BigInteger::new([
328        0x71875719b422efb8,
329        0x43658e68a93612,
330        0x9fa756be2011e833,
331        0xaa2b2cb08dac497,
332    ]));
333    let f3 = Fr::from(BigInteger::new([
334        0x6d6618ac6b4a8381,
335        0x5b9eb35d711ee1da,
336        0xce83310e6ac4105d,
337        0x98032e0f206320a,
338    ]));
339    assert_eq!(f1 * &f2, f3);
340}
341
342#[test]
343fn test_fr_from_str() {
344    let f100_from_repr = Fr::from(BigInteger::new([0x64, 0, 0, 0]));
345    let f100 = Fr::from_str("100").unwrap();
346    assert_eq!(f100_from_repr, f100);
347}