LCOV - code coverage report
Current view: top level - src/jamidht/eth/libdevcrypto - Common.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 25 32 78.1 %
Date: 2024-04-26 09:41:19 Functions: 6 8 75.0 %

          Line data    Source code
       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             : */
      17             : /** @file Common.cpp
      18             :  * @author Alex Leverington <nessence@gmail.com>
      19             :  * @author Gav Wood <i@gavwood.com>
      20             :  * @date 2014
      21             :  */
      22             : 
      23             : #include "Common.h"
      24             : #include <secp256k1.h>
      25             : #include <libdevcore/SHA3.h>
      26             : #include <memory>
      27             : using namespace std;
      28             : using namespace dev;
      29             : using namespace dev::crypto;
      30             : 
      31             : namespace {
      32             : 
      33             : secp256k1_context const*
      34        1131 : getCtx()
      35             : {
      36             :     struct secp256k1Deleter {
      37          22 :         void operator()(secp256k1_context* b) { secp256k1_context_destroy(b); }
      38             :     };
      39             :     static std::unique_ptr<secp256k1_context, secp256k1Deleter>
      40        1131 :         s_ctx(secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
      41        1131 :     return s_ctx.get();
      42             : }
      43             : 
      44             : } // namespace
      45             : 
      46             : bool
      47           0 : dev::SignatureStruct::isValid() const noexcept
      48             : {
      49           0 :     static const h256 s_max {"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"};
      50           0 :     static const h256 s_zero;
      51             : 
      52           0 :     return (v <= 1 && r > s_zero && s > s_zero && r < s_max && s < s_max);
      53             : }
      54             : 
      55             : Public
      56        1131 : dev::toPublic(Secret const& _secret)
      57             : {
      58        1131 :     auto* ctx = getCtx();
      59             :     secp256k1_pubkey rawPubkey;
      60             :     // Creation will fail if the secret key is invalid.
      61        1131 :     if (!secp256k1_ec_pubkey_create(ctx, &rawPubkey, _secret.data()))
      62           0 :         return {};
      63             :     std::array<uint8_t, 65> serializedPubkey;
      64        1131 :     size_t serializedPubkeySize = serializedPubkey.size();
      65        1131 :     secp256k1_ec_pubkey_serialize(ctx,
      66             :                                   serializedPubkey.data(),
      67             :                                   &serializedPubkeySize,
      68             :                                   &rawPubkey,
      69             :                                   SECP256K1_EC_UNCOMPRESSED);
      70        1131 :     assert(serializedPubkeySize == serializedPubkey.size());
      71             :     // Expect single byte header of value 0x04 -- uncompressed public key.
      72        1131 :     assert(serializedPubkey[0] == 0x04);
      73             :     // Create the Public skipping the header.
      74        1131 :     return Public {&serializedPubkey[1], Public::ConstructFromPointer};
      75             : }
      76             : 
      77             : Address
      78        1131 : dev::toAddress(Public const& _public)
      79             : {
      80        1131 :     return right160(sha3(_public.ref()));
      81             : }
      82             : 
      83             : Address
      84           0 : dev::toAddress(Secret const& _secret)
      85             : {
      86           0 :     return toAddress(toPublic(_secret));
      87             : }
      88             : 
      89        1131 : KeyPair::KeyPair(Secret const& _sec)
      90        1131 :     : m_secret(_sec)
      91        1131 :     , m_public(toPublic(_sec))
      92             : {
      93             :     // Assign address only if the secret key is valid.
      94        1131 :     if (m_public)
      95        1131 :         m_address = toAddress(m_public);
      96        1131 : }
      97             : 
      98             : KeyPair
      99         547 : KeyPair::create()
     100             : {
     101             :     while (true) {
     102         547 :         KeyPair keyPair(Secret::random());
     103         547 :         if (keyPair.address())
     104        1094 :             return keyPair;
     105         547 :     }
     106             : }

Generated by: LCOV version 1.14