FK20 CUDA
fptest_distributive.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 
17 
18  printf("=== RUN %s\n", __func__);
19 
20  bool pass = true;
21  size_t count = 0;
22  fp_t a, b, c, u, v, w;
23 
24  for (int i=0; i<TESTVALS; i++) {
25  fp_cpy(a, testval[i]);
26 
27  for (int j=0; j<TESTVALS; j++) {
28  fp_cpy(b, testval[j]);
29 
30  for (int k=j; k<TESTVALS; k++) {
31  fp_cpy(c, testval[k]);
32 
33  fp_cpy(u, a);
34  fp_mul(u, u, b); // ab
35 
36  fp_cpy(v, a);
37  fp_mul(v, v, c); // ac
38 
39  fp_add(u, u, v); // ab+ac
40 
41  fp_cpy(v, a);
42  fp_cpy(w, b);
43  fp_add(w, w, c); // b+c
44  fp_mul(v, v, w); // a(b+c)
45 
46  if (fp_neq(u, v)) {
47  pass = false;
48 
49  printf("%d,%d: FAILED: inconsistent result\n", i, j);
50  fp_print("a = ", testval[i]);
51  fp_print("b = ", testval[j]);
52  fp_print("c = ", testval[k]);
53  fp_print("ab+ac = ", u);
54  fp_print("a(b+c) = ", v);
55  }
56  ++count;
57  }
58  }
59  }
60  printf("%ld tests\n", count);
61 
62  PRINTPASS(pass);
63 }
64 
74 
75  printf("=== RUN %s\n", __func__);
76 
77  bool pass = true;
78  size_t count = 0;
79  fp_t a, b, c, u, v;
80 
81  for (int i=0; i<TESTVALS; i++) {
82  fp_cpy(a, testval[i]);
83 
84  for (int j=i; j<TESTVALS; j++) {
85  fp_cpy(b, testval[j]);
86 
87  for (int k=0; k<TESTVALS; k++) {
88  fp_cpy(c, testval[k]);
89 
90  fp_cpy(u, a);
91  fp_mul(u, u, c); // ac
92 
93  fp_cpy(v, b);
94  fp_mul(v, v, c); // bc
95 
96  fp_add(u, u, v); // ac+bc
97 
98  fp_cpy(v, a);
99  fp_add(v, v, b); // a+b
100  fp_mul(v, v, c); // (a+b)c
101 
102  if (fp_neq(u, v)) {
103  pass = false;
104 
105  printf("%d,%d: FAILED: inconsistent result\n", i, j);
106  fp_print("a = ", testval[i]);
107  fp_print("b = ", testval[j]);
108  fp_print("c = ", testval[k]);
109  fp_print("ac+bc = ", u);
110  fp_print("(a+b)c = ", v);
111  }
112  ++count;
113  }
114  }
115  }
116  printf("%ld tests\n", count);
117 
118  PRINTPASS(pass);
119 }
120 
130 
131  printf("=== RUN %s\n", __func__);
132 
133  bool pass = true;
134  size_t count = 0;
135  fp_t a, b, c, u, v, w;
136 
137  for (int i=0; i<TESTVALS; i++) {
138  fp_cpy(a, testval[i]);
139 
140  for (int j=0; j<TESTVALS; j++) {
141  fp_cpy(b, testval[j]);
142 
143  for (int k=0; k<TESTVALS; k++) {
144  fp_cpy(c, testval[k]);
145 
146  fp_cpy(u, a);
147  fp_mul(u, u, b); // ab
148 
149  fp_cpy(v, a);
150  fp_mul(v, v, c); // ac
151 
152  fp_sub(u, u, v); // ab-ac
153 
154  fp_cpy(v, a);
155  fp_cpy(w, b);
156  fp_sub(w, w, c); // b-c
157  fp_mul(v, v, w); // a(b-c)
158 
159  if (fp_neq(u, v)) {
160  pass = false;
161 
162  printf("%d,%d: FAILED: inconsistent result\n", i, j);
163  fp_print("a = ", testval[i]);
164  fp_print("b = ", testval[j]);
165  fp_print("c = ", testval[k]);
166  fp_print("ab-ac = ", u);
167  fp_print("a(b-c) = ", v);
168  }
169  ++count;
170  }
171  }
172  }
173  printf("%ld tests\n", count);
174 
175  PRINTPASS(pass);
176 }
177 
187 
188  printf("=== RUN %s\n", __func__);
189 
190  bool pass = true;
191  size_t count = 0;
192  fp_t a, b, c, u, v;
193 
194  for (int i=0; i<TESTVALS; i++) {
195  fp_cpy(a, testval[i]);
196 
197  for (int j=0; j<TESTVALS; j++) {
198  fp_cpy(b, testval[j]);
199 
200  for (int k=0; k<TESTVALS; k++) {
201  fp_cpy(c, testval[k]);
202 
203  fp_cpy(u, a);
204  fp_mul(u, u, c); // ac
205 
206  fp_cpy(v, b);
207  fp_mul(v, v, c); // bc
208 
209  fp_sub(u, u, v); // ac-bc
210 
211  fp_cpy(v, a);
212  fp_sub(v, v, b); // a-b
213  fp_mul(v, v, c); // (a-b)c
214 
215  if (fp_neq(u, v)) {
216  pass = false;
217 
218  printf("%d,%d: FAILED: inconsistent result\n", i, j);
219  fp_print("a = ", testval[i]);
220  fp_print("b = ", testval[j]);
221  fp_print("c = ", testval[k]);
222  fp_print("ac-bc = ", u);
223  fp_print("(a-b)c = ", v);
224  }
225  ++count;
226  }
227  }
228  }
229  printf("%ld tests\n", count);
230 
231  PRINTPASS(pass);
232 }
233 
234 // 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
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__ void fp_mul(fp_t &z, const fp_t &x, const fp_t &y)
Multiplies two Fp residues x and y, stores in z.
Definition: fp_mul.cu:17
__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_sub(fp_t &z, const fp_t &x, const fp_t &y)
Calculates the difference of two residues modulo p and stores it into z.
Definition: fp_sub.cu:16
__managed__ testval_t testval[TESTVALS]
Definition: fptest.cu:8
#define TESTVALS
Definition: fptest.cuh:13
__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):
#define PRINTPASS(pass)
Definition: test.h:25