void check_com_negs (void) { static const struct { unsigned long bit; mp_size_t inp_size; mp_limb_t inp_n[5]; mp_size_t want_size; mp_limb_t want_n[5]; } data[] = { { GMP_NUMB_BITS, 2, { 1, 1 }, 1, { 1 } }, { GMP_NUMB_BITS+1, 2, { 1, 1 }, 2, { 1, 3 } }, { GMP_NUMB_BITS, 2, { 0, 1 }, 2, { 0, 2 } }, { GMP_NUMB_BITS+1, 2, { 0, 1 }, 2, { 0, 3 } }, }; mpz_t inp, got, want; int i; mpz_init (got); mpz_init (want); mpz_init (inp); for (i = 0; i < numberof (data); i++) { mpz_set_n (inp, data[i].inp_n, data[i].inp_size); mpz_neg (inp, inp); mpz_set_n (want, data[i].want_n, data[i].want_size); mpz_neg (want, want); mpz_set (got, inp); mpz_combit (got, data[i].bit); if (mpz_cmp (got, want) != 0) { printf ("mpz_combit: wrong on neg data[%d]\n", i); mpz_trace ("inp ", inp); printf ("bit %lu\n", data[i].bit); mpz_trace ("got ", got); mpz_trace ("want", want); abort (); } } mpz_clear (inp); mpz_clear (got); mpz_clear (want); }
/* See that mpz_tstbit matches a twos complement calculated explicitly, for various low zeros. */ void check_tstbit (void) { #define MAX_ZEROS 3 #define NUM_LIMBS 3 mp_limb_t pos[1+NUM_LIMBS+MAX_ZEROS]; mp_limb_t neg[1+NUM_LIMBS+MAX_ZEROS]; mpz_t z; unsigned long i; int zeros, low1; int got, want; mpz_init (z); for (zeros = 0; zeros <= MAX_ZEROS; zeros++) { MPN_ZERO (pos, numberof(pos)); mpn_random2 (pos+zeros, NUM_LIMBS); for (low1 = 0; low1 <= 1; low1++) { if (low1) pos[0] |= 1; refmpn_neg_n (neg, pos, numberof(neg)); mpz_set_n (z, neg, numberof(neg)); mpz_neg (z, z); for (i = 0; i < numberof(pos)*GMP_NUMB_BITS; i++) { got = mpz_tstbit (z, i); want = refmpn_tstbit (pos, i); if (got != want) { printf ("wrong at bit %lu, with %d zeros\n", i, zeros); printf ("z neg "); debug_mp (z, -16); mpz_set_n (z, pos, numberof(pos)); printf ("pos "); debug_mp (z, -16); mpz_set_n (z, neg, numberof(neg)); printf ("neg "); debug_mp (z, -16); exit (1); } } } } mpz_clear (z); }
void check_data_inplace_ui (void) { static const struct { mp_limb_t w[6]; unsigned long y; } data[] = { { { 0 }, 0 }, { { 0 }, 1 }, { { 1 }, 1 }, { { 2 }, 1 }, { { 123 }, 1 }, { { 123 }, ULONG_MAX }, { { M }, 1 }, { { M }, ULONG_MAX }, { { 123, 456 }, 1 }, { { M, M }, 1 }, { { 123, 456 }, ULONG_MAX }, { { M, M }, ULONG_MAX }, { { 123, 456, 789 }, 1 }, { { M, M, M }, 1 }, { { 123, 456, 789 }, ULONG_MAX }, { { M, M, M }, ULONG_MAX }, }; mpz_t w, y; int i; mpz_init (w); mpz_init (y); for (i = 0; i < numberof (data); i++) { mpz_set_n (w, data[i].w, (mp_size_t) numberof(data[i].w)); mpz_set_ui (y, data[i].y); check_all_inplace (w, y); } mpz_clear (w); mpz_clear (y); }
void ecc_scalar_get (const struct ecc_scalar *s, mpz_t z) { mpz_set_n (z, s->p, s->ecc->size); }
void check_data (void) { static const struct { mp_limb_t w[6]; mp_limb_t x[6]; mp_limb_t y[6]; } data[] = { /* reducing to zero */ { { 1 }, { 1 }, { 1 } }, { { 2 }, { 1 }, { 2 } }, { { 0,1 }, { 0,1 }, { 1 } }, /* reducing to 1 */ { { 0,1 }, { M }, { 1 } }, { { 0,0,1 }, { M,M }, { 1 } }, { { 0,0,0,1 }, { M,M,M }, { 1 } }, { { 0,0,0,0,1 }, { M,M,M,M }, { 1 } }, /* reducing to -1 */ { { M }, { 0,1 }, { 1 } }, { { M,M }, { 0,0,1 }, { 1 } }, { { M,M,M }, { 0,0,0,1 }, { 1 } }, { { M,M,M,M }, { 0,0,0,0,1 }, { 1 } }, /* carry out of addmul */ { { M }, { 1 }, { 1 } }, { { M,M }, { 1 }, { 1 } }, { { M,M,M }, { 1 }, { 1 } }, /* borrow from submul */ { { 0,1 }, { 1 }, { 1 } }, { { 0,0,1 }, { 1 }, { 1 } }, { { 0,0,0,1 }, { 1 }, { 1 } }, /* borrow from submul */ { { 0,0,1 }, { 0,1 }, { 1 } }, { { 0,0,0,1 }, { 0,1 }, { 1 } }, { { 0,0,0,0,1 }, { 0,1 }, { 1 } }, /* more borrow from submul */ { { M }, { 0,1 }, { 1 } }, { { M }, { 0,0,1 }, { 1 } }, { { M }, { 0,0,0,1 }, { 1 } }, { { M }, { 0,0,0,0,1 }, { 1 } }, /* big borrow from submul */ { { 0,0,1 }, { M,M }, { M } }, { { 0,0,0,1 }, { M,M }, { M } }, { { 0,0,0,0,1 }, { M,M }, { M } }, /* small w */ { { 0,1 }, { M,M }, { M } }, { { 0,1 }, { M,M,M }, { M } }, { { 0,1 }, { M,M,M,M }, { M } }, { { 0,1 }, { M,M,M,M,M }, { M } }, }; mpz_t w, x, y; int i; mpz_init (w); mpz_init (x); mpz_init (y); for (i = 0; i < numberof (data); i++) { mpz_set_n (w, data[i].w, (mp_size_t) numberof(data[i].w)); mpz_set_n (x, data[i].x, (mp_size_t) numberof(data[i].x)); mpz_set_n (y, data[i].y, (mp_size_t) numberof(data[i].y)); check_all (w, x, y); } mpz_clear (w); mpz_clear (x); mpz_clear (y); }