📄 mpi-test.c
字号:
IFOK( mp_mul(&t2, &p3, &a) ); /* reuse same m as before */ res = mp_invmod_xgcd(&a, &m, &c); if (res != MP_UNDEF) goto CLEANUP6; res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */ if (res != MP_UNDEF) {CLEANUP6: reason("error: invmod test 6 succeeded, should have failed.\n"); return 1; } mp_clear(&a); mp_clear(&m); mp_clear(&c); mp_clear(&t1); mp_init(&a); mp_init(&m); mp_init(&c); mp_init(&t1); /* test 7: Even modulus, even a, should fail */ IFOK( mp_mul(&p3, &t3, &m) ); /* even m */ /* reuse t2 */ IFOK( mp_mul(&p1, &t2, &a) ); /* even a */ res = mp_invmod_xgcd(&a, &m, &c); if (res != MP_UNDEF) goto CLEANUP7; res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */ if (res != MP_UNDEF) {CLEANUP7: reason("error: invmod test 7 succeeded, should have failed.\n"); return 1; } mp_clear(&a); mp_clear(&c); mp_clear(&t1); mp_init(&a); mp_init(&c); mp_init(&t1); /* test 8: Even modulus - a is odd, not relatively prime to m */ /* reuse m */ IFOK( mp_mul(&p3, &p1, &a) ); /* even a */ res = mp_invmod_xgcd(&a, &m, &c); if (res != MP_UNDEF) goto CLEANUP8; res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */ if (res != MP_UNDEF) {CLEANUP8: reason("error: invmod test 8 succeeded, should have failed.\n"); return 1; } mp_clear(&a); mp_clear(&m); mp_clear(&c); mp_clear(&t1); mp_init(&a); mp_init(&m); mp_init(&c); mp_init(&t1); /* test 9: Even modulus - m has factor 2**k, k < 32 * - a is odd, relatively prime to m, */ IFOK( mp_mul(&p3, &t4, &m) ); /* even m */ IFOK( mp_mul(&p1, &p2, &a) ); 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 9 computed %s, expected %s\n", g_intbuf, a_intbuf); return 1; } mp_clear(&m); mp_clear(&t1); mp_clear(&c); mp_init(&m); mp_init(&t1); mp_init(&c); /* test 10: Even modulus - m has factor 2**k, k > 32 * - a is odd, relatively prime to m, */ IFOK( mp_mul(&p3, &t3, &m) ); /* even m */ /* reuse a */ 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 10 computed %s, expected %s\n", g_intbuf, a_intbuf); return 1; } mp_clear(&t1); mp_clear(&c); mp_init(&t1); mp_init(&c); /* test 11: Even modulus - m is a power of 2, 2**k | k < 32 * - a is odd, relatively prime to m, */ IFOK( mp_invmod(&a, &t4, &t1) ); IFOK( mp_invmod_xgcd(&a, &t4, &c) ); if (mp_cmp(&t1, &c) != 0) { mp_toradix(&t1, g_intbuf, 16); mp_toradix(&c, a_intbuf, 16); reason("error: invmod test 11 computed %s, expected %s\n", g_intbuf, a_intbuf); return 1; } mp_clear(&t1); mp_clear(&c); mp_init(&t1); mp_init(&c); /* test 12: Even modulus - m is a power of 2, 2**k | k > 32 * - a is odd, relatively prime to m, */ IFOK( mp_invmod(&a, &t3, &t1) ); IFOK( mp_invmod_xgcd(&a, &t3, &c) ); if (mp_cmp(&t1, &c) != 0) { mp_toradix(&t1, g_intbuf, 16); mp_toradix(&c, a_intbuf, 16); reason("error: invmod test 12 computed %s, expected %s\n", g_intbuf, a_intbuf); return 1; } mp_clear(&a); mp_clear(&m); mp_clear(&c); mp_clear(&t1); mp_clear(&t2); mp_clear(&t3); mp_clear(&t4); mp_clear(&p1); mp_clear(&p2); mp_clear(&p3); mp_clear(&p4); mp_clear(&p5); return 0;}/*------------------------------------------------------------------------*/int test_cmp_d(void){ mp_int a; mp_init(&a); mp_read_radix(&a, mp8, 16); if(mp_cmp_d(&a, md8) >= 0) { reason("error: %s >= " DIGIT_FMT "\n", mp8, md8); mp_clear(&a); return 1; } mp_read_radix(&a, mp5, 16); if(mp_cmp_d(&a, md8) <= 0) { reason("error: %s <= " DIGIT_FMT "\n", mp5, md8); mp_clear(&a); return 1; } mp_read_radix(&a, mp6, 16); if(mp_cmp_d(&a, md1) != 0) { reason("error: %s != " DIGIT_FMT "\n", mp6, md1); mp_clear(&a); return 1; } mp_clear(&a); return 0;}/*------------------------------------------------------------------------*/int test_cmp_z(void){ mp_int a; mp_init(&a); mp_read_radix(&a, mp6, 16); if(mp_cmp_z(&a) != 0) { reason("error: someone thinks a zero value is non-zero\n"); mp_clear(&a); return 1; } mp_read_radix(&a, mp1, 16); if(mp_cmp_z(&a) <= 0) { reason("error: someone thinks a positive value is non-positive\n"); mp_clear(&a); return 1; } mp_read_radix(&a, mp4, 16); if(mp_cmp_z(&a) >= 0) { reason("error: someone thinks a negative value is non-negative\n"); mp_clear(&a); return 1; } mp_clear(&a); return 0;}/*------------------------------------------------------------------------*/int test_cmp(void){ mp_int a, b; mp_init(&a); mp_init(&b); mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16); if(mp_cmp(&a, &b) <= 0) { reason("error: %s <= %s\n", mp3, mp4); mp_clear(&a); mp_clear(&b); return 1; } mp_read_radix(&b, mp3, 16); if(mp_cmp(&a, &b) != 0) { reason("error: %s != %s\n", mp3, mp3); mp_clear(&a); mp_clear(&b); return 1; } mp_read_radix(&a, mp5, 16); if(mp_cmp(&a, &b) >= 0) { reason("error: %s >= %s\n", mp5, mp3); mp_clear(&a); mp_clear(&b); return 1; } mp_read_radix(&a, mp5a, 16); if(mp_cmp_int(&a, 1000000) >= 0 || (mp_cmp_int(&a, -5000000) <= 0) || (mp_cmp_int(&a, -4938110) != 0)) { reason("error: long integer comparison failed (%s)", mp5a); mp_clear(&a); mp_clear(&b); return 1; } mp_clear(&a); mp_clear(&b); return 0;}/*------------------------------------------------------------------------*/int test_cmp_mag(void){ mp_int a, b; mp_init(&a); mp_init(&b); mp_read_radix(&a, mp5, 16); mp_read_radix(&b, mp4, 16); if(mp_cmp_mag(&a, &b) >= 0) { reason("error: %s >= %s\n", mp5, mp4); mp_clear(&a); mp_clear(&b); return 1; } mp_read_radix(&b, mp5, 16); if(mp_cmp_mag(&a, &b) != 0) { reason("error: %s != %s\n", mp5, mp5); mp_clear(&a); mp_clear(&b); return 1; } mp_read_radix(&a, mp1, 16); if(mp_cmp_mag(&b, &a) >= 0) { reason("error: %s >= %s\n", mp5, mp1); mp_clear(&a); mp_clear(&b); return 1; } mp_clear(&a); mp_clear(&b); return 0;}/*------------------------------------------------------------------------*/int test_parity(void){ mp_int a; mp_init(&a); mp_read_radix(&a, mp1, 16); if(!mp_isodd(&a)) { reason("error: expected operand to be odd, but it isn't\n"); mp_clear(&a); return 1; } mp_read_radix(&a, mp6, 16); if(!mp_iseven(&a)) { reason("error: expected operand to be even, but it isn't\n"); mp_clear(&a); return 1; } mp_clear(&a); return 0;}/*------------------------------------------------------------------------*/int test_gcd(void){ mp_int a, b; int out = 0; mp_init(&a); mp_init(&b); mp_read_radix(&a, mp7, 16); mp_read_radix(&b, mp1, 16); mp_gcd(&a, &b, &a); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, g_mp71) != 0) { reason("error: computed %s, expected %s\n", g_intbuf, g_mp71); out = 1; } mp_clear(&a); mp_clear(&b); return out;}/*------------------------------------------------------------------------*/int test_lcm(void){ mp_int a, b; int out = 0; mp_init(&a); mp_init(&b); mp_read_radix(&a, mp10, 16); mp_read_radix(&b, mp11, 16); mp_lcm(&a, &b, &a); mp_toradix(&a, g_intbuf, 16); if(strcmp(g_intbuf, l_mp1011) != 0) { reason("error: computed %s, expected%s\n", g_intbuf, l_mp1011); out = 1; } mp_clear(&a); mp_clear(&b); return out;}/*------------------------------------------------------------------------*/int test_convert(void){ int ix; mp_int a; mp_init(&a); mp_read_radix(&a, mp9, 16); for(ix = LOW_RADIX; ix <= HIGH_RADIX; ix++) { mp_toradix(&a, g_intbuf, ix); if(strcmp(g_intbuf, v_mp9[ix - LOW_RADIX]) != 0) { reason("error: radix %d, computed %s, expected %s\n", ix, g_intbuf, v_mp9[ix - LOW_RADIX]); mp_clear(&a); return 1; } } mp_clear(&a); return 0;}/*------------------------------------------------------------------------*/int test_raw(void){ int len, out = 0; mp_int a; char *buf; mp_init(&a); mp_read_radix(&a, mp4, 16); len = mp_raw_size(&a); if(len != sizeof(b_mp4)) { reason("error: test_raw: expected length %d, computed %d\n", sizeof(b_mp4), len); mp_clear(&a); return 1; } buf = calloc(len, sizeof(char)); mp_toraw(&a, buf); if(memcmp(buf, b_mp4, sizeof(b_mp4)) != 0) { reason("error: test_raw: binary output does not match test vector\n"); out = 1; } free(buf); mp_clear(&a); return out;}/*------------------------------------------------------------------------*/int test_pprime(void){ mp_int p; int err = 0; mp_err res; mp_init(&p); mp_read_radix(&p, mp7, 16); if(mpp_pprime(&p, 5) != MP_YES) { reason("error: %s failed Rabin-Miller test, but is prime\n", mp7); err = 1; } IFOK( mp_set_int(&p, 9) ); res = mpp_pprime(&p, 50); if (res == MP_YES) { reason("error: 9 is composite but passed Rabin-Miller test\n"); err = 1; } else if (res != MP_NO) { reason("test mpp_pprime(9, 50) failed: error %d\n", res); err = 1; } IFOK( mp_set_int(&p, 15) ); res = mpp_pprime(&p, 50); if (res == MP_YES) { reason("error: 15 is composite but passed Rabin-Miller test\n"); err = 1; } else if (res != MP_NO) { reason("test mpp_pprime(15, 50) failed: error %d\n", res); err = 1; } mp_clear(&p); return err;}/*------------------------------------------------------------------------*/int test_fermat(void){ mp_int p; mp_err res; int err = 0; mp_init(&p); mp_read_radix(&p, mp7, 16); if((res = mpp_fermat(&p, 2)) != MP_YES) { reason("error: %s failed Fermat test on 2: %s\n", mp7, mp_strerror(res)); ++err; } if((res = mpp_fermat(&p, 3)) != MP_YES) { reason("error: %s failed Fermat test on 3: %s\n", mp7, mp_strerror(res)); ++err; } mp_clear(&p); return err;}/*------------------------------------------------------------------------*//* Like fprintf(), but only if we are behaving in a verbose manner */void reason(char *fmt, ...){ va_list ap; if(!g_verbose) return; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap);}/*------------------------------------------------------------------------*//* HERE THERE BE DRAGONS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -