-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.cpp
More file actions
158 lines (144 loc) · 4.44 KB
/
Decoder.cpp
File metadata and controls
158 lines (144 loc) · 4.44 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Decoder.cpp
* Author: hn
*
* Created on August 10, 2017, 12:55 PM
*/
#include "Decoder.h"
Decoder::Decoder(int mlen, int numDel, int numChecker, int lengthExtension = 1):Encoder( mlen, numDel+numChecker, lengthExtension){
this->numDel = numDel;
x.SetLength(numDel);
}
void Decoder::decode(string &s, Vec<ZZ_p> p){
this->s = &s;
vector<Vec<ZZ_p>> d = baseCase(s);
//for (int i = 0; i < d.size(); ++i) {
//cout << d[i] <<endl;
//}
Vec<ZZ_p> pprime;
pprime = p - (d[numDel]*enVec);
//cout << pprime <<endl;
vector<long> root(numDel, 0);
if(CaseGenFast_rec(0, numBlock, 0, pprime, root, d)==true){
return;
//get bin string
}
cout<<"Decoding failure"<<endl;
//raise exception
}
bool Decoder::CaseGenFast_rec(long first, long last, long idx, Vec<ZZ_p> pprime, vector<long> root, vector<Vec<ZZ_p>> d) {
if(idx!=numDel-1){
for (long current = first; current < last; current++) {
if (current > first){
pprime+=d[numDel][current] * enVec[current];
}
root[idx] = current;
if(CaseGenFast_rec(current, last, idx + 1, pprime, root, d)==true){
//recover the message
return true;
}
if (idx==0 || current > first){
pprime-=d[idx][current]*enVec[current];
}
}
}
else{
for (long current = first; current < last; current++){
root[idx] = current;
if (current != first){
pprime+=d[numDel][current]*enVec[current];
}
vector<long> uroot;
uroot.push_back(root[0]);
int count = 0;
for (int j = 0; j < root.size(); ++j) {
if(root[j]!=uroot[count]){
uroot.push_back(root[j]);
count++;
}
}
//print(root,"root");
//print(uroot,"uroot");
//cout<<"pprime: "<<pprime<<endl;
if(solveGC(pprime, uroot)==true){
for (int i = 0; i < uroot.size(); ++i) {
long l = std::count(root.begin(),root.end(),uroot[i]);
s->replace(uroot[i]*blockLength,blockLength-l,zz2bin(x[i],blockLength));
}
//cout<<"success: x = "<<x<<endl;
return true;
}
// cout<<endl;
if (idx==0 || current != first){
pprime-=d[idx][current]*enVec[current];
}
}
}
return false;
}
vector<Vec<ZZ_p>> Decoder::baseCase(string s) {
vector<long> dels(numDel,numBlock-1);
vector<Vec<ZZ_p>> d(numDel+1);
d[0]=bin2zz(breakString(s, dels));
d[0][numBlock-1]=0;
for (int i = 1; i <= numDel; i++) {
dels[i-1]=0;
d[i]=bin2zz(breakString(s, dels));
for (int j = 0; j < dels.size(); ++j) {
d[i][dels[j]]=0;
}
}
return d;
}
bool Decoder::solveGC(Vec<ZZ_p> pprime, vector<long> dels) {
long numDelBlock=dels.size();
if(numDelBlock==1){
// cout<<"called one"<<endl;
return solveGC(pprime,dels[0]);
}
else {
Mat<ZZ_p> A;
A.SetDims(numDelBlock, numVec);
for (int i = 0; i < numDelBlock; ++i) {
A[i] = enVec[dels[i]];
}
A = transpose(A);
A.SetDims(numDelBlock, numDelBlock);
pprime.SetLength(numDelBlock);
ZZ_p det = determinant(A);
solve(det, A, x, pprime);
A.SetDims(numVec, numDelBlock);
pprime.SetLength(numVec);
ZZ_p y;
for (int j = numDelBlock; j < numVec; ++j) {
InnerProduct(y, A[j], x);
if (y != pprime[j]) {
return false;
}
}
return true;
}
}
bool Decoder::solveGC(Vec<ZZ_p> pprime, long dels) {
x.SetLength(1);
div(x[0], pprime[0], enVec[dels][0]);
for (int j = 1; j < numVec; j++) {
// cout<<enVec[dels][j] * x[0]<<" and "<<pprime[j]<<endl;
if (enVec[dels][j] * x[0] != pprime[j]) {
return false;
}
}
return true;
}
void Decoder::print(vector<long> vec, string name) {
cout<<name<<": ";
for (int i = 0; i < vec.size(); ++i) {
cout<<vec[i]<<" ";
}
cout<<endl;
}