mirror of
https://github.com/aicodix/code.git
synced 2026-04-27 22:35:44 +00:00
added Mersenne 31 prime field
This commit is contained in:
parent
413d3d0d1d
commit
f7ed8e13d5
2 changed files with 186 additions and 0 deletions
56
tests/m31_test.cc
Normal file
56
tests/m31_test.cc
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Test for the Mersenne 2^31-1 prime field arithmetic
|
||||
|
||||
Copyright 2026 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include "mersenne_31.hh"
|
||||
|
||||
void random_test(uint32_t count)
|
||||
{
|
||||
typedef CODE::Mersenne31 M31;
|
||||
std::random_device rd;
|
||||
typedef std::default_random_engine generator;
|
||||
typedef std::uniform_int_distribution<int> distribution;
|
||||
auto rand0 = std::bind(distribution(0, M31::P-1), generator(rd()));
|
||||
auto rand1 = std::bind(distribution(1, M31::P-1), generator(rd()));
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
uint32_t a = rand0();
|
||||
for (uint32_t j = 0; j < count; ++j) {
|
||||
uint32_t b = rand0();
|
||||
assert((M31(a) * M31(b))() == uint32_t((uint64_t(a) * b) % M31::P));
|
||||
}
|
||||
}
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
uint32_t a = rand1();
|
||||
assert(rcp(M31(a)) * M31(a) == M31(1));
|
||||
}
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
uint32_t a = rand0();
|
||||
for (uint32_t j = 0; j < count; ++j) {
|
||||
uint32_t b = rand0();
|
||||
assert((M31(a) + M31(b))() == (a + b) % M31::P);
|
||||
}
|
||||
}
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
uint32_t a = rand0();
|
||||
for (uint32_t j = 0; j < count; ++j) {
|
||||
uint32_t b = rand0();
|
||||
assert((M31(a) - M31(b))() == (a - b + M31::P) % M31::P);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
random_test(10000);
|
||||
std::cerr << "Mersenne 2^31-1 prime field arithmetic test passed!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue