-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmain.cpp
161 lines (128 loc) · 4.11 KB
/
main.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
#include "../tests_cryptoTools/UnitTests.h"
#include "Tutorials/Network.h"
#include "cryptoTools/Network/Channel.h"
#include "cryptoTools/Network/IOService.h"
#include <cryptoTools/Common/Matrix.h>
#include "cryptoTools/Common/CuckooIndex.h"
#include "cryptoTools/Common/CLP.h"
using namespace osuCrypto;
#include <sstream>
#include <fstream>
#ifdef ENABLE_CIRCUITS
#include <cryptoTools/Circuit/BetaLibrary.h>
void print_aes_bristol()
{
//{
// auto name = "./AES-expanded.txt";
// std::ifstream file(name);
// BetaCircuit cir2;
// cir2.readBristol(file);
// std::cout << "and " << cir2.mNonlinearGateCount << std::endl;
//}
for (auto rounds : { 10, /*12, */14 })//
{
BetaLibrary lib;
BetaCircuit cir;
BetaBundle input1(256);
BetaBundle k(128 * rounds + 128);
BetaBundle c(128);
cir.addInputBundle(k);
cir.addInputBundle(input1);
cir.addOutputBundle(c);
// m is the fist 128 bits and cMask is the second of input1.
BetaBundle m, cMask;
m.mWires.insert(
m.mWires.end(),
input1.mWires.begin(),
input1.mWires.begin() + 128);
cMask.mWires.insert(
cMask.mWires.end(),
input1.mWires.begin() + 128,
input1.mWires.begin() + 256);
// c = AES_k(m)
lib.aes_exapnded_build(cir, m, k, c);
// c = c ^ cMask
lib.bitwiseXor_build(cir, c, cMask, c);
auto name = "./aes_r_" + std::to_string(rounds) + ".txt";
{
std::ofstream ofile(name);
cir.writeBristol(ofile);
}
std::ifstream file(name);
BetaCircuit cir2;
cir2.readBristol(file);
std::vector<BitVector> in(2), out1(1), out2(1);
in[0].resize(k.size());
in[1].resize(input1.size());
out1[0].resize(128);
out2[0].resize(128);
PRNG prng(ZeroBlock);
AES aes(prng.get<block>());
for (u64 i = 0; i < 3; ++i)
{
in[1].randomize(prng);
if (rounds == 10)
{
memcpy(in[0].data(), aes.mRoundKey.data(), 11 * 16);
}
else
{
in[0].randomize(prng);
}
cir.evaluate(in, out1);
cir2.evaluate(in, out2);
if (out1[0] != out2[0])
{
std::cout << "failed \n";
std::cout << out1[0] << std::endl;
std::cout << out2[0] << std::endl;
}
else
{
if (rounds == 10)
{
block message = in[1].getSpan<block>()[0];
block mask = in[1].getSpan<block>()[1];
block ctxt = aes.ecbEncBlock(message) ^ mask;
if (neq(ctxt, out1[0].getSpan<block>()[0]))
{
std::cout << "failed bad val" << std::endl;
}
else
{
std::cout << "passed! " << cir.mNonlinearGateCount << std::endl;
}
}
else
{
std::cout << "passed " << std::endl;
}
}
std::cout
<< "k " << in[0] << "\n"
<< "m " << in[1] << "\n"
<< "c " << out1[0] << std::endl;
}
}
}
#endif
int main(int argc, char** argv)
{
CLP cmd(argc, argv);
if (cmd.isSet("tut"))
{
networkTutorial();
}
else if(cmd.isSet("u"))
{
tests_cryptoTools::Tests.runIf(cmd);
}
else
{
std::cout << "Run the unit tests with:\n\n\t"
<< Color::Green << cmd.mProgramName << " -u\n\n" << Color::Default
<< "Run the network tutorial with:\n\n\t"
<< Color::Green << cmd.mProgramName << " -tut" << Color::Default
<< std::endl;
}
}