Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Pour symmetry and variable input coin arity #19

Closed
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ SRCS= \
$(LIBZEROCASH)/CoinCommitment.cpp \
$(LIBZEROCASH)/Coin.cpp \
$(LIBZEROCASH)/MintTransaction.cpp \
$(LIBZEROCASH)/PourInput.cpp \
$(LIBZEROCASH)/PourOutput.cpp \
$(LIBZEROCASH)/PourTransaction.cpp \
$(LIBZEROCASH)/ZerocashParams.cpp \
$(TESTUTILS)/timer.cpp
Expand Down
44 changes: 44 additions & 0 deletions libzerocash/PourInput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** @file
*****************************************************************************

Implementation of interfaces for the class PourInput.

See PourInput.h .

*****************************************************************************
* @author This file is part of libzerocash, developed by the Zerocash
* project and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#include "IncrementalMerkleTree.h"
#include "PourInput.h"

namespace libzerocash {

PourInput::PourInput(int tree_depth): old_coin(), old_address(), merkle_index(), path() {
this->old_coin = Coin(this->old_address.getPublicAddress(), 0);

// dummy merkle tree
IncrementalMerkleTree merkleTree(tree_depth);

// commitment from coin
std::vector<bool> commitment(cm_size * 8);
convertBytesVectorToVector(this->old_coin.getCoinCommitment().getCommitmentValue(), commitment);

// insert commitment into the merkle tree
std::vector<bool> index;
merkleTree.insertElement(commitment, index);

merkleTree.getWitness(index, this->path);

this->merkle_index = 1;
}

PourInput::PourInput(Coin old_coin,
Address old_address,
size_t merkle_index,
merkle_authentication_path path) : old_coin(old_coin), old_address(old_address), merkle_index(merkle_index), path(path) {
};

} /* namespace libzerocash */
37 changes: 37 additions & 0 deletions libzerocash/PourInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** @file
*****************************************************************************

Declaration of interfaces for the class PourInput.

*****************************************************************************
* @author This file is part of libzerocash, developed by the Zerocash
* project and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef POURINPUT_H_
#define POURINPUT_H_

#include "Coin.h"
#include "ZerocashParams.h"

namespace libzerocash {

class PourInput {
public:
PourInput(int tree_depth);

PourInput(Coin old_coin,
Address old_address,
size_t merkle_index,
merkle_authentication_path path);

Coin old_coin;
Address old_address;
size_t merkle_index;
merkle_authentication_path path;
};

} /* namespace libzerocash */

#endif /* POURINPUT_H_ */
29 changes: 29 additions & 0 deletions libzerocash/PourOutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** @file
*****************************************************************************

Implementation of interfaces for the class PourOutput.

See PourOutput.h .

*****************************************************************************
* @author This file is part of libzerocash, developed by the Zerocash
* project and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#include "PourOutput.h"

namespace libzerocash {

PourOutput::PourOutput(uint64_t val) {
Address dummy_to_address;

this->to_address = dummy_to_address.getPublicAddress();
this->new_coin = Coin(dummy_to_address.getPublicAddress(), val);
}

PourOutput::PourOutput(const Coin new_coin,
const PublicAddress to_address) : new_coin(new_coin), to_address(to_address) {
}

} /* namespace libzerocash */
32 changes: 32 additions & 0 deletions libzerocash/PourOutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** @file
*****************************************************************************

Declaration of interfaces for the class PourOutput.

*****************************************************************************
* @author This file is part of libzerocash, developed by the Zerocash
* project and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef POUROUTPUT_H_
#define POUROUTPUT_H_

#include "Coin.h"
#include "ZerocashParams.h"

namespace libzerocash {

class PourOutput {
public:
PourOutput(uint64_t val);
PourOutput(const Coin new_coin,
const PublicAddress to_address);

Coin new_coin;
PublicAddress to_address;
};

} /* namespace libzerocash */

#endif /* POUROUTPUT_H_ */
113 changes: 96 additions & 17 deletions libzerocash/PourTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ using CryptoPP::PK_EncryptorFilter;

#include "Zerocash.h"
#include "PourTransaction.h"
#include "PourInput.h"
#include "PourOutput.h"

#include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
#include "zerocash_pour_ppzksnark/zerocash_pour_gadget.hpp"
Expand All @@ -43,6 +45,50 @@ PourTransaction::PourTransaction(): cm_1(), cm_2() {

}

