diff options
author | Jack Lloyd <[email protected]> | 2018-08-15 07:46:36 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-08-15 07:46:36 -0400 |
commit | b3e15b49ad0946b141c78ac9bf25ef654b0eb017 (patch) | |
tree | 21bcda657a9dd59eb9eff0df989cf5da17813955 /src | |
parent | d4ab524798edd7609f9db7db5b050459fa7ad238 (diff) |
Remove support for 8 or 16 bit BigInt words
It turned out 8 bit was very broken (failed to compile, due to
overload problems with functions taking uint8_t vs word).
16 bit words work aside from a test failure, but is really slow.
Practically speaking we are not in a position to support 16-bit CPUs
very well. And being able to assume sizeof(word) >= sizeof(uint32_t)
allows simplifying some code.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/ffi/ffi.h | 3 | ||||
-rw-r--r-- | src/lib/ffi/ffi_mp.cpp | 41 | ||||
-rw-r--r-- | src/lib/math/mp/mp_madd.h | 11 | ||||
-rw-r--r-- | src/lib/math/numbertheory/curve_nistp.h | 4 | ||||
-rw-r--r-- | src/lib/math/numbertheory/nistp_redc.cpp | 8 | ||||
-rw-r--r-- | src/lib/utils/types.h | 8 | ||||
-rw-r--r-- | src/tests/test_ffi.cpp | 6 |
7 files changed, 52 insertions, 29 deletions
diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 0b61693ce..58d446d55 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -646,6 +646,9 @@ BOTAN_PUBLIC_API(2,1) int botan_mp_is_zero(const botan_mp_t mp); BOTAN_PUBLIC_API(2,1) int botan_mp_is_odd(const botan_mp_t mp); BOTAN_PUBLIC_API(2,1) int botan_mp_is_even(const botan_mp_t mp); +BOTAN_PUBLIC_API(2,8) int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y); +BOTAN_PUBLIC_API(2,8) int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y); + BOTAN_PUBLIC_API(2,1) int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y); BOTAN_PUBLIC_API(2,1) int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y); BOTAN_PUBLIC_API(2,1) int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y); diff --git a/src/lib/ffi/ffi_mp.cpp b/src/lib/ffi/ffi_mp.cpp index c7854ce9a..38a83e91b 100644 --- a/src/lib/ffi/ffi_mp.cpp +++ b/src/lib/ffi/ffi_mp.cpp @@ -138,17 +138,52 @@ int botan_mp_destroy(botan_mp_t mp) int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) { - return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) + safe_get(y); }); + return BOTAN_FFI_DO(Botan::BigInt, result, res, { + if(result == x) + res += safe_get(y); + else + res = safe_get(x) + safe_get(y); + }); } int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) { - return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) - safe_get(y); }); + return BOTAN_FFI_DO(Botan::BigInt, result, res, { + if(result == x) + res -= safe_get(y); + else + res = safe_get(x) - safe_get(y); + }); + } + +int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) + { + return BOTAN_FFI_DO(Botan::BigInt, result, res, { + if(result == x) + res += static_cast<Botan::word>(y); + else + res = safe_get(x) + static_cast<Botan::word>(y); + }); + } + +int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) + { + return BOTAN_FFI_DO(Botan::BigInt, result, res, { + if(result == x) + res -= static_cast<Botan::word>(y); + else + res = safe_get(x) - static_cast<Botan::word>(y); + }); } int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) { - return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) * safe_get(y); }); + return BOTAN_FFI_DO(Botan::BigInt, result, res, { + if(result == x) + res *= safe_get(y); + else + res = safe_get(x) * safe_get(y); + }); } int botan_mp_div(botan_mp_t quotient, diff --git a/src/lib/math/mp/mp_madd.h b/src/lib/math/mp/mp_madd.h index 4807fcd04..4f34efe39 100644 --- a/src/lib/math/mp/mp_madd.h +++ b/src/lib/math/mp/mp_madd.h @@ -14,15 +14,10 @@ namespace Botan { -#if (BOTAN_MP_WORD_BITS == 8) - typedef uint16_t dword; - #define BOTAN_HAS_MP_DWORD -#elif (BOTAN_MP_WORD_BITS == 16) - typedef uint32_t dword; - #define BOTAN_HAS_MP_DWORD -#elif (BOTAN_MP_WORD_BITS == 32) +#if (BOTAN_MP_WORD_BITS == 32) typedef uint64_t dword; #define BOTAN_HAS_MP_DWORD + #elif (BOTAN_MP_WORD_BITS == 64) #if defined(BOTAN_TARGET_HAS_NATIVE_UINT128) typedef uint128_t dword; @@ -32,7 +27,7 @@ namespace Botan { #endif #else - #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 + #error BOTAN_MP_WORD_BITS must be 32 or 64 #endif #if defined(BOTAN_TARGET_ARCH_IS_X86_32) && (BOTAN_MP_WORD_BITS == 32) diff --git a/src/lib/math/numbertheory/curve_nistp.h b/src/lib/math/numbertheory/curve_nistp.h index c9936a338..710b06dec 100644 --- a/src/lib/math/numbertheory/curve_nistp.h +++ b/src/lib/math/numbertheory/curve_nistp.h @@ -23,8 +23,6 @@ namespace Botan { BOTAN_PUBLIC_API(2,0) const BigInt& prime_p521(); BOTAN_PUBLIC_API(2,0) void redc_p521(BigInt& x, secure_vector<word>& ws); -#if (BOTAN_MP_WORD_BITS == 32) || (BOTAN_MP_WORD_BITS == 64) - #define BOTAN_HAS_NIST_PRIME_REDUCERS_W32 BOTAN_PUBLIC_API(2,0) const BigInt& prime_p384(); @@ -39,8 +37,6 @@ BOTAN_PUBLIC_API(2,0) void redc_p224(BigInt& x, secure_vector<word>& ws); BOTAN_PUBLIC_API(2,0) const BigInt& prime_p192(); BOTAN_PUBLIC_API(2,0) void redc_p192(BigInt& x, secure_vector<word>& ws); -#endif - } #endif diff --git a/src/lib/math/numbertheory/nistp_redc.cpp b/src/lib/math/numbertheory/nistp_redc.cpp index b74a2f9c6..1c2855784 100644 --- a/src/lib/math/numbertheory/nistp_redc.cpp +++ b/src/lib/math/numbertheory/nistp_redc.cpp @@ -91,10 +91,8 @@ inline uint32_t get_uint32_t(const BigInt& x, size_t i) { #if (BOTAN_MP_WORD_BITS == 32) return x.word_at(i); -#elif (BOTAN_MP_WORD_BITS == 64) - return static_cast<uint32_t>(x.word_at(i/2) >> ((i % 2)*32)); #else - #error "Not implemented" + return static_cast<uint32_t>(x.word_at(i/2) >> ((i % 2)*32)); #endif } @@ -103,10 +101,8 @@ inline void set_words(BigInt& x, size_t i, uint32_t R0, uint32_t R1) #if (BOTAN_MP_WORD_BITS == 32) x.set_word_at(i, R0); x.set_word_at(i+1, R1); -#elif (BOTAN_MP_WORD_BITS == 64) - x.set_word_at(i/2, (static_cast<uint64_t>(R1) << 32) | R0); #else - #error "Not implemented" + x.set_word_at(i/2, (static_cast<uint64_t>(R1) << 32) | R0); #endif } diff --git a/src/lib/utils/types.h b/src/lib/utils/types.h index e25682a2f..476701a1f 100644 --- a/src/lib/utils/types.h +++ b/src/lib/utils/types.h @@ -92,16 +92,12 @@ using u32bit = std::uint32_t; using u64bit = std::uint64_t; using s32bit = std::int32_t; -#if (BOTAN_MP_WORD_BITS == 8) - typedef uint8_t word; -#elif (BOTAN_MP_WORD_BITS == 16) - typedef uint16_t word; -#elif (BOTAN_MP_WORD_BITS == 32) +#if (BOTAN_MP_WORD_BITS == 32) typedef uint32_t word; #elif (BOTAN_MP_WORD_BITS == 64) typedef uint64_t word; #else - #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 + #error BOTAN_MP_WORD_BITS must be 32 or 64 #endif } diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index eb0eacdf4..680e70f8b 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -1121,7 +1121,7 @@ class FFI_Unit_Tests final : public Test TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes)); result.test_eq("Expected size for MP 5", bn_bytes, 1); - botan_mp_set_from_int(x, 80); + botan_mp_add_u32(x, x, 75); TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes)); result.test_eq("Expected size for MP 80", bn_bytes, 1); @@ -1129,7 +1129,9 @@ class FFI_Unit_Tests final : public Test TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len)); result.test_eq("botan_mp_add", std::string(str_buf), "80"); - botan_mp_set_from_int(x, 259); + botan_mp_sub_u32(x, x, 80); + TEST_FFI_RC(1, botan_mp_is_zero, (x)); + botan_mp_add_u32(x, x, 259); TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes)); result.test_eq("Expected size for MP 259", bn_bytes, 2); |