4 ref_modinv (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
11 mpz_roinit_n (a, ap, mn);
12 mpz_roinit_n (m, mp, mn);
14 mpz_gcdext (g, s, NULL, a, m);
15 if (mpz_cmp_ui (g, 1) == 0)
20 ASSERT (mpz_sgn (s) > 0);
22 mpz_limbs_copy (rp, s, mn);
34 zero_p (const struct ecc_modulo *m, const mp_limb_t *xp)
36 return mpn_zero_p (xp, m->size)
37 || mpn_cmp (xp, m->m, m->size) == 0;
40 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
44 test_modulo (gmp_randstate_t rands, const char *name,
45 const struct ecc_modulo *m)
56 a = xalloc_limbs (m->size);
57 ai = xalloc_limbs (2*m->size);
58 ref = xalloc_limbs (m->size);;
59 scratch = xalloc_limbs (m->invert_itch);
61 /* Check behaviour for zero input */
62 mpn_zero (a, m->size);
63 memset (ai, 17, m->size * sizeof(*ai));
64 m->invert (m, ai, a, scratch);
67 fprintf (stderr, "%s->invert failed for zero input (bit size %u):\n",
69 fprintf (stderr, "p = ");
70 mpn_out_str (stderr, 16, m->m, m->size);
71 fprintf (stderr, "\nt = ");
72 mpn_out_str (stderr, 16, ai, m->size);
73 fprintf (stderr, " (bad)\n");
77 /* Check behaviour for a = m */
78 memset (ai, 17, m->size * sizeof(*ai));
79 m->invert (m, ai, m->m, scratch);
82 fprintf (stderr, "%s->invert failed for a = p input (bit size %u):\n",
85 fprintf (stderr, "p = ");
86 mpn_out_str (stderr, 16, m->m, m->size);
87 fprintf (stderr, "\nt = ");
88 mpn_out_str (stderr, 16, ai, m->size);
89 fprintf (stderr, " (bad)\n");
93 for (j = 0; j < COUNT; j++)
96 mpz_rrandomb (r, rands, m->size * GMP_NUMB_BITS);
98 mpz_urandomb (r, rands, m->size * GMP_NUMB_BITS);
100 mpz_limbs_copy (a, r, m->size);
102 if (!ref_modinv (ref, a, m->m, m->size))
105 fprintf (stderr, "Test %u (bit size %u) not invertible mod %s.\n",
106 j, m->bit_size, name);
109 m->invert (m, ai, a, scratch);
110 if (mpn_cmp (ref, ai, m->size))
112 fprintf (stderr, "%s->invert failed (test %u, bit size %u):\n",
113 name, j, m->bit_size);
114 fprintf (stderr, "a = ");
115 mpz_out_str (stderr, 16, r);
116 fprintf (stderr, "\np = ");
117 mpn_out_str (stderr, 16, m->m, m->size);
118 fprintf (stderr, "\nt = ");
119 mpn_out_str (stderr, 16, ai, m->size);
120 fprintf (stderr, " (bad)\nr = ");
121 mpn_out_str (stderr, 16, ref, m->size);
137 gmp_randstate_t rands;
140 gmp_randinit_default (rands);
142 for (i = 0; ecc_curves[i]; i++)
144 test_modulo (rands, "p", &ecc_curves[i]->p);
145 test_modulo (rands, "q", &ecc_curves[i]->q);
147 gmp_randclear (rands);