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