FK20 CUDA
fptest_sqr.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 FpTestSqr(testval_t *testval) {
17 
18  printf("=== RUN %s\n", __func__);
19 
20  bool pass = true;
21  size_t count = 0;
22 
23  const fp_t
24  _1 = { 1, 0, 0, 0, 0, 0 },
25  _2 = { 2, 0, 0, 0, 0, 0 },
26  _4 = { 4, 0, 0, 0, 0, 0 },
27  _6 = { 6, 0, 0, 0, 0, 0 },
28  _16 = { 16, 0, 0, 0, 0, 0 },
29  _36 = { 36, 0, 0, 0, 0, 0 };
30 
31  fp_t x, xsqr, x2, x4, x8, x12, l, r;
32 
33  // (x+n)^2 == x^2 + 2nx + n^2
34 
35  for (int i=0; pass && i<TESTVALS; i++) {
36 
37  fp_cpy(x, testval[i]);
38 
39  fp_sqr(xsqr, x);
40  fp_x2(x2, x); // n = 1
41  fp_x4(x4, x); // n = 2
42  fp_x8(x8, x); // n = 4
43  fp_x12(x12, x); // n = 6
44 
45  // l = (x+1)^2
46  fp_add(l, x, _1);
47  fp_sqr(l, l);
48 
49  // r = x^2 + 2x + 1
50  fp_add(r, xsqr, x2);
51  fp_add(r, r, _1);
52 
53  if (fp_neq(l, r)) {
54  pass = false;
55 
56  printf("%d: FAILED\n", i);
57  fp_print("x : ", x);
58  fp_print("(x+1)^2 : ", l);
59  fp_print("x^2+2x+1 : ", r);
60  break;
61  }
62  ++count;
63 
64  // l = (x+2)^2
65  fp_add(l, x, _2);
66  fp_sqr(l, l);
67 
68  // r = x^2 + 4x + 4
69  fp_add(r, xsqr, x4);
70  fp_add(r, r, _4);
71 
72  if (fp_neq(l, r)) {
73  pass = false;
74 
75  printf("%d: FAILED\n", i);
76  fp_print("x : ", x);
77  fp_print("(x+2)^2 : ", l);
78  fp_print("x^2+4x+4 : ", r);
79  break;
80  }
81  ++count;
82 
83  // l = (x+4)^2
84  fp_add(l, x, _4);
85  fp_sqr(l, l);
86 
87  // r = x^2 + 8x + 16
88  fp_add(r, xsqr, x8);
89  fp_add(r, r, _16);
90 
91  if (fp_neq(l, r)) {
92  pass = false;
93 
94  printf("%d: FAILED\n", i);
95  fp_print("x : ", x);
96  fp_print("(x+4)^2 : ", l);
97  fp_print("x^2+8x+16 : ", r);
98  break;
99  }
100  ++count;
101 
102  // l = (x+6)^2
103  fp_add(l, x, _6);
104  fp_sqr(l, l);
105 
106  // r = x^2 + 12x + 36
107  fp_add(r, xsqr, x12);
108  fp_add(r, r, _36);
109 
110  if (fp_neq(l, r)) {
111  pass = false;
112 
113  printf("%d: FAILED\n", i);
114  fp_print("x : ", x);
115  fp_print("(x+6)^2 : ", l);
116  fp_print("x^2+12x+36 : ", r);
117  break;
118  }
119  ++count;
120  }
121 
122  printf("%ld tests\n", count);
123 
124  PRINTPASS(pass);
125 }
126 
135 __global__ void FpTestSqr2(testval_t *testval) {
136 
137  printf("=== RUN %s\n", __func__);
138 
139  bool pass = true;
140  size_t count = 0;
141 
142  fp_t x, xsqr, x2, y, l, r;
143 
144  // (x+y)^2 == x^2 + 2xy + y^2
145 
146  for (int i=0; pass && i<TESTVALS; i++) {
147  fp_cpy(x, testval[i]);
148  fp_sqr(xsqr, x);
149  fp_x2(x2, x);
150 
151  for (int j=i; pass && j<TESTVALS; j++) {
152 
153  // l = (x+y)^2
154  fp_add(l, x, y);
155  fp_sqr(l, l);
156 
157  // r = x^2 + 2xy + y^2
158  fp_add(r, x2, y); // 2x+y
159  fp_mul(r, r, y); // 2xy+y^2
160  fp_add(r, xsqr, r);
161 
162  if (fp_neq(l, r)) {
163  pass = false;
164 
165  printf("%d: FAILED\n", i);
166  fp_print("x : ", x);
167  fp_print("y : ", y);
168  fp_print("(x+y)^2 : ", l);
169  fp_print("x^2+2xy+y^2 : ", r);
170  break;
171  }
172  ++count;
173  }
174  }
175 
176  printf("%ld tests\n", count);
177 
178  PRINTPASS(pass);
179 }
180 
181 // 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_x8(fp_t &z, const fp_t &x)
Multiplies x by 8 and stores the result into z.
Definition: fp_x8.cu:15
__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
__device__ void fp_x12(fp_t &z, const fp_t &x)
Multiplies the residue mod p x by 12 and stores the result into z.
Definition: fp_x12.cu:15
__device__ void fp_x4(fp_t &z, const fp_t &x)
Multiplies x by 4 and stores the result into z.
Definition: fp_x4.cu:15
__device__ void fp_sqr(fp_t &z, const fp_t &x)
Computes the square of the residue x modulo p and stores it in z.
Definition: fp_sqr.cu:16
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
__managed__ testval_t testval[TESTVALS]
Definition: fptest.cu:8
#define TESTVALS
Definition: fptest.cuh:13
__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
#define PRINTPASS(pass)
Definition: test.h:25