FK20 CUDA
fptest_fibonacci.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 #define ITERATIONS 100000
9 
16 __global__ void FpTestFibonacci(testval_t *) {
17 
18  printf("=== RUN %s\n", __func__);
19 
20  bool pass = true;
21  size_t count = 0;
22 
23  fp_t x, y, t, u;
24 
25  fp_one(x);
26  fp_one(y);
27 
28  for (int i=0; i<ITERATIONS; i++) {
29 
30  fp_cpy(t, x);
31  fp_add(x, x, y);
32 
33  fp_cpy(u, x);
34  fp_sub(u, u, t);
35 
36  if (fp_neq(u, y)) {
37  fp_print("x =", x);
38  fp_print("y =", y);
39  fp_print("x+y =", t);
40  fp_print("x+y-x =", u);
41  pass = false;
42  break;
43  }
44 
45  ++count;
46 
47  fp_cpy(t, y);
48  fp_add(y, y, x);
49 
50  fp_cpy(u, y);
51  fp_sub(u, u, t);
52 
53  if (fp_neq(u, x)) {
54  fp_print("x =", x);
55  fp_print("y =", y);
56  fp_print("x+y =", t);
57  fp_print("x+y-y =", u);
58  pass = false;
59  break;
60  }
61 
62  ++count;
63  }
64 
65  for (int i=0; i<ITERATIONS; i++) {
66  fp_sub(y, y, x);
67  fp_sub(x, x, y);
68  }
69 
70  if (!fp_isone(x) || !fp_isone(y)) {
71  printf("Reverse iteration failed\n");
72  fp_print("x =", x);
73  fp_print("y =", y);
74  pass = false;
75  }
76  else
77  ++count;
78 
79  printf("%ld tests passed\n", count);
80 
81  PRINTPASS(pass);
82 }
83 
84 // 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__ __host__ void fp_one(fp_t &z)
Sets z to one.
Definition: fp.cu:26
__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__ __host__ void fp_cpy(fp_t &z, const fp_t &x)
Copy from x into z.
Definition: fp_cpy.cu:14
__device__ bool fp_isone(const fp_t &x)
Checks if the residue x modulo p is congruent to one.
Definition: fp_isone.cu:13
__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
#define ITERATIONS
__global__ void FpTestFibonacci(testval_t *)
Test addition and subtraction in Fp using a fibonacci sequence (chain dependency) from 1 to ITERATION...
#define PRINTPASS(pass)
Definition: test.h:25