FK20 CUDA
fptest.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 
8 __managed__ testval_t testval[TESTVALS];
9 
11 
16 void init() {
17 
18  testinit();
19 
20  printf("%s\n", __func__);
21 
22  uint64_t
23  p0 = 0xB9FEFFFFFFFFAAAB,
24  p1 = 0x1EABFFFEB153FFFF,
25  p2 = 0x6730D2A0F6B0F624,
26  p3 = 0x64774B84F38512BF,
27  p4 = 0x4B1BA7B6434BACD7,
28  p5 = 0x1A0111EA397FE69A;
29 
30  int i = 0;
31 
32  for (int j=0; j<TESTVALS; j++) {
33  testval[j][0] = 0;
34  testval[j][1] = 0;
35  testval[j][2] = 0;
36  testval[j][3] = 0;
37  testval[j][4] = 0;
38  testval[j][5] = 0;
39  }
40 
41  {
42  testval[i][0] = p0;
43  testval[i][1] = p1;
44  testval[i][2] = p2;
45  testval[i][3] = p3;
46  testval[i][4] = p4;
47  testval[i][5] = p5;
48  }
49  i++;
50 
51  {
52  testval[i][0] = ~p0;
53  testval[i][1] = ~p1;
54  testval[i][2] = ~p2;
55  testval[i][3] = ~p3;
56  testval[i][4] = ~p4;
57  testval[i][5] = ~p5;
58  }
59  i++;
60 
61  i++; // The third value is 0
62 
63  for (int j=0; j<64; i++,j++) { testval[i][0] = 1ULL << j; }
64  for (int j=0; j<64; i++,j++) { testval[i][1] = 1ULL << j; }
65  for (int j=0; j<64; i++,j++) { testval[i][2] = 1ULL << j; }
66  for (int j=0; j<64; i++,j++) { testval[i][3] = 1ULL << j; }
67  for (int j=0; j<64; i++,j++) { testval[i][4] = 1ULL << j; }
68  for (int j=0; j<64; i++,j++) { testval[i][5] = 1ULL << j; }
69 
70  for (int j=2; j<386; i++,j++) {
71  testval[i][0] = ~testval[j][0];
72  testval[i][1] = ~testval[j][1];
73  testval[i][2] = ~testval[j][2];
74  testval[i][3] = ~testval[j][3];
75  testval[i][4] = ~testval[j][4];
76  testval[i][5] = ~testval[j][5];
77  }
78 
79  FILE *pf = fopen("/dev/urandom", "r");
80 
81  if (!pf)
82  return;
83 
84  size_t result = fread(&testval[i], sizeof(testval_t), TESTVALS-i, pf);
85 
86  printf("Fixed/random test values: %d/%d\n", i, TESTVALS-i);
87 }
88 
90 
91 #define TEST(X) \
92  start = clock(); \
93  X <<<1,block>>> (&testval[0]); \
94  err = cudaDeviceSynchronize(); \
95  end = clock(); \
96  if (err != cudaSuccess) printf("Error %d\n", err); \
97  printf(" (%.2f s)\n", (end - start) * (1.0 / CLOCKS_PER_SEC));
98 
100 
108 int main(int argc, char **argv) {
109  clock_t start, end;
110  cudaError_t err;
111 
112  int level = 0;
113 
114  if (argc > 1)
115  level = atoi(argv[1]);
116 
117  init();
118 
119  dim3 block = 1;
120 
121  TEST(FpTestKAT);
122  if (err != cudaSuccess) {
123  return err;
124  }
125 
127 
128  if (level >= 1) {
129  TEST(FpTestCmp);
131  TEST(FpTestAdd);
132  TEST(FpTestSub);
133  TEST(FpTestSqr);
134  TEST(FpTestMul);
135  TEST(FpTestInv);
136  }
137 
138  if (level >= 2) {
139  TEST(FpTestSqr2);
142  }
143 
144  if (level >= 3) {
146  TEST(FpTestMMA);
152  }
153 
154  /*
155  TEST(FpTestCopy);
156  TEST(FpTestNeg);
157  TEST(FpTestDouble);
158  TEST(FpTestTriple);
159  TEST(FpTestAdd);
160  TEST(FpTestSub);
161  TEST(FpTestSquare);
162  TEST(FpTestMul);
163 
164  TEST(FpTestReflexivity);
165  TEST(FpTestSymmetry);
166  TEST(FpTestAdditiveIdentity);
167  TEST(FpTestMultiplicativeIdentity);
168  TEST(FpTestAdditiveInverse);
169  TEST(FpTestMultiplicativeInverse);
170  */
171 
172  err = cudaDeviceSynchronize();
173 
174  if (err != cudaSuccess)
175  fprintf(stderr, "Error %d\n", err);
176 
177  return err;
178 }
179 
180 // vim: ts=4 et sw=4 si
void init()
Variable initialization for the tests.
Definition: fptest.cu:16
int main(int argc, char **argv)
Run tests on Fp functions.
Definition: fptest.cu:108
#define TEST(X)
Definition: fptest.cu:91
__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
__global__ void FpTestCmp(testval_t *testval)
Test for the comparison function in Fp; checks for inconsistencies in the following properties:
Definition: fptest_cmp.cu:21
__global__ void FpTestSubDistributiveRight(testval_t *testval)
Check the distributive property of multiplication in Fp (right of subtraction):
__global__ void FpTestSubDistributiveLeft(testval_t *testval)
Check the distributive property of multiplication in Fp (left of subtraction):
__global__ void FpTestAddDistributiveLeft(testval_t *testval)
Check the distributive property of multiplication in Fp (left of addition):
__global__ void FpTestAddDistributiveRight(testval_t *testval)
Check the distributive property of multiplication in Fp (right of addition):
__global__ void FpTestFibonacci(testval_t *)
Test addition and subtraction in Fp using a fibonacci sequence (chain dependency) from 1 to ITERATION...
__global__ void FpTestInv(testval_t *testval)
Test for multiplicative inverse mod p in Fp.
Definition: fptest_inv.cu:15
__global__ void FpTestKAT(testval_t *)
Test for fp functions using KAT.
Definition: fptest_kat.cu:18
__global__ void FpTestMMA(testval_t *testval)
Test for multiply-multiply-add. Compare with current standalone implementation of multiplication adn ...
Definition: fptest_mma.cu:15
__global__ void FpTestMul(testval_t *testval)
Multiplication test, comparing with the native uint64_t multiplication.
Definition: fptest_mul.cu:109
__global__ void FpTestAssociativeMul(testval_t *testval)
Test for the associative property of multiplication.
Definition: fptest_mul.cu:60
__global__ void FpTestCommutativeMul(testval_t *testval)
Test for the commutative property of addition.
Definition: fptest_mul.cu:16
__global__ void FpTestMulConst(testval_t *testval)
Test self consistency in multiplication by constant:
__global__ void FpTestSqr2(testval_t *testval)
Test for squaring on Fp. Checks for self consistency:
Definition: fptest_sqr.cu:135
__global__ void FpTestSqr(testval_t *testval)
Test for squaring on Fp. Checks for self consistency:
Definition: fptest_sqr.cu:16
__global__ void FpTestSub(testval_t *testval)
Test for subtraction in Fp.
Definition: fptest_sub.cu:16
void testinit()
Sets a global variable to true if the STDOUT is a terminal. Needs to be done like so because while a ...
Definition: test.cu:18