//==========================================================C // Speed test program for A^B (mod N) C // A,B,N are 32 bits positive integer C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2011/04/05 C // ( JAMSTEC ES Adviser & Waseda University ) C //==========================================================C #include #include #include typedef signed int i32; typedef unsigned int u32; typedef signed long long int i64; typedef unsigned long long int u64; //==========================================================C // Compute A*B (mod P) with int C // A,P are 32 bits and B is 64 bits integer C //----------------------------------------------------------C // A int, In, First input value C // B long, In, Second input value C // P int, In, Input value of Mod C // return int, Out, Compute A^B (mod P) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2011/04/05 C // ( Tokyo Kougei University ) C //==========================================================C static i32 ModMul(i32 A, i64 B, i32 P) { i32 C; C = (i32)( ((i64)A*B) % P); return C; } //==========================================================C // Compute A^B (mod P) with int C // High speed method by 2,2^2,2^4,2^8,... multiply C // A,P are 32 bits and B is 64 bits integer C //----------------------------------------------------------C // A int, In, Input value of Base C // B long, In, Input value of Exponent C // P int, In, Input value of Mod C // return int, Out, Compute A^B (mod P) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2011/03/28 C // ( Tokyo Kougei University ) C //==========================================================C i32 ModPow1(i32 A, i32 B, i32 P) { i32 C = 1, AW, BW; AW = A; BW = B; while (BW > 0) { if((BW&1) == 1) { C = ModMul(C, AW, P); } AW = ModMul(AW, AW, P); // AW=AW^2 (mod P) BW = BW >> 1; // BW=BW/2 } return C; } //==========================================================C // Compute A^B (mod P) with int C // A,P are 32 bits and B is 64 bits integer C //----------------------------------------------------------C // A int, In, Input value of Base C // B long, In, Input value of Exponent C // P int, In, Input value of Mod C // return int, Out, Compute A^B (mod P) C //----------------------------------------------------------C // Written by Yasunori Ushiro , 2011/03/28 C // ( Tokyo Kougei University ) C //==========================================================C i32 ModPow2(i32 A, i32 B, i32 P) { i32 k, C = 1; for (k=0; k