forked from wolfgangmauerer/libtrevisan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GF2Es.h
94 lines (74 loc) · 2.47 KB
/
GF2Es.h
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
/* This file is part of libtrevisan, a modular implementation of
Trevisan's randomness extraction construction.
Copyright (C) 2011-2012, Wolfgang Mauerer <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libtrevisan. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GF2ES_H
#define GF2ES_H
// Simple extension of NTL's GF2E class that allows for
// directly assigning unsigned long values without much ado
#include <NTL/GF2E.h>
#include <NTL/GF2X.h>
#include <NTL/GF2EX.h>
#include <NTL/tools.h>
#include <vector>
NTL_CLIENT
// "s" is for "sane value assignment"
// It's strange that this is seriously required -- seems
// fairly bogus. However, all conversion methods from long
// to GF2X and GF2E allow only for setting a _single_ bit,
// that is, the zeroeth coefficient. No idea what the rationale
// behind this is...
class GF2Es: public GF2E {
public:
GF2Es() : GF2E() { }
GF2Es(_ntl_ulong val) : GF2E() {
(void)this->setValue(val);
}
GF2E *setValue(_ntl_ulong val) {
if (val == 0) {
clear(this->LoopHole());
this->LoopHole().xrep[0] = 0;
return this;
}
// Okay because _ntl_ulong is guaranteed to fit into one word
this->LoopHole().xrep.SetLength(1);
this->LoopHole().xrep[0] = val;
rem(this->LoopHole(), this->LoopHole(), GF2E::modulus());
return this;
};
GF2E *setValue(vector<_ntl_ulong> &val) {
this->LoopHole().xrep.SetLength(val.size());
bool all_zero = true;
for (unsigned i = 0; i < val.size(); i++) {
this->LoopHole().xrep[i] = val[i];
if (val[i] != 0)
all_zero = false;
}
if (all_zero) {
clear(this->LoopHole());
return this;
}
rem(this->LoopHole(), this->LoopHole(), GF2E::modulus());
return this;
};
};
// Same again for setting coefficients on GF2EX without having to explicitely
// create GF2X instances first.
inline void SetCoeff_s(GF2EX& x, long i, _ntl_ulong a) {
if (a == 0)
SetCoeff(x, i, GF2E::zero());
else {
GF2Es c(a);
SetCoeff(x, i, c);
}
}
#endif