Skip to content

Instantly share code, notes, and snippets.

@ykm11
Last active March 11, 2020 06:07
Show Gist options
  • Save ykm11/0cfc66d4112e59059ba9e09dade52d7c to your computer and use it in GitHub Desktop.
Save ykm11/0cfc66d4112e59059ba9e09dade52d7c to your computer and use it in GitHub Desktop.
mpnのテスト
#include<iostream>
#include<gmpxx.h>
#include <time.h>
static const int N = 100000;
static void set_mpz_t(mpz_t& z, const uint64_t* p, int n) {
int s = n;
while (s > 0) {
if (p[s - 1]) break;
s--;
}
z->_mp_alloc = n;
z->_mp_size = s;
z->_mp_d = (mp_limb_t*)const_cast<uint64_t*>(p);
}
void bench_mpn() {
uint64_t z[8] = {0, 0, 0, 0,
0, 0, 0, 0};
mpz_t mp, ma, mb;
uint64_t p[4] = {18446744073709551597UL, 18446744073709551615UL,
18446744073709551615UL, 9223372036854775807UL};
uint64_t a[4] = {12049265542313455944UL, 18428321212165697550UL,
18222554336221631568UL, 10491275619564141046UL};
uint64_t b[4] = {2858909295677371682UL, 16561264083360324415UL,
17568274556329439016UL, 11311893355857881102UL};
set_mpz_t(mp, p, 4);
set_mpz_t(ma, a, 4);
set_mpz_t(mb, b, 4);
uint64_t q[5] = {0, 0, 0, 0, 0};
uint64_t r[4] = {0, 0, 0, 0};
time_t begin = clock();
for (int i = 0; i < N; i++) {
mpn_mul_n((mp_limb_t*)z, (const mp_limb_t*)a, (const mp_limb_t*)b, 4);
mpn_tdiv_qr((mp_limb_t *)q, (mp_limb_t *)r, 0,
(const mp_limb_t *)z, 8, (const mp_limb_t *)p, 4);
}
time_t end = clock();
printf("[+] mpn mul: %f usec\n", double(end-begin)*1e6/CLOCKS_PER_SEC/N);
std::cout << ma << std::endl;
std::cout << mb << std::endl;
std::cout << mp << std::endl;
mpz_t mr;
set_mpz_t(mr, r, 4);
std::cout << mr << std::endl;
}
void bench_mpz() {
mpz_class a, b, p;
mpz_class z;
a = mpz_class("91987f9740d639f6fce3849f1ebe9450ffbe8c8149eeb00ea737972bc5f2b148", 16);
b = mpz_class("9cfbeb12fd02a80ef3cf0ca0d68caf28e5d56df040209b3f27ace2db75e97522", 16);
p = mpz_class("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10);
time_t begin = clock();
for (int i = 0; i < N; i++) {
mpz_mul(z.get_mpz_t(), a.get_mpz_t(), b.get_mpz_t());
mpz_mod(z.get_mpz_t(), z.get_mpz_t(), p.get_mpz_t());
}
time_t end = clock();
printf("[+] mpz mul: %f usec\n", double(end-begin)*1e6/CLOCKS_PER_SEC/N);
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << p << std::endl;
std::cout << z << std::endl << std::endl;
}
int main() {
bench_mpz();
bench_mpn();
}
// $ g++ mpn_test.cpp -lgmpxx -lgmp -lpthread -Wall -Wextra
// $ ./a.out
[+] mpz mul mod: 0.144330 usec
65854804397986044180016985716769972144040936268822385523537747773291481968968
71005905414564569448038202826108414854517662862897876748557178882117023135010
57896044618658097711785492504343953926634992332820282019728792003956564819949
15960092100224962172000327907369490154236460060176828892693191134550031904710
[+] mpn mul mod: 0.107470 usec
65854804397986044180016985716769972144040936268822385523537747773291481968968
71005905414564569448038202826108414854517662862897876748557178882117023135010
57896044618658097711785492504343953926634992332820282019728792003956564819949
15960092100224962172000327907369490154236460060176828892693191134550031904710
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment