⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpi-test.c

📁 支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509v3证书等安全协议或标准的开发库编译用到NSPR
💻 C
📖 第 1 页 / 共 4 页
字号:
    if(SIGN(&a) != SIGN(&b)) {      reason("error: sign of original is %d, sign of copy is %d\n", 	     SIGN(&a), SIGN(&b));    } else {      reason("error: original precision is %d, copy precision is %d\n",	     USED(&a), USED(&b));    }    mp_clear(&a); mp_clear(&b);    return 1;  }  for(ix = 0; ix < USED(&b); ix++) {    if(DIGIT(&a, ix) != DIGIT(&b, ix)) {      reason("error: digit %d " DIGIT_FMT " != " DIGIT_FMT "\n",	     ix, DIGIT(&a, ix), DIGIT(&b, ix));      mp_clear(&a); mp_clear(&b);      return 1;    }  }       mp_clear(&a); mp_clear(&b);  return 0;}/*------------------------------------------------------------------------*/int test_exch(void){  mp_int  a, b;  mp_init(&a); mp_init(&b);  mp_read_radix(&a, mp7, 16); mp_read_radix(&b, mp1, 16);  mp_exch(&a, &b);  mp_toradix(&a, g_intbuf, 16);  mp_clear(&a);  if(strcmp(g_intbuf, mp1) != 0) {    mp_clear(&b);    reason("error: exchange failed\n");    return 1;  }  mp_toradix(&b, g_intbuf, 16);  mp_clear(&b);  if(strcmp(g_intbuf, mp7) != 0) {    reason("error: exchange failed\n");    return 1;  }  return 0;}/*------------------------------------------------------------------------*/int test_zero(void){  mp_int   a;  mp_init(&a); mp_read_radix(&a, mp7, 16);  mp_zero(&a);  if(USED(&a) != 1 || DIGIT(&a, 1) != 0) {    mp_toradix(&a, g_intbuf, 16);    reason("error: result is %s\n", g_intbuf);    mp_clear(&a);    return 1;  }  mp_clear(&a);  return 0;}/*------------------------------------------------------------------------*/int test_set(void){  mp_int   a;  /* Test single digit set */  mp_init(&a); mp_set(&a, 5);  if(DIGIT(&a, 0) != 5) {    mp_toradix(&a, g_intbuf, 16);    reason("error: result is %s, expected 5\n", g_intbuf);    mp_clear(&a);    return 1;  }  /* Test integer set */  mp_set_int(&a, -4938110);  mp_toradix(&a, g_intbuf, 16);  mp_clear(&a);  if(strcmp(g_intbuf, mp5a) != 0) {    reason("error: result is %s, expected %s\n", g_intbuf, mp5a);    return 1;  }  return 0;}/*------------------------------------------------------------------------*/int test_abs(void){  mp_int  a;  mp_init(&a); mp_read_radix(&a, mp4, 16);  mp_abs(&a, &a);    if(SIGN(&a) != ZPOS) {    reason("error: sign of result is negative\n");    mp_clear(&a);    return 1;  }  mp_clear(&a);  return 0;}/*------------------------------------------------------------------------*/int test_neg(void){  mp_int  a;  mp_sign s;  mp_init(&a); mp_read_radix(&a, mp4, 16);  s = SIGN(&a);  mp_neg(&a, &a);  if(SIGN(&a) == s) {    reason("error: sign of result is same as sign of nonzero input\n");    mp_clear(&a);    return 1;  }  mp_clear(&a);  return 0;}/*------------------------------------------------------------------------*/int test_add_d(void){  mp_int  a;  mp_init(&a);    mp_read_radix(&a, mp5, 16);  mp_add_d(&a, md4, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, s_mp5d4) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, s_mp5d4);    mp_clear(&a);    return 1;  }  mp_read_radix(&a, mp2, 16);  mp_add_d(&a, md5, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, s_mp2d5) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, s_mp2d5);    mp_clear(&a);    return 1;  }  mp_clear(&a);  return 0;}/*------------------------------------------------------------------------*/int test_add(void){  mp_int  a, b;  int     res = 0;  mp_init(&a); mp_init(&b);  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp3, 16);  mp_add(&a, &b, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, s_mp13) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, s_mp13);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp4, 16);  mp_add(&a, &b, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, s_mp34) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, s_mp34);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp4, 16); mp_read_radix(&b, mp6, 16);  mp_add(&a, &b, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, s_mp46) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, s_mp46);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp14, 16); mp_read_radix(&b, mp15, 16);  mp_add(&a, &b, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, s_mp1415) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, s_mp1415);    res = 1;  } CLEANUP:  mp_clear(&a); mp_clear(&b);  return res;}/*------------------------------------------------------------------------*/int test_sub_d(void){  mp_int   a;  mp_init(&a);  mp_read_radix(&a, mp5, 16);  mp_sub_d(&a, md4, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, d_mp5d4) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, d_mp5d4);    mp_clear(&a);    return 1;  }  mp_read_radix(&a, mp6, 16);    mp_sub_d(&a, md2, &a);  mp_toradix(&a, g_intbuf, 16);    mp_clear(&a);  if(strcmp(g_intbuf, d_mp6d2) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, d_mp6d2);    return 1;  }  return 0;}/*------------------------------------------------------------------------*/int test_sub(void){  mp_int  a, b;  mp_init(&a); mp_init(&b);  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp2, 16);  mp_sub(&a, &b, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, d_mp12) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, d_mp12);    mp_clear(&a); mp_clear(&b);    return 1;  }  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);  mp_sub(&a, &b, &a);  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, d_mp34) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, d_mp34);    mp_clear(&a); mp_clear(&b);    return 1;  }  mp_clear(&a); mp_clear(&b);  return 0;}/*------------------------------------------------------------------------*/int test_mul_d(void){  mp_int   a;  mp_init(&a);  mp_read_radix(&a, mp1, 16);  IFOK( mp_mul_d(&a, md4, &a) );  mp_toradix(&a, g_intbuf, 16);    if(strcmp(g_intbuf, p_mp1d4) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp1d4);        mp_clear(&a);    return 1;  }  mp_read_radix(&a, mp8, 16);  IFOK( mp_mul_d(&a, md6, &a) );  mp_toradix(&a, g_intbuf, 16);  mp_clear(&a);  if(strcmp(g_intbuf, p_mp8d6) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp8d6);     return 1;  }  return 0;}/*------------------------------------------------------------------------*/int test_mul(void){  mp_int   a, b;  int      res = 0;  mp_init(&a); mp_init(&b);  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp2, 16);  IFOK( mp_mul(&a, &b, &a) );  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, p_mp12) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp12);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);  IFOK( mp_mul(&a, &b, &a) );  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, p_mp34) !=0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp34);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp5, 16); mp_read_radix(&b, mp7, 16);  IFOK( mp_mul(&a, &b, &a) );  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, p_mp57) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp57);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp11, 16); mp_read_radix(&b, mp13, 16);  IFOK( mp_mul(&a, &b, &a) );  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, p_mp1113) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp1113);    res = 1; goto CLEANUP;  }  mp_read_radix(&a, mp14, 16); mp_read_radix(&b, mp15, 16);  IFOK( mp_mul(&a, &b, &a) );  mp_toradix(&a, g_intbuf, 16);  if(strcmp(g_intbuf, p_mp1415) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp1415);    res = 1;  } CLEANUP:  mp_clear(&a); mp_clear(&b);  return res;}/*------------------------------------------------------------------------*/int test_sqr(void){  mp_int  a;  mp_init(&a); mp_read_radix(&a, mp2, 16);  mp_sqr(&a, &a);  mp_toradix(&a, g_intbuf, 16);  mp_clear(&a);  if(strcmp(g_intbuf, p_mp22) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, p_mp22);    return 1;  }  return 0;}/*------------------------------------------------------------------------*/int test_div_d(void){  mp_int    a, q;  mp_digit  r;  int       err = 0;  mp_init(&a); mp_init(&q);  mp_read_radix(&a, mp3, 16);  IFOK( mp_div_d(&a, md6, &q, &r) );  mp_toradix(&q, g_intbuf, 16);  if(strcmp(g_intbuf, q_mp3d6) != 0) {    reason("error: computed q = %s, expected %s\n", g_intbuf, q_mp3d6);    ++err;  }  sprintf(g_intbuf, "%X", r);  if(strcmp(g_intbuf, r_mp3d6) != 0) {    reason("error: computed r = %s, expected %s\n", g_intbuf, r_mp3d6);    ++err;  }  mp_read_radix(&a, mp9, 16);  IFOK( mp_div_d(&a, 16, &q, &r) );  mp_toradix(&q, g_intbuf, 16);  if(strcmp(g_intbuf, q_mp9c16) != 0) {    reason("error: computed q = %s, expected %s\n", g_intbuf, q_mp9c16);    ++err;  }  sprintf(g_intbuf, "%X", r);  if(strcmp(g_intbuf, r_mp9c16) != 0) {    reason("error: computed r = %s, expected %s\n", g_intbuf, r_mp9c16);    ++err;  }  mp_clear(&a); mp_clear(&q);  return err;}/*------------------------------------------------------------------------*/int test_div_2(void){  mp_int  a;  mp_init(&a); mp_read_radix(&a, mp7, 16);  IFOK( mp_div_2(&a, &a) );  mp_toradix(&a, g_intbuf, 16);  mp_clear(&a);  if(strcmp(g_intbuf, q_mp7c2) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, q_mp7c2);    return 1;  }      return 0;}/*------------------------------------------------------------------------*/int test_div_2d(void){  mp_int  a, q, r;  mp_init(&q); mp_init(&r);  mp_init(&a); mp_read_radix(&a, mp13, 16);  IFOK( mp_div_2d(&a, 64, &q, &r) );  mp_clear(&a);  mp_toradix(&q, g_intbuf, 16);  if(strcmp(g_intbuf, q_mp13c) != 0) {    reason("error: computed %s, expected %s\n", g_intbuf, q_mp13c);    mp_clear(&q); mp_clear(&r);    return 1;  }  mp_clear(&q);  mp_toradix(&r, g_intbuf, 16);  if(strcmp(g_intbuf, r_mp13c) != 0) {    reason("error, computed %s, expected %s\n", g_intbuf, r_mp13c);    mp_clear(&r);    return 1;  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -