@@ -23,9 +23,6 @@ static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch
2323{
2424 return UintToArith256 (uint256 (vch));
2525}
26- // Takes a number written in hex (with most significant digits first).
27- static inline arith_uint256 arith_uint256S (std::string_view str) { return UintToArith256 (uint256S (str)); }
28-
2926const unsigned char R1Array[] =
3027 " \x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2 "
3128 " \x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d " ;
@@ -39,8 +36,6 @@ const unsigned char R2Array[] =
3936 " \x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7 " ;
4037const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char >(R2Array,R2Array+32 ));
4138
42- const char R1LplusR2L[] = " 549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C" ;
43-
4439const unsigned char ZeroArray[] =
4540 " \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 "
4641 " \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 " ;
@@ -97,27 +92,25 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
9792 }
9893 BOOST_CHECK (ZeroL == (OneL << 256 ));
9994
100- // String Constructor and Copy Constructor
101- BOOST_CHECK (arith_uint256S (" 0x" + R1L.ToString ()) == R1L);
102- BOOST_CHECK (arith_uint256S (" 0x" + R2L.ToString ()) == R2L);
103- BOOST_CHECK (arith_uint256S (" 0x" + ZeroL.ToString ()) == ZeroL);
104- BOOST_CHECK (arith_uint256S (" 0x" + OneL.ToString ()) == OneL);
105- BOOST_CHECK (arith_uint256S (" 0x" + MaxL.ToString ()) == MaxL);
106- BOOST_CHECK (arith_uint256S (R1L.ToString ()) == R1L);
107- BOOST_CHECK (arith_uint256S (" 0x" + R1L.ToString () + " " ) == R1L);
108- BOOST_CHECK (arith_uint256S (" " ) == ZeroL);
109- BOOST_CHECK (arith_uint256S (" 1" ) == OneL);
110- BOOST_CHECK (R1L == arith_uint256S (R1ArrayHex));
95+ // Construct from hex string
96+ BOOST_CHECK_EQUAL (UintToArith256 (uint256::FromHex (R1L.ToString ()).value ()), R1L);
97+ BOOST_CHECK_EQUAL (UintToArith256 (uint256::FromHex (R2L.ToString ()).value ()), R2L);
98+ BOOST_CHECK_EQUAL (UintToArith256 (uint256::FromHex (ZeroL.ToString ()).value ()), ZeroL);
99+ BOOST_CHECK_EQUAL (UintToArith256 (uint256::FromHex (OneL.ToString ()).value ()), OneL);
100+ BOOST_CHECK_EQUAL (UintToArith256 (uint256::FromHex (MaxL.ToString ()).value ()), MaxL);
101+ BOOST_CHECK_EQUAL (UintToArith256 (uint256::FromHex (R1ArrayHex).value ()), R1L);
102+
103+ // Copy constructor
111104 BOOST_CHECK (arith_uint256 (R1L) == R1L);
112105 BOOST_CHECK ((arith_uint256 (R1L^R2L)^R2L) == R1L);
113106 BOOST_CHECK (arith_uint256 (ZeroL) == ZeroL);
114107 BOOST_CHECK (arith_uint256 (OneL) == OneL);
115108
116109 // uint64_t constructor
117- BOOST_CHECK (( R1L & arith_uint256S ( " 0xffffffffffffffff" )) == arith_uint256 ( R1LLow64) );
118- BOOST_CHECK (ZeroL == arith_uint256 ( 0 ) );
119- BOOST_CHECK (OneL == arith_uint256 ( 1 ) );
120- BOOST_CHECK ( arith_uint256S ( " 0xffffffffffffffff" ) == arith_uint256 ( 0xffffffffffffffffULL ) );
110+ BOOST_CHECK_EQUAL ( R1L & arith_uint256{ 0xffffffffffffffff }, arith_uint256{ R1LLow64} );
111+ BOOST_CHECK_EQUAL (ZeroL, arith_uint256{ 0 } );
112+ BOOST_CHECK_EQUAL (OneL, arith_uint256{ 1 } );
113+ BOOST_CHECK_EQUAL (arith_uint256{ 0xffffffffffffffff }, arith_uint256{ 0xffffffffffffffffULL } );
121114
122115 // Assignment (from base_uint)
123116 arith_uint256 tmpL = ~ZeroL; BOOST_CHECK (tmpL == ~ZeroL);
@@ -284,15 +277,12 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
284277
285278 BOOST_CHECK_LT (ZeroL,
286279 OneL);
287- // Verify hex number representation has the most significant digits first.
288- BOOST_CHECK_LT (arith_uint256S (" 0000000000000000000000000000000000000000000000000000000000000001" ),
289- arith_uint256S (" 1000000000000000000000000000000000000000000000000000000000000000" ));
290280}
291281
292282BOOST_AUTO_TEST_CASE ( plusMinus )
293283{
294284 arith_uint256 TmpL = 0 ;
295- BOOST_CHECK (R1L + R2L == arith_uint256S (R1LplusR2L ));
285+ BOOST_CHECK_EQUAL (R1L + R2L, UintToArith256 (uint256{ " 549fb09fea236a1ea3e31d4d58f1b1369288d204211ca751527cfc175767850c " } ));
296286 TmpL += R1L;
297287 BOOST_CHECK (TmpL == R1L);
298288 TmpL += R2L;
@@ -356,8 +346,8 @@ BOOST_AUTO_TEST_CASE( multiply )
356346
357347BOOST_AUTO_TEST_CASE ( divide )
358348{
359- arith_uint256 D1L{arith_uint256S ( " AD7133AC1977FA2B7 " )};
360- arith_uint256 D2L{arith_uint256S ( " ECD751716 " )};
349+ arith_uint256 D1L{UintToArith256 (uint256{ " 00000000000000000000000000000000000000000000000ad7133ac1977fa2b7 " } )};
350+ arith_uint256 D2L{UintToArith256 (uint256{ " 0000000000000000000000000000000000000000000000000000000ecd751716 " } )};
361351 BOOST_CHECK ((R1L / D1L).ToString () == " 00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a" );
362352 BOOST_CHECK ((R1L / D2L).ToString () == " 000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a" );
363353 BOOST_CHECK (R1L / OneL == R1L);
@@ -571,4 +561,21 @@ BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% cover
571561 CHECKBITWISEOPERATOR (R1,~R2,&)
572562}
573563
564+ BOOST_AUTO_TEST_CASE (conversion)
565+ {
566+ for (const arith_uint256& arith : {ZeroL, OneL, R1L, R2L}) {
567+ const auto u256{uint256::FromHex (arith.GetHex ()).value ()};
568+ BOOST_CHECK_EQUAL (UintToArith256 (ArithToUint256 (arith)), arith);
569+ BOOST_CHECK_EQUAL (UintToArith256 (u256), arith);
570+ BOOST_CHECK_EQUAL (u256, ArithToUint256 (arith));
571+ BOOST_CHECK_EQUAL (ArithToUint256 (arith).GetHex (), UintToArith256 (u256).GetHex ());
572+ }
573+
574+ for (uint8_t num : {0 , 1 , 0xff }) {
575+ BOOST_CHECK_EQUAL (UintToArith256 (uint256{num}), arith_uint256{num});
576+ BOOST_CHECK_EQUAL (uint256{num}, ArithToUint256 (arith_uint256{num}));
577+ BOOST_CHECK_EQUAL (UintToArith256 (uint256{num}), num);
578+ }
579+ }
580+
574581BOOST_AUTO_TEST_SUITE_END ()
0 commit comments