Selaa lähdekoodia

Modpow Germain

mawinkle 6 vuotta sitten
vanhempi
commit
80749c484a
2 muutettua tiedostoa jossa 12 lisäystä ja 9 poistoa
  1. 8 1
      BigInt64.hpp
  2. 4 8
      test.cpp

+ 8 - 1
BigInt64.hpp

@@ -52,9 +52,16 @@ struct BigInt{
 	inline BigInt(const BigInt& o) : data(o.data), signum(o.signum){}
 	inline BigInt(BigInt&& o) : data(std::move(o.data)), signum(o.signum){}
 	inline BigInt(const std::string& o){
+		signum = 1;
 		auto it = o.rbegin();
 		BigInt baseTen(1);
 		while(it != o.rend()){
+			if(*it == '-'){
+				assert(it != o.rbegin());
+				assert((++it) == o.rend());
+				signum = -1;
+				break;
+			}
 			BigInt l = baseTen.mult(BigInt(*it - '0'));
 			adda(l);
 			baseTen = baseTen.mult(BigInt(10));
@@ -596,7 +603,7 @@ struct BigInt{
 				t.moda(mod);
 				o.suba(BigInt(1));
 			}
-			std::cout << "Reduced" << std::endl;
+			//::cout << "Reduced" << std::endl;
 			o.trim();
 			if(o == 1){t = t.mult(odd);t.moda(mod);return t;}
 		}

+ 4 - 8
test.cpp

@@ -28,15 +28,11 @@ std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
 }
 int main(){
 	xoshiro_256 gen(1);
-	BigInt a(323);
-	BigInt b(343453423ull);
-	//BigInt mod(gen, 5);
-	BigInt mod("179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624227998859");
-	mod.suba(BigInt(234723947982312ull));
-	std::cout << "Mod " << mod.toString() << std::endl;
-	std::cout << mod.rawString() << std::endl;
+	BigInt a(2);
+	BigInt b(gen, 4);
+	BigInt mod("9301165293246235069759966068146313776551258669855356477271940698500929939755418247622530571466332330697816620308003246225290293476785304004840090056840661553451916748315356563734257724978000166406621823207925733850455027807451108123161768212073821382033500073069184011344280494573919716117539258909003");
 	std::cout << "(" << a.toString() << " ^ " << std::flush;
-	a = a.modPow(b,mod);
+	a = a.modPow(b, mod);
 	std::cout << "" << b.toString() << ")";
 	std::cout << " mod " << mod.toString() << "\n";
 	std::cout << " = " << a.toString() << "\n";