PIVX Core  5.6.99
P2P Digital Currency
Coin.cpp
Go to the documentation of this file.
1 
12 // Copyright (c) 2017-2021 The PIVX Core developers
13 
14 #include <stdexcept>
15 #include <iostream>
16 #include "Coin.h"
17 #include "Commitment.h"
18 #include "pubkey.h"
19 
20 namespace libzerocoin {
21 
22 //PublicCoin class
24  params(p) {
25  if (!this->params->initialized) {
26  throw std::runtime_error("Params are not initialized");
27  }
28  // Assume this will get set by another method later
30 };
31 
33  params(p), value(coin), denomination(d) {
34  if (!this->params->initialized) {
35  throw std::runtime_error("Params are not initialized");
36  }
37  if (denomination == 0) {
38  throw std::runtime_error("Denomination does not exist");
39  }
40 };
41 
43 {
45  return error("%s: ERROR: PublicCoin::validate value is too low: %s", __func__, value.GetDec());
46  }
47 
49  return error("%s: ERROR: PublicCoin::validate value is too high, max: %s, received: %s",
50  __func__, this->params->accumulatorParams.maxCoinValue, value.GetDec());
51  }
52 
54  return error("%s: ERROR: PublicCoin::validate value is not prime. Value: %s, Iterations: %d",
55  __func__, value.GetDec(), params->zkp_iterations);
56  }
57 
58  return true;
59 }
60 
61 int ExtractVersionFromSerial(const CBigNum& bnSerial)
62 {
63  try {
64  //Serial is marked as v2 only if the first byte is 0xF
65  arith_uint256 nMark = bnSerial.getuint256() >> (256 - V2_BITSHIFT);
66  if (nMark == arith_uint256(0xf))
67  return PUBKEY_VERSION;
68  } catch (const std::range_error& e) {
69  //std::cout << "ExtractVersionFromSerial(): " << e.what() << std::endl;
70  // Only serial version 2 appeared with this range error..
71  return 2;
72  }
73 
74  return 1;
75 }
76 
77 //Remove the first four bits for V2 serials
79 {
80  const uint256& serial = ArithToUint256(bnSerial.getuint256() & (~ARITH_UINT256_ZERO >> V2_BITSHIFT));
81  CBigNum bnSerialAdjusted;
82  bnSerialAdjusted.setuint256(serial);
83  return bnSerialAdjusted;
84 }
85 
86 
87 bool IsValidSerial(const ZerocoinParams* params, const CBigNum& bnSerial)
88 {
89  if (bnSerial <= 0)
90  return false;
91 
92  if (ExtractVersionFromSerial(bnSerial) < PUBKEY_VERSION)
93  return bnSerial < params->coinCommitmentGroup.groupOrder;
94 
95  // If V2, the serial is marked with 0xF in the first 4 bits. So It's always > groupOrder.
96  // This is removed for the adjusted serial - so it's always < groupOrder.
97  // So we check only the bitsize here.
98  return bnSerial.bitSize() <= 256;
99 }
100 
101 
102 bool IsValidCommitmentToCoinRange(const ZerocoinParams* params, const CBigNum& bnCommitment)
103 {
104  return bnCommitment > BN_ZERO && bnCommitment < params->serialNumberSoKCommitmentGroup.modulus;
105 }
106 
107 
109 {
110  const arith_uint256& hashedPubkey = UintToArith256(Hash(pubkey.begin(), pubkey.end())) >> V2_BITSHIFT;
111  arith_uint256 uintSerial = (arith_uint256(0xF) << (256 - V2_BITSHIFT)) | hashedPubkey;
112  return CBigNum(ArithToUint256(uintSerial));
113 }
114 
115 
116 } /* namespace libzerocoin */
PublicCoin class for the Zerocoin library.
Commitment and CommitmentProof classes for the Zerocoin library.
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
const arith_uint256 ARITH_UINT256_ZERO
const CBigNum BN_ZERO
constant bignum instances
Definition: bignum.h:232
C++ wrapper for BIGNUM.
Definition: bignum.h:35
bool isPrime(const int checks=15) const
Miller-Rabin primality test on this element.
Definition: bignum.cpp:261
arith_uint256 getuint256() const
Definition: bignum.cpp:113
std::string GetDec() const
Definition: bignum.cpp:326
int bitSize() const
Returns the size in bits of the underlying bignum.
Definition: bignum.cpp:78
void setuint256(uint256 n)
Definition: bignum.cpp:108
An encapsulated public key.
Definition: pubkey.h:44
const unsigned char * end() const
Definition: pubkey.h:124
const unsigned char * begin() const
Definition: pubkey.h:123
256-bit unsigned big integer.
CBigNum maxCoinValue
Upper bound on the value for a committed coin.
Definition: Params.h:108
CBigNum minCoinValue
Lower bound on the value for committed coin.
Definition: Params.h:102
CBigNum groupOrder
The order of the group.
Definition: Params.h:57
CBigNum modulus
The modulus for the group.
Definition: Params.h:52
const ZerocoinParams * params
Definition: Coin.h:82
bool validate() const
Checks that coin is prime and in the appropriate range given the parameters.
Definition: Coin.cpp:42
PublicCoin(const ZerocoinParams *p, Stream &strm)
Definition: Coin.h:52
CoinDenomination denomination
Definition: Coin.h:84
IntegerGroupParams coinCommitmentGroup
The Quadratic Residue group from which we form a coin as a commitment to a serial number.
Definition: Params.h:169
AccumulatorAndProofParams accumulatorParams
Definition: Params.h:163
IntegerGroupParams serialNumberSoKCommitmentGroup
One of two groups used to form a commitment to a coin (which it self is a commitment to a serial numb...
Definition: Params.h:177
uint32_t zkp_iterations
The number of iterations to use in the serial number proof.
Definition: Params.h:183
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
bool IsValidCommitmentToCoinRange(const ZerocoinParams *params, const CBigNum &bnCommitment)
Definition: Coin.cpp:102
CBigNum GetAdjustedSerial(const CBigNum &bnSerial)
Definition: Coin.cpp:78
int ExtractVersionFromSerial(const CBigNum &bnSerial)
Definition: Coin.cpp:61
CBigNum ExtractSerialFromPubKey(const CPubKey pubkey)
Definition: Coin.cpp:108
bool error(const char *fmt, const Args &... args)
Definition: system.h:77