LCOV - code coverage report
Current view: top level - src/test/fuzz - key.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 245 245 100.0 %
Date: 2023-10-05 15:40:34 Functions: 15 15 100.0 %
Branches: 347 678 51.2 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2020-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <chainparams.h>
       6                 :            : #include <key.h>
       7                 :            : #include <key_io.h>
       8                 :            : #include <outputtype.h>
       9                 :            : #include <policy/policy.h>
      10                 :            : #include <pubkey.h>
      11                 :            : #include <rpc/util.h>
      12                 :            : #include <script/keyorigin.h>
      13                 :            : #include <script/script.h>
      14                 :            : #include <script/sign.h>
      15                 :            : #include <script/signingprovider.h>
      16                 :            : #include <script/solver.h>
      17         [ +  - ]:        173 : #include <streams.h>
      18         [ +  - ]:        173 : #include <test/fuzz/FuzzedDataProvider.h>
      19                 :            : #include <test/fuzz/fuzz.h>
      20                 :            : #include <test/fuzz/util.h>
      21                 :            : #include <util/chaintype.h>
      22                 :            : #include <util/strencodings.h>
      23                 :            : 
      24                 :            : #include <array>
      25                 :            : #include <cassert>
      26                 :            : #include <cstddef>
      27                 :        173 : #include <cstdint>
      28                 :            : #include <numeric>
      29                 :            : #include <optional>
      30                 :            : #include <string>
      31                 :            : #include <vector>
      32                 :            : 
      33                 :          3 : void initialize_key()
      34                 :            : {
      35                 :          3 :     ECC_Start();
      36                 :          3 :     SelectParams(ChainType::REGTEST);
      37                 :          3 : }
      38                 :            : 
      39   [ +  -  -  + ]:        392 : FUZZ_TARGET(key, .init = initialize_key)
      40                 :            : {
      41                 :         92 :     const CKey key = [&] {
      42                 :         46 :         CKey k;
      43         [ +  - ]:         46 :         k.Set(buffer.begin(), buffer.end(), true);
      44                 :         46 :         return k;
      45         [ +  - ]:         46 :     }();
      46   [ +  -  +  + ]:         46 :     if (!key.IsValid()) {
      47                 :          2 :         return;
      48                 :            :     }
      49                 :            : 
      50                 :            :     {
      51   [ +  -  +  -  :         44 :         assert(key.begin() + key.size() == key.end());
             +  -  +  - ]
      52   [ +  -  +  - ]:         44 :         assert(key.IsCompressed());
      53   [ +  -  +  - ]:         44 :         assert(key.size() == 32);
      54   [ +  -  +  -  :         44 :         assert(DecodeSecret(EncodeSecret(key)) == key);
             +  -  +  - ]
      55                 :            :     }
      56                 :            : 
      57                 :            :     {
      58                 :         44 :         CKey invalid_key;
      59   [ +  -  +  - ]:         44 :         assert(!(invalid_key == key));
      60   [ +  -  +  - ]:         44 :         assert(!invalid_key.IsCompressed());
      61   [ +  -  +  - ]:         44 :         assert(!invalid_key.IsValid());
      62   [ +  -  +  - ]:         44 :         assert(invalid_key.size() == 0);
      63                 :         44 :     }
      64                 :            : 
      65                 :            :     {
      66                 :         44 :         CKey uncompressed_key;
      67         [ +  - ]:         44 :         uncompressed_key.Set(buffer.begin(), buffer.end(), false);
      68   [ +  -  +  - ]:         44 :         assert(!(uncompressed_key == key));
      69   [ +  -  +  - ]:         44 :         assert(!uncompressed_key.IsCompressed());
      70   [ +  -  +  - ]:         44 :         assert(key.size() == 32);
      71   [ +  -  +  -  :         44 :         assert(uncompressed_key.begin() + uncompressed_key.size() == uncompressed_key.end());
             +  -  +  - ]
      72   [ +  -  +  - ]:         44 :         assert(uncompressed_key.IsValid());
      73                 :         44 :     }
      74                 :        173 : 
      75                 :            :     {
      76                 :         44 :         CKey copied_key;
      77   [ +  -  +  -  :         44 :         copied_key.Set(key.begin(), key.end(), key.IsCompressed());
             +  -  +  - ]
      78   [ +  -  +  - ]:         44 :         assert(copied_key == key);
      79                 :         44 :     }
      80                 :            : 
      81                 :            :     {
      82         [ +  - ]:         44 :         CKey negated_key = key;
      83         [ +  - ]:         44 :         negated_key.Negate();
      84   [ +  -  +  - ]:         44 :         assert(negated_key.IsValid());
      85   [ +  -  +  - ]:         44 :         assert(!(negated_key == key));
      86                 :            : 
      87         [ +  - ]:         44 :         negated_key.Negate();
      88   [ +  -  +  - ]:         44 :         assert(negated_key == key);
      89                 :         44 :     }
      90                 :            : 
      91         [ +  - ]:         44 :     const uint256 random_uint256 = Hash(buffer);
      92                 :            : 
      93                 :            :     {
      94                 :         44 :         CKey child_key;
      95         [ +  - ]:         44 :         ChainCode child_chaincode;
      96         [ +  - ]:         44 :         const bool ok = key.Derive(child_key, child_chaincode, 0, random_uint256);
      97         [ +  - ]:         44 :         assert(ok);
      98   [ +  -  +  - ]:         44 :         assert(child_key.IsValid());
      99   [ +  -  +  - ]:         44 :         assert(!(child_key == key));
     100   [ +  -  +  - ]:         44 :         assert(child_chaincode != random_uint256);
     101                 :         44 :     }
     102                 :            : 
     103         [ +  - ]:         44 :     const CPubKey pubkey = key.GetPubKey();
     104                 :            : 
     105                 :            :     {
     106   [ +  -  +  - ]:         44 :         assert(pubkey.size() == 33);
     107   [ +  -  +  - ]:         44 :         assert(key.VerifyPubKey(pubkey));
     108   [ +  -  +  -  :         44 :         assert(pubkey.GetHash() != random_uint256);
                   +  - ]
     109   [ +  -  +  -  :         44 :         assert(pubkey.begin() + pubkey.size() == pubkey.end());
             +  -  +  - ]
     110   [ +  -  +  -  :         44 :         assert(pubkey.data() == pubkey.begin());
                   +  - ]
     111   [ +  -  +  - ]:         44 :         assert(pubkey.IsCompressed());
     112   [ +  -  +  - ]:         44 :         assert(pubkey.IsValid());
     113   [ +  -  +  - ]:         44 :         assert(pubkey.IsFullyValid());
     114   [ +  -  +  -  :         44 :         assert(HexToPubKey(HexStr(pubkey)) == pubkey);
          +  -  +  -  +  
                      - ]
     115   [ +  -  +  - ]:         44 :         assert(GetAllDestinationsForKey(pubkey).size() == 3);
     116                 :            :     }
     117                 :            : 
     118                 :            :     {
     119         [ +  - ]:         44 :         DataStream data_stream{};
     120         [ +  - ]:         44 :         pubkey.Serialize(data_stream);
     121                 :            : 
     122         [ +  - ]:         44 :         CPubKey pubkey_deserialized;
     123         [ +  - ]:         44 :         pubkey_deserialized.Unserialize(data_stream);
     124   [ +  -  +  - ]:         44 :         assert(pubkey_deserialized == pubkey);
     125                 :         44 :     }
     126                 :            : 
     127                 :            :     {
     128         [ +  - ]:         44 :         const CScript tx_pubkey_script = GetScriptForRawPubKey(pubkey);
     129   [ +  -  +  - ]:         44 :         assert(!tx_pubkey_script.IsPayToScriptHash());
     130   [ +  -  +  - ]:         44 :         assert(!tx_pubkey_script.IsPayToWitnessScriptHash());
     131   [ +  -  +  - ]:         44 :         assert(!tx_pubkey_script.IsPushOnly());
     132   [ +  -  +  - ]:         44 :         assert(!tx_pubkey_script.IsUnspendable());
     133   [ +  -  +  - ]:         44 :         assert(tx_pubkey_script.HasValidOps());
     134   [ +  -  +  - ]:         44 :         assert(tx_pubkey_script.size() == 35);
     135                 :            : 
     136   [ +  -  +  - ]:         44 :         const CScript tx_multisig_script = GetScriptForMultisig(1, {pubkey});
     137   [ +  -  +  - ]:         44 :         assert(!tx_multisig_script.IsPayToScriptHash());
     138   [ +  -  +  - ]:         44 :         assert(!tx_multisig_script.IsPayToWitnessScriptHash());
     139   [ +  -  +  - ]:         44 :         assert(!tx_multisig_script.IsPushOnly());
     140   [ +  -  +  - ]:         44 :         assert(!tx_multisig_script.IsUnspendable());
     141   [ +  -  +  - ]:         44 :         assert(tx_multisig_script.HasValidOps());
     142   [ +  -  +  - ]:         44 :         assert(tx_multisig_script.size() == 37);
     143                 :            : 
     144                 :         44 :         FillableSigningProvider fillable_signing_provider;
     145   [ +  -  +  - ]:         44 :         assert(!IsSegWitOutput(fillable_signing_provider, tx_pubkey_script));
     146   [ +  -  +  - ]:         44 :         assert(!IsSegWitOutput(fillable_signing_provider, tx_multisig_script));
     147   [ +  -  +  - ]:         44 :         assert(fillable_signing_provider.GetKeys().size() == 0);
     148   [ +  -  +  -  :         44 :         assert(!fillable_signing_provider.HaveKey(pubkey.GetID()));
                   +  - ]
     149                 :            : 
     150         [ +  - ]:         44 :         const bool ok_add_key = fillable_signing_provider.AddKey(key);
     151         [ +  - ]:         44 :         assert(ok_add_key);
     152   [ +  -  +  -  :         44 :         assert(fillable_signing_provider.HaveKey(pubkey.GetID()));
                   +  - ]
     153                 :            : 
     154                 :         44 :         FillableSigningProvider fillable_signing_provider_pub;
     155   [ +  -  +  -  :         44 :         assert(!fillable_signing_provider_pub.HaveKey(pubkey.GetID()));
                   +  - ]
     156                 :            : 
     157         [ +  - ]:         44 :         const bool ok_add_key_pubkey = fillable_signing_provider_pub.AddKeyPubKey(key, pubkey);
     158         [ +  - ]:         44 :         assert(ok_add_key_pubkey);
     159   [ +  -  +  -  :         44 :         assert(fillable_signing_provider_pub.HaveKey(pubkey.GetID()));
                   +  - ]
     160                 :            : 
     161                 :            :         TxoutType which_type_tx_pubkey;
     162         [ +  - ]:         44 :         const bool is_standard_tx_pubkey = IsStandard(tx_pubkey_script, std::nullopt, which_type_tx_pubkey);
     163         [ +  - ]:         44 :         assert(is_standard_tx_pubkey);
     164         [ +  - ]:         44 :         assert(which_type_tx_pubkey == TxoutType::PUBKEY);
     165                 :            : 
     166                 :            :         TxoutType which_type_tx_multisig;
     167         [ +  - ]:         44 :         const bool is_standard_tx_multisig = IsStandard(tx_multisig_script, std::nullopt, which_type_tx_multisig);
     168         [ +  - ]:         44 :         assert(is_standard_tx_multisig);
     169         [ +  - ]:         44 :         assert(which_type_tx_multisig == TxoutType::MULTISIG);
     170                 :            : 
     171                 :         44 :         std::vector<std::vector<unsigned char>> v_solutions_ret_tx_pubkey;
     172         [ +  - ]:         44 :         const TxoutType outtype_tx_pubkey = Solver(tx_pubkey_script, v_solutions_ret_tx_pubkey);
     173         [ +  - ]:         44 :         assert(outtype_tx_pubkey == TxoutType::PUBKEY);
     174         [ +  - ]:         44 :         assert(v_solutions_ret_tx_pubkey.size() == 1);
     175         [ +  - ]:         44 :         assert(v_solutions_ret_tx_pubkey[0].size() == 33);
     176                 :            : 
     177                 :         44 :         std::vector<std::vector<unsigned char>> v_solutions_ret_tx_multisig;
     178         [ +  - ]:         44 :         const TxoutType outtype_tx_multisig = Solver(tx_multisig_script, v_solutions_ret_tx_multisig);
     179         [ +  - ]:         44 :         assert(outtype_tx_multisig == TxoutType::MULTISIG);
     180         [ +  - ]:         44 :         assert(v_solutions_ret_tx_multisig.size() == 3);
     181         [ +  - ]:         44 :         assert(v_solutions_ret_tx_multisig[0].size() == 1);
     182         [ +  - ]:         44 :         assert(v_solutions_ret_tx_multisig[1].size() == 33);
     183         [ +  - ]:         44 :         assert(v_solutions_ret_tx_multisig[2].size() == 1);
     184                 :            : 
     185                 :         44 :         OutputType output_type{};
     186         [ +  - ]:         44 :         const CTxDestination tx_destination = GetDestinationForKey(pubkey, output_type);
     187         [ +  - ]:         44 :         assert(output_type == OutputType::LEGACY);
     188   [ +  -  +  - ]:         44 :         assert(IsValidDestination(tx_destination));
     189   [ +  -  +  - ]:         44 :         assert(PKHash{pubkey} == *std::get_if<PKHash>(&tx_destination));
     190                 :            : 
     191         [ +  - ]:         44 :         const CScript script_for_destination = GetScriptForDestination(tx_destination);
     192   [ +  -  +  - ]:         44 :         assert(script_for_destination.size() == 25);
     193                 :            : 
     194         [ +  - ]:         44 :         const std::string destination_address = EncodeDestination(tx_destination);
     195   [ +  -  +  -  :         44 :         assert(DecodeDestination(destination_address) == tx_destination);
                   +  - ]
     196                 :            : 
     197         [ +  - ]:         44 :         const CPubKey pubkey_from_address_string = AddrToPubKey(fillable_signing_provider, destination_address);
     198   [ +  -  +  - ]:         44 :         assert(pubkey_from_address_string == pubkey);
     199                 :            : 
     200         [ +  - ]:         44 :         CKeyID key_id = pubkey.GetID();
     201   [ +  -  +  - ]:         44 :         assert(!key_id.IsNull());
     202   [ +  -  +  - ]:         44 :         assert(key_id == CKeyID{key_id});
     203   [ +  -  +  -  :         44 :         assert(key_id == GetKeyForDestination(fillable_signing_provider, tx_destination));
                   +  - ]
     204                 :            : 
     205         [ +  - ]:         44 :         CPubKey pubkey_out;
     206         [ +  - ]:         44 :         const bool ok_get_pubkey = fillable_signing_provider.GetPubKey(key_id, pubkey_out);
     207         [ +  - ]:         44 :         assert(ok_get_pubkey);
     208                 :            : 
     209                 :         44 :         CKey key_out;
     210         [ +  - ]:         44 :         const bool ok_get_key = fillable_signing_provider.GetKey(key_id, key_out);
     211         [ +  - ]:         44 :         assert(ok_get_key);
     212   [ +  -  +  - ]:         44 :         assert(fillable_signing_provider.GetKeys().size() == 1);
     213   [ +  -  +  - ]:         44 :         assert(fillable_signing_provider.HaveKey(key_id));
     214                 :            : 
     215                 :         44 :         KeyOriginInfo key_origin_info;
     216         [ +  - ]:         44 :         const bool ok_get_key_origin = fillable_signing_provider.GetKeyOrigin(key_id, key_origin_info);
     217         [ +  - ]:         44 :         assert(!ok_get_key_origin);
     218                 :         44 :     }
     219                 :            : 
     220                 :            :     {
     221   [ +  -  +  -  :         44 :         const std::vector<unsigned char> vch_pubkey{pubkey.begin(), pubkey.end()};
                   +  - ]
     222   [ +  -  +  - ]:         44 :         assert(CPubKey::ValidSize(vch_pubkey));
     223   [ +  -  +  -  :         44 :         assert(!CPubKey::ValidSize({pubkey.begin(), pubkey.begin() + pubkey.size() - 1}));
          +  -  +  -  +  
                -  +  - ]
     224                 :            : 
     225   [ +  -  +  - ]:         44 :         const CPubKey pubkey_ctor_1{vch_pubkey};
     226   [ +  -  +  - ]:         44 :         assert(pubkey == pubkey_ctor_1);
     227                 :            : 
     228         [ +  - ]:         44 :         const CPubKey pubkey_ctor_2{vch_pubkey.begin(), vch_pubkey.end()};
     229   [ +  -  +  - ]:         44 :         assert(pubkey == pubkey_ctor_2);
     230                 :            : 
     231         [ +  - ]:         44 :         CPubKey pubkey_set;
     232         [ +  - ]:         44 :         pubkey_set.Set(vch_pubkey.begin(), vch_pubkey.end());
     233   [ +  -  +  - ]:         44 :         assert(pubkey == pubkey_set);
     234                 :         44 :     }
     235                 :            : 
     236                 :            :     {
     237         [ +  - ]:         44 :         const CPubKey invalid_pubkey{};
     238   [ +  -  +  - ]:         44 :         assert(!invalid_pubkey.IsValid());
     239   [ +  -  +  - ]:         44 :         assert(!invalid_pubkey.IsFullyValid());
     240   [ +  -  +  - ]:         44 :         assert(!(pubkey == invalid_pubkey));
     241   [ +  -  +  - ]:         44 :         assert(pubkey != invalid_pubkey);
     242   [ +  -  +  - ]:         44 :         assert(pubkey < invalid_pubkey);
     243                 :            :     }
     244                 :            : 
     245                 :            :     {
     246                 :            :         // Cover CPubKey's operator[](unsigned int pos)
     247                 :         44 :         unsigned int sum = 0;
     248   [ +  -  +  + ]:       1496 :         for (size_t i = 0; i < pubkey.size(); ++i) {
     249         [ +  - ]:       1452 :             sum += pubkey[i];
     250                 :       1452 :         }
     251   [ +  -  +  -  :         44 :         assert(std::accumulate(pubkey.begin(), pubkey.end(), 0U) == sum);
             +  -  +  - ]
     252                 :            :     }
     253                 :            : 
     254                 :            :     {
     255                 :         44 :         CPubKey decompressed_pubkey = pubkey;
     256   [ +  -  +  - ]:         44 :         assert(decompressed_pubkey.IsCompressed());
     257                 :            : 
     258         [ +  - ]:         44 :         const bool ok = decompressed_pubkey.Decompress();
     259         [ +  - ]:         44 :         assert(ok);
     260   [ +  -  +  - ]:         44 :         assert(!decompressed_pubkey.IsCompressed());
     261   [ +  -  +  - ]:         44 :         assert(decompressed_pubkey.size() == 65);
     262                 :            :     }
     263                 :            : 
     264                 :            :     {
     265                 :         44 :         std::vector<unsigned char> vch_sig;
     266         [ +  - ]:         44 :         const bool ok = key.Sign(random_uint256, vch_sig, false);
     267         [ +  - ]:         44 :         assert(ok);
     268   [ +  -  +  - ]:         44 :         assert(pubkey.Verify(random_uint256, vch_sig));
     269   [ +  -  +  - ]:         44 :         assert(CPubKey::CheckLowS(vch_sig));
     270                 :            : 
     271         [ +  - ]:         44 :         const std::vector<unsigned char> vch_invalid_sig{vch_sig.begin(), vch_sig.begin() + vch_sig.size() - 1};
     272   [ +  -  +  - ]:         44 :         assert(!pubkey.Verify(random_uint256, vch_invalid_sig));
     273   [ +  -  +  - ]:         44 :         assert(!CPubKey::CheckLowS(vch_invalid_sig));
     274                 :         44 :     }
     275                 :            : 
     276                 :            :     {
     277                 :         44 :         std::vector<unsigned char> vch_compact_sig;
     278         [ +  - ]:         44 :         const bool ok_sign_compact = key.SignCompact(random_uint256, vch_compact_sig);
     279         [ +  - ]:         44 :         assert(ok_sign_compact);
     280                 :            : 
     281         [ +  - ]:         44 :         CPubKey recover_pubkey;
     282         [ +  - ]:         44 :         const bool ok_recover_compact = recover_pubkey.RecoverCompact(random_uint256, vch_compact_sig);
     283         [ +  - ]:         44 :         assert(ok_recover_compact);
     284   [ +  -  +  - ]:         44 :         assert(recover_pubkey == pubkey);
     285                 :         44 :     }
     286                 :            : 
     287                 :            :     {
     288         [ +  - ]:         44 :         CPubKey child_pubkey;
     289         [ +  - ]:         44 :         ChainCode child_chaincode;
     290         [ +  - ]:         44 :         const bool ok = pubkey.Derive(child_pubkey, child_chaincode, 0, random_uint256);
     291         [ +  - ]:         44 :         assert(ok);
     292   [ +  -  +  - ]:         44 :         assert(child_pubkey != pubkey);
     293   [ +  -  +  - ]:         44 :         assert(child_pubkey.IsCompressed());
     294   [ +  -  +  - ]:         44 :         assert(child_pubkey.IsFullyValid());
     295   [ +  -  +  - ]:         44 :         assert(child_pubkey.IsValid());
     296   [ +  -  +  - ]:         44 :         assert(child_pubkey.size() == 33);
     297   [ +  -  +  - ]:         44 :         assert(child_chaincode != random_uint256);
     298                 :            :     }
     299                 :            : 
     300         [ +  - ]:         44 :     const CPrivKey priv_key = key.GetPrivKey();
     301                 :            : 
     302                 :            :     {
     303         [ +  + ]:        132 :         for (const bool skip_check : {true, false}) {
     304                 :         88 :             CKey loaded_key;
     305         [ +  - ]:         88 :             const bool ok = loaded_key.Load(priv_key, pubkey, skip_check);
     306         [ +  - ]:         88 :             assert(ok);
     307   [ +  -  +  - ]:         88 :             assert(key == loaded_key);
     308                 :         88 :         }
     309                 :            :     }
     310         [ -  + ]:         46 : }
     311                 :            : 
     312   [ +  -  -  + ]:        427 : FUZZ_TARGET(ellswift_roundtrip, .init = initialize_key)
     313                 :            : {
     314                 :         81 :     FuzzedDataProvider fdp{buffer.data(), buffer.size()};
     315                 :            : 
     316                 :         81 :     CKey key = ConsumePrivateKey(fdp, /*compressed=*/true);
     317   [ +  -  +  + ]:         81 :     if (!key.IsValid()) return;
     318                 :            : 
     319         [ +  - ]:         80 :     auto ent32 = fdp.ConsumeBytes<std::byte>(32);
     320         [ +  - ]:         80 :     ent32.resize(32);
     321                 :            : 
     322   [ +  -  +  - ]:         80 :     auto encoded_ellswift = key.EllSwiftCreate(ent32);
     323         [ +  - ]:         80 :     auto decoded_pubkey = encoded_ellswift.Decode();
     324                 :            : 
     325   [ +  -  +  - ]:         80 :     assert(key.VerifyPubKey(decoded_pubkey));
     326         [ -  + ]:         81 : }
     327                 :            : 
     328   [ +  -  -  + ]:        440 : FUZZ_TARGET(bip324_ecdh, .init = initialize_key)
     329                 :            : {
     330                 :         94 :     FuzzedDataProvider fdp{buffer.data(), buffer.size()};
     331                 :            : 
     332                 :            :     // We generate private key, k1.
     333                 :         94 :     CKey k1 = ConsumePrivateKey(fdp, /*compressed=*/true);
     334   [ +  -  +  + ]:         94 :     if (!k1.IsValid()) return;
     335                 :            : 
     336                 :            :     // They generate private key, k2.
     337         [ +  - ]:         93 :     CKey k2 = ConsumePrivateKey(fdp, /*compressed=*/true);
     338   [ +  -  +  + ]:         93 :     if (!k2.IsValid()) return;
     339                 :            : 
     340                 :            :     // We construct an ellswift encoding for our key, k1_ellswift.
     341         [ +  - ]:         92 :     auto ent32_1 = fdp.ConsumeBytes<std::byte>(32);
     342         [ +  - ]:         92 :     ent32_1.resize(32);
     343   [ +  -  +  - ]:         92 :     auto k1_ellswift = k1.EllSwiftCreate(ent32_1);
     344                 :            : 
     345                 :            :     // They construct an ellswift encoding for their key, k2_ellswift.
     346         [ +  - ]:         92 :     auto ent32_2 = fdp.ConsumeBytes<std::byte>(32);
     347         [ +  - ]:         92 :     ent32_2.resize(32);
     348   [ +  -  +  - ]:         92 :     auto k2_ellswift = k2.EllSwiftCreate(ent32_2);
     349                 :            : 
     350                 :            :     // They construct another (possibly distinct) ellswift encoding for their key, k2_ellswift_bad.
     351         [ +  - ]:         92 :     auto ent32_2_bad = fdp.ConsumeBytes<std::byte>(32);
     352         [ +  - ]:         92 :     ent32_2_bad.resize(32);
     353   [ +  -  +  - ]:         92 :     auto k2_ellswift_bad = k2.EllSwiftCreate(ent32_2_bad);
     354   [ +  -  +  -  :         92 :     assert((ent32_2_bad == ent32_2) == (k2_ellswift_bad == k2_ellswift));
                   +  - ]
     355                 :            : 
     356                 :            :     // Determine who is who.
     357         [ +  - ]:         92 :     bool initiating = fdp.ConsumeBool();
     358                 :            : 
     359                 :            :     // We compute our shared secret using our key and their public key.
     360         [ +  - ]:         92 :     auto ecdh_secret_1 = k1.ComputeBIP324ECDHSecret(k2_ellswift, k1_ellswift, initiating);
     361                 :            :     // They compute their shared secret using their key and our public key.
     362         [ +  - ]:         92 :     auto ecdh_secret_2 = k2.ComputeBIP324ECDHSecret(k1_ellswift, k2_ellswift, !initiating);
     363                 :            :     // Those must match, as everyone is behaving correctly.
     364   [ +  -  +  - ]:         92 :     assert(ecdh_secret_1 == ecdh_secret_2);
     365                 :            : 
     366   [ +  -  +  + ]:         92 :     if (k1_ellswift != k2_ellswift) {
     367                 :            :         // Unless the two keys are exactly identical, acting as the wrong party breaks things.
     368         [ +  - ]:         91 :         auto ecdh_secret_bad = k1.ComputeBIP324ECDHSecret(k2_ellswift, k1_ellswift, !initiating);
     369   [ +  -  +  - ]:         91 :         assert(ecdh_secret_bad != ecdh_secret_1);
     370                 :         91 :     }
     371                 :            : 
     372   [ +  -  +  + ]:         92 :     if (k2_ellswift_bad != k2_ellswift) {
     373                 :            :         // Unless both encodings created by them are identical, using the second one breaks things.
     374         [ +  - ]:         19 :         auto ecdh_secret_bad = k1.ComputeBIP324ECDHSecret(k2_ellswift_bad, k1_ellswift, initiating);
     375   [ +  -  +  - ]:         19 :         assert(ecdh_secret_bad != ecdh_secret_1);
     376                 :         19 :     }
     377         [ -  + ]:         94 : }

Generated by: LCOV version 1.14