mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
Format first-party source according to .clang-format
This commit is contained in:
committed by
manojsdoshi
parent
65dfc5d19e
commit
50760c6935
@@ -17,12 +17,12 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <ripple/beast/utility/rngfill.h>
|
||||
#include <ripple/crypto/csprng.h>
|
||||
#include <ripple/protocol/PublicKey.h>
|
||||
#include <ripple/protocol/SecretKey.h>
|
||||
#include <ripple/protocol/Seed.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <ripple/beast/utility/rngfill.h>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -37,73 +37,69 @@ public:
|
||||
using blob = std::vector<std::uint8_t>;
|
||||
|
||||
template <class FwdIter, class Container>
|
||||
static
|
||||
void
|
||||
hex_to_binary (FwdIter first, FwdIter last, Container& out)
|
||||
static void
|
||||
hex_to_binary(FwdIter first, FwdIter last, Container& out)
|
||||
{
|
||||
struct Table
|
||||
{
|
||||
int val[256];
|
||||
Table ()
|
||||
Table()
|
||||
{
|
||||
std::fill (val, val+256, 0);
|
||||
std::fill(val, val + 256, 0);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
val ['0'+i] = i;
|
||||
val['0' + i] = i;
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
val ['A'+i] = 10 + i;
|
||||
val ['a'+i] = 10 + i;
|
||||
val['A' + i] = 10 + i;
|
||||
val['a' + i] = 10 + i;
|
||||
}
|
||||
}
|
||||
int operator[] (int i)
|
||||
int
|
||||
operator[](int i)
|
||||
{
|
||||
return val[i];
|
||||
return val[i];
|
||||
}
|
||||
};
|
||||
|
||||
static Table lut;
|
||||
out.reserve (std::distance (first, last) / 2);
|
||||
out.reserve(std::distance(first, last) / 2);
|
||||
while (first != last)
|
||||
{
|
||||
auto const hi (lut[(*first++)]);
|
||||
auto const lo (lut[(*first++)]);
|
||||
out.push_back ((hi*16)+lo);
|
||||
auto const hi(lut[(*first++)]);
|
||||
auto const lo(lut[(*first++)]);
|
||||
out.push_back((hi * 16) + lo);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
uint256
|
||||
static uint256
|
||||
hex_to_digest(std::string const& s)
|
||||
{
|
||||
blob b;
|
||||
hex_to_binary (s.begin (), s.end (), b);
|
||||
hex_to_binary(s.begin(), s.end(), b);
|
||||
return uint256{b};
|
||||
}
|
||||
|
||||
static
|
||||
PublicKey
|
||||
static PublicKey
|
||||
hex_to_pk(std::string const& s)
|
||||
{
|
||||
blob b;
|
||||
hex_to_binary (s.begin (), s.end (), b);
|
||||
hex_to_binary(s.begin(), s.end(), b);
|
||||
return PublicKey{Slice{b.data(), b.size()}};
|
||||
}
|
||||
|
||||
static
|
||||
SecretKey
|
||||
static SecretKey
|
||||
hex_to_sk(std::string const& s)
|
||||
{
|
||||
blob b;
|
||||
hex_to_binary (s.begin (), s.end (), b);
|
||||
hex_to_binary(s.begin(), s.end(), b);
|
||||
return SecretKey{Slice{b.data(), b.size()}};
|
||||
}
|
||||
|
||||
static
|
||||
Buffer
|
||||
static Buffer
|
||||
hex_to_sig(std::string const& s)
|
||||
{
|
||||
blob b;
|
||||
hex_to_binary (s.begin (), s.end (), b);
|
||||
hex_to_binary(s.begin(), s.end(), b);
|
||||
return Buffer{Slice{b.data(), b.size()}};
|
||||
}
|
||||
|
||||
@@ -181,16 +177,26 @@ public:
|
||||
void
|
||||
testCanonicality()
|
||||
{
|
||||
testcase ("secp256k1 canonicality");
|
||||
testcase("secp256k1 canonicality");
|
||||
|
||||
#if 0
|
||||
makeCanonicalityTestVectors();
|
||||
#else
|
||||
auto const digest = hex_to_digest("34C19028C80D21F3F48C9354895F8D5BF0D5EE7FF457647CF655F5530A3022A7");
|
||||
auto const pk = hex_to_pk("025096EB12D3E924234E7162369C11D8BF877EDA238778E7A31FF0AAC5D0DBCF37");
|
||||
auto const sk = hex_to_sk("AA921417E7E5C299DA4EEC16D1CAA92F19B19F2A68511F68EC73BBB2F5236F3D");
|
||||
auto const sig = hex_to_sig("3045022100B49D07F0E934BA468C0EFC78117791408D1FB8B63A6492AD395AC2F360F246600220508739DB0A2EF81676E39F459C8BBB07A09C3E9F9BEB696294D524D479D62740");
|
||||
auto const non = hex_to_sig("3046022100B49D07F0E934BA468C0EFC78117791408D1FB8B63A6492AD395AC2F360F24660022100AF78C624F5D107E9891C60BA637444F71A129E47135D36D92AFD39B856601A01");
|
||||
auto const digest = hex_to_digest(
|
||||
"34C19028C80D21F3F48C9354895F8D5BF0D5EE7FF457647CF655F5530A3022A7");
|
||||
auto const pk = hex_to_pk(
|
||||
"025096EB12D3E924234E7162369C11D8BF877EDA238778E7A31FF0AAC5D0DBCF3"
|
||||
"7");
|
||||
auto const sk = hex_to_sk(
|
||||
"AA921417E7E5C299DA4EEC16D1CAA92F19B19F2A68511F68EC73BBB2F5236F3D");
|
||||
auto const sig = hex_to_sig(
|
||||
"3045022100B49D07F0E934BA468C0EFC78117791408D1FB8B63A6492AD395AC2F3"
|
||||
"60F246600220508739DB0A2EF81676E39F459C8BBB07A09C3E9F9BEB696294D524"
|
||||
"D479D62740");
|
||||
auto const non = hex_to_sig(
|
||||
"3046022100B49D07F0E934BA468C0EFC78117791408D1FB8B63A6492AD395AC2F3"
|
||||
"60F24660022100AF78C624F5D107E9891C60BA637444F71A129E47135D36D92AFD"
|
||||
"39B856601A01");
|
||||
|
||||
{
|
||||
auto const canonicality = ecdsaCanonicality(sig);
|
||||
@@ -207,118 +213,103 @@ public:
|
||||
BEAST_EXPECT(verifyDigest(pk, digest, sig, false));
|
||||
BEAST_EXPECT(verifyDigest(pk, digest, sig, true));
|
||||
BEAST_EXPECT(verifyDigest(pk, digest, non, false));
|
||||
BEAST_EXPECT(! verifyDigest(pk, digest, non, true));
|
||||
BEAST_EXPECT(!verifyDigest(pk, digest, non, true));
|
||||
#endif
|
||||
}
|
||||
|
||||
void testDigestSigning()
|
||||
void
|
||||
testDigestSigning()
|
||||
{
|
||||
testcase ("secp256k1 digest");
|
||||
testcase("secp256k1 digest");
|
||||
|
||||
for (std::size_t i = 0; i < 32; i++)
|
||||
{
|
||||
auto const [pk, sk] = randomKeyPair (KeyType::secp256k1);
|
||||
auto const [pk, sk] = randomKeyPair(KeyType::secp256k1);
|
||||
|
||||
BEAST_EXPECT(pk == derivePublicKey (KeyType::secp256k1, sk));
|
||||
BEAST_EXPECT(*publicKeyType (pk) == KeyType::secp256k1);
|
||||
BEAST_EXPECT(pk == derivePublicKey(KeyType::secp256k1, sk));
|
||||
BEAST_EXPECT(*publicKeyType(pk) == KeyType::secp256k1);
|
||||
|
||||
for (std::size_t j = 0; j < 32; j++)
|
||||
{
|
||||
uint256 digest;
|
||||
beast::rngfill (
|
||||
digest.data(),
|
||||
digest.size(),
|
||||
crypto_prng());
|
||||
beast::rngfill(digest.data(), digest.size(), crypto_prng());
|
||||
|
||||
auto sig = signDigest (
|
||||
pk, sk, digest);
|
||||
auto sig = signDigest(pk, sk, digest);
|
||||
|
||||
BEAST_EXPECT(sig.size() != 0);
|
||||
BEAST_EXPECT(verifyDigest (pk,
|
||||
digest, sig, true));
|
||||
BEAST_EXPECT(verifyDigest(pk, digest, sig, true));
|
||||
|
||||
// Wrong digest:
|
||||
BEAST_EXPECT(!verifyDigest (pk,
|
||||
~digest, sig, true));
|
||||
BEAST_EXPECT(!verifyDigest(pk, ~digest, sig, true));
|
||||
|
||||
// Slightly change the signature:
|
||||
if (auto ptr = sig.data())
|
||||
ptr[j % sig.size()]++;
|
||||
|
||||
// Wrong signature:
|
||||
BEAST_EXPECT(!verifyDigest (pk,
|
||||
digest, sig, true));
|
||||
BEAST_EXPECT(!verifyDigest(pk, digest, sig, true));
|
||||
|
||||
// Wrong digest and signature:
|
||||
BEAST_EXPECT(!verifyDigest (pk,
|
||||
~digest, sig, true));
|
||||
BEAST_EXPECT(!verifyDigest(pk, ~digest, sig, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testSigning (KeyType type)
|
||||
void
|
||||
testSigning(KeyType type)
|
||||
{
|
||||
for (std::size_t i = 0; i < 32; i++)
|
||||
{
|
||||
auto const [pk, sk] = randomKeyPair (type);
|
||||
auto const [pk, sk] = randomKeyPair(type);
|
||||
|
||||
BEAST_EXPECT(pk == derivePublicKey (type, sk));
|
||||
BEAST_EXPECT(*publicKeyType (pk) == type);
|
||||
BEAST_EXPECT(pk == derivePublicKey(type, sk));
|
||||
BEAST_EXPECT(*publicKeyType(pk) == type);
|
||||
|
||||
for (std::size_t j = 0; j < 32; j++)
|
||||
{
|
||||
std::vector<std::uint8_t> data (64 + (8 * i) + j);
|
||||
beast::rngfill (
|
||||
data.data(),
|
||||
data.size(),
|
||||
crypto_prng());
|
||||
std::vector<std::uint8_t> data(64 + (8 * i) + j);
|
||||
beast::rngfill(data.data(), data.size(), crypto_prng());
|
||||
|
||||
auto sig = sign (
|
||||
pk, sk,
|
||||
makeSlice (data));
|
||||
auto sig = sign(pk, sk, makeSlice(data));
|
||||
|
||||
BEAST_EXPECT(sig.size() != 0);
|
||||
BEAST_EXPECT(verify(pk,
|
||||
makeSlice(data), sig, true));
|
||||
BEAST_EXPECT(verify(pk, makeSlice(data), sig, true));
|
||||
|
||||
// Construct wrong data:
|
||||
auto badData = data;
|
||||
|
||||
// swaps the smallest and largest elements in buffer
|
||||
std::iter_swap (
|
||||
std::min_element (badData.begin(), badData.end()),
|
||||
std::max_element (badData.begin(), badData.end()));
|
||||
std::iter_swap(
|
||||
std::min_element(badData.begin(), badData.end()),
|
||||
std::max_element(badData.begin(), badData.end()));
|
||||
|
||||
// Wrong data: should fail
|
||||
BEAST_EXPECT(!verify (pk,
|
||||
makeSlice(badData), sig, true));
|
||||
BEAST_EXPECT(!verify(pk, makeSlice(badData), sig, true));
|
||||
|
||||
// Slightly change the signature:
|
||||
if (auto ptr = sig.data())
|
||||
ptr[j % sig.size()]++;
|
||||
|
||||
// Wrong signature: should fail
|
||||
BEAST_EXPECT(!verify (pk,
|
||||
makeSlice(data), sig, true));
|
||||
BEAST_EXPECT(!verify(pk, makeSlice(data), sig, true));
|
||||
|
||||
// Wrong data and signature: should fail
|
||||
BEAST_EXPECT(!verify (pk,
|
||||
makeSlice(badData), sig, true));
|
||||
BEAST_EXPECT(!verify(pk, makeSlice(badData), sig, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testBase58 ()
|
||||
void
|
||||
testBase58()
|
||||
{
|
||||
testcase ("Base58");
|
||||
testcase("Base58");
|
||||
|
||||
// Ensure that parsing some well-known secret keys works
|
||||
{
|
||||
auto const sk1 = generateSecretKey (
|
||||
KeyType::secp256k1,
|
||||
generateSeed ("masterpassphrase"));
|
||||
auto const sk1 = generateSecretKey(
|
||||
KeyType::secp256k1, generateSeed("masterpassphrase"));
|
||||
|
||||
auto const sk2 = parseBase58<SecretKey> (
|
||||
auto const sk2 = parseBase58<SecretKey>(
|
||||
TokenType::NodePrivate,
|
||||
"pnen77YEeUd4fFKG7iycBWcwKpTaeFRkW2WFostaATy1DSupwXe");
|
||||
BEAST_EXPECT(sk2);
|
||||
@@ -327,11 +318,10 @@ public:
|
||||
}
|
||||
|
||||
{
|
||||
auto const sk1 = generateSecretKey (
|
||||
KeyType::ed25519,
|
||||
generateSeed ("masterpassphrase"));
|
||||
auto const sk1 = generateSecretKey(
|
||||
KeyType::ed25519, generateSeed("masterpassphrase"));
|
||||
|
||||
auto const sk2 = parseBase58<SecretKey> (
|
||||
auto const sk2 = parseBase58<SecretKey>(
|
||||
TokenType::NodePrivate,
|
||||
"paKv46LztLqK3GaKz1rG2nQGN6M4JLyRtxFBYFTw4wAVHtGys36");
|
||||
BEAST_EXPECT(sk2);
|
||||
@@ -340,13 +330,12 @@ public:
|
||||
}
|
||||
|
||||
// Try converting short, long and malformed data
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, ""));
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, " "));
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, "!35gty9mhju8nfjl"));
|
||||
BEAST_EXPECT(!parseBase58<SecretKey>(TokenType::NodePrivate, ""));
|
||||
BEAST_EXPECT(!parseBase58<SecretKey>(TokenType::NodePrivate, " "));
|
||||
BEAST_EXPECT(!parseBase58<SecretKey>(
|
||||
TokenType::NodePrivate, "!35gty9mhju8nfjl"));
|
||||
|
||||
auto const good = toBase58 (
|
||||
TokenType::NodePrivate,
|
||||
randomSecretKey());
|
||||
auto const good = toBase58(TokenType::NodePrivate, randomSecretKey());
|
||||
|
||||
// Short (non-empty) strings
|
||||
{
|
||||
@@ -357,8 +346,9 @@ public:
|
||||
|
||||
while (!s.empty())
|
||||
{
|
||||
s.erase (r(s) % s.size(), 1);
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, s));
|
||||
s.erase(r(s) % s.size(), 1);
|
||||
BEAST_EXPECT(
|
||||
!parseBase58<SecretKey>(TokenType::NodePrivate, s));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,18 +356,19 @@ public:
|
||||
for (std::size_t i = 1; i != 16; i++)
|
||||
{
|
||||
auto s = good;
|
||||
s.resize (s.size() + i, s[i % s.size()]);
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, s));
|
||||
s.resize(s.size() + i, s[i % s.size()]);
|
||||
BEAST_EXPECT(!parseBase58<SecretKey>(TokenType::NodePrivate, s));
|
||||
}
|
||||
|
||||
// Strings with invalid Base58 characters
|
||||
for (auto c : std::string ("0IOl"))
|
||||
for (auto c : std::string("0IOl"))
|
||||
{
|
||||
for (std::size_t i = 0; i != good.size(); ++i)
|
||||
{
|
||||
auto s = good;
|
||||
s[i % s.size()] = c;
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, s));
|
||||
BEAST_EXPECT(
|
||||
!parseBase58<SecretKey>(TokenType::NodePrivate, s));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,39 +379,35 @@ public:
|
||||
for (auto c : std::string("ansrJqtv7"))
|
||||
{
|
||||
s[0] = c;
|
||||
BEAST_EXPECT(!parseBase58<SecretKey> (TokenType::NodePrivate, s));
|
||||
BEAST_EXPECT(
|
||||
!parseBase58<SecretKey>(TokenType::NodePrivate, s));
|
||||
}
|
||||
}
|
||||
|
||||
// Try some random secret keys
|
||||
std::array <SecretKey, 32> keys;
|
||||
std::array<SecretKey, 32> keys;
|
||||
|
||||
for (std::size_t i = 0; i != keys.size(); ++i)
|
||||
keys[i] = randomSecretKey();
|
||||
|
||||
for (std::size_t i = 0; i != keys.size(); ++i)
|
||||
{
|
||||
auto const si = toBase58 (
|
||||
TokenType::NodePrivate,
|
||||
keys[i]);
|
||||
auto const si = toBase58(TokenType::NodePrivate, keys[i]);
|
||||
BEAST_EXPECT(!si.empty());
|
||||
|
||||
auto const ski = parseBase58<SecretKey> (
|
||||
TokenType::NodePrivate, si);
|
||||
auto const ski = parseBase58<SecretKey>(TokenType::NodePrivate, si);
|
||||
BEAST_EXPECT(ski && keys[i] == *ski);
|
||||
|
||||
for (std::size_t j = i; j != keys.size(); ++j)
|
||||
{
|
||||
BEAST_EXPECT((keys[i] == keys[j]) == (i == j));
|
||||
|
||||
auto const sj = toBase58 (
|
||||
TokenType::NodePrivate,
|
||||
keys[j]);
|
||||
auto const sj = toBase58(TokenType::NodePrivate, keys[j]);
|
||||
|
||||
BEAST_EXPECT((si == sj) == (i == j));
|
||||
|
||||
auto const skj = parseBase58<SecretKey> (
|
||||
TokenType::NodePrivate, sj);
|
||||
auto const skj =
|
||||
parseBase58<SecretKey>(TokenType::NodePrivate, sj);
|
||||
BEAST_EXPECT(skj && keys[j] == *skj);
|
||||
|
||||
BEAST_EXPECT((*ski == *skj) == (i == j));
|
||||
@@ -428,15 +415,15 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void testMiscOperations ()
|
||||
void
|
||||
testMiscOperations()
|
||||
{
|
||||
testcase ("Miscellaneous operations");
|
||||
testcase("Miscellaneous operations");
|
||||
|
||||
auto const sk1 = generateSecretKey (
|
||||
KeyType::secp256k1,
|
||||
generateSeed ("masterpassphrase"));
|
||||
auto const sk1 = generateSecretKey(
|
||||
KeyType::secp256k1, generateSeed("masterpassphrase"));
|
||||
|
||||
SecretKey sk2 (sk1);
|
||||
SecretKey sk2(sk1);
|
||||
BEAST_EXPECT(sk1 == sk2);
|
||||
|
||||
SecretKey sk3;
|
||||
@@ -445,21 +432,22 @@ public:
|
||||
BEAST_EXPECT(sk3 == sk2);
|
||||
}
|
||||
|
||||
void run() override
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testBase58();
|
||||
testDigestSigning();
|
||||
testMiscOperations();
|
||||
testCanonicality();
|
||||
|
||||
testcase ("secp256k1");
|
||||
testcase("secp256k1");
|
||||
testSigning(KeyType::secp256k1);
|
||||
|
||||
testcase ("ed25519");
|
||||
testcase("ed25519");
|
||||
testSigning(KeyType::ed25519);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(SecretKey,protocol,ripple);
|
||||
BEAST_DEFINE_TESTSUITE(SecretKey, protocol, ripple);
|
||||
|
||||
} // ripple
|
||||
} // namespace ripple
|
||||
|
||||
Reference in New Issue
Block a user