36TEST_CASE(
"MP Big Int Test 00",
"[big_int_t][arithmetic][math]" ) {
40 BigInt a = 1_u64, b = 2_u64, c = 3_u64, d = 4_u64, e = 256_u64;
41 std::cout <<
"big_int 1:: " << a.
to_dec_string(
true) << std::endl;
42 std::cout <<
"big_int 1:: " << a.
to_hex_string(
true) << std::endl;
43 std::cout <<
"big_int 2:: " << b.to_dec_string(
true) << std::endl;
44 std::cout <<
"big_int 3:: " << c.to_dec_string(
true) << std::endl;
45 std::cout <<
"big_int 256:: " << e.to_dec_string(
true) << std::endl;
46 std::cout <<
"big_int 256:: " << e.to_hex_string(
true) << std::endl;
47 REQUIRE( 1 == a.
bits() );
48 REQUIRE( 1 == a.
bytes() );
49 REQUIRE( 1 == a.
sign() );
52 std::cout <<
"big_int 1+2:: " << r.
to_dec_string(
true) << std::endl;
57 std::cout <<
"big_int 2*2:: " << r.
to_dec_string(
true) << std::endl;
62 std::cout <<
"big_int 2/2:: " << r.
to_dec_string(
true) << std::endl;
66 REQUIRE( 2 == b.bits() );
67 REQUIRE( 1 == b.bytes() );
68 REQUIRE( 1 == b.sign() );
70 REQUIRE( 2 == c.bits() );
71 REQUIRE( 1 == c.bytes() );
72 REQUIRE( 1 == c.sign() );
74 REQUIRE( 9 == e.bits() );
75 REQUIRE( 2 == e.bytes() );
76 REQUIRE( 1 == e.sign() );
79 BigInt a = BigInt::from_s32(-1), b = BigInt::from_s32(-2), c = BigInt::from_s32(-3), d = BigInt::from_s32(-256);
80 std::cout <<
"big_int -1:: " << a.
to_dec_string(
true) << std::endl;
81 std::cout <<
"big_int -2:: " << b.to_dec_string(
true) << std::endl;
82 std::cout <<
"big_int -3:: " << c.to_dec_string(
true) << std::endl;
83 std::cout <<
"big_int -256:: " << d.to_dec_string(
true) << std::endl;
84 REQUIRE( 1 == a.
bits() );
85 REQUIRE( 1 == a.
bytes() );
86 REQUIRE( BigInt::negative == a.
sign() );
88 REQUIRE( 2 == b.bits() );
89 REQUIRE( 1 == b.bytes() );
90 REQUIRE(BigInt::negative == b.sign() );
92 REQUIRE( 2 == c.bits() );
93 REQUIRE( 1 == c.bytes() );
94 REQUIRE(BigInt::negative == c.sign() );
96 REQUIRE( 9 == d.bits() );
97 REQUIRE( 2 == d.bytes() );
98 REQUIRE(BigInt::negative == d.sign() );
101TEST_CASE(
"MP Big Int Test 01",
"[big_int_t][arithmetic][math]" ) {
103 BigInt a = 0xffffffffffffffff_u64, b = 0x12000000ffffffff_u64;
104 std::cout <<
"big_int a:: " << a.
to_dec_string(
true) << std::endl;
105 std::cout <<
"big_int a:: " << a.
to_hex_string(
true) << std::endl;
106 std::cout <<
"big_int b:: " << b.to_dec_string(
true) << std::endl;
107 std::cout <<
"big_int b:: " << b.to_hex_string(
true) << std::endl;
110 std::cout <<
"big_int a*b:: " << ab.
to_dec_string(
true) << std::endl;
111 std::cout <<
"big_int a*b:: " << ab.
to_hex_string(
true) << std::endl;
115 REQUIRE(
zero < ten );
116 REQUIRE(
zero < thirty );
117 REQUIRE( ten < thirty );
119 REQUIRE( ten >
zero );
120 REQUIRE( thirty > ten );
121 REQUIRE( thirty >
zero );
123 REQUIRE( ten <= ten );
124 REQUIRE( ten <= thirty );
126 REQUIRE( thirty >= thirty );
127 REQUIRE( thirty >= ten );
129 REQUIRE( thirty == thirty );
130 REQUIRE( thirty != ten );
133 REQUIRE( ten ==
max(
zero, ten) );
134 REQUIRE( ten ==
clamp(
zero, ten, thirty) );
135 REQUIRE( thirty ==
clamp(forty, ten, thirty) );
138TEST_CASE(
"MP Big Int Test 02",
"[big_int_t][arithmetic][math]" ) {
151TEST_CASE(
"MP Big Int Dec Test 10",
"[big_int_t][inout][math]" ) {
153 uint8_t a_u8[] = { 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe,
154 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
155 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
158 BigInt a(a_u8,
sizeof(a_u8), lb_endian_t::little);
159 std::cout <<
"big_int zero:: " << a.
to_dec_string(
true) << std::endl;
160 std::cout <<
"big_int zero:: " << a.
to_hex_string(
true) << std::endl;
161 REQUIRE( 23 ==
sizeof(a_u8) );
163 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
166 std::cout <<
"zero.buf[" <<
std::to_string(i) <<
"]: 0x" << s1 << std::endl;
169 REQUIRE(
sizeof(a_u8)*8 == a.
bits() );
170 REQUIRE(
sizeof(a_u8) == a.
bytes() );
172 uint8_t buf[
sizeof(a_u8)];
174 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
179 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
182 std::cout <<
"le.buf[" <<
std::to_string(i) <<
"]: 0x" << s1 << std::endl;
185 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
186 REQUIRE( (uint32_t)a_u8[i] == (uint32_t)buf[i] );
189 std::cout <<
"big_int le:: " << b.
to_dec_string(
true) << std::endl;
190 std::cout <<
"big_int le:: " << b.
to_hex_string(
true) << std::endl;
194 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
199 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
202 std::cout <<
"be.buf[" <<
std::to_string(i) <<
"]: 0x" << s1 << std::endl;
205 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
206 REQUIRE( (uint32_t)a_u8[
sizeof(a_u8)-i-1] == (uint32_t)buf[i] );
209 std::cout <<
"big_int be:: " << b.
to_dec_string(
true) << std::endl;
210 std::cout <<
"big_int be:: " << b.
to_hex_string(
true) << std::endl;
215TEST_CASE(
"MP Big Int Dec Test 11",
"[big_int_t][inout][math]" ) {
217 uint8_t a_u8[] = { 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe,
218 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
219 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
222 BigInt a(
"0xffeeddccbbaa998877665544332211fedcba9876543210");
223 std::cout <<
"big_int zero:: " << a.
to_dec_string(
true) << std::endl;
224 std::cout <<
"big_int zero:: " << a.
to_hex_string(
true) << std::endl;
225 REQUIRE( 23 ==
sizeof(a_u8) );
226 REQUIRE(
sizeof(a_u8)*8 == a.
bits() );
227 REQUIRE(
sizeof(a_u8) == a.
bytes() );
229 uint8_t buf[
sizeof(a_u8)];
231 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
235 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
236 REQUIRE( (uint32_t)a_u8[i] == (uint32_t)buf[i] );
239 std::cout <<
"big_int le:: " << b.
to_dec_string(
true) << std::endl;
240 std::cout <<
"big_int le:: " << b.
to_hex_string(
true) << std::endl;
244 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
248 for(
size_t i=0; i<
sizeof(a_u8); ++i) {
249 REQUIRE( (uint32_t)a_u8[
sizeof(a_u8)-i-1] == (uint32_t)buf[i] );
252 std::cout <<
"big_int be:: " << b.
to_dec_string(
true) << std::endl;
253 std::cout <<
"big_int be:: " << b.
to_hex_string(
true) << std::endl;
258TEST_CASE(
"MP Big Int Error Handling Test 88",
"[big_int_t][error][arithmetic][math]" ) {
265 BigInt a = BigInt::from_s32(-1), b = BigInt::from_s32(-1), r;
math_error_t::div_by_zero, i.e.
Arbitrary precision integer type.
size_t bits() const noexcept
Returns bit length of this integer.
size_t bytes() const
Returns byte length of this integer.
std::string to_dec_string(bool add_details=false) const noexcept
std::string to_hex_string(bool add_details=false) const noexcept
sign_t sign() const noexcept
Return the sign of the integer.
size_t binary_encode(uint8_t output[], size_t byte_len, const lb_endian_t byte_order) const noexcept
Stores this number to the value in buf with given byte_len, considering the given byte_order.
uint8_t byte_at(size_t n) const noexcept
@ little
Identifier for little endian, equivalent to endian::little.
@ big
Identifier for big endian, equivalent to endian::big.
std::string to_string(const alphabet &v) noexcept
constexpr T clamp(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1)...
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
mp::BigInt pow(mp::BigInt b, mp::BigInt e)
std::string & byteHexString(std::string &dest, const uint8_t value, const bool lowerCase) noexcept
Produce a hexadecimal string representation of the given byte value.
constexpr const jau::fraction_i64 zero(0l, 1lu)
zero is 0/1
big_int_t (this file) (c) 2024 Gothel Software e.K.
constexpr const size_t mp_word_bits
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
TEST_CASE("MP Big Int Test 00", "[big_int_t][arithmetic][math]")