mirror of
https://github.com/aicodix/code.git
synced 2026-04-27 14:30:36 +00:00
added prime field arithmetic
This commit is contained in:
parent
55bd3302d8
commit
fbbc655589
2 changed files with 181 additions and 0 deletions
36
tests/pf_test.cc
Normal file
36
tests/pf_test.cc
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
Test for the prime field arithmetic
|
||||
|
||||
Copyright 2024 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include "prime_field.hh"
|
||||
|
||||
template <typename TYPE, TYPE PRIME>
|
||||
void exhaustive_test()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue