FK20 CUDA
fptest_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 "fp.cuh"
6 #include "fptest.cuh"
7 
16 __global__ void FpTestAdd(testval_t *testval) {
17 
18  printf("=== RUN %s\n", __func__);
19 
20  bool pass = true;
21  size_t count = 0;
22 
23  fp_t x, l, r;
24 
25  // 2x + x == 3x
26 
27  for (int i=0; pass && i<TESTVALS; i++) {
28  fp_cpy(x, testval[i]);
29 
30  fp_x2(l, x);
31  fp_add(l, l, x);
32 
33  fp_x3(r, x);
34 
35  if (fp_neq(l, r)) {
36  pass = false;
37 
38  printf("%d: FAILED\n", i);
39  fp_print("x : ", x);
40  fp_print("2x+x : ", l);
41  fp_print("3x : ", r);
42  }
43  ++count;
44  }
45 
46  printf("%ld tests\n", count);
47 
48  PRINTPASS(pass);
49 }
50 
60 
61  printf("=== RUN %s\n", __func__);
62 
63  bool pass = true;
64  size_t count = 0;
65 
66  for (int i=0; i<TESTVALS; i++) {
67  for (int j=0; j<TESTVALS; j++) {
68  fp_t x, y;
69 
70  fp_cpy(x, testval[i]);
71  fp_cpy(y, testval[j]);
72 
73  fp_add(x, x, testval[j]); // x + y
74  fp_add(y, y, testval[i]); // y + x
75 
76  if (fp_neq(x, y)) {
77  pass = false;
78 
79  printf("%d,%d: FAILED: inconsistent result\n", i, j);
80  fp_print("x = ", testval[i]);
81  fp_print("y = ", testval[j]);
82  fp_print("x+y = ", x);
83  fp_print("y+x = ", y);
84  }
85  ++count;
86  }
87  }
88  printf("%ld tests\n", count);
89 
90  PRINTPASS(pass);
91 }
92 
102 
103  printf("=== RUN %s\n", __func__);
104 
105  bool pass = true;
106  size_t count = 0;
107 
108  for (int i=0; i<TESTVALS; i++) {
109  for (int j=0; j<TESTVALS; j++) {
110  for (int k=0; k<TESTVALS; k++) {
111  fp_t a, b, c;
112 
113  fp_cpy(a, testval[i]); // x
114  fp_cpy(b, testval[j]); // y
115  fp_cpy(c, testval[i]); // x
116 
117  fp_add(a, a, testval[j]); // x + y
118  fp_add(a, a, testval[k]); // (x + y) + z
119 
120  fp_add(b, b, testval[k]); // y + z
121  fp_add(c, c, b); // x + (y + z)
122 
123  if (fp_neq(a, c)) {
124  pass = false;
125 
126  printf("%d,%d,%d: FAILED: inconsistent result\n", i, j, k);
127  fp_print("x = ", testval[i]);
128  fp_print("y = ", testval[j]);
129  fp_print("z = ", testval[k]);
130  fp_print("(x+y)+z = ", a);
131  fp_print("x+(y+z) = ", c);
132  }
133  ++count;
134  }
135  }
136  }
137  printf("%ld tests\n", count);
138 
139  PRINTPASS(pass);
140 }
141 
142 // vim: ts=4 et sw=4 si
__device__ void fp_print(const char *s, const fp_t &x)
Prints the canonical representation of x to STDOUT.
Definition: fp.cu:39
__device__ bool fp_neq(const fp_t &x, const fp_t &y)
Compares two fp_t residues.
Definition: fp_neq.cu:14
__device__ void fp_add(fp_t &z, const fp_t &x, const fp_t &y)
Computes the sum of two residues x and y modulo p and stores it in z. Device only function.
Definition: fp_add.cu:17
__device__ void fp_x2(fp_t &z, const fp_t &x)
Multiplies x by 2 and stores the result into z.
Definition: fp_x2.cu:15
uint64_t fp_t[6]
Residue modulo p. Any 384-bit representative of each residue is allowed, and stored as a 6-element li...
Definition: fp.cuh:14
__device__ __host__ void fp_cpy(fp_t &z, const fp_t &x)
Copy from x into z.
Definition: fp_cpy.cu:14
__device__ void fp_x3(fp_t &z, const fp_t &x)
Multiplies x by 3 and stores the result into z.
Definition: fp_x3.cu:15
__managed__ testval_t testval[TESTVALS]
Definition: fptest.cu:8
#define TESTVALS
Definition: fptest.cuh:13
__global__ void FpTestAssociativeAdd(testval_t *testval)
Test for the associative property of addition in Fp.
Definition: fptest_add.cu:101
__global__ void FpTestAdd(testval_t *testval)
Test for addition in Fp.
Definition: fptest_add.cu:16
__global__ void FpTestCommutativeAdd(testval_t *testval)
Test for the commutative property of addition in Fp.
Definition: fptest_add.cu:59
#define PRINTPASS(pass)
Definition: test.h:25