CoinSpend.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef COINSPEND_H_
  3. #define COINSPEND_H_
  4. #include "Params.h"
  5. #include "Coin.h"
  6. #include "Commitment.h"
  7. #include "../bignum.h"
  8. #include "Accumulator.h"
  9. #include "AccumulatorProofOfKnowledge.h"
  10. #include "SerialNumberSignatureOfKnowledge.h"
  11. #include "SpendMetaData.h"
  12. #include "../serialize.h"
  13. namespace libzerocoin {
  14. /** The complete proof needed to spend a zerocoin.
  15. * Composes together a proof that a coin is accumulated
  16. * and that it has a given serial number.
  17. */
  18. class CoinSpend {
  19. public:
  20. template<typename Stream>
  21. CoinSpend(const Params* p, Stream& strm):denomination(ZQ_PEDERSEN),
  22. accumulatorPoK(&p->accumulatorParams),
  23. serialNumberSoK(p),
  24. commitmentPoK(&p->serialNumberSoKCommitmentGroup, &p->accumulatorParams.accumulatorPoKCommitmentGroup) {
  25. strm >> *this;
  26. }
  27. /**Generates a proof spending a zerocoin.
  28. *
  29. * To use this, provide an unspent PrivateCoin, the latest Accumulator
  30. * (e.g from the most recent Ecoin block) containing the public part
  31. * of the coin, a witness to that, and whatever medeta data is needed.
  32. *
  33. * Once constructed, this proof can be serialized and sent.
  34. * It is validated simply be calling validate.
  35. * @warning Validation only checks that the proof is correct
  36. * @warning for the specified values in this class. These values must be validated
  37. * Clients ought to check that
  38. * 1) params is the right params
  39. * 2) the accumulator actually is in some block
  40. * 3) that the serial number is unspent
  41. * 4) that the transaction
  42. *
  43. * @param p cryptographic parameters
  44. * @param coin The coin to be spend
  45. * @param a The current accumulator containing the coin
  46. * @param witness The witness showing that the accumulator contains the coin
  47. * @param m arbitrary meta data related to the spend that might be needed by Ecoin
  48. * (i.e. the transaction hash)
  49. * @throw ZerocoinException if the process fails
  50. */
  51. CoinSpend(const Params* p, const PrivateCoin& coin, Accumulator& a, const AccumulatorWitness& witness, const SpendMetaData& m);
  52. /** Returns the serial number of the coin spend by this proof.
  53. *
  54. * @return the coin's serial number
  55. */
  56. const CBigNum& getCoinSerialNumber();
  57. /**Gets the denomination of the coin spent in this proof.
  58. *
  59. * @return the denomination
  60. */
  61. const CoinDenomination getDenomination();
  62. bool Verify(const Accumulator& a, const SpendMetaData &metaData) const;
  63. IMPLEMENT_SERIALIZE
  64. (
  65. READWRITE(denomination);
  66. READWRITE(accCommitmentToCoinValue);
  67. READWRITE(serialCommitmentToCoinValue);
  68. READWRITE(coinSerialNumber);
  69. READWRITE(accumulatorPoK);
  70. READWRITE(serialNumberSoK);
  71. READWRITE(commitmentPoK);
  72. )
  73. private:
  74. const Params *params;
  75. const uint256 signatureHash(const SpendMetaData &m) const;
  76. // Denomination is stored as an INT because storing
  77. // and enum raises amigiuities in the serialize code //FIXME if possible
  78. int denomination;
  79. CBigNum accCommitmentToCoinValue;
  80. CBigNum serialCommitmentToCoinValue;
  81. CBigNum coinSerialNumber;
  82. AccumulatorProofOfKnowledge accumulatorPoK;
  83. SerialNumberSignatureOfKnowledge serialNumberSoK;
  84. CommitmentProofOfKnowledge commitmentPoK;
  85. };
  86. } /* namespace libzerocoin */
  87. #endif /* COINSPEND_H_ */