Ring Daemon 16.0.0
Loading...
Searching...
No Matches
Common.cpp
Go to the documentation of this file.
1/*
2 This file is part of cpp-ethereum.
3
4 cpp-ethereum is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 cpp-ethereum is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16*/
23#include "Common.h"
24#include <secp256k1.h>
25#include <libdevcore/SHA3.h>
26#include <memory>
27using namespace std;
28using namespace dev;
29using namespace dev::crypto;
30
31namespace {
32
33secp256k1_context const*
34getCtx()
35{
36 struct secp256k1Deleter {
37 void operator()(secp256k1_context* b) { secp256k1_context_destroy(b); }
38 };
39 static std::unique_ptr<secp256k1_context, secp256k1Deleter>
40 s_ctx(secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
41 return s_ctx.get();
42}
43
44} // namespace
45
46bool
48{
49 static const h256 s_max {"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"};
50 static const h256 s_zero;
51
52 return (v <= 1 && r > s_zero && s > s_zero && r < s_max && s < s_max);
53}
54
56dev::toPublic(Secret const& _secret)
57{
58 auto* ctx = getCtx();
59 secp256k1_pubkey rawPubkey;
60 // Creation will fail if the secret key is invalid.
61 if (!secp256k1_ec_pubkey_create(ctx, &rawPubkey, _secret.data()))
62 return {};
63 std::array<uint8_t, 65> serializedPubkey;
64 size_t serializedPubkeySize = serializedPubkey.size();
65 secp256k1_ec_pubkey_serialize(ctx,
66 serializedPubkey.data(),
67 &serializedPubkeySize,
68 &rawPubkey,
69 SECP256K1_EC_UNCOMPRESSED);
70 assert(serializedPubkeySize == serializedPubkey.size());
71 // Expect single byte header of value 0x04 -- uncompressed public key.
72 assert(serializedPubkey[0] == 0x04);
73 // Create the Public skipping the header.
74 return Public {&serializedPubkey[1], Public::ConstructFromPointer};
75}
76
78dev::toAddress(Public const& _public)
79{
80 return right160(sha3(_public.ref()));
81}
82
84dev::toAddress(Secret const& _secret)
85{
86 return toAddress(toPublic(_secret));
87}
88
90 : m_secret(_sec)
91 , m_public(toPublic(_sec))
92{
93 // Assign address only if the secret key is valid.
94 if (m_public)
95 m_address = toAddress(m_public);
96}
97
100{
101 while (true) {
102 KeyPair keyPair(Secret::random());
103 if (keyPair.address())
104 return keyPair;
105 }
106}
bytesRef ref()
Definition FixedHash.h:209
Simple class that represents a "key pair".
static KeyPair create()
Create a new, randomly generated object.
Definition Common.cpp:99
KeyPair()=default
Address const & address() const
Retrieve the associated address of the public key.
static SecureFixedHash< T > random()
Definition FixedHash.h:465
uint8_t const * data() const
Definition FixedHash.h:463
Definition Address.h:25
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition SHA3.cpp:196
Address toAddress(Public const &_public)
Convert a public key to address.
Definition Common.cpp:78
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition Common.cpp:56
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
Definition FixedHash.h:507
bool isValid() const noexcept
Definition Common.cpp:47