#ifndef __BIGNUM_C #define __BIGNUM_C #include "common.h" #include "multiply.h" #if defined(USE_SSE2) #if defined(HAVE_INTRIN_H) #include #elif defined(HAVE_X86INTRIN_H) #include #elif defined(HAVE_EMMINTRIN_H) #include #include #endif #endif static inline unsigned is_odd(uint64_t x) { return 1 == (x & 1); } static inline unsigned is_even(uint64_t x) { return !is_odd(x); } /** * Check if a multi-word integer x is greater than or equal to y. * * @param x The first term * @param y The second term * @param nw The number of words that make up x and y * @return 1 if x>=y, 0 if x y[i]; lower = x[i] < y[i]; result |= mask & (greater | (lower << 1)); mask &= (greater ^ lower) - 1; } return result<2; } /* * Subtract a multi-word integer b from a. * * @param out The location where the multi-word result is stored * @param a Number to subtract from * @param b Number to subtract * @param nw The number of words of both a and b * @result 0 if there is no borrow, 1 otherwise */ STATIC unsigned sub(uint64_t *out, const uint64_t *a, const uint64_t *b, size_t nw) { size_t i; unsigned borrow1 , borrow2; borrow2 = 0; for (i=0; i a[i]; out[i] = a[i] - b[i]; borrow1 |= borrow2 > out[i]; out[i] -= borrow2; borrow2 = borrow1; } return borrow2; } /* * Multiply a multi-word integer a by a 64-bit scalar k and * then add the result to the multi-word integer t. * * @param t The multi-word integer accumulator * @param tw The number of words of t * @param a The multi-word integer to multiply with the scalar * @param aw The number of words of a * @param k The 64-bit scalar multiplier */ STATIC void addmul(uint64_t *t, size_t tw, const uint64_t *a, size_t aw, uint64_t k) { size_t i; uint64_t carry; carry = 0; for (i=0; i tmp1[i]; tmp2[i] = tmp1[i] - modulus[i]; borrow1 |= borrow2 > tmp2[i]; tmp2[i] -= borrow2; borrow2 = borrow1; } /* * If there is no borrow or if there is carry, * tmp1[] is larger than modulus, so we must return tmp2[]. */ mod_select(out, tmp2, tmp1, carry | (borrow2 ^ 1), nw); } /* * Subtract two multi-word numbers with modulo arithmetic. * * @param out The locaton where the multi-word result (nw words) is stored * @param a The number it will be subtracted from (nw words) * @param b The number to subtract (nw wordS) * @param modulus The modulus (nw words) * @param tmp1 A temporary area (nw words) * @param tmp2 A temporary area (nw words) * @param nw The number of 64-bit words in all parameters * @return 0 for success, the relevant error code otherwise */ int sub_mod(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *modulus, uint64_t *tmp1, uint64_t *tmp2, size_t nw) { unsigned i; unsigned carry, borrow1 , borrow2; /* * Compute difference in tmp1[], and add modulus[] * to tmp1[] into tmp2[]. */ borrow2 = 0; carry = 0; for (i=0; i a[i]; tmp1[i] = a[i] - b[i]; borrow1 |= borrow2 > tmp1[i]; tmp1[i] -= borrow2; borrow2 = borrow1; tmp2[i] = tmp1[i] + carry; carry = tmp2[i] < carry; tmp2[i] += modulus[i]; carry += tmp2[i] < modulus[i]; } /* * If there is no borrow, tmp[] is smaller than modulus. */ mod_select(out, tmp2, tmp1, borrow2, nw); return 0; } #endif /* __BIGNUM_C **/