FK20 CUDA
frtest_add.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 
17 
18  printf("=== RUN %s\n", __func__);
19 
20  bool pass = true;
21  size_t count = 0;
22 
23  for (int i=0; i<TESTVALS; i++) {
24  for (int j=0; j<TESTVALS; j++) {
25  fr_t x, y;
26 
27  fr_cpy(x, testval[i]);
28  fr_cpy(y, testval[j]);
29 
30  fr_add(x, testval[j]); // x + y
31  fr_add(y, testval[i]); // y + x
32 
33  if (fr_neq(x, y)) {
34  pass = false;
35 
36  printf("%d,%d: FAILED: inconsistent result\n", i, j);
37  fr_print("x = ", testval[i]);
38  fr_print("y = ", testval[j]);
39  fr_print("x+y = ", x);
40  fr_print("y+x = ", y);
41  }
42  ++count;
43  }
44  }
45  printf("%ld tests\n", count);
46 
47  PRINTPASS(pass);
48 }
49 
59 
60  printf("=== RUN %s\n", __func__);
61 
62  bool pass = true;
63  size_t count = 0;
64 
65  for (int i=0; i<TESTVALS; i++) {
66  for (int j=0; j<TESTVALS; j++) {
67  for (int k=0; k<TESTVALS; k++) {
68  fr_t a, b, c;
69 
70  fr_cpy(a, testval[i]); // x
71  fr_cpy(b, testval[j]); // y
72  fr_cpy(c, testval[i]); // x
73 
74  fr_add(a, testval[j]); // x + y
75  fr_add(a, testval[k]); // (x + y) + z
76 
77  fr_add(b, testval[k]); // y + z
78  fr_add(c, b); // x + (y + z)
79 
80  if (fr_neq(a, c)) {
81  pass = false;
82 
83  printf("%d,%d,%d: FAILED: inconsistent result\n", i, j, k);
84  fr_print("x = ", testval[i]);
85  fr_print("y = ", testval[j]);
86  fr_print("z = ", testval[k]);
87  fr_print("(x+y)+z = ", a);
88  fr_print("x+(y+z) = ", c);
89  }
90  ++count;
91  }
92  }
93  }
94  printf("%ld tests\n", count);
95 
96  PRINTPASS(pass);
97 }
98 
99 // 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_add(fr_t &z, const fr_t &x)
Computes the sum of two residues x and z modulo r and stores it in z. Device only function.
Definition: fr_add.cu:16
__global__ void FrTestCommutativeAdd(testval_t *testval)
Test the cumulative property of addition.
Definition: frtest_add.cu:16
__global__ void FrTestAssociativeAdd(testval_t *testval)
Test the associative property of addition.
Definition: frtest_add.cu:58
#define PRINTPASS(pass)
Definition: test.h:25