-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNTLUtils.cpp
180 lines (156 loc) · 3.64 KB
/
NTLUtils.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* NTLUtils.c
*
* Created on: Mar 2, 2013
* Author: Dusan Klinec (ph4r05)
*
* License: GPLv3 [http://www.gnu.org/licenses/gpl-3.0.html]
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
// NTL dependencies
#include "NTLUtils.h"
#include <iomanip>
#include <iostream>
#include <fstream>
NTL_CLIENT
using namespace std;
using namespace NTL;
void dumpVector(NTL::vec_GF2E& a){
unsigned int i, len = a.length();
for (i=0; i<len; i++){
cout << " " << GF2EHEX(a[i]) << " ";
if (((i+1) % 16) == 0) cout << endl;
}
cout << endl;
}
void dumpVector(NTL::vec_GF2X& a){
unsigned int i, len = a.length();
for (i=0; i<len; i++){
cout << " " << GF2EHEX(a[i]) << " ";
if (((i+1) % 16) == 0) cout << endl;
}
cout << endl;
}
void dumpVector(NTL::vec_GF2& a){
unsigned int i, len = a.length();
for (i=0; i<len; i++){
cout << " " << a[i] << " ";
if (((i+1) % 16) == 0) cout << endl;
}
cout << endl;
}
void dumpVector(long * a, size_t len){
dumpVectorT<long>(a, len);
}
void dumpVector(GF2E * a, size_t len){
unsigned int i;
for (i=0; i<len; i++){
cout << " " << GF2EHEX(a[i]) << " ";
if (((i+1) % 16) == 0) cout << endl;
}
cout << endl;
}
void dumpMatrix(NTL::mat_GF2E& a){
unsigned int i,j, n = a.NumRows(), m=a.NumCols();
for (i=0; i<n; i++){
for(j=0; j<m; j++){
cout << " " << GF2EHEX(a[i][j]) << " ";
}
cout << endl;
}
cout << endl;
}
void dumpMatrix(ostream& out, NTL::mat_GF2& a, bool newLine){
int i,j, n = a.NumRows(), m=a.NumCols();
for (i=0; i<n; i++){
for(j=0; j<m; j++){
if (newLine) out << " ";
out << a.get(i,j);
if (newLine) out << " ";
else out << ", ";
}
if (newLine) out << endl;
else out << " | ";
}
if (newLine) out << endl;
}
void dumpMatrix(NTL::mat_GF2& a){
dumpMatrix(cout, a, true);
}
void dumpMatrixN(NTL::mat_GF2 a){
dumpMatrix(cout, a, true);
}
std::string dumpMatrix2str(NTL::mat_GF2& a, bool newLine){
std::ostringstream out;
dumpMatrix(out, a, newLine);
return out.str();
}
/**
* Initializes matrix from specified array.
* Data must be at least dimension of given matrix.
*/
int initMatrix(mat_GF2& M, long *data){
long i,j,n,m;
for(i=0, n=M.NumRows(); i<n; i++){
for(j=0, m=M.NumCols(); j<m; j++){
M.put(i,j,data[n*i+j]);
}
}
return 0;
}
void dumpMatrix(ostream& out, NTL::mat_GF2E& a){
unsigned int i,j, n = a.NumRows(), m=a.NumCols();
for (i=0; i<n; i++){
for(j=0; j<m; j++){
out << GF2EHEX(a[i][j]) << ",";
}
out << "|";
}
}
void dumpVector(ostream& out, NTL::vec_GF2E& a){
unsigned int i, len = a.length();
for (i=0; i<len; i++){
out << GF2EHEX(a[i]) << ",";
}
}
std::string dumpVector2str(NTL::vec_GF2E& a){
std::ostringstream out;
dumpVector(out, a);
return out.str();
}
void dumpVector(ostream& out, NTL::GF2E * a, size_t len){
unsigned int i;
for (i=0; i<len; i++){
out << GF2EHEX(a[i]) << ",";
}
}
void matrix2vector(const NTL::mat_GF2E& src, NTL::vec_GF2E& dst, bool byRows){
int i,j, n = byRows ? src.NumRows() : src.NumCols(), m = byRows ? src.NumCols() : src.NumRows();
dst.SetLength(n*m);
for(i=0; i<n; i++){
for(j=0; j<m; j++){
dst.put(i*m + j, byRows ? src.get(i, j) : src.get(j, i));
}
}
}
void vector2matrix(const NTL::vec_GF2E& src, NTL::mat_GF2E& dst, int rowLen, bool byRows){
int i,n=src.length();
dst.SetDims(n/rowLen, rowLen);
for(i=0; i<n; i++){
if (byRows)
dst.put(i / rowLen, i % rowLen, src.get(i));
else
dst.put(i % rowLen, i / rowLen, src.get(i));
}
}
void charArr_to_vec_GF2E(const unsigned char * arr, size_t len, NTL::vec_GF2E& dst){
unsigned int j;
dst.SetLength(len);
for(j=0; j<len; j++){
dst.put(j, GF2EFromLong((unsigned long)arr[j], 8));
}
}