-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGF.cpp
More file actions
77 lines (69 loc) · 1.29 KB
/
GF.cpp
File metadata and controls
77 lines (69 loc) · 1.29 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "GF.h"
#include <iostream>
#include <math.h> /* log */
#include <string>
#include <NTL/ZZ_p.h>
using namespace std;
using namespace NTL;
GF::GF(int mlen, int lengthExtension = 1)
{
this->mlen = mlen;
setDem(lengthExtension); //set numBlock and blockLength
this->gf = nextPrime(pow(2, blockLength));
ZZ_p::init(ZZ(gf));
this->gfMinus2 = gf - 2;
}
GF::~GF()
{
}
long GF::gfdiv(long den, long num = 1)
{
long temp = 1;
for (long i = 0; i < gfMinus2; i++)
{
temp = (temp*den) % gf;
}
long inv = temp;
if (inv<0)
inv += gf;
if (num == 1)
return inv;
else
return (num*inv) % gf;
}
void GF::toString()
{
cout << "numBlock: " + to_string(numBlock)
+ " - blockLength: " + to_string(blockLength)
+ " - mlen: " + to_string(mlen)
+ " - gf: " + to_string(gf)+"\n";
}
bool GF::isPrime(long num)
{
if (num < 2)
return false;
long max = sqrt(num) + 1;
for (long i = 2; i < max; i++)
{
if (num % i == 0)
return false;
}
return true;
}
long GF::nextPrime(long num)
{
while (!isPrime(num))
{
num++;
}
return num;
}
void GF::setDem(int lengthExtension = 1)
{
if (lengthExtension <= 0)
throw "Negative length extension.";
blockLength = lengthExtension*ceil(log2(mlen));
if (blockLength > mlen)
throw "Block too long.";
numBlock = ceil(mlen / blockLength);
}