4 ref_mod (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
7 mpn_tdiv_qr (q, rp, 0, ap, 2*mn, mp, mn);
10 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
11 #define MAX_SIZE (2*MAX_ECC_SIZE)
17 gmp_randstate_t state;
18 mp_limb_t a[MAX_SIZE];
19 mp_limb_t m[MAX_SIZE];
20 mp_limb_t ref[MAX_SIZE];
24 gmp_randinit_default (state);
28 for (i = 0; ecc_curves[i]; i++)
30 const struct ecc_curve *ecc = ecc_curves[i];
32 for (j = 0; j < COUNT; j++)
35 mpz_rrandomb (r, state, 2*ecc->size * GMP_NUMB_BITS);
37 mpz_urandomb (r, state, 2*ecc->size * GMP_NUMB_BITS);
39 mpz_limbs_copy (a, r, 2*ecc->size);
41 ref_mod (ref, a, ecc->p, ecc->size);
43 mpn_copyi (m, a, 2*ecc->size);
45 if (mpn_cmp (m, ecc->p, ecc->size) >= 0)
46 mpn_sub_n (m, m, ecc->p, ecc->size);
48 if (mpn_cmp (m, ref, ecc->size))
50 fprintf (stderr, "ecc->modp failed: bit_size = %u\n",
52 gmp_fprintf (stderr, "a = %Nx\n", a, 2*ecc->size);
53 gmp_fprintf (stderr, "m = %Nx (bad)\n", m, ecc->size);
54 gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
58 if (ecc->Bmodp_size < ecc->size)
60 mpn_copyi (m, a, 2*ecc->size);
61 ecc_generic_modp (ecc, m);
62 if (mpn_cmp (m, ecc->p, ecc->size) >= 0)
63 mpn_sub_n (m, m, ecc->p, ecc->size);
65 if (mpn_cmp (m, ref, ecc->size))
67 fprintf (stderr, "ecc_generic_modp failed: bit_size = %u\n",
69 gmp_fprintf (stderr, "a = %Nx\n", a, 2*ecc->size);
70 gmp_fprintf (stderr, "m = %Nx (bad)\n", m, ecc->size);
71 gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
76 ref_mod (ref, a, ecc->q, ecc->size);
78 mpn_copyi (m, a, 2*ecc->size);
80 if (mpn_cmp (m, ecc->q, ecc->size) >= 0)
81 mpn_sub_n (m, m, ecc->q, ecc->size);
83 if (mpn_cmp (m, ref, ecc->size))
85 fprintf (stderr, "ecc->modq failed: bit_size = %u\n",
87 gmp_fprintf (stderr, "a = %Nx\n", a, 2*ecc->size);
88 gmp_fprintf (stderr, "m = %Nx (bad)\n", m, ecc->size);
89 gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
93 if (ecc->Bmodp_size < ecc->size)
95 mpn_copyi (m, a, 2*ecc->size);
96 ecc_generic_modq (ecc, m);
97 if (mpn_cmp (m, ecc->q, ecc->size) >= 0)
98 mpn_sub_n (m, m, ecc->q, ecc->size);
100 if (mpn_cmp (m, ref, ecc->size))
102 fprintf (stderr, "ecc_generic_modp failed: bit_size = %u\n",
104 gmp_fprintf (stderr, "a = %Nx\n", a, 2*ecc->size);
105 gmp_fprintf (stderr, "m = %Nx (bad)\n", m, ecc->size);
106 gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
114 gmp_randclear (state);