aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp141
1 files changed, 141 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..ce1573f
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,141 @@
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <stdexcept>
+#include <cstdlib>
+#include <cctype>
+#include <ctime>
+#include <cassert>
+#include "bigint.h"
+#include "numalgo.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;
+ }
+ }
+}
+
+void performrsa(){
+ PrivateKey privkey;
+ Bigint p(1000000007),q(3000000019);
+ privkey.pub.mod=3000000040000000133LL;
+ privkey.pub.exp=65537;
+ {
+ Bigint x;
+ Bigint one(1);
+ egcd((p-one)*(q-one),privkey.pub.exp,x,privkey.pexp);
+ }
+ cout<<"d = "<<privkey.pexp<<endl;
+ Bigint msg(123456789);
+ cout<<"msg = "<<msg<<endl;
+ Bigint encr=encrypt(privkey.pub,msg);
+ cout<<"encr = "<<encr<<endl;
+ Bigint msg2=decrypt(privkey,encr);
+ cout<<"msg = "<<msg2<<endl;
+}
+
+int main(int,char**){
+ // biginttest();
+ // repl(argc,argv);
+ performrsa();
+ // cout<<Bigint(0)-Bigint(69255535LL)<<endl;
+ // cout<<Bigint(0)-Bigint(669255535LL)<<endl;
+ // cout<<Bigint(0)-Bigint(5669255535LL)<<endl;
+ // cout<<Bigint(0)-Bigint(75669255535LL)<<endl;
+ // cout<<Bigint(0)-Bigint(775669255535LL)<<endl;
+ // cout<<Bigint(0)-Bigint(5775669255535LL)<<endl;
+ // cout<<Bigint(0)-Bigint(45775669255535LL)<<endl;
+}