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.
 
 
 
 
 

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