FK20 CUDA
frtest_mul.cu
Go to the documentation of this file.
1 // bls12_381: Arithmetic for BLS12-381
2 // Copyright 2022-2023 Dag Arne Osvik
3 // Copyright 2022-2023 Luan Cardoso dos Santos
4 
5 #include "fr.cuh"
6 #include "frtest.cuh"
7 
16 
17  printf("=== RUN %s\n", __func__);
18 
19  bool pass = true;
20  size_t count = 0;
21 
22  for (int i=0; i<TESTVALS; i++) {
23  for (int j=0; j<TESTVALS; j++) {
24  fr_t x, y;
25 
26  fr_cpy(x, testval[i]);
27  fr_cpy(y, testval[j]);
28 
29  fr_mul(x, testval[j]); // x * y
30  fr_mul(y, testval[i]); // y * x
31 
32  if (fr_neq(x, y)) {
33  pass = false;
34 
35  printf("%d,%d: FAILED: inconsistent result\n", i, j);
36  fr_print("x = ", testval[i]);
37  fr_print("y = ", testval[j]);
38  fr_print("x*y = ", x);
39  fr_print("y*x = ", y);
40  }
41  ++count;
42  }
43  }
44  printf("%ld tests\n", count);
45 
46  PRINTPASS(pass);
47 }
48 
57 
58  printf("=== RUN %s\n", __func__);
59 
60  bool pass = true;
61  size_t count = 0;
62 
63  for (int i=0; i<TESTVALS; i++) {
64  for (int j=0; j<TESTVALS; j++) {
65  for (int k=0; k<TESTVALS; k++) {
66  fr_t a, b, c;
67 
68  fr_cpy(a, testval[i]); // x
69  fr_cpy(b, testval[j]); // y
70  fr_cpy(c, testval[i]); // x
71 
72  fr_mul(a, testval[j]); // x * y
73  fr_mul(a, testval[k]); // (x * y) * z
74 
75  fr_mul(b, testval[k]); // y * z
76  fr_mul(c, b); // x * (y * z)
77 
78  if (fr_neq(a, c)) {
79  pass = false;
80 
81  printf("%d,%d,%d: FAILED: inconsistent result\n", i, j, k);
82  fr_print("x = ", testval[i]);
83  fr_print("y = ", testval[j]);
84  fr_print("z = ", testval[k]);
85  fr_print("(x*y)*z = ", a);
86  fr_print("x*(y*z) = ", c);
87  }
88  ++count;
89  }
90  }
91  }
92  printf("%ld tests\n", count);
93 
94  PRINTPASS(pass);
95 }
96 
97 // vim: ts=4 et sw=4 si
__managed__ testval_t testval[TESTVALS]
Definition: fptest.cu:8
#define TESTVALS
Definition: fptest.cuh:13
__device__ void fr_print(const char *s, const fr_t &x)
prints the canonical representation of x to STDOUT.
Definition: fr.cu:41
__device__ bool fr_neq(const fr_t &x, const fr_t &y)
Compares two fr_t residues.
Definition: fr_neq.cu:15
uint64_t fr_t[4]
Subgroup element stored as a 256-bit array (a 4-element little-endian array of uint64_t)....
Definition: fr.cuh:24
__device__ __host__ void fr_cpy(fr_t &z, const fr_t &x)
Copy from x into z.
Definition: fr_cpy.cu:14
__device__ void fr_mul(fr_t &z, const fr_t &x)
Multiply two residues module r z and x, stores back into z.
Definition: fr_mul.cu:13
__global__ void FrTestAssociativeMul(testval_t *testval)
Test of the associative property of multiplication (x*y)*z == x*(y*z)
Definition: frtest_mul.cu:56
__global__ void FrTestCommutativeMul(testval_t *testval)
Test of the commutative property of multiplication x*y == y*x.
Definition: frtest_mul.cu:15
#define PRINTPASS(pass)
Definition: test.h:25