-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpf_test.cc
40 lines (35 loc) · 1.01 KB
/
pf_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Test for the prime field arithmetic
Copyright 2024 Ahmet Inan <inan@aicodix.de>
*/
#include <limits>
#include <cstdint>
#include <cassert>
#include <iostream>
#include <type_traits>
#include "prime_field.hh"
template <typename TYPE, TYPE PRIME>
void exhaustive_test()
{
assert(std::is_unsigned<TYPE>::value);
assert(std::numeric_limits<TYPE>::max() / (PRIME-1) >= (PRIME-1));
typedef CODE::PrimeField<TYPE, PRIME> PF;
for (TYPE a = 0; a < PRIME; ++a)
for (TYPE b = 0; b < PRIME; ++b)
assert((PF(a) * PF(b))() == (a * b) % PRIME);
for (TYPE a = 1; a < PRIME; ++a)
assert(rcp(PF(a)) * PF(a) == PF(1));
for (TYPE a = 0; a < PRIME; ++a)
for (TYPE b = 0; b < PRIME; ++b)
assert((PF(a) + PF(b))() == (a + b) % PRIME);
for (TYPE a = 0; a < PRIME; ++a)
for (TYPE b = 0; b < PRIME; ++b)
assert((PF(a) - PF(b))() == (a - b + PRIME) % PRIME);
}
int main()
{
exhaustive_test<uint32_t, 257>();
exhaustive_test<uint64_t, 65537>();
std::cerr << "Prime field arithmetic test passed!" << std::endl;
return 0;
}