PourTransaction::PourTransaction(ZerocashParams& params,
const std::vector<unsigned char>& pubkeyHash,
const MerkleRootType& rt,
std::vector<PourInput> inputs,
std::vector<PourOutput> outputs,
uint64_t vpub_in,
uint64_t vpub_out
) :
publicInValue(v_size), publicOutValue(v_size), serialNumber_1(sn_size), serialNumber_2(sn_size), MAC_1(h_size), MAC_2(h_size)
{
if (inputs.size() > 2 || outputs.size() > 2) {
throw std::length_error("Too many inputs or outputs specified");
}

while (inputs.size() < 2) {
// Push a dummy input of value 0.
inputs.push_back(PourInput(params.getTreeDepth()));
}

while (outputs.size() < 2) {
// Push a dummy output of value 0.
outputs.push_back(PourOutput(0));
}

init(1,
params,
rt,
inputs[0].old_coin,
inputs[1].old_coin,
inputs[0].old_address,
inputs[1].old_address,
inputs[0].merkle_index,
inputs[1].merkle_index,
inputs[0].path,
inputs[1].path,
outputs[0].to_address,
outputs[1].to_address,
vpub_in,
vpub_out,
pubkeyHash,
outputs[0].new_coin,
outputs[1].new_coin);
}

PourTransaction::PourTransaction(uint16_t version_num,
ZerocashParams& params,
const MerkleRootType& rt,
Expand All @@ -56,15 +102,40 @@ PourTransaction::PourTransaction(uint16_t version_num,
const merkle_authentication_path& patMAC_2,
const PublicAddress& addr_1_new,
const PublicAddress& addr_2_new,
uint64_t v_pub,
uint64_t v_pub_in,
uint64_t v_pub_out,
const std::vector<unsigned char>& pubkeyHash,
const Coin& c_1_new,
const Coin& c_2_new) :
publicValue(v_size), serialNumber_1(sn_size), serialNumber_2(sn_size), MAC_1(h_size), MAC_2(h_size)
publicInValue(v_size), publicOutValue(v_size), serialNumber_1(sn_size), serialNumber_2(sn_size), MAC_1(h_size), MAC_2(h_size)
{
init(version_num, params, rt, c_1_old, c_2_old, addr_1_old, addr_2_old, patMerkleIdx_1, patMerkleIdx_2,
patMAC_1, patMAC_2, addr_1_new, addr_2_new, v_pub_in, v_pub_out, pubkeyHash, c_1_new, c_2_new);
}

void PourTransaction::init(uint16_t version_num,
ZerocashParams& params,
const MerkleRootType& rt,
const Coin& c_1_old,
const Coin& c_2_old,
const Address& addr_1_old,
const Address& addr_2_old,
const size_t patMerkleIdx_1,
const size_t patMerkleIdx_2,
const merkle_authentication_path& patMAC_1,
const merkle_authentication_path& patMAC_2,
const PublicAddress& addr_1_new,
const PublicAddress& addr_2_new,
uint64_t v_pub_in,
uint64_t v_pub_out,
const std::vector<unsigned char>& pubkeyHash,
const Coin& c_1_new,
const Coin& c_2_new)
{
this->version = version_num;

convertIntToBytesVector(v_pub, this->publicValue);
convertIntToBytesVector(v_pub_in, this->publicInValue);
convertIntToBytesVector(v_pub_out, this->publicOutValue);

this->cm_1 = c_1_new.getCoinCommitment();
this->cm_2 = c_2_new.getCoinCommitment();
Expand All @@ -84,7 +155,8 @@ PourTransaction::PourTransaction(uint16_t version_num,
std::vector<bool> nonce_old_2_bv(rho_size * 8);
std::vector<bool> val_new_1_bv(v_size * 8);
std::vector<bool> val_new_2_bv(v_size * 8);
std::vector<bool> val_pub_bv(v_size * 8);
std::vector<bool> val_in_pub_bv(v_size * 8);
std::vector<bool> val_out_pub_bv(v_size * 8);
std::vector<bool> val_old_1_bv(v_size * 8);
std::vector<bool> val_old_2_bv(v_size * 8);
std::vector<bool> cm_new_1_bv(cm_size * 8);
Expand Down Expand Up @@ -129,7 +201,8 @@ PourTransaction::PourTransaction(uint16_t version_num,
convertIntToBytesVector(c_2_new.getValue(), v_new_2_conv);
libzerocash::convertBytesVectorToVector(v_new_2_conv, val_new_2_bv);

convertBytesVectorToVector(this->publicValue, val_pub_bv);
convertBytesVectorToVector(this->publicInValue, val_in_pub_bv);
convertBytesVectorToVector(this->publicOutValue, val_out_pub_bv);

std::vector<bool> nonce_old_1(rho_size * 8);
copy(nonce_old_1_bv.begin(), nonce_old_1_bv.end(), nonce_old_1.begin());
Expand Down Expand Up @@ -196,7 +269,7 @@ PourTransaction::PourTransaction(uint16_t version_num,
convertVectorToBytesVector(MAC_2_bv, this->MAC_2);

if(this->version > 0){
zerocash_pour_proof<ZerocashParams::zerocash_pp> proofObj = zerocash_pour_ppzksnark_prover<ZerocashParams::zerocash_pp>(params.getProvingKey(),
auto proofObj = zerocash_pour_ppzksnark_prover<ZerocashParams::zerocash_pp>(params.getProvingKey(),
{ patMAC_1, patMAC_2 },
{ patMerkleIdx_1, patMerkleIdx_2 },
root_bv,
Expand All @@ -207,14 +280,15 @@ PourTransaction::PourTransaction(uint16_t version_num,
{ nonce_new_1_bv, nonce_new_2_bv },
{ nonce_old_1_bv, nonce_old_2_bv },
{ val_new_1_bv, val_new_2_bv },
val_pub_bv,
val_in_pub_bv,
val_out_pub_bv,
{ val_old_1_bv, val_old_2_bv },
h_S_bv);

std::stringstream ss;
ss << proofObj;
this->zkSNARK = ss.str();
}else{
} else {
this->zkSNARK = std::string(1235,'A');
}

Expand Down Expand Up @@ -296,7 +370,8 @@ bool PourTransaction::verify(ZerocashParams& params,
if (pubkeyHash.size() != h_size) { return false; }
if (this->serialNumber_1.size() != sn_size) { return false; }
if (this->serialNumber_2.size() != sn_size) { return false; }
if (this->publicValue.size() != v_size) { return false; }
if (this->publicInValue.size() != v_size) { return false; }
if (this->publicOutValue.size() != v_size) { return false; }
if (this->MAC_1.size() != h_size) { return false; }
if (this->MAC_2.size() != h_size) { return false; }

Expand All @@ -305,7 +380,8 @@ bool PourTransaction::verify(ZerocashParams& params,
std::vector<bool> sn_old_2_bv(sn_size * 8);
std::vector<bool> cm_new_1_bv(cm_size * 8);
std::vector<bool> cm_new_2_bv(cm_size * 8);
std::vector<bool> val_pub_bv(v_size * 8);
std::vector<bool> val_in_pub_bv(v_size * 8);
std::vector<bool> val_out_pub_bv(v_size * 8);
std::vector<bool> MAC_1_bv(h_size * 8);
std::vector<bool> MAC_2_bv(h_size * 8);

Expand All @@ -314,7 +390,8 @@ bool PourTransaction::verify(ZerocashParams& params,
convertBytesVectorToVector(this->serialNumber_2, sn_old_2_bv);
convertBytesVectorToVector(this->cm_1.getCommitmentValue(), cm_new_1_bv);
convertBytesVectorToVector(this->cm_2.getCommitmentValue(), cm_new_2_bv);
convertBytesVectorToVector(this->publicValue, val_pub_bv);
convertBytesVectorToVector(this->publicInValue, val_in_pub_bv);
convertBytesVectorToVector(this->publicOutValue, val_out_pub_bv);
convertBytesVectorToVector(this->MAC_1, MAC_1_bv);
convertBytesVectorToVector(this->MAC_2, MAC_2_bv);

Expand All @@ -339,7 +416,8 @@ bool PourTransaction::verify(ZerocashParams& params,
root_bv,
{ sn_old_1_bv, sn_old_2_bv },
{ cm_new_1_bv, cm_new_2_bv },
val_pub_bv,
val_in_pub_bv,
val_out_pub_bv,
h_S_bv,
{ MAC_1_bv, MAC_2_bv },
proof_SNARK);
Expand Down Expand Up @@ -377,11 +455,12 @@ const CoinCommitmentValue& PourTransaction::getNewCoinCommitmentValue2() const{
return this->cm_2.getCommitmentValue();
}

/**
* Returns the amount of money this transaction converts back into basecoin.
*/
uint64_t PourTransaction::getMonetaryValueOut() const{
return convertBytesVectorToInt(this->publicValue);
uint64_t PourTransaction::getPublicValueIn() const{
return convertBytesVectorToInt(this->publicInValue);
}

uint64_t PourTransaction::getPublicValueOut() const{
return convertBytesVectorToInt(this->publicOutValue);
}

} /* namespace libzerocash */
Loading