4 ref_redc (mp_limb_t
*rp
, const mp_limb_t
*ap
, const mp_limb_t
*mp
, mp_size_t mn
)
11 mpz_setbit (t
, mn
* GMP_NUMB_BITS
);
13 mpz_roinit_n (m
, mp
, mn
);
16 while (an
> 0 && ap
[an
-1] == 0)
19 mpz_roinit_n (a
, ap
, an
);
25 mpz_limbs_copy (rp
, t
, mn
);
30 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
31 #define MAX_SIZE (2*MAX_ECC_SIZE)
37 gmp_randstate_t state
;
38 mp_limb_t a
[MAX_SIZE
];
39 mp_limb_t m
[MAX_SIZE
];
40 mp_limb_t ref
[MAX_SIZE
];
44 gmp_randinit_default (state
);
48 for (i
= 0; ecc_curves
[i
]; i
++)
50 const struct ecc_curve
*ecc
= ecc_curves
[i
];
55 for (j
= 0; j
< COUNT
; j
++)
58 mpz_rrandomb (r
, state
, 2*ecc
->size
* GMP_NUMB_BITS
);
60 mpz_urandomb (r
, state
, 2*ecc
->size
* GMP_NUMB_BITS
);
62 mpz_limbs_copy (a
, r
, 2*ecc
->size
);
64 ref_redc (ref
, a
, ecc
->p
, ecc
->size
);
66 mpn_copyi (m
, a
, 2*ecc
->size
);
68 if (mpn_cmp (m
, ecc
->p
, ecc
->size
) >= 0)
69 mpn_sub_n (m
, m
, ecc
->p
, ecc
->size
);
71 if (mpn_cmp (m
, ref
, ecc
->size
))
73 fprintf (stderr
, "ecc->redc failed: bit_size = %u\n",
75 gmp_fprintf (stderr
, "a = %Nx\n", a
, 2*ecc
->size
);
76 gmp_fprintf (stderr
, "m = %Nx (bad)\n", m
, ecc
->size
);
77 gmp_fprintf (stderr
, "ref = %Nx\n", ref
, ecc
->size
);
81 mpn_copyi (m
, a
, 2*ecc
->size
);
82 ecc_generic_redc (ecc
, m
);
83 if (mpn_cmp (m
, ecc
->p
, ecc
->size
) >= 0)
84 mpn_sub_n (m
, m
, ecc
->p
, ecc
->size
);
86 if (mpn_cmp (m
, ref
, ecc
->size
))
88 fprintf (stderr
, "ecc_generic_redc failed: bit_size = %u\n",
90 gmp_fprintf (stderr
, "a = %Nx\n", a
, 2*ecc
->size
);
91 gmp_fprintf (stderr
, "m = %Nx (bad)\n", m
, ecc
->size
);
92 gmp_fprintf (stderr
, "ref = %Nx\n", ref
, ecc
->size
);
99 gmp_randclear (state
);