#include <openssl/bn.h>
#include "bn_internal.h"
#ifndef HAVE_BN_ADD_WORDS
BN_ULONG
bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
{
BN_ULONG carry = 0;
while (n >= 4) {
bn_qwaddqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
carry, &carry, &r[3], &r[2], &r[1], &r[0]);
a += 4;
b += 4;
r += 4;
n -= 4;
}
while (n > 0) {
bn_addw_addw(a[0], b[0], carry, &carry, &r[0]);
a++;
b++;
r++;
n--;
}
return carry;
}
#endif
#ifndef HAVE_BN_SUB_WORDS
BN_ULONG
bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
{
BN_ULONG borrow = 0;
while (n >= 4) {
bn_qwsubqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
borrow, &borrow, &r[3], &r[2], &r[1], &r[0]);
a += 4;
b += 4;
r += 4;
n -= 4;
}
while (n > 0) {
bn_subw_subw(a[0], b[0], borrow, &borrow, &r[0]);
a++;
b++;
r++;
n--;
}
return borrow;
}
#endif
#ifndef HAVE_BN_SUB_WORDS_BORROW
BN_ULONG
bn_sub_words_borrow(const BN_ULONG *a, const BN_ULONG *b, size_t n)
{
BN_ULONG borrow = 0;
BN_ULONG r;
while (n >= 4) {
bn_qwsubqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
borrow, &borrow, &r, &r, &r, &r);
a += 4;
b += 4;
n -= 4;
}
while (n > 0) {
bn_subw_subw(a[0], b[0], borrow, &borrow, &r);
a++;
b++;
n--;
}
return borrow;
}
#endif
#ifndef HAVE_BN_ADD_WORDS_MASKED
BN_ULONG
bn_add_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
BN_ULONG mask, size_t n)
{
BN_ULONG carry = 0;
while (n >= 4) {
bn_qwaddqw(a[3], a[2], a[1], a[0], b[3] & mask, b[2] & mask,
b[1] & mask, b[0] & mask, carry, &carry, &r[3], &r[2],
&r[1], &r[0]);
a += 4;
b += 4;
r += 4;
n -= 4;
}
while (n > 0) {
bn_addw_addw(a[0], b[0] & mask, carry, &carry, &r[0]);
a++;
b++;
r++;
n--;
}
return carry;
}
#endif
#ifndef HAVE_BN_SUB_WORDS_MASKED
BN_ULONG
bn_sub_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
BN_ULONG mask, size_t n)
{
BN_ULONG borrow = 0;
while (n >= 4) {
bn_qwsubqw(a[3], a[2], a[1], a[0], b[3] & mask, b[2] & mask,
b[1] & mask, b[0] & mask, borrow, &borrow, &r[3], &r[2],
&r[1], &r[0]);
a += 4;
b += 4;
r += 4;
n -= 4;
}
while (n > 0) {
bn_subw_subw(a[0], b[0] & mask, borrow, &borrow, &r[0]);
a++;
b++;
r++;
n--;
}
return borrow;
}
#endif