Test |BN_uadd| and |BN_usub|.

Also, update the documentation about aliasing for |BN_usub|. It might
be better to find a way to factor out the shared logic between the
tests of these functions and the tests of |BN_add| and |BN_usub|, but
doing so would end up up creating a lot of parameters due to the many
distinct strings used in the messages.

Change-Id: Ic9d714858212fc92aa6bbcc3959576fe6bbf58c3
Reviewed-on: https://boringssl-review.googlesource.com/8593
Reviewed-by: David Benjamin <davidben@google.com>
This commit is contained in:
Brian Smith 2016-07-01 14:12:40 -10:00 committed by David Benjamin
parent e4bf8b3e05
commit 3d4030b5f7
2 changed files with 41 additions and 1 deletions

View File

@ -336,6 +336,46 @@ static bool TestSum(FileTest *t, BN_CTX *ctx) {
return false;
}
// Test |BN_uadd| and |BN_usub| with the prerequisites they are documented as
// having. Note that these functions are frequently used when the
// prerequisites don't hold. In those cases, they are supposed to work as if
// the prerequisite hold, but we don't test that yet. TODO: test that.
if (!BN_is_negative(a.get()) &&
!BN_is_negative(b.get()) && BN_cmp(a.get(), b.get()) >= 0) {
if (!BN_uadd(ret.get(), a.get(), b.get()) ||
!ExpectBIGNUMsEqual(t, "A +u B", sum.get(), ret.get()) ||
!BN_usub(ret.get(), sum.get(), a.get()) ||
!ExpectBIGNUMsEqual(t, "Sum -u A", b.get(), ret.get()) ||
!BN_usub(ret.get(), sum.get(), b.get()) ||
!ExpectBIGNUMsEqual(t, "Sum -u B", a.get(), ret.get())) {
return false;
}
// Test that the functions work when |r| and |a| point to the same |BIGNUM|,
// or when |r| and |b| point to the same |BIGNUM|. TODO: Test the case where
// all of |r|, |a|, and |b| point to the same |BIGNUM|.
if (!BN_copy(ret.get(), a.get()) ||
!BN_uadd(ret.get(), ret.get(), b.get()) ||
!ExpectBIGNUMsEqual(t, "A +u B (r is a)", sum.get(), ret.get()) ||
!BN_copy(ret.get(), b.get()) ||
!BN_uadd(ret.get(), a.get(), ret.get()) ||
!ExpectBIGNUMsEqual(t, "A +u B (r is b)", sum.get(), ret.get()) ||
!BN_copy(ret.get(), sum.get()) ||
!BN_usub(ret.get(), ret.get(), a.get()) ||
!ExpectBIGNUMsEqual(t, "Sum -u A (r is a)", b.get(), ret.get()) ||
!BN_copy(ret.get(), a.get()) ||
!BN_usub(ret.get(), sum.get(), ret.get()) ||
!ExpectBIGNUMsEqual(t, "Sum -u A (r is b)", b.get(), ret.get()) ||
!BN_copy(ret.get(), sum.get()) ||
!BN_usub(ret.get(), ret.get(), b.get()) ||
!ExpectBIGNUMsEqual(t, "Sum -u B (r is a)", a.get(), ret.get()) ||
!BN_copy(ret.get(), b.get()) ||
!BN_usub(ret.get(), sum.get(), ret.get()) ||
!ExpectBIGNUMsEqual(t, "Sum -u B (r is b)", a.get(), ret.get())) {
return false;
}
}
// Test with |BN_add_word| and |BN_sub_word| if |b| is small enough.
BN_ULONG b_word = BN_get_word(b.get());
if (!BN_is_negative(b.get()) && b_word != (BN_ULONG)-1) {

View File

@ -388,7 +388,7 @@ OPENSSL_EXPORT int BN_add_word(BIGNUM *a, BN_ULONG w);
OPENSSL_EXPORT int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
/* BN_usub sets |r| = |a| - |b|, where |a| and |b| are non-negative integers,
* |b| < |a| and |r| must be a distinct pointer from |a| and |b|. It returns
* |b| < |a| and |r| may be the same pointer as either |a| or |b|. It returns
* one on success and zero on allocation failure. */
OPENSSL_EXPORT int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);