-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.cc
More file actions
179 lines (170 loc) · 4.61 KB
/
Copy pathparallel.cc
File metadata and controls
179 lines (170 loc) · 4.61 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<random>
#include<algorithm>
#include<cstdlib>
#include<cassert>
#include<set>
#include<thread>
#include<future>
#include<iostream>
using namespace std;
//Key_Gen
vector<int> keyGen(int& length, int& weight){
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> distrib(0, length);
vector<int> key(length);
for(int j =0; j < length; j++){
auto temp = distrib(gen);
if (temp <= weight){
key[j] = 1;
}
}
return key;
}
//Sample_Gen
vector<int> errorGen(int& length){
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> distrib(0,1);
vector<int> temp(length);
for(int j = 0; j < length; j++){
temp[j] = distrib(gen);
}
return temp;
}
//XOR
vector<int> add( vector<int> u, vector<int> v){
assert(u.size() == v.size());
vector<int> res(u.size());
for (int i = 0; i < u.size(); i++){
res[i] = (u[i] ^ v[i]);
}
return res;
}
int weight(vector<int>& u){
return count(u.begin(),u.end(),1);
}
int weight_short(vector<int>& u){
return count(u.begin()+1,u.end(),1);
}
//generating x' so that (x,x') = weight that can split into two parts.
vector<int> pairGen(vector<int>& v, int& l){
vector<int> temp(v.begin(), v.end());
auto length = v.size()-1;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> distrib(0,length);
vector<int> position_zero;
vector<int> position_one;
if(weight(v) < l || (length + 1 - weight(v)) < l){
cout << "Problem here" << endl;
}
while(position_zero.size() < l){
auto pos = distrib(gen);
if(v[pos] == 0){
if(find(position_zero.begin(),position_zero.end(),pos)== position_zero.end()){
position_zero.emplace_back(pos);
}
}
}
while(position_one.size() < l){
auto pos = distrib(gen);
if(v[pos] == 1){
if(find(position_one.begin(), position_one.end(), pos)== position_one.end()){
position_one.emplace_back(pos);
}
}
}
assert(position_zero.size() == position_one.size());
/*cout << position_zero.size() << endl;
cout << position_one.size() << endl;
cout << position_zero;
cout << position_one;*/
for(int j = 0; j < position_zero.size(); j++)
{
assert(v[position_zero[j]] == 0);
temp[position_zero[j]] = 1;
}
for(int j = 0; j < position_one.size(); j++)
{
assert(v[position_one[j]] == 1);
temp[position_one[j]] = 0;
}
return temp;
}
int sum_mod6(vector<int>& k, vector<int>& sample){
assert(k.size() == sample.size());
int sum{0};
for(int j = 0; j < k.size(); j++){
sum = (sum + k[j]*sample[j])%6;
}
return sum;
}
int prf(vector<int>& k, vector<int>& sample){
assert(k.size() == sample.size());
auto temp = sum_mod6(k,sample);
if(temp == 0 || temp == 1 || temp == 2){
return 0;
}
else{
return 1;
}
}// OK
int prf1(vector<int>& k, vector<int>& sample){
int sum2{0};
int sum3{0};
for(int j = 0; j < k.size(); j++){
sum2 = (sum2 + k[j]*sample[j])% 2;
sum3 = (sum3 +k[j]*sample[j])% 3;
}
return (sum2 + sum3)%2;
}
int main_func(int length, float samples, int l){
int res{0};
set<vector<int>> mySet;
vector<int> key = errorGen(length);
while(weight(key) < length/2 - 5 || weight(key)> length/2 +5){
key = errorGen(length);
}
while(mySet.size() < samples){
auto temp = errorGen(length);
if( prf(key,temp) == 0){
mySet.insert(temp);
}
}
for( auto it = mySet.begin(); it != mySet.end(); it++ ){
auto v = *it;
auto temp = pairGen(v,l);
auto sum = add(v,temp);
assert( weight(sum) == 2*l );
if(prf(key,temp) == 0){
res++;
}
}
return res;
}
//Multithreading version
int main()
{
int length = 384;
float samples = 100000;
int nthreads = 12;
vector<int> results(nthreads);
vector<future<int>> futures(nthreads);
int w = 310;
int l = 12;
std::cout << l << endl;
std::cout << "Total sample per thread: " << samples << endl;
for(decltype(futures)::size_type i = 0; i< nthreads; i++){
futures[i] = std::async(main_func, length, samples, l);
}
for(decltype(futures)::size_type i = 0; i < nthreads; i++){
results[i] = futures[i].get();
}
float result{0};
for(auto i = 0; i < nthreads; i++){
result = result + static_cast<float>(results[i]);
}
cout << "End generating" << endl;
cout << (result/(nthreads*samples) - 0.5)<<endl;
}