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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <cassert>
#include "aes.h"
#include "base64.h"
#include "bigint.h"
#include "envelope.h"
#include "numalgo.h"
#include "primes.h"
#include "rng.h"
#include "rsa.h"
using namespace std;
class eof_error : public runtime_error{
public:
eof_error()
:runtime_error("EOF"){}
};
int64_t rand64(){
return ((int64_t)rand()<<32)+(((int64_t)rand()%2)<<31)+rand();
}
Bigint readevalexpr(istream &is){
Bigint a;
is>>a;
if(is.eof())throw eof_error();
// cerr<<"Read "<<a<<endl;
if(!is.fail())return a;
is.clear();
string s;
is>>s;
assert(!is.fail());
a=readevalexpr(is);
Bigint b=readevalexpr(is);
//cerr<<"Operation "<<s<<" on "<<a<<" and "<<b<<endl;
if(s=="add")return a+b;
else if(s=="sub")return a-b;
else if(s=="mul")return a*b;
else if(s=="div")return a.divmod(b).first;
else if(s=="mod")return a.divmod(b).second;
else {
cerr<<"Unknown operation '"<<s<<'\''<<endl;
assert(false);
}
}
void biginttest(){
srand(time(NULL));
// cerr<<Bigint(599428191)*Bigint(10)<<endl;
// cerr<<hex<<Bigint(599428191)*Bigint(10)<<endl;
#if 1
{
Bigint bi;
assert(RAND_MAX==(1U<<31)-1);
for(int i=0;i<500000;i++){
int64_t a=rand64(),b=rand64();
if(a+b<0){i--; continue;}
stringstream s1,s2,s3;
s1<<a+b;
s2<<Bigint(a+b);
s3<<Bigint(a)+Bigint(b);
assert(s1.str()==s2.str()&&s1.str()==s3.str());
}
}
#endif
#if 1
{
for(int i=0;i<1000;i++){
int64_t n=rand64();
istringstream ss(to_string(n));
Bigint bi;
ss>>bi;
assert(bi==Bigint(n));
}
}
#endif
#if 1
{
string s="4405994068155852661780322209877856931246944549396705884037139443014164958640201650440984581318995014";
istringstream iss(s);
Bigint bi;
iss>>bi;
uint32_t digs[11]={1752788038,953502834,2175607868,1627159508,1754291416,1207689192,3196357285,3165170272,3313904421,3194703103,2062};
for(int i=0;i<11;i++)assert(bi._digit(i)==digs[i]);
ostringstream oss;
oss<<bi;
assert(oss.str()==s);
}
#endif
}
void repl(int argc,char **argv){
istream *in;
if(argc==2)in=new ifstream(argv[1]);
else in=&cin;
for(int i=0;;i++){
try {
cout<<readevalexpr(*in)<<endl;
} catch(eof_error){
break;
}
}
if(in!=&cin)delete in;
}
void testisqrt(int argc,char **argv){
int randsize=argc==2?strtol(argv[1],nullptr,10):1;
assert(randsize>=1);
for(int i=0;i<1000;i++){
Bigint n(rand64());
for(int j=1;j<randsize;j++){
n<<=63;
n+=rand64();
}
// cout<<hex<<n<<dec<<endl;
Bigint root(isqrt(n));
assert(root*root<=n);
root+=Bigint::one;
assert(root*root>n);
}
}
void performrsa(){
RSA::PrivateKey privkey;
Bigint p(1000000007),q(3000000019U);
privkey.pub.mod=(int64_t)3000000040000000133LL;
privkey.pub.exp=65537;
{
Bigint x;
egcd((p-Bigint::one)*(q-Bigint::one),privkey.pub.exp,x,privkey.pexp);
}
cout<<"d = "<<privkey.pexp<<endl;
Bigint msg(123456789);
cout<<"msg = "<<msg<<endl;
Bigint encr=RSA::encrypt(msg,privkey.pub);
cout<<"encr = "<<encr<<endl;
Bigint msg2=RSA::decrypt(encr,privkey);
cout<<"msg = "<<msg2<<endl;
}
void pseudolist(bool(*func)(const Bigint&)){
fillsmallprimes();
for(int i=2;i<65000;i++){
// cerr<<"TRYING "<<i<<endl;
bool actualprime=binary_search(smallprimes.begin(),smallprimes.end(),i);
if(!func(Bigint(i))){
if(actualprime){
cerr<<"Test misleadingly said that "<<i<<" isn't prime, while it is!"<<endl;
exit(1);
}
continue;
}
if(!actualprime)cout<<i<<' ';
}
cout<<endl;
}
void listprimes(){
int n=0;
Bigint x(3);
x*=x;
x*=x;
x*=x;
x*=x;
x*=x;
x*=x;
x*=x;
Bigint y(x+10000);
cout<<x<<' '<<y<<endl;
for(Bigint i(x);i<=y;i+=1){
if(bailliePSW(i)){
cout<<i<<endl;
n++;
}
}
cout<<n<<endl;
}
int main(int argc,char **argv){
(void)argc;
(void)argv;
// biginttest();
// repl(argc,argv);
// performrsa();
// testisqrt(argc,argv);
// randprime(Bigint(20),Bigint(42));
// strongLucasPrime(Bigint(5));
// cout<<strongLucasPrime(Bigint(5777))<<endl;
// pseudolist(strongPseudoPrime2);
// pseudolist(strongLucasPrime);
// listprimes();
// Rng rng("wachtwoord");
// for(int i=0;i<100000;i++)cout<<rng.get()<<endl;
// for(int i=0;i<10000;i++)cout<<arc4random()<<endl;
/*KeyRng rng("wachtwoord"); //for DieHarder
char data[4];
for(int i=0;i<10000000;i++){
*(uint32_t*)data=rng.get();
fwrite(data,1,4,stdout);
}*/
/*string s;
while(true){
char c=cin.get();
if(!cin)break;
s.push_back(c);
}
cout<<Base64::encode(s)<<endl;
// cout<<Base64::decode(s)<<flush;*/
// AES::test();
// cout<<Base64::encode(AES::encrypt("goeiemorgen dit is leuke data enzoo","123456789abcdefx",AES::AES_128_CBC))<<endl;
// cout<<AES::decrypt(Base64::decode("dv2N3n2FHsD7LU2PAZnZm/bLQQSaaZoDDFjy3VrSu2JCHN5KuyBlinlh9C71IGTbT4/WvRKLp6dp1TfrKXIG2w=="),"123456789abcdefx",AES::AES_128_CBC)<<endl;
RSA::PrivateKey privkey(RSA::genkey(2048,"goeiemorgen"));
cerr<<"privkey.pexp="<<privkey.pexp<<endl;
// stringstream ss("146142527763115590918816237475833862275078269531224490080731579415134484175830244922030574137988310092068583287060550616357026989772917226684940867892409");
// Bigint d;
// ss>>d;
// cout<<RSA::decrypt(d,privkey)<<endl;
string crypt(Envelope::encrypt("hallo hallo dit is leuke data enzo",privkey.pub));
cout<<Base64::encode(crypt)<<endl;
string decr(Envelope::decrypt(crypt,privkey));
cout<<decr<<endl;
}
|