Coin.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef COIN_H_
  3. #define COIN_H_
  4. #include "../bignum.h"
  5. #include "Params.h"
  6. namespace libzerocoin {
  7. enum CoinDenomination {
  8. ZQ_LOVELACE = 1,
  9. ZQ_GOLDWASSER = 10,
  10. ZQ_RACKOFF = 25,
  11. ZQ_PEDERSEN = 50,
  12. ZQ_WILLIAMSON = 100 // Malcolm J. Williamson,
  13. // the scientist who actually invented
  14. // Public key cryptography
  15. };
  16. /** A Public coin is the part of a coin that
  17. * is published to the network and what is handled
  18. * by other clients. It contains only the value
  19. * of commitment to a serial number and the
  20. * denomination of the coin.
  21. */
  22. class PublicCoin {
  23. public:
  24. template<typename Stream>
  25. PublicCoin(const Params* p, Stream& strm): params(p) {
  26. strm >> *this;
  27. }
  28. PublicCoin( const Params* p);
  29. /**Generates a public coin
  30. *
  31. * @param p cryptographic paramters
  32. * @param coin the value of the commitment.
  33. * @param denomination The denomination of the coin. Defaults to ZQ_PEDERSEN
  34. */
  35. PublicCoin( const Params* p, const CBigNum& coin, const CoinDenomination d = ZQ_PEDERSEN);
  36. const CBigNum& getValue() const;
  37. const CoinDenomination getDenomination() const;
  38. bool operator==(const PublicCoin& rhs) const;
  39. bool operator!=(const PublicCoin& rhs) const;
  40. /** Checks that a coin prime
  41. * and in the appropriate range
  42. * given the parameters
  43. * @return true if valid
  44. */
  45. bool validate() const;
  46. IMPLEMENT_SERIALIZE
  47. (
  48. READWRITE(value);
  49. READWRITE(denomination);
  50. )
  51. private:
  52. const Params* params;
  53. CBigNum value;
  54. // Denomination is stored as an INT because storing
  55. // and enum raises amigiuities in the serialize code //FIXME if possible
  56. int denomination;
  57. };
  58. /**
  59. * A private coin. As the name implies, the content
  60. * of this should stay private except PublicCoin.
  61. *
  62. * Contains a coin's serial number, a commitment to it,
  63. * and opening randomness for the commitment.
  64. *
  65. * @warning Failure to keep this secret(or safe),
  66. * @warning will result in the theft of your coins
  67. * @warning and a TOTAL loss of anonymity.
  68. */
  69. class PrivateCoin {
  70. public:
  71. template<typename Stream>
  72. PrivateCoin(const Params* p, Stream& strm): params(p) {
  73. strm >> *this;
  74. }
  75. PrivateCoin(const Params* p,const CoinDenomination denomination = ZQ_PEDERSEN);
  76. const PublicCoin& getPublicCoin() const;
  77. const CBigNum& getSerialNumber() const;
  78. const CBigNum& getRandomness() const;
  79. IMPLEMENT_SERIALIZE
  80. (
  81. READWRITE(publicCoin);
  82. READWRITE(randomness);
  83. READWRITE(serialNumber);
  84. )
  85. private:
  86. const Params* params;
  87. PublicCoin publicCoin;
  88. CBigNum randomness;
  89. CBigNum serialNumber;
  90. /**
  91. * @brief Mint a new coin.
  92. * @param denomination the denomination of the coin to mint
  93. * @throws ZerocoinException if the process takes too long
  94. *
  95. * Generates a new Zerocoin by (a) selecting a random serial
  96. * number, (b) committing to this serial number and repeating until
  97. * the resulting commitment is prime. Stores the
  98. * resulting commitment (coin) and randomness (trapdoor).
  99. **/
  100. void mintCoin(const CoinDenomination denomination);
  101. /**
  102. * @brief Mint a new coin using a faster process.
  103. * @param denomination the denomination of the coin to mint
  104. * @throws ZerocoinException if the process takes too long
  105. *
  106. * Generates a new Zerocoin by (a) selecting a random serial
  107. * number, (b) committing to this serial number and repeating until
  108. * the resulting commitment is prime. Stores the
  109. * resulting commitment (coin) and randomness (trapdoor).
  110. * This routine is substantially faster than the
  111. * mintCoin() routine, but could be more vulnerable
  112. * to timing attacks. Don't use it if you think someone
  113. * could be timing your coin minting.
  114. **/
  115. void mintCoinFast(const CoinDenomination denomination);
  116. };
  117. } /* namespace libzerocoin */
  118. #endif /* COIN_H_ */