#include #include #define uint unsigned int #define u8 unsigned char #include using namespace std; class MPI { // FIXME: Make this protected public: mpz_t n; public: class MPIError { }; MPI () { mpz_init (n); } MPI (uint i) { mpz_init (n) ; mpz_set_ui (n, i); } MPI (const char *c){ mpz_init (n) ; if (mpz_set_str (n, c, 0) == -1) throw MPIError (); } MPI (const char *c, int base) { mpz_init (n); if (mpz_set_str (n, c, base) == -1) throw MPIError (); } MPI (const MPI &m) { mpz_init_set (n, m.n); } ~MPI () { mpz_clear (n); } void set_to (uint i) { mpz_set_ui (n, i); } void set_to (const char *c, int base) { if (mpz_set_str (n, c, base) == -1) throw MPIError (); } void set_to (const MPI &m) { mpz_set (n, m.n); } void set_to_binary (const u8 *, uint); void add (uint i) { mpz_add_ui (n, n, i); } void add (const MPI &m) { mpz_add (n, n, m.n); } void sub (uint i) { mpz_sub_ui (n, n, i); } void sub (const MPI &m) { mpz_sub (n, n, m.n); } void modpow (uint i, const MPI &m) { mpz_powm_ui (n, n, i, m.n); } void modpow (const MPI &m, const MPI &m2){ mpz_powm (n, n, m.n, m2.n); } void power (uint i) { mpz_pow_ui (n, n, i); } int prime_test (const int rounds) { return mpz_probab_prime_p (n, rounds); } void mod (const MPI &m) { mpz_mod (n, n, m.n); } void mul (const MPI &m) { mpz_mul (n, n, m.n); } void div (const MPI &m, MPI &r) { mpz_tdiv_qr (n, r.n, n, m.n); } bool equ_to (uint i) { return mpz_cmp_ui (n, i) == 0; } bool equ_to (const MPI &m) { return mpz_cmp (n, m.n) == 0; } void and_op (const MPI &m) { mpz_and (n, n, m.n); } int compareTo (const MPI &m) { return mpz_cmp (n, m.n); } int comp (const MPI &m) { return mpz_cmp (n, m.n) == 0; } void modinvert (const MPI &m) { mpz_invert (n, n, m.n); } bool lessthan (const MPI &m) { return (mpz_cmp (n, m.n) < 0); } int bitsize () const { return mpz_sizeinbase (n, 2); } int bytesize () const { return mpz_sizeinbase (n, 0x100); } int size () const { return mpz_size (n); } string get_string (uint); int opgp (char *buf, char **obuf); int opgp (u8 *buf, u8 **obuf) { return opgp ((char *) buf, (char **) obuf); } int bin_dump (u8 *, uint); void get_buf (u8 *, uint); }; int main() { MPI p("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" "49286651ECE65381FFFFFFFFFFFFFFFF"); MPI one("0x1"); MPI two("0x2"); printf("p is prime returns %d\n", p.prime_test(256)); MPI twopone(p); twopone.add(p); twopone.add(one); printf("2p+1 is prime returns %d\n", twopone.prime_test(256)); MPI halfpmo(p); MPI remainder; halfpmo.sub(one); halfpmo.div(two, remainder); printf("(p-1)/2 is prime returns %d\n", halfpmo.prime_test(256)); return 0; }