Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace tabs with spaces #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/bloom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
using namespace std;

CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn, unsigned char nFlagsIn) :
/**
/**
* The ideal size for a bloom filter with a given number of elements and false positive rate is:
* - nElements * log(fp rate) / ln(2)^2
* We ignore filter parameters which will create a bloom filter larger than the protocol limits
*/
vData(min((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
vData(min((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
/**
* The ideal number of hash functions is filter size * ln(2) / number of elements
* Again, we ignore filter parameters which will create a bloom filter with more hash functions than the protocol limits
* See https://en.wikipedia.org/wiki/Bloom_filter for an explanation of these formulas
*/
isFull(false),
isEmpty(false),
isFull(false),
isEmpty(false),
nHashFuncs(min((unsigned int)(vData.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
nTweak(nTweakIn),
nFlags(nFlagsIn)
Expand All @@ -52,8 +52,8 @@ inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<
}

void CBloomFilter::setFull(){
isFull = false;
}
isFull = false;
}

void CBloomFilter::insert(const vector<unsigned char>& vKey)
{
Expand Down Expand Up @@ -155,7 +155,7 @@ bool CBloomFilter::Merge(const CBloomFilter& filter) {
}
}

// TODO: FIX ME, this is slow for the coinSpend unserialization.
// TODO: FIX ME, this is slow for the coinSpend unserialization.
bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
{
bool fFound = false;
Expand Down
2 changes: 1 addition & 1 deletion src/libzerocoin/Accumulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const CBigNum& AccumulatorWitness::getValue() const {
}

const PublicCoin& AccumulatorWitness::getPublicCoin() const {
return this->element;
return this->element;
}

bool AccumulatorWitness::VerifyWitness(const Accumulator& a, const PublicCoin &publicCoin) const {
Expand Down
12 changes: 6 additions & 6 deletions src/libzerocoin/Accumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ class AccumulatorWitness {
*/
void addRawValue(const CBigNum& bnValue);

/**
*
* @return the value of the witness
*/
const CBigNum& getValue() const;
const PublicCoin& getPublicCoin() const;
/**
*
* @return the value of the witness
*/
const CBigNum& getValue() const;
const PublicCoin& getPublicCoin() const;
void resetValue(const Accumulator& checkpoint, const PublicCoin coin);

/** Checks that this is a witness to the accumulation of coin
Expand Down
222 changes: 111 additions & 111 deletions src/libzerocoin/Coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ namespace libzerocoin {

//PublicCoin class
PublicCoin::PublicCoin(const ZerocoinParams* p):
params(p) {
if (this->params->initialized == false) {
throw std::runtime_error("Params are not initialized");
}
params(p) {
if (this->params->initialized == false) {
throw std::runtime_error("Params are not initialized");
}
// Assume this will get set by another method later
denomination = ZQ_ERROR;
};

PublicCoin::PublicCoin(const ZerocoinParams* p, const CBigNum& coin, const CoinDenomination d):
params(p), value(coin) {
if (this->params->initialized == false) {
throw std::runtime_error("Params are not initialized");
}

denomination = d;
for(const CoinDenomination denom : zerocoinDenomList) {
if(denom == d)
denomination = d;
}
params(p), value(coin) {
if (this->params->initialized == false) {
throw std::runtime_error("Params are not initialized");
}

denomination = d;
for(const CoinDenomination denom : zerocoinDenomList) {
if(denom == d)
denomination = d;
}
if(denomination == 0){
std::cout << "denom does not exist\n";
throw std::runtime_error("Denomination does not exist");
}
std::cout << "denom does not exist\n";
throw std::runtime_error("Denomination does not exist");
}
};

bool PublicCoin::validate() const
Expand All @@ -70,10 +70,10 @@ bool PublicCoin::validate() const

//PrivateCoin class
PrivateCoin::PrivateCoin(const ZerocoinParams* p, const CoinDenomination denomination, bool fMintNew): params(p), publicCoin(p) {
// Verify that the parameters are valid
if(this->params->initialized == false) {
throw std::runtime_error("Params are not initialized");
}
// Verify that the parameters are valid
if(this->params->initialized == false) {
throw std::runtime_error("Params are not initialized");
}

if (fMintNew) {
#ifdef ZEROCOIN_FAST_MINT
Expand Down Expand Up @@ -148,122 +148,122 @@ bool GenerateKeyPair(const CBigNum& bnGroupOrder, const uint256& nPrivkey, CKey&

const CPubKey PrivateCoin::getPubKey() const
{
CKey key;
key.SetPrivKey(privkey, true);
return key.GetPubKey();
CKey key;
key.SetPrivKey(privkey, true);
return key.GetPubKey();
}

bool PrivateCoin::sign(const uint256& hash, vector<unsigned char>& vchSig) const
{
CKey key;
key.SetPrivKey(privkey, true);
return key.Sign(hash, vchSig);
CKey key;
key.SetPrivKey(privkey, true);
return key.Sign(hash, vchSig);
}

void PrivateCoin::mintCoin(const CoinDenomination denomination) {
// Repeat this process up to MAX_COINMINT_ATTEMPTS times until
// we obtain a prime number
for(uint32_t attempt = 0; attempt < MAX_COINMINT_ATTEMPTS; attempt++) {

// Generate a random serial number in the range 0...{q-1} where
// "q" is the order of the commitment group.
// And where the serial also doubles as a public key
CKey key;
CBigNum s;
// Repeat this process up to MAX_COINMINT_ATTEMPTS times until
// we obtain a prime number
for(uint32_t attempt = 0; attempt < MAX_COINMINT_ATTEMPTS; attempt++) {

// Generate a random serial number in the range 0...{q-1} where
// "q" is the order of the commitment group.
// And where the serial also doubles as a public key
CKey key;
CBigNum s;
bool isValid = false;
while (!isValid) {
isValid = GenerateKeyPair(this->params->coinCommitmentGroup.groupOrder, uint256(0), key, s);
}

// Generate a Pedersen commitment to the serial number "s"
Commitment coin(&params->coinCommitmentGroup, s);

// Now verify that the commitment is a prime number
// in the appropriate range. If not, we'll throw this coin
// away and generate a new one.
if (coin.getCommitmentValue().isPrime(ZEROCOIN_MINT_PRIME_PARAM) &&
coin.getCommitmentValue() >= params->accumulatorParams.minCoinValue &&
coin.getCommitmentValue() <= params->accumulatorParams.maxCoinValue) {
// Found a valid coin. Store it.
this->serialNumber = s;
this->randomness = coin.getRandomness();
this->publicCoin = PublicCoin(params,coin.getCommitmentValue(), denomination);
this->privkey = key.GetPrivKey();
this->version = 2;

// Success! We're done.
return;
}
}

// We only get here if we did not find a coin within
// MAX_COINMINT_ATTEMPTS. Throw an exception.
throw std::runtime_error("Unable to mint a new Zerocoin (too many attempts)");
// Generate a Pedersen commitment to the serial number "s"
Commitment coin(&params->coinCommitmentGroup, s);

// Now verify that the commitment is a prime number
// in the appropriate range. If not, we'll throw this coin
// away and generate a new one.
if (coin.getCommitmentValue().isPrime(ZEROCOIN_MINT_PRIME_PARAM) &&
coin.getCommitmentValue() >= params->accumulatorParams.minCoinValue &&
coin.getCommitmentValue() <= params->accumulatorParams.maxCoinValue) {
// Found a valid coin. Store it.
this->serialNumber = s;
this->randomness = coin.getRandomness();
this->publicCoin = PublicCoin(params,coin.getCommitmentValue(), denomination);
this->privkey = key.GetPrivKey();
this->version = 2;

// Success! We're done.
return;
}
}

// We only get here if we did not find a coin within
// MAX_COINMINT_ATTEMPTS. Throw an exception.
throw std::runtime_error("Unable to mint a new Zerocoin (too many attempts)");
}

void PrivateCoin::mintCoinFast(const CoinDenomination denomination) {

// Generate a random serial number in the range 0...{q-1} where
// "q" is the order of the commitment group.
// And where the serial also doubles as a public key
CKey key;
CBigNum s;
// Generate a random serial number in the range 0...{q-1} where
// "q" is the order of the commitment group.
// And where the serial also doubles as a public key
CKey key;
CBigNum s;
bool isValid = false;
while (!isValid) {
isValid = GenerateKeyPair(this->params->coinCommitmentGroup.groupOrder, uint256(0), key, s);
}
// Generate a random number "r" in the range 0...{q-1}
CBigNum r = CBigNum::randBignum(this->params->coinCommitmentGroup.groupOrder);
// Manually compute a Pedersen commitment to the serial number "s" under randomness "r"
// C = g^s * h^r mod p
CBigNum commitmentValue = this->params->coinCommitmentGroup.g.pow_mod(s, this->params->coinCommitmentGroup.modulus).mul_mod(this->params->coinCommitmentGroup.h.pow_mod(r, this->params->coinCommitmentGroup.modulus), this->params->coinCommitmentGroup.modulus);
// Repeat this process up to MAX_COINMINT_ATTEMPTS times until
// we obtain a prime number
for (uint32_t attempt = 0; attempt < MAX_COINMINT_ATTEMPTS; attempt++) {
// First verify that the commitment is a prime number
// in the appropriate range. If not, we'll throw this coin
// away and generate a new one.
if (commitmentValue.isPrime(ZEROCOIN_MINT_PRIME_PARAM) &&
commitmentValue >= params->accumulatorParams.minCoinValue &&
commitmentValue <= params->accumulatorParams.maxCoinValue) {
// Found a valid coin. Store it.
this->serialNumber = s;
this->randomness = r;
this->publicCoin = PublicCoin(params, commitmentValue, denomination);
this->privkey = key.GetPrivKey();
this->version = 2;

// Success! We're done.
return;
}
// Generate a new random "r_delta" in 0...{q-1}
CBigNum r_delta = CBigNum::randBignum(this->params->coinCommitmentGroup.groupOrder);

// The commitment was not prime. Increment "r" and recalculate "C":
// r = r + r_delta mod q
// C = C * h mod p
r = (r + r_delta) % this->params->coinCommitmentGroup.groupOrder;
commitmentValue = commitmentValue.mul_mod(this->params->coinCommitmentGroup.h.pow_mod(r_delta, this->params->coinCommitmentGroup.modulus), this->params->coinCommitmentGroup.modulus);
}
// We only get here if we did not find a coin within
// MAX_COINMINT_ATTEMPTS. Throw an exception.
throw std::runtime_error("Unable to mint a new Zerocoin (too many attempts)");
// Generate a random number "r" in the range 0...{q-1}
CBigNum r = CBigNum::randBignum(this->params->coinCommitmentGroup.groupOrder);

// Manually compute a Pedersen commitment to the serial number "s" under randomness "r"
// C = g^s * h^r mod p
CBigNum commitmentValue = this->params->coinCommitmentGroup.g.pow_mod(s, this->params->coinCommitmentGroup.modulus).mul_mod(this->params->coinCommitmentGroup.h.pow_mod(r, this->params->coinCommitmentGroup.modulus), this->params->coinCommitmentGroup.modulus);

// Repeat this process up to MAX_COINMINT_ATTEMPTS times until
// we obtain a prime number
for (uint32_t attempt = 0; attempt < MAX_COINMINT_ATTEMPTS; attempt++) {
// First verify that the commitment is a prime number
// in the appropriate range. If not, we'll throw this coin
// away and generate a new one.
if (commitmentValue.isPrime(ZEROCOIN_MINT_PRIME_PARAM) &&
commitmentValue >= params->accumulatorParams.minCoinValue &&
commitmentValue <= params->accumulatorParams.maxCoinValue) {
// Found a valid coin. Store it.
this->serialNumber = s;
this->randomness = r;
this->publicCoin = PublicCoin(params, commitmentValue, denomination);
this->privkey = key.GetPrivKey();
this->version = 2;

// Success! We're done.
return;
}

// Generate a new random "r_delta" in 0...{q-1}
CBigNum r_delta = CBigNum::randBignum(this->params->coinCommitmentGroup.groupOrder);

// The commitment was not prime. Increment "r" and recalculate "C":
// r = r + r_delta mod q
// C = C * h mod p
r = (r + r_delta) % this->params->coinCommitmentGroup.groupOrder;
commitmentValue = commitmentValue.mul_mod(this->params->coinCommitmentGroup.h.pow_mod(r_delta, this->params->coinCommitmentGroup.modulus), this->params->coinCommitmentGroup.modulus);
}

// We only get here if we did not find a coin within
// MAX_COINMINT_ATTEMPTS. Throw an exception.
throw std::runtime_error("Unable to mint a new Zerocoin (too many attempts)");
}

int ExtractVersionFromSerial(const CBigNum& bnSerial)
{
//Serial is marked as v2 only if the first byte is 0xF
//Serial is marked as v2 only if the first byte is 0xF
//std::cout << "uint256 serial: " << bnSerial.getuint256().GetHex() << std::endl;
uint256 nMark = bnSerial.getuint256() >> (256 - PrivateCoin::V2_BITSHIFT);
//std::cout << "nMark: " << nMark.GetHex() << std::endl;
if (nMark == 0xf)
return PrivateCoin::PUBKEY_VERSION;
uint256 nMark = bnSerial.getuint256() >> (256 - PrivateCoin::V2_BITSHIFT);
//std::cout << "nMark: " << nMark.GetHex() << std::endl;
if (nMark == 0xf)
return PrivateCoin::PUBKEY_VERSION;

return 1;
return 1;
}

//Remove the first four bits for V2 serials
Expand Down
4 changes: 2 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,7 @@ void RelayInv(CInv& inv)
{
LOCK(cs_vNodes);
BOOST_FOREACH (CNode* pnode, vNodes){
if((pnode->nServices == NODE_BLOOM_WITHOUT_MN || pnode->nServices == NODE_BLOOM_LIGHT_ZC) && inv.IsMasterNodeType())continue;
if((pnode->nServices == NODE_BLOOM_WITHOUT_MN || pnode->nServices == NODE_BLOOM_LIGHT_ZC) && inv.IsMasterNodeType())continue;
if (pnode->nVersion >= ActiveProtocol())
pnode->PushInventory(inv);
}
Expand Down Expand Up @@ -2176,7 +2176,7 @@ void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
Fuzz(GetArg("-fuzzmessagestest", 10));

if (ssSend.size() == 0) {
LEAVE_CRITICAL_SECTION(cs_vSend);
LEAVE_CRITICAL_SECTION(cs_vSend);
return;
}

Expand Down
10 changes: 5 additions & 5 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ enum {
// but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
NODE_BLOOM = (1 << 2),

// NODE_BLOOM_WITHOUT_MN means the node has the same features as NODE_BLOOM with the only difference
// that the node doens't want to receive master nodes messages. (the 1<<3 was not picked as constant because on bitcoin 0.14 is witness and we want that update here )
// NODE_BLOOM_WITHOUT_MN means the node has the same features as NODE_BLOOM with the only difference
// that the node doens't want to receive master nodes messages. (the 1<<3 was not picked as constant because on bitcoin 0.14 is witness and we want that update here )

NODE_BLOOM_WITHOUT_MN = (1 << 4),
NODE_BLOOM_WITHOUT_MN = (1 << 4),


// NODE_BLOOM_LIGHT_ZC means the node has the same feature as NODE_BLOOM_WITHOUT_MN with the addition of
// support for the light zerocoin protocol.
// NODE_BLOOM_LIGHT_ZC means the node has the same feature as NODE_BLOOM_WITHOUT_MN with the addition of
// support for the light zerocoin protocol.
NODE_BLOOM_LIGHT_ZC = (1 << 5),

// Bits 24-31 are reserved for temporary experiments. Just pick a bit that
Expand Down