PIVX Core  5.6.99
P2P Digital Currency
CoinSpend.cpp
Go to the documentation of this file.
1 
12 // Copyright (c) 2017-2021 The PIVX Core developers
13 
14 #include "CoinSpend.h"
15 #include <iostream>
16 #include <sstream>
17 
18 namespace libzerocoin
19 {
20 
22 {
23  CHashWriter h(0, 0);
26 
27  if (version >= PUBKEY_VERSION)
28  h << spendType;
29 
30  return h.GetHash();
31 }
32 
33 std::string CoinSpend::ToString() const
34 {
35  std::stringstream ss;
36  ss << "CoinSpend:\n version=" << (int)version << " signatureHash=" << signatureHash().GetHex() << " spendtype=" << spendType << "\n";
37  return ss.str();
38 }
39 
41 {
42  return IsValidSerial(params, coinSerialNumber);
43 }
44 
45 //Additional verification layer that requires the spend be signed by the private key associated with the serial
47 {
48  const int coinVersion = getCoinVersion();
49  //No private key for V1
50  if (coinVersion < PUBKEY_VERSION)
51  return true;
52 
53  try {
54  //V2 serial requires that the signature hash be signed by the public key associated with the serial
55  arith_uint256 hashedPubkey = UintToArith256(Hash(pubkey.begin(), pubkey.end())) >> V2_BITSHIFT;
56  if (hashedPubkey != GetAdjustedSerial(coinSerialNumber).getuint256()) {
57  //cout << "CoinSpend::HasValidSignature() hashedpubkey is not equal to the serial!\n";
58  return false;
59  }
60  } catch (const std::range_error& e) {
61  //std::cout << "HasValidSignature() error: " << e.what() << std::endl;
62  throw InvalidSerialException("Serial longer than 256 bits");
63  }
64 
65  return pubkey.Verify(signatureHash(), vchSig);
66 }
67 
69 {
70  CBigNum bnSerial = coinSerialNumber;
71  bnSerial = bnSerial % params->coinCommitmentGroup.groupOrder;
72  return bnSerial;
73 }
74 
75 std::vector<unsigned char> CoinSpend::ParseSerial(CDataStream& s) {
76  unsigned int nSize = ReadCompactSize(s);
77  s.movePos(nSize);
78  nSize = ReadCompactSize(s);
79  s.movePos(nSize);
81  s >> coinSerialNumber;
82  return coinSerialNumber.getvch();
83 }
84 
85 void CoinSpend::setPubKey(CPubKey pkey, bool fUpdateSerial) {
86  this->pubkey = pkey;
87  if (fUpdateSerial) {
89  }
90 }
91 
92 } /* namespace libzerocoin */
CoinSpend class for the Zerocoin library.
arith_uint256 UintToArith256(const uint256 &a)
CBaseDataStream & movePos(size_t nSize)
Definition: streams.h:280
C++ wrapper for BIGNUM.
Definition: bignum.h:35
std::vector< unsigned char > getvch() const
Definition: bignum.cpp:139
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:216
uint256 GetHash()
Definition: hash.h:236
An encapsulated public key.
Definition: pubkey.h:44
const unsigned char * end() const
Definition: pubkey.h:124
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:169
const unsigned char * begin() const
Definition: pubkey.h:123
256-bit unsigned big integer.
std::string GetHex() const
Definition: uint256.cpp:21
void setPubKey(CPubKey pkey, bool fUpdateSerial=false)
Definition: CoinSpend.cpp:85
virtual const uint256 signatureHash() const
Definition: CoinSpend.cpp:21
bool HasValidSerial(ZerocoinParams *params) const
Definition: CoinSpend.cpp:40
CommitmentProofOfKnowledge commitmentPoK
Definition: CoinSpend.h:138
CBigNum accCommitmentToCoinValue
Definition: CoinSpend.h:134
CBigNum CalculateValidSerial(ZerocoinParams *params)
Definition: CoinSpend.cpp:68
std::string ToString() const
Definition: CoinSpend.cpp:33
CoinDenomination denomination
Definition: CoinSpend.h:123
AccumulatorProofOfKnowledge accumulatorPoK
Definition: CoinSpend.h:136
int getCoinVersion() const
Definition: CoinSpend.h:93
std::vector< unsigned char > vchSig
Definition: CoinSpend.h:128
static std::vector< unsigned char > ParseSerial(CDataStream &s)
Definition: CoinSpend.cpp:75
bool HasValidSignature() const
Definition: CoinSpend.cpp:46
CBigNum serialCommitmentToCoinValue
Definition: CoinSpend.h:135
CBigNum groupOrder
The order of the group.
Definition: Params.h:57
IntegerGroupParams coinCommitmentGroup
The Quadratic Residue group from which we form a coin as a commitment to a serial number.
Definition: Params.h:169
256-bit opaque blob.
Definition: uint256.h:138
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:173
bool IsValidSerial(const ZerocoinParams *params, const CBigNum &bnSerial)
Definition: Coin.cpp:87
CBigNum GetAdjustedSerial(const CBigNum &bnSerial)
Definition: Coin.cpp:78
CBigNum ExtractSerialFromPubKey(const CPubKey pubkey)
Definition: Coin.cpp:108
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:359