-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTemplateField.cpp
More file actions
executable file
·133 lines (96 loc) · 2.78 KB
/
Copy pathTemplateField.cpp
File metadata and controls
executable file
·133 lines (96 loc) · 2.78 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Created by meital on 15/11/16.
//
#include "TemplateField.h"
using namespace NTL;
template <>
TemplateField<ZZ_p>::TemplateField(long fieldParam) {
this->fieldParam = fieldParam;
this->elementSizeInBytes = NumBytes(fieldParam);//round up to the next byte
this->elementSizeInBits = this->elementSizeInBytes*8;
ZZ_p::init(ZZ(fieldParam));
auto randomKey = prg.generateKey(128);
prg.setKey(randomKey);
m_ZERO = new ZZ_p(0);
m_ONE = new ZZ_p(1);
}
/*
* The i-th field element. The ordering is arbitrary, *except* that
* the 0-th field element must be the neutral w.r.t. addition, and the
* 1-st field element must be the neutral w.r.t. multiplication.
*/
template <>
GF2E TemplateField<GF2E>::GetElement(long b) {
if(b == 1)
{
return *GetOne();
}
if(b == 0)
{
return *GetZero();
}
GF2X element;
for(int i=0; i < fieldParam; i++) {
// set the coefficient of x^i to 1
SetCoeff(element,i,(b >> i) & 1);
}
return to_GF2E(element);
}
template <>
ZZ_p TemplateField<ZZ_p>::GetElement(long b) {
if(b == 1)
{
return *m_ONE;
}
if(b == 0)
{
return *m_ZERO;
}
else{
ZZ_p element(b);
return element;
}
}
/**
* the function create a field by:
* generate the irreducible polynomial x^8 + x^4 + x^3 + x + 1 to work with
* init the field with the newly generated polynomial
*/
template <>
TemplateField<GF2E>::TemplateField(long fieldParam) {
this->fieldParam = fieldParam;
this->elementSizeInBytes = fieldParam/8;
this->elementSizeInBits = elementSizeInBytes*8;
GF2X irreduciblePolynomial = BuildSparseIrred_GF2X(fieldParam);
GF2E::init(irreduciblePolynomial);
auto randomKey = prg.generateKey(128);
prg.setKey(randomKey);
m_ZERO = new GF2E(0);
m_ONE = new GF2E(1);
}
template <>
void TemplateField<GF2E>::elementToBytes(unsigned char* elemenetInBytes, GF2E& element){
BytesFromGF2X(elemenetInBytes,rep(element),fieldParam/8);
}
template <>
GF2E TemplateField<GF2E>::bytesToElement(unsigned char* elemenetInBytes){
//first create a GF2X
GF2X polynomialElement;
//translate the bytes into a GF2X element
GF2XFromBytes(polynomialElement, elemenetInBytes, fieldParam/8);
//convert the GF2X to GF2E
return to_GF2E(polynomialElement);
}
template <>
void TemplateField<ZZ_p>::elementToBytes(unsigned char* elemenetInBytes, ZZ_p& element){
BytesFromZZ(elemenetInBytes,rep(element),elementSizeInBytes);
}
template <>
ZZ_p TemplateField<ZZ_p>::bytesToElement(unsigned char* elemenetInBytes){
//first create a ZZ
ZZ zz;
//translate the bytes into a ZZ element
ZZFromBytes(zz, elemenetInBytes, elementSizeInBytes);
//convert the ZZ to ZZ_p
return to_ZZ_p(zz);
}