diff --git a/src/lib/math/bigint/big_code.cpp b/src/lib/math/bigint/big_code.cpp index 2ef986e6770..97f22deec3a 100644 --- a/src/lib/math/bigint/big_code.cpp +++ b/src/lib/math/bigint/big_code.cpp @@ -12,8 +12,11 @@ #include #include #include -#include +#include #include +#include +#include +#include namespace Botan { @@ -35,6 +38,16 @@ consteval size_t decimal_conversion_radix_digits() { } } +/* +* Return floor(x / 10) using a multiplication by ceil(2^(W+3) / 10) +*/ +constexpr word divide_by_10(word x) { + constexpr word reciprocal = (WordInfo::max / 5) * 4 + 1; + word hi = 0; + word_madd2(x, reciprocal, &hi); + return hi >> 3; +} + } // namespace std::string BigInt::to_dec_string() const { @@ -42,65 +55,80 @@ std::string BigInt::to_dec_string() const { constexpr word conversion_radix = decimal_conversion_radix(); constexpr size_t radix_digits = decimal_conversion_radix_digits(); + // Each division by the radix removes at least this many bits from the value + constexpr size_t radix_bits = std::bit_width(conversion_radix) - 1; + + constexpr auto div_radix = divide_precomp::setup(conversion_radix); + + /* + * A division produces its quotient words most significant first, which is + * the order the following division consumes them in. So several successive + * divisions can be done in a single pass over the value. The remainders are + * independent dependency chains which the CPU can execute in parallel. + */ + constexpr size_t divisions_per_pass = 3; + // (over-)estimate of the number of digits needed; log2(10) ~ 3.3219 const size_t digit_estimate = static_cast(1 + (static_cast(this->bits()) / 3.32)); // (over-)estimate of db such that conversion_radix^db > *this const size_t digit_blocks = (digit_estimate + radix_digits - 1) / radix_digits; - BigInt value = *this; - value.set_sign(Positive); - - // Extract groups of digits into words - std::vector digit_groups(digit_blocks); + secure_vector value(_data(), _data() + sig_words()); + size_t value_bits = this->bits(); - for(size_t i = 0; i != digit_blocks; ++i) { - word remainder = 0; - ct_divide_word(value, conversion_radix, value, remainder); - digit_groups[i] = remainder; - } + // The digits are written starting from the least significant, at the + // end of the string. The extra leading character leaves room for a sign. + std::string s(1 + digit_blocks * radix_digits, '0'); + size_t s_pos = s.size(); - BOTAN_ASSERT_NOMSG(value.is_zero()); + for(size_t i = 0; i < digit_blocks; i += divisions_per_pass) { + const size_t value_words = (value_bits + WordInfo::bits - 1) / WordInfo::bits; - // Extract digits from the groups - std::vector digits(digit_blocks * radix_digits); + // Extract groups of digits into words + std::array digit_groups = {}; - constexpr auto div_10 = divide_precomp::setup(10); + for(size_t j = value_words; j > 0; --j) { + word w = value[j - 1]; + for(size_t k = 0; k != divisions_per_pass; ++k) { + const auto [q, r] = div_radix.divmod_2to1_ct(digit_groups[k], w); + digit_groups[k] = r; + w = q; + } + value[j - 1] = w; + } - for(size_t i = 0; i != digit_blocks; ++i) { - word remainder = digit_groups[i]; - for(size_t j = 0; j != radix_digits; ++j) { - const auto [q, digit] = div_10.divmod_2to1_ct(0, remainder); - digits[radix_digits * i + j] = static_cast(digit); - remainder = q; + for(size_t k = 0; k != divisions_per_pass; ++k) { + if(i + k < digit_blocks) { + // Extract digits from the group + word remainder = digit_groups[k]; + for(size_t j = 0; j != radix_digits; ++j) { + const word q = divide_by_10(remainder); + s_pos -= 1; + s[s_pos] = static_cast('0' + (remainder - q * 10)); // assumes ASCII + remainder = q; + } + } else { + BOTAN_ASSERT_NOMSG(digit_groups[k] == 0); + } } - } - // remove leading zeros - while(!digits.empty() && digits.back() == 0) { - digits.pop_back(); + value_bits -= std::min(value_bits, divisions_per_pass * radix_bits); } - BOTAN_ASSERT_NOMSG(digit_estimate >= digits.size()); + BOTAN_ASSERT_NOMSG(CT::all_zeros(value.data(), value.size()).as_bool()); - // Reverse the digits to big-endian and format to text - std::string s; - s.reserve(1 + digits.size()); + // remove leading zeros, but always keeping at least one digit + size_t first_digit = std::min(s.find_first_not_of('0'), s.size() - 1); - if(signum() < 0) { - s += '-'; - } + BOTAN_ASSERT_NOMSG(digit_estimate >= s.size() - first_digit); - // Reverse and convert to textual digits - // TODO(Botan4) use std::ranges::reverse_view here once available (need newer Clang) - // NOLINTNEXTLINE(modernize-loop-convert) - for(auto i = digits.rbegin(); i != digits.rend(); ++i) { - s.push_back(*i + '0'); // assumes ASCII + if(signum() < 0) { + first_digit -= 1; + s[first_digit] = '-'; } - if(s.empty()) { - s += '0'; - } + s.erase(0, first_digit); return s; } @@ -148,34 +176,35 @@ BigInt BigInt::from_radix_digits(std::string_view digits, size_t radix) { constexpr word conversion_radix = decimal_conversion_radix(); constexpr size_t radix_digits = decimal_conversion_radix_digits(); - BigInt r; + const size_t blocks = (digits.size() + radix_digits - 1) / radix_digits; - // Handle the initial partial block (if digit count is not a multiple of radix_digits) + // After b blocks the value is less than conversion_radix^b so fits in b words + secure_vector r(blocks); + + // The initial block is short if the digit count is not a multiple of radix_digits const size_t partial_block = digits.size() % radix_digits; + size_t block_digits = (partial_block > 0) ? partial_block : radix_digits; + size_t offset = 0; - if(partial_block > 0) { + for(size_t b = 0; b != blocks; ++b) { word acc = 0; - for(size_t i = 0; i < partial_block; ++i) { - const char c = digits[i]; + for(size_t j = 0; j != block_digits; ++j) { + const char c = digits[offset + j]; BOTAN_ARG_CHECK(c >= '0' && c <= '9', "Invalid decimal character"); acc = acc * 10 + static_cast(c - '0'); } - r += acc; - } + offset += block_digits; + block_digits = radix_digits; - // Process full blocks of radix_digits - for(size_t i = partial_block; i != digits.size(); i += radix_digits) { - word acc = 0; - for(size_t j = 0; j < radix_digits; ++j) { - const char c = digits[i + j]; - BOTAN_ARG_CHECK(c >= '0' && c <= '9', "Invalid decimal character"); - acc = acc * 10 + static_cast(c - '0'); + // r = r * conversion_radix + acc + word carry = acc; + for(size_t i = 0; i != b; ++i) { + r[i] = word_madd2(r[i], conversion_radix, &carry); } - r *= conversion_radix; - r += acc; + r[b] = carry; } - return r; + return BigInt::_from_words(r); } else { throw Invalid_Argument("BigInt::from_radix_digits unknown radix"); } diff --git a/src/lib/utils/ct_utils.h b/src/lib/utils/ct_utils.h index dfd38e12c96..f56f84ebec5 100644 --- a/src/lib/utils/ct_utils.h +++ b/src/lib/utils/ct_utils.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -453,8 +454,22 @@ class Mask final { * Return a Mask which is set if x < y */ static constexpr Mask is_lt(T x, T y) { - T u = x ^ ((x ^ y) | ((x - y) ^ x)); - return Mask::expand_top_bit(u); +#if defined(BOTAN_TARGET_HAS_NATIVE_UINT128) + using widest = uint128_t; +#else + using widest = uint64_t; +#endif + + if constexpr(sizeof(T) < sizeof(widest)) { + // Compute x - y in a wider type: the bits above the size of T + // are all set if x < y, and all clear otherwise + using W = std::conditional_t<(sizeof(T) < sizeof(uint64_t)), uint64_t, widest>; + const W diff = static_cast(x) - static_cast(y); + return Mask(value_barrier(static_cast(diff >> (8 * sizeof(T))))); + } else { + const T u = x ^ ((x ^ y) | ((x - y) ^ x)); + return Mask::expand_top_bit(u); + } } /** diff --git a/src/tests/data/bn/from_radix.vec b/src/tests/data/bn/from_radix.vec index 982334adb71..2e1ccda04f8 100644 --- a/src/tests/data/bn/from_radix.vec +++ b/src/tests/data/bn/from_radix.vec @@ -140,6 +140,251 @@ Output = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF Input = 0000000340282366920938463463374607431768211456 Output = 0100000000000000000000000000000000 +# Powers of ten and neighbors +Input = 99999999 +Output = 05F5E0FF + +Input = 100000000 +Output = 05F5E100 + +Input = 999999999 +Output = 3B9AC9FF + +Input = 1000000000 +Output = 3B9ACA00 + +Input = 9999999999 +Output = 02540BE3FF + +Input = 10000000000 +Output = 02540BE400 + +Input = 999999999999999999 +Output = 0DE0B6B3A763FFFF + +Input = 1000000000000000000 +Output = 0DE0B6B3A7640000 + +Input = 9999999999999999999 +Output = 8AC7230489E7FFFF + +Input = 10000000000000000000 +Output = 8AC7230489E80000 + +Input = 99999999999999999999 +Output = 056BC75E2D630FFFFF + +Input = 100000000000000000000 +Output = 056BC75E2D63100000 + +Input = 999999999999999999999999999 +Output = 033B2E3C9FD0803CE7FFFFFF + +Input = 1000000000000000000000000000 +Output = 033B2E3C9FD0803CE8000000 + +Input = 9999999999999999999999999999999999999 +Output = 0785EE10D5DA46D900F4369FFFFFFFFF + +Input = 10000000000000000000000000000000000000 +Output = 0785EE10D5DA46D900F436A000000000 + +Input = 99999999999999999999999999999999999999 +Output = 4B3B4CA85A86C47A098A223FFFFFFFFF + +Input = 100000000000000000000000000000000000000 +Output = 4B3B4CA85A86C47A098A224000000000 + +Input = 999999999999999999999999999999999999999 +Output = 02F050FE938943ACC45F65567FFFFFFFFF + +Input = 1000000000000000000000000000000000000000 +Output = 02F050FE938943ACC45F65568000000000 + +Input = 99999999999999999999999999999999999999999999999999999999 +Output = 04140C78940F6A24FDFFC78873D4490D20FFFFFFFFFFFFFF + +Input = 100000000000000000000000000000000000000000000000000000000 +Output = 04140C78940F6A24FDFFC78873D4490D2100000000000000 + +Input = 999999999999999999999999999999999999999999999999999999999 +Output = 28C87CB5C89A2571EBFDCB54864ADA8349FFFFFFFFFFFFFF + +Input = 1000000000000000000000000000000000000000000000000000000000 +Output = 28C87CB5C89A2571EBFDCB54864ADA834A00000000000000 + +Input = 9999999999999999999999999999999999999999999999999999999999 +Output = 0197D4DF19D605767337E9F14D3EEC8920E3FFFFFFFFFFFFFF + +Input = 10000000000000000000000000000000000000000000000000000000000 +Output = 0197D4DF19D605767337E9F14D3EEC8920E400000000000000 + +Input = 9999999999999999999999999999999999999999999999999999999999999999999999999999 +Output = 161BCCA7119915B50764B4ABE86529797775A5F171950FFFFFFFFFFFFFFFFFFF + +Input = 10000000000000000000000000000000000000000000000000000000000000000000000000000 +Output = 161BCCA7119915B50764B4ABE86529797775A5F1719510000000000000000000 + +Input = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 +Output = 0BFC2EF456AE276E89E3FEDD8C321A67E93A4802B0727839301BF4A67FFFFFFFFFFFFFFFFFFFFFFF + +Input = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +Output = 0BFC2EF456AE276E89E3FEDD8C321A67E93A4802B0727839301BF4A6800000000000000000000000 + +Input = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 +Output = 067F43FBE77A37F8B7309320C32B3CD31DB2332B93F1E94F637A54290FE2A55CD163FFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +Output = 067F43FBE77A37F8B7309320C32B3CD31DB2332B93F1E94F637A54290FE2A55CD1640000000000000000000000000000 + +# Powers of two and neighbors +Input = 9223372036854775807 +Output = 7FFFFFFFFFFFFFFF + +Input = 9223372036854775808 +Output = 8000000000000000 + +Input = 18446744073709551615 +Output = FFFFFFFFFFFFFFFF + +Input = 18446744073709551616 +Output = 010000000000000000 + +Input = 36893488147419103231 +Output = 01FFFFFFFFFFFFFFFF + +Input = 36893488147419103232 +Output = 020000000000000000 + +Input = 85070591730234615865843651857942052863 +Output = 3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 85070591730234615865843651857942052864 +Output = 40000000000000000000000000000000 + +Input = 340282366920938463463374607431768211455 +Output = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 340282366920938463463374607431768211456 +Output = 0100000000000000000000000000000000 + +Input = 784637716923335095479473677900958302012794430558004314111 +Output = 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 784637716923335095479473677900958302012794430558004314112 +Output = 200000000000000000000000000000000000000000000000 + +Input = 1569275433846670190958947355801916604025588861116008628223 +Output = 3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 1569275433846670190958947355801916604025588861116008628224 +Output = 400000000000000000000000000000000000000000000000 + +Input = 6277101735386680763835789423207666416102355444464034512895 +Output = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 6277101735386680763835789423207666416102355444464034512896 +Output = 01000000000000000000000000000000000000000000000000 + +Input = 115792089237316195423570985008687907853269984665640564039457584007913129639935 +Output = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 115792089237316195423570985008687907853269984665640564039457584007913129639936 +Output = 010000000000000000000000000000000000000000000000000000000000000000 + +Input = 615656346818663737691860001564743965704370926101022604186692084441339402679643915803347910232576806887603562348543 +Output = 03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 615656346818663737691860001564743965704370926101022604186692084441339402679643915803347910232576806887603562348544 +Output = 040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + +Input = 1231312693637327475383720003129487931408741852202045208373384168882678805359287831606695820465153613775207124697087 +Output = 07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 1231312693637327475383720003129487931408741852202045208373384168882678805359287831606695820465153613775207124697088 +Output = 080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + +Input = 39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306815 +Output = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306816 +Output = 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + +Input = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095 +Output = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF + +Input = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096 +Output = 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + +# Decimal with long runs of zero digits +Input = 1000000000000000000000000000000000000000000000000000000001 +Output = 28C87CB5C89A2571EBFDCB54864ADA834A00000000000001 + +Input = 10000000000000000000000000000000000000000000000000000000010000000000000000000 +Output = 161BCCA7119915B50764B4ABE86529797775A5F1719510008AC7230489E80000 + +Input = 700000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000 +Output = 53E548AE5EC31405C53BF80ED55EB8D76097F812D3214991327596868F944D6E1C9E66C000000000 + +Input = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345 +Output = 0BFC2EF456AE276E89E3FEDD8C321A67E93A4802B0727839301BF4A6800000000000000000003039 + +Input = 1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009 +Output = 0123788900DDFEE22D1B7BD5DD3067DFBFE767BAEB76FEC5AAAA761A65165F3223D4835EA96557FB845D5663BA6573103FC4CC000000000000000000000000000000000000000009 + +# Longer decimal inputs +Input = 14933102336720182474947656453055305569545361728 +Output = 029D9F9B97B8D71C96B00A23FBD5B78AABCE1940 + +Input = 148155273351386467340664197686615817542788995195911098548 +Output = 060AD02FAC9D7BB4DAD8E56D2E0BD8BB8F76D5D1E71D50B4 + +Input = 9355948070842976299133580776484140298340887909548274138511 +Output = 017D90A7D54CDFEBB1AF3D8E2BD738E954CF3A8C9BC49EBD8F + +Input = 5089496457929132188219202781418786638113801932765096813456029228670528288787 +Output = 0B408D8BA8121381822C3EF460A35B0DBE5E09FCCE06F7217C09480100587813 + +Input = 10500550190751984745302924273541256437663220033888752633313148159571191819325 +Output = 173719CD880954B0C69EF2F1C7BB39F4523411AD76C2459D1986DE4B65DA6C3D + +Input = 56484244475426718045272489658904534324412879422085572443819873998921599385226774339192233589675 +Output = 06C50A4BF57C2B7DE62E70E2941A8924F2240081267BE792EB0B5F684BE706064D0591B3FCA6FBAB + +Input = 435890826577662632094552041322401328837073354115701822824507472869037119347393663181866655630287 +Output = 343DEE17E4EF189A84DBCA69C5EA14015D0CB96ED1D0A13C60526B7D2D07F80CF0E207F439E143CF + +Input = 899016530187410670914760671134705520883820240740506249123141118963753782682847735207386682461896804620545997337037 +Output = 05D74DA0CB6EF1D27AA0598FF075959E0BEDA7C5F6682DFEB8F53CCBFC41A5D2B1E957113130E2468825D62FDD46F1CD + +Input = 9269831082130693027267851565172825832012288942970584401615143081729113576088200258339238969021042408842169357000461172956458016080217 +Output = 0343D26163D3D2A72AA8696E69B653E306DB205B28F3E39CD9F7B3A02BDF581C10291C62D87EC4F849EC40BE4A8259530D63799D0DBC8159 + +Input = 24925101172937678796063847930240685323903452595970428028917850322715859681963128450378292696265564486397698834102977771643039227277568104914084491690278 +Output = 79D4D3CAEB285C8C64B959660C6E244931283A2A2E1F2D3BB243F9FC4CF4946BC2B3075E506F4AB127AF78BEF3ABE174B01F2B5EC4EBC991E5AE0CF6100126 + +Input = 58408173673344908281935334744052863163192985036659544917841497830579486078256104306125475510322611358668981255298161561207339062788411676475128416167742895 +Output = 045B3534B51B8B80560ABFE2EBD25660FD5153E1E3F2853D5EC8EE5420D51ACDD90E19E63C459DEA8890A7777B6D5F3F8D0F1FB8FC2B334D6E508AB2E5E74F4DAF + +Input = 558123207662253042318917813050278202133333756872591548687843028466888760119803632908924624841158727938344635931349470278124793581635743015163995932675172842628517671051618 +Output = 93E34A763E7033DD7BCB06C91274B17EB962E0AD00845A48B1D25A3DF14EE866258F285FB5F7FE305DEACE563065702D5BC4E1BCF741A8A395A2A81F6E91ADA3062A1F3475A162 + +Input = 9796767699336992942827032121545859548854448304327167059376917986250974403121872085291644098714352749211067896941452275762109495351249564949583740525299358865142174702359698433657497616056444 +Output = 8CB91FAD3E3883B44B737A80B57549C992F764EC33FF066E1453212711610E5796373E66CB2606E19E663D8C1F378C721A800606A36CB7F94CFBC1151279EC2F97AACF89A98EE303A1BD22929F007C + +Input = 5356090575998664400620389822529700526456791701076657302298918322493973010839742502242590842533084366192211552897039053995940932835540074269371778447562242215441285092735707590826403272826944565496586370409642486490575358335247740 +Output = E2185942DE9F6D6D7B719F2F44453E1F237B0166EA7ADD031B7F8EF3406254E44558301FA43DFBDF129069961527D0B8873FB86A33A05BBD48D7A803CD1A6DA0F831CDF78955EAD5E10DCF30B76F8A25410590D9F119F84702EE8C8F39E57C + +Input = 599106433828350236952429363424113074013928956354706848857651937310729252188221046030086151418727201142034553996328732642277907661956940140594432506346018961811806198519145184221473166912888586833040031869953695965651417740217138437229113838000303827352799847462292173007767257785828255437218071218152846700101 +Output = 035527E7D48FE30CFC2D767DB3AE1AEBC2192BC59B00A62E0E6D3E5B106578DC79F35ACBDC1280ECEAAB21B85A1D0558FC13D2BAC2DB797B022B40FFCA721230607C10706F8116A06F440C6AC7738FB7874584A56E6BE63087356C57DF411052FD434FE9B4B7461D884259758425D16BF1C3FAD8327A88AA43045F94C3EF44AE45 + +Input = 35669962082926146555902680161888061619922230714022148845848240765491693055045760821001799588947607875690454109472830232824428003629422973009190721513665541552771810541399329955558689253406051011130907502922415212467531649961954370878817422756795551666584126123016853434353213273343107017815007234096257681076433085513191882274147037293935145585561872993858392070067684193613093830233647336905661860153658841859679392149620206705109378789929939073754435237525272049975041496387371482634872878400669939385304892111845078228545809748386179621484571716618924249052311377460488039569554163773022858702293231439720351736020 +Output = 011A8F7EC0644E115F1789A9768B46EB1FB57110C854F38E1425B736CB11ED934770D1DD68C2EDC19FCDEFC54576A0C9D89FB5C518525C352FD84F3AE6DD1ECFE0724DDB1680053B2CB82E9FA3A6692EB404F73E803B374126BDB096E0CFC6327068295C1034D36EE2FBCA6694771DA2497FF24BFCB05EFDE822302EEB70E7ACC7ABE47A2826E8ED58A6EC81CFB8C20BEF13BDD96E72894A97C458C6817DC52CA57709D6AD715883E768C803292A5404DF956A9B356F86C92DEDD5680C711ED74F018B48DD16320321BC5F11BEE51FC78F0B5E4423E521C57AC6D9A2045BD693FF912B05DB454D115E55F7361E322B3CBF0F81F3AB034CD1689EF149E4E8AF20D4 + +Input = 5055975609045757627303986290138723221015108773984695957469642962970383346958990751590034487001656355948033736623175028623178947694799307950374986298230016927947465610323281166652873852563083818328794477897835745397313558995906987666929766573593977739985634341730916971836796708996116687131253578428052007835787582279673083708819949856080631419367331199244621040894643937757199894053967450019577752971082573630149092971231347563717166703048271811060499916631708389032936470479532638379015470616673095420838063906672906286434683139122042452302308029736350159083254633899929683082381454753103542424484658512327002247362254089110098498364029655311420049795820537971707284376122850093138684367319008267695773709708542656745427939302463961761902205676902429357074578738748414952872168279152709252169812943369199407708813270790994529283841317865954031290742795648805506666172916972398692577898346201674853943318883629261629830059044721914021936498697449850638437496659449436715849002885874390625283282821691537848608767307607388508945381592413306042486454587504617875616061221029653440898382800643752417932350853847852112309479558357484418904335439689456692670960770813410500372760072142629996098854521879832697146739324690709187957645484928 +Output = 04D7515CE9C8FB125B0B5A428C8DF92361C57850887EA3AA4567D206A3996614A1E9B6FC5FDC6C0D1A0F8CBB52027E008BF9A4B74C84797B364F0B8F6EC2E387690B38B5960D8E84B204B08FC049BE4084B123D98C086516C4A2F610795F74A3F6B7FA76C32D106F307884B517C0E59A1CEE9DAD2F7C6F1AC4EABF7A341D373BEA3F187C74BD207D0CD2184C41AF72DBBC0851579E4A299B46B4BFCB718E4A17FC0B5114AA57944A8E4925549A760C7721FB37859FAD34658D2FD14235B22E32F22237B72B6B452E7EA525DA0EE1260581FE6151C5B2EC36D766E9B17CAC29E60E3EC6D6A19481E7E2D1FE2C52E6E7FD5231A40B97BD0B78BF4F770D5A6A5F8444A3AD90EE9095C511AF2BB42982B4443EACBA24B75F7E096B6B2438686DCE4C5BAAA658A93E1C3BBBD8396F62E515DC5A1B1CEC6A54FEA11139B44C5DB87038EB7CC613F092C27D6BCB4EE653028E9833BD315AB043D4DD8ED18F6E8B88F33BFD54FC48B099D690945F7A87139096709A3A96BDEFD7475FAF6BF4037D9E5BC608704183A68380BE98C7D5956265B6208C1D744E7320FC77787B34E28E9C15D21630F470EBCF08EC96528C6E7227C2786D016487D4FD425A9424518D56D5F3FC13C2728B1B293BB9B50FA6142F46D237D4B799A36E359BEC76B9BE5869F70D6E2F2964261C7B1A449E8FDBF4769063B2724D948F8276D5F2E635DA4BC3E779A380 + + # Hexadecimal inputs Radix = 16 diff --git a/src/tests/test_bigint.cpp b/src/tests/test_bigint.cpp index 184207186c0..492e15ccbd9 100644 --- a/src/tests/test_bigint.cpp +++ b/src/tests/test_bigint.cpp @@ -43,6 +43,7 @@ class BigInt_Unit_Tests final : public Test { results.push_back(test_is_prime_strong()); results.push_back(test_encode()); results.push_back(test_bigint_io()); + results.push_back(test_decimal_round_trip()); results.push_back(test_get_substring()); return results; @@ -233,6 +234,29 @@ class BigInt_Unit_Tests final : public Test { return result; } + static Test::Result test_decimal_round_trip() { + Test::Result result("BigInt decimal round trip"); + + auto rng = Test::new_rng("bigint_decimal_round_trip"); + + const auto check_round_trip = [&](const BigInt& n) { + const std::string dec = n.to_dec_string(); + result.test_bn_eq("decimal round trip", BigInt::from_radix_digits(dec, 10), n); + result.test_str_eq("negative decimal", (-n).to_dec_string(), "-" + dec); + }; + + for(size_t bits = 1; bits <= 1024; ++bits) { + check_round_trip(BigInt(*rng, bits)); + check_round_trip(BigInt::power_of_2(bits) - 1); + } + + for(const size_t bits : {2048, 4096, 10000}) { + check_round_trip(BigInt(*rng, bits)); + } + + return result; + } + static Test::Result test_bigint_io() { Test::Result result("BigInt IO operators"); @@ -1403,6 +1427,14 @@ class BigInt_FromRadix_Tests final : public Text_Based_Test { const Botan::BigInt n = Botan::BigInt::from_radix_digits(input, radix); const auto serialized = n.serialize(); result.test_bin_eq("from_radix_digits", serialized, expected_output); + + if(radix == 10) { + // Decimal formatting does not include leading zeros + const size_t first_nonzero = input.find_first_not_of('0'); + const std::string canonical = (first_nonzero == std::string::npos) ? "0" : input.substr(first_nonzero); + const auto value = Botan::BigInt::from_bytes(vars.get_req_bin("Output")); + result.test_str_eq("to_dec_string", value.to_dec_string(), canonical); + } } else if(header == "Invalid") { try { Botan::BigInt::from_radix_digits(input, radix); diff --git a/src/tests/test_ct_utils.cpp b/src/tests/test_ct_utils.cpp index 14f93d21800..9df8cd910e9 100644 --- a/src/tests/test_ct_utils.cpp +++ b/src/tests/test_ct_utils.cpp @@ -9,7 +9,9 @@ #include #include #include +#include #include +#include namespace Botan_Tests { @@ -46,6 +48,12 @@ class CT_Mask_Tests final : public Test { result.test_u32_eq("CT::is_less32", Botan::CT::Mask::is_lt(0xFFFFFFFF, 5).value(), 0x00000000); result.test_u32_eq("CT::is_less32", Botan::CT::Mask::is_lt(5, 0xFFFFFFFF).value(), 0xFFFFFFFF); + test_mask_comparisons("uint8_t", result); + test_mask_comparisons("uint16_t", result); + test_mask_comparisons("uint32_t", result); + test_mask_comparisons("uint64_t", result); + test_mask_comparisons("size_t", result); + for(const auto bad_input : {0, 1}) { for(size_t input_length = 0; input_length != 64; ++input_length) { for(size_t offset = 0; offset != input_length + 5; ++offset) { @@ -78,6 +86,60 @@ class CT_Mask_Tests final : public Test { return {result}; } + + private: + template + void test_mask_comparisons(const char* type_name, Result& result) { + using Botan::CT::Mask; + + const auto tname = Botan::fmt("CT::Mask<{}> comparisons", type_name); + constexpr size_t tbits = sizeof(T) * 8; + constexpr T all_set = static_cast(~static_cast(0)); + + const auto matches = [&](T x, T y) { + const T lt = (x < y) ? all_set : 0; + const T gt = (x > y) ? all_set : 0; + + return Mask::is_lt(x, y).value() == lt && Mask::is_gt(x, y).value() == gt && + Mask::is_lte(x, y).value() == static_cast(~gt) && + Mask::is_gte(x, y).value() == static_cast(~lt); + }; + + // Values around each power of 2 + std::vector values = {0, all_set}; + for(size_t b = 0; b != tbits; ++b) { + const T pow2 = static_cast(static_cast(1) << b); + values.push_back(static_cast(pow2 - 1)); + values.push_back(pow2); + values.push_back(static_cast(pow2 + 1)); + } + + for(size_t i = 0; i != 256; ++i) { + T v = 0; + for(size_t j = 0; j != sizeof(T); ++j) { + v = static_cast((v << 8) | this->rng().next_byte()); + } + values.push_back(v); + } + + for(const T x : values) { + bool ok = matches(x, static_cast(x - 1)) && matches(x, static_cast(x + 1)); + for(const T y : values) { + ok = ok && matches(x, y); + } + result.test_is_true(tname, ok); + } + + if constexpr(sizeof(T) == 1) { + for(size_t x = 0; x != 256; ++x) { + bool ok = true; + for(size_t y = 0; y != 256; ++y) { + ok = ok && matches(static_cast(x), static_cast(y)); + } + result.test_is_true(tname, ok); + } + } + } }; BOTAN_REGISTER_TEST("ct_utils", "ct_mask", CT_Mask_Tests);