From 459f4a5e7aecd0a069d4ea4d6ea92a5611a17502 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 31 Oct 2016 17:26:24 +0100 Subject: [PATCH] add setbit, clearbit, testbit and xor to bignum --- bignum.c | 27 +++++++++++++++++++++++++++ bignum.h | 8 ++++++++ 2 files changed, 35 insertions(+) diff --git a/bignum.c b/bignum.c index eed53d22be..b2a20f9747 100644 --- a/bignum.c +++ b/bignum.c @@ -296,6 +296,33 @@ void bn_rshift(bignum256 *a) a->val[8] >>= 1; } +// sets bit in bignum +void bn_setbit(bignum256 *a, uint8_t bit) +{ + a->val[bit / 30] |= (1 << (bit % 30)); +} + +// clears bit in bignum +void bn_clearbit(bignum256 *a, uint8_t bit) +{ + a->val[bit / 30] &= ~(1 << (bit % 30)); +} + +// tests bit in bignum +uint32_t bn_testbit(bignum256 *a, uint8_t bit) +{ + return a->val[bit / 30] & (1 << (bit % 30)); +} + +// a = b ^ c +void bn_xor(bignum256 *a, const bignum256 *b, const bignum256 *c) +{ + int i; + for (i = 0; i < 9; i++) { + a->val[i] = b->val[i] ^ c->val[i]; + } +} + // multiply x by 1/2 modulo prime. // it computes x = (x & 1) ? (x + prime) >> 1 : x >> 1. // assumes x is normalized. diff --git a/bignum.h b/bignum.h index 451e344891..83ae314ce9 100644 --- a/bignum.h +++ b/bignum.h @@ -105,6 +105,14 @@ void bn_lshift(bignum256 *a); void bn_rshift(bignum256 *a); +void bn_setbit(bignum256 *a, uint8_t bit); + +void bn_clearbit(bignum256 *a, uint8_t bit); + +uint32_t bn_testbit(bignum256 *a, uint8_t bit); + +void bn_xor(bignum256 *a, const bignum256 *b, const bignum256 *c); + void bn_mult_half(bignum256 *x, const bignum256 *prime); void bn_mult_k(bignum256 *x, uint8_t k, const bignum256 *prime);