Ring Daemon
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 {
38 void operator()(secp256k1_context* b) { secp256k1_context_destroy(b); }
39 };
40 static std::unique_ptr<secp256k1_context, secp256k1Deleter> s_ctx(
41 secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
42 return s_ctx.get();
43}
44
45} // namespace
46
47bool
49{
50 static const h256 s_max {"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"};
51 static const h256 s_zero;
52
53 return (v <= 1 && r > s_zero && s > s_zero && r < s_max && s < s_max);
54}
55
57dev::toPublic(Secret const& _secret)
58{
59 auto* ctx = getCtx();
60 secp256k1_pubkey rawPubkey;
61 // Creation will fail if the secret key is invalid.
62 if (!secp256k1_ec_pubkey_create(ctx, &rawPubkey, _secret.data()))
63 return {};
64 std::array<uint8_t, 65> serializedPubkey;
65 size_t serializedPubkeySize = serializedPubkey.size();
66 secp256k1_ec_pubkey_serialize(ctx,
67 serializedPubkey.data(),
68 &serializedPubkeySize,
69 &rawPubkey,
70 SECP256K1_EC_UNCOMPRESSED);
71 assert(serializedPubkeySize == serializedPubkey.size());
72 // Expect single byte header of value 0x04 -- uncompressed public key.
73 assert(serializedPubkey[0] == 0x04);
74 // Create the Public skipping the header.
75 return Public {&serializedPubkey[1], Public::ConstructFromPointer};
76}
77
79dev::toAddress(Public const& _public)
80{
81 return right160(sha3(_public.ref()));
82}
83
85dev::toAddress(Secret const& _secret)
86{
87 return toAddress(toPublic(_secret));
88}
89
91 : m_secret(_sec)
92 , m_public(toPublic(_sec))
93{
94 // Assign address only if the secret key is valid.
95 if (m_public)
96 m_address = toAddress(m_public);
97}
98
101{
102 while (true) {
103 KeyPair keyPair(Secret::random());
104 if (keyPair.address())
105 return keyPair;
106 }
107}
bytesRef ref()
Definition FixedHash.h:204
Simple class that represents a "key pair".
static KeyPair create()
Create a new, randomly generated object.
Definition Common.cpp:100
KeyPair()=default
Address const & address() const
Retrieve the associated address of the public key.
static SecureFixedHash< T > random()
Definition FixedHash.h:441
uint8_t const * data() const
Definition FixedHash.h:439
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:197
Address toAddress(Public const &_public)
Convert a public key to address.
Definition Common.cpp:79
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition Common.cpp:57
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
Definition FixedHash.h:482
bool isValid() const noexcept
Definition Common.cpp:48