Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / testsuite / ecc-mod-test.c
blob658f54036c38d9903cc340d989b86b421c00b9b6
1 #include "testutils.h"
3 static void
4 ref_mod (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
6 mp_limb_t q[mn + 1];
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)
12 #define COUNT 50000
14 void
15 test_main (void)
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];
21 unsigned i;
22 mpz_t r;
24 gmp_randinit_default (state);
26 mpz_init (r);
28 for (i = 0; ecc_curves[i]; i++)
30 const struct ecc_curve *ecc = ecc_curves[i];
31 unsigned j;
32 for (j = 0; j < COUNT; j++)
34 if (j & 1)
35 mpz_rrandomb (r, state, 2*ecc->size * GMP_NUMB_BITS);
36 else
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);
44 ecc->modp (ecc, m);
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",
51 ecc->bit_size);
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);
55 abort ();
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",
68 ecc->bit_size);
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);
72 abort ();
76 ref_mod (ref, a, ecc->q, ecc->size);
78 mpn_copyi (m, a, 2*ecc->size);
79 ecc->modq (ecc, m);
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",
86 ecc->bit_size);
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);
90 abort ();
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",
103 ecc->bit_size);
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);
107 abort ();
113 mpz_clear (r);
114 gmp_randclear (state);