You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

241 lines
5.6 KiB

  1. #include "field.h"
  2. #include "test.h"
  3. #include <gmp.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. mpz_t mp_field;
  7. static mask_t mpz_to_field (
  8. struct field_t *out,
  9. const mpz_t in
  10. ) {
  11. uint8_t ser[FIELD_BYTES];
  12. mpz_t modded;
  13. memset(ser,0,sizeof(ser));
  14. mpz_init(modded);
  15. mpz_mod(modded, in, mp_field);
  16. mpz_export(ser, NULL, -1, 1, -1, 0, modded);
  17. mask_t succ = field_deserialize(out, ser);
  18. return succ;
  19. }
  20. static mask_t field_assert_eq_gmp(
  21. const char *descr,
  22. const struct field_t *a,
  23. const struct field_t *b,
  24. const struct field_t *x,
  25. const mpz_t y,
  26. float lowBound,
  27. float highBound
  28. ) {
  29. uint8_t xser[FIELD_BYTES], yser[FIELD_BYTES];
  30. mpz_t modded;
  31. memset(yser,0,sizeof(yser));
  32. field_serialize(xser, x);
  33. mpz_init(modded);
  34. mpz_mod(modded, y, mp_field);
  35. mpz_export(yser, NULL, -1, 1, -1, 0, modded);
  36. unsigned int i;
  37. for (i=0; i<sizeof(*x)/sizeof(x->limb[0]); i++) {
  38. int radix_bits = 1 + (sizeof(x->limb[0]) * FIELD_BITS - 1) / sizeof(*x);
  39. word_t yardstick = (i==sizeof(*x)/sizeof(x->limb[0])/2) ?
  40. (1ull<<radix_bits) - 2 : (1ull<<radix_bits) - 1; // FIELD_MAGIC
  41. if (x->limb[i] < yardstick * lowBound || x->limb[i] > yardstick * highBound) {
  42. youfail();
  43. printf(" Limb %d -> " PRIxWORDfull " is out of bounds (%0.2f, %0.2f) for test %s (yardstick = " PRIxWORDfull ")\n",
  44. i, x->limb[i], lowBound, highBound, descr, yardstick);
  45. break;
  46. }
  47. }
  48. if (memcmp(xser,yser,FIELD_BYTES)) {
  49. youfail();
  50. printf(" Failed arithmetic test %s\n", descr);
  51. field_print(" a", a);
  52. field_print(" b", b);
  53. field_print(" goldi", x);
  54. printf(" gmp = 0x");
  55. int j;
  56. for (j=FIELD_BYTES-1; j>=0; j--) {
  57. printf("%02x", yser[j]);
  58. }
  59. printf("\n");
  60. return MASK_FAILURE;
  61. }
  62. mpz_clear(modded);
  63. return MASK_SUCCESS;
  64. }
  65. static mask_t test_add_sub (
  66. const mpz_t x,
  67. const mpz_t y,
  68. word_t word
  69. ) {
  70. struct field_t xx,yy,tt;
  71. mpz_t t;
  72. mask_t succ = MASK_SUCCESS;
  73. succ = mpz_to_field(&xx,x);
  74. succ &= mpz_to_field(&yy,y);
  75. mpz_init(t);
  76. field_add(&tt,&xx,&yy);
  77. mpz_add(t,x,y);
  78. succ &= field_assert_eq_gmp("add",&xx,&yy,&tt,t,0,2.1);
  79. field_sub(&tt,&xx,&yy);
  80. field_bias(&tt,2);
  81. mpz_sub(t,x,y);
  82. succ &= field_assert_eq_gmp("sub",&xx,&yy,&tt,t,0,3.1);
  83. field_copy(&tt,&xx);
  84. field_addw(&tt,word);
  85. mpz_add_ui(t,x,word);
  86. succ &= field_assert_eq_gmp("addw",&xx,&yy,&tt,t,0,2.1);
  87. field_copy(&tt,&xx);
  88. field_subw(&tt,word);
  89. field_bias(&tt,1);
  90. mpz_sub_ui(t,x,word);
  91. succ &= field_assert_eq_gmp("subw",&xx,&yy,&tt,t,0,2.1);
  92. /*
  93. if (!succ) {
  94. field_print(" x", &xx);
  95. field_print(" y", &yy);
  96. }
  97. */
  98. mpz_clear(t);
  99. return succ;
  100. }
  101. static mask_t test_mul_sqr (
  102. const mpz_t x,
  103. const mpz_t y,
  104. word_t word
  105. ) {
  106. struct field_t xx,yy,tt;
  107. mpz_t t;
  108. mask_t succ = MASK_SUCCESS;
  109. succ = mpz_to_field(&xx,x);
  110. succ &= mpz_to_field(&yy,y);
  111. mpz_init(t);
  112. field_mul(&tt,&xx,&yy);
  113. mpz_mul(t,x,y);
  114. succ &= field_assert_eq_gmp("mul",&xx,&yy,&tt,t,0,1.1);
  115. field_mulw(&tt,&xx,word);
  116. mpz_mul_ui(t,x,word);
  117. succ &= field_assert_eq_gmp("mulw",&xx,&yy,&tt,t,0,1.1);
  118. field_sqr(&tt,&xx);
  119. mpz_mul(t,x,x);
  120. succ &= field_assert_eq_gmp("sqrx",&xx,&yy,&tt,t,0,1.1);
  121. field_sqr(&tt,&yy);
  122. mpz_mul(t,y,y);
  123. succ &= field_assert_eq_gmp("sqy",&xx,&yy,&tt,t,0,1.1);
  124. if (!succ) {
  125. field_print(" x", &xx);
  126. field_print(" y", &yy);
  127. }
  128. mpz_clear(t);
  129. return succ;
  130. }
  131. static mask_t test_isr (
  132. const mpz_t x
  133. ) {
  134. struct field_t xx,yy,ss,tt;
  135. mask_t succ = 0;
  136. succ = mpz_to_field(&xx,x);
  137. field_isr(&ss,&xx);
  138. field_sqr(&tt,&ss);
  139. field_mul(&yy,&xx,&tt);
  140. field_addw(&tt,1);
  141. succ |= field_is_zero(&tt);
  142. field_subw(&tt,2);
  143. field_bias(&tt,1);
  144. succ |= field_is_zero(&tt);
  145. field_addw(&tt,1);
  146. if (~succ) {
  147. youfail();
  148. printf("ISR failure.\n");
  149. field_print(" x", &xx);
  150. field_print(" s", &ss);
  151. field_print(" t", &tt);
  152. }
  153. return succ;
  154. }
  155. void dbg_gmp_printf(const mpz_t x);
  156. void dbg_gmp_printf(const mpz_t x) {
  157. gmp_printf("DEBUG: 0x%Zx\n", x);
  158. }
  159. int test_arithmetic (void) {
  160. int j, ntests = 100000;
  161. gmp_randstate_t state;
  162. gmp_randinit_mt(state);
  163. mpz_init(mp_field);
  164. mpz_import(mp_field, FIELD_BYTES, -1, 1, -1, 0, FIELD_MODULUS);
  165. mpz_t x,y;
  166. mpz_init(x);
  167. mpz_init(y);
  168. mask_t succ = MASK_SUCCESS;
  169. int radix_bits = sizeof(word_t) * FIELD_BITS / sizeof(field_t);
  170. for (j=0; j<ntests; j++) {
  171. if (j<256) {
  172. mpz_set_ui(x,0);
  173. mpz_set_ui(y,0);
  174. mpz_setbit(x,(j%16)*28);
  175. mpz_setbit(y,(j/16)*28);
  176. } else if (j&1) {
  177. mpz_rrandomb(x, state, FIELD_BITS);
  178. mpz_rrandomb(y, state, FIELD_BITS);
  179. } else {
  180. mpz_urandomb(x, state, FIELD_BITS);
  181. mpz_urandomb(y, state, FIELD_BITS);
  182. }
  183. word_t word = gmp_urandomm_ui (state, 1ull<<radix_bits);
  184. succ &= test_add_sub(x,y,word);
  185. succ &= test_mul_sqr(x,y,word);
  186. if (j < 1000)
  187. succ &= test_isr(x);
  188. // TODO: test neg, cond_neg, set_ui, wrd, srd, inv, ...?
  189. }
  190. mpz_clear(x);
  191. mpz_clear(y);
  192. mpz_clear(mp_field);
  193. gmp_randclear(state);
  194. return succ ? 0 : 1;
  195. }