-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTField.cpp
134 lines (112 loc) · 2.36 KB
/
TField.cpp
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
/*
* TField.cpp
*
* Created on: Aug 9, 2016
* Author: hila
*/
#include "TField.h"
bool TField::m_instanceFlag = false;
TField* TField::m_single = NULL;
/**
* 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
*/
TField::TField() {
GF2X irreduciblePolynomial = BuildSparseIrred_GF2X(8);
GF2E::init(irreduciblePolynomial);
GF2X pone0;
SetCoeff(pone0, 0, 0);
m_ZERO = new TFieldElement(GF2X(0));
GF2X pone;
SetCoeff(pone, 0, 1);
m_ONE = new TFieldElement(GF2X(1));
}
// Read field description from a config file
/*TField TField::ReadField ()
{
// how to read
}*/
/*
* 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.
*/
TFieldElement TField::GetElement(uint8_t b) {
if(b == 1)
{
return *GetOne();
}
if(b == 0)
{
return *GetZero();
}
TFieldElement element;
bitset<8> bits(b);
for(int i=0; i < 8; i++) {
// set the coefficient of x^i to 1
SetCoeff(element.getElement(),i,bits[i]);
}
return element;
}
/*
* 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.
*/
TFieldElement TField::GetElementByValue(uint8_t b) {
if(b == 1)
{
return *this->GetOne();
}
if(b == 0)
{
return *this->GetZero();
}
TFieldElement element;
bitset<8> bits(b);
for(int i=0; i < 8; i++) {
// set the coefficient of x^i to 1
SetCoeff(element.getElement(),i,bits[i]);
}
return element;
}
/**
* return the field
*/
TField* TField::getInstance()
{
if(!m_instanceFlag)
{
m_single = new TField();
m_instanceFlag = true;
return m_single;
}
else
{
return m_single;
}
}
/**
* A random random field element, uniform distribution
*/
TFieldElement TField::Random() {
TFieldElement randomElement;
PRG & prg = PRG::instance();
uint8_t b = prg.getRandom();
randomElement = GetElement(b);
return randomElement;
}
TFieldElement* TField::GetZero()
{
return m_ZERO;
}
TFieldElement* TField::GetOne()
{
return m_ONE;
}
TField::~TField() {
m_instanceFlag = false;
delete m_ZERO;
delete m_ONE;
}