📄 mpi-test.c
字号:
mp_clear(&r); return 0;}/*------------------------------------------------------------------------*/int test_div(void){ mp_int a, b, r; int err = 0; mp_init(&a); mp_init(&b); mp_init(&r); mp_read_radix(&a, mp4, 16); mp_read_radix(&b, mp2, 16); IFOK( mp_div(&a, &b, &a, &r) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, q_mp42) != 0) { reason("error: test 1 computed quot %s, expected %s\n", g_intbuf, q_mp42); ++err; } mp_toradix(&r, g_intbuf, 16); if(strcmp(g_intbuf, r_mp42) != 0) { reason("error: test 1 computed rem %s, expected %s\n", g_intbuf, r_mp42); ++err; } mp_read_radix(&a, mp4, 16); mp_read_radix(&b, mp5a, 16); IFOK( mp_div(&a, &b, &a, &r) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, q_mp45a) != 0) { reason("error: test 2 computed quot %s, expected %s\n", g_intbuf, q_mp45a); ++err; } mp_toradix(&r, g_intbuf, 16); if(strcmp(g_intbuf, r_mp45a) != 0) { reason("error: test 2 computed rem %s, expected %s\n", g_intbuf, r_mp45a); ++err; } mp_read_radix(&a, mp14, 16); mp_read_radix(&b, mp4, 16); IFOK( mp_div(&a, &b, &a, &r) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, q_mp1404) != 0) { reason("error: test 3 computed quot %s, expected %s\n", g_intbuf, q_mp1404); ++err; } mp_toradix(&r, g_intbuf, 16); if(strcmp(g_intbuf, r_mp1404) != 0) { reason("error: test 3 computed rem %s, expected %s\n", g_intbuf, r_mp1404); ++err; } mp_clear(&a); mp_clear(&b); mp_clear(&r); return err;}/*------------------------------------------------------------------------*/int test_expt_d(void){ mp_int a; mp_init(&a); mp_read_radix(&a, mp5, 16); mp_expt_d(&a, md9, &a); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); if(strcmp(g_intbuf, e_mp5d9) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, e_mp5d9); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_expt(void){ mp_int a, b; mp_init(&a); mp_init(&b); mp_read_radix(&a, mp7, 16); mp_read_radix(&b, mp8, 16); mp_expt(&a, &b, &a); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&b); if(strcmp(g_intbuf, e_mp78) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, e_mp78); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_2expt(void){ mp_int a; mp_init(&a); mp_2expt(&a, md3); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); if(strcmp(g_intbuf, e_mpc2d3) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, e_mpc2d3); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_sqrt(void){ mp_int a; int res = 0; mp_init(&a); mp_read_radix(&a, mp9, 16); mp_sqrt(&a, &a); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, t_mp9) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, t_mp9); res = 1; goto CLEANUP; } mp_read_radix(&a, mp15, 16); mp_sqrt(&a, &a); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, t_mp15) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, t_mp15); res = 1; } CLEANUP: mp_clear(&a); return res;}/*------------------------------------------------------------------------*/int test_mod_d(void){ mp_int a; mp_digit r; mp_init(&a); mp_read_radix(&a, mp5, 16); IFOK( mp_mod_d(&a, md5, &r) ); sprintf(g_intbuf, "%X", r); mp_clear(&a); if(strcmp(g_intbuf, r_mp5d5) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, r_mp5d5); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_mod(void){ mp_int a, m; mp_init(&a); mp_init(&m); mp_read_radix(&a, mp4, 16); mp_read_radix(&m, mp7, 16); IFOK( mp_mod(&a, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&m); if(strcmp(g_intbuf, r_mp47) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, r_mp47); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_addmod(void){ mp_int a, b, m; mp_init(&a); mp_init(&b); mp_init(&m); mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16); mp_read_radix(&m, mp5, 16); IFOK( mp_addmod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&b); mp_clear(&m); if(strcmp(g_intbuf, ms_mp345) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, ms_mp345); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_submod(void){ mp_int a, b, m; mp_init(&a); mp_init(&b); mp_init(&m); mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16); mp_read_radix(&m, mp5, 16); IFOK( mp_submod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&b); mp_clear(&m); if(strcmp(g_intbuf, md_mp345) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, md_mp345); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_mulmod(void){ mp_int a, b, m; mp_init(&a); mp_init(&b); mp_init(&m); mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16); mp_read_radix(&m, mp5, 16); IFOK( mp_mulmod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&b); mp_clear(&m); if(strcmp(g_intbuf, mp_mp345) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, mp_mp345); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_sqrmod(void){ mp_int a, m; mp_init(&a); mp_init(&m); mp_read_radix(&a, mp3, 16); mp_read_radix(&m, mp5, 16); IFOK( mp_sqrmod(&a, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&m); if(strcmp(g_intbuf, mp_mp335) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, mp_mp335); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_exptmod(void){ mp_int a, b, m; int res = 0; mp_init(&a); mp_init(&b); mp_init(&m); mp_read_radix(&a, mp8, 16); mp_read_radix(&b, mp1, 16); mp_read_radix(&m, mp7, 16); IFOK( mp_exptmod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, me_mp817) != 0) { reason("case 1: error: computed %s, expected %s\n", g_intbuf, me_mp817); res = 1; goto CLEANUP; } mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp5, 16); mp_read_radix(&m, mp12, 16); IFOK( mp_exptmod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, me_mp1512) != 0) { reason("case 2: error: computed %s, expected %s\n", g_intbuf, me_mp1512); res = 1; goto CLEANUP; } mp_read_radix(&a, mp5, 16); mp_read_radix(&b, mp1, 16); mp_read_radix(&m, mp14, 16); IFOK( mp_exptmod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, me_mp5114) != 0) { reason("case 3: error: computed %s, expected %s\n", g_intbuf, me_mp5114); res = 1; } mp_read_radix(&a, mp16, 16); mp_read_radix(&b, mp17, 16); mp_read_radix(&m, mp18, 16); IFOK( mp_exptmod(&a, &b, &m, &a) ); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, me_mp161718) != 0) { reason("case 4: error: computed %s, expected %s\n", g_intbuf, me_mp161718); res = 1; } CLEANUP: mp_clear(&a); mp_clear(&b); mp_clear(&m); return res;}/*------------------------------------------------------------------------*/int test_exptmod_d(void){ mp_int a, m; mp_init(&a); mp_init(&m); mp_read_radix(&a, mp5, 16); mp_read_radix(&m, mp7, 16); IFOK( mp_exptmod_d(&a, md4, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&m); if(strcmp(g_intbuf, me_mp5d47) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, me_mp5d47); return 1; } return 0;}/*------------------------------------------------------------------------*/int test_invmod(void){ mp_int a, m, c; mp_int p1, p2, p3, p4, p5; mp_int t1, t2, t3, t4; mp_err res; /* 5 128-bit primes. */ static const char ivp1[] = { "AAD8A5A2A2BEF644BAEE7DB0CA643719" }; static const char ivp2[] = { "CB371AD2B79A90BCC88D0430663E40B9" }; static const char ivp3[] = { "C6C818D4DF2618406CA09280C0400099" }; static const char ivp4[] = { "CE949C04512E68918006B1F0D7E93F27" }; static const char ivp5[] = { "F8EE999B6416645040687440E0B89F51" }; mp_init(&a); mp_init(&m); mp_read_radix(&a, mp2, 16); mp_read_radix(&m, mp7, 16); IFOK( mp_invmod(&a, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&m); if(strcmp(g_intbuf, i_mp27) != 0) { reason("error: invmod test 1 computed %s, expected %s\n", g_intbuf, i_mp27); return 1; } mp_init(&a); mp_init(&m); mp_read_radix(&a, mp20, 16); mp_read_radix(&m, mp19, 16); IFOK( mp_invmod(&a, &m, &a) ); mp_toradix(&a, g_intbuf, 16); mp_clear(&a); mp_clear(&m); if(strcmp(g_intbuf, i_mp2019) != 0) { reason("error: invmod test 2 computed %s, expected %s\n", g_intbuf, i_mp2019); return 1; }/* Need the following test cases: Odd modulus - a is odd, relatively prime to m - a is odd, not relatively prime to m - a is even, relatively prime to m - a is even, not relatively prime to m Even modulus - a is even (should fail) - a is odd, not relatively prime to m - a is odd, relatively prime to m, m is not a power of 2 - m has factor 2**k, k < 32 - m has factor 2**k, k > 32 m is a power of 2, 2**k - k < 32 - k > 32*/ mp_init(&a); mp_init(&m); mp_init(&c); mp_init(&p1); mp_init(&p2); mp_init(&p3); mp_init(&p4); mp_init(&p5); mp_init(&t1); mp_init(&t2); mp_init(&t3); mp_init(&t4); mp_read_radix(&p1, ivp1, 16); mp_read_radix(&p2, ivp2, 16); mp_read_radix(&p3, ivp3, 16); mp_read_radix(&p4, ivp4, 16); mp_read_radix(&p5, ivp5, 16); IFOK( mp_2expt(&t2, 68) ); /* t2 = 2**68 */ IFOK( mp_2expt(&t3, 128) ); /* t3 = 2**128 */ IFOK( mp_2expt(&t4, 31) ); /* t4 = 2**31 *//* test 3: Odd modulus - a is odd, relatively prime to m */ IFOK( mp_mul(&p1, &p2, &a) ); IFOK( mp_mul(&p3, &p4, &m) ); IFOK( mp_invmod(&a, &m, &t1) ); IFOK( mp_invmod_xgcd(&a, &m, &c) ); if (mp_cmp(&t1, &c) != 0) { mp_toradix(&t1, g_intbuf, 16); mp_toradix(&c, a_intbuf, 16); reason("error: invmod test 3 computed %s, expected %s\n", g_intbuf, a_intbuf); return 1; } mp_clear(&a); mp_clear(&t1); mp_clear(&c); mp_init(&a); mp_init(&t1); mp_init(&c); /* test 4: Odd modulus - a is odd, NOT relatively prime to m */ IFOK( mp_mul(&p1, &p3, &a) ); /* reuse same m as before */ res = mp_invmod_xgcd(&a, &m, &c); if (res != MP_UNDEF) goto CLEANUP4; res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */ if (res != MP_UNDEF) {CLEANUP4: reason("error: invmod test 4 succeeded, should have failed.\n"); return 1; } mp_clear(&a); mp_clear(&t1); mp_clear(&c); mp_init(&a); mp_init(&t1); mp_init(&c); /* test 5: Odd modulus - a is even, relatively prime to m */ IFOK( mp_mul(&p1, &t2, &a) ); /* reuse m */ IFOK( mp_invmod(&a, &m, &t1) ); IFOK( mp_invmod_xgcd(&a, &m, &c) ); if (mp_cmp(&t1, &c) != 0) { mp_toradix(&t1, g_intbuf, 16); mp_toradix(&c, a_intbuf, 16); reason("error: invmod test 5 computed %s, expected %s\n", g_intbuf, a_intbuf); return 1; } mp_clear(&a); mp_clear(&t1); mp_clear(&c); mp_init(&a); mp_init(&t1); mp_init(&c); /* test 6: Odd modulus - a is odd, NOT relatively prime to m */ /* reuse t2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -