Skip to content

Commit

Permalink
Add PourProver interface for slowly morphing the API into something t…
Browse files Browse the repository at this point in the history
…hat zcash can interact with nicer. This is obviously only a step in the right direction.
  • Loading branch information
ebfull committed Dec 30, 2015
1 parent b67adce commit e79cd2d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SRCS= \
$(LIBZEROCASH)/MintTransaction.cpp \
$(LIBZEROCASH)/PourInput.cpp \
$(LIBZEROCASH)/PourOutput.cpp \
$(LIBZEROCASH)/PourProver.cpp \
$(LIBZEROCASH)/PourTransaction.cpp \
$(LIBZEROCASH)/ZerocashParams.cpp \
$(TESTUTILS)/timer.cpp
Expand Down
1 change: 1 addition & 0 deletions libzerocash/CoinCommitment.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace libzerocash {
class CoinCommitment {

friend class PourTransaction;
friend class PourProver;

public:
CoinCommitment();
Expand Down
12 changes: 12 additions & 0 deletions libzerocash/PourProver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @file
*****************************************************************************
Implementation of interfaces for the class PourProver.
*****************************************************************************
* @author This file is part of libzerocash, developed by the Zerocash
* project and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#include "PourProver.h"

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

"New newline at end of file."

65 changes: 65 additions & 0 deletions libzerocash/PourProver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/** @file
*****************************************************************************
Declaration of interfaces for the class PourProver.
*****************************************************************************
* @author This file is part of libzerocash, developed by the Zerocash
* project and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef POURPROVER_H_
#define POURPROVER_H_

#include "ZerocashParams.h"
#include "boost/array.hpp"
#include "PourTransaction.h"
#include "CoinCommitment.h"

namespace libzerocash {

class PourProver {
public:
static bool VerifyProof(
ZerocashParams& params,
const std::vector<unsigned char>& pubkeyHash,
const std::vector<unsigned char>& rt,
const uint64_t vpub_old,
const uint64_t vpub_new,

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

Fix indentation. (Note: Fixing indentation always involves removing all tab characters.)

const boost::array<std::vector<unsigned char>, 2> serials,
const boost::array<std::vector<unsigned char>, 2> commitments,
const boost::array<std::vector<unsigned char>, 2> macs,

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

Replace 2's with NUM_POUR_INPUTS or NUM_POUR_OUTPUTS.

const std::string &zkSNARK
) {
PourTransaction pourtx;

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

Why do we mutate pourtx rather than constructing it as a valid state?

This comment has been minimized.

Copy link
@ebfull

ebfull Jan 14, 2016

Author

The PourTransaction constructor creates a pour proof right now, we want to just place the internal fields. (We're getting rid of PourTransaction if I have my druthers.)


pourtx.version = 1;
pourtx.publicOldValue.resize(8);
pourtx.publicNewValue.resize(8);
convertIntToBytesVector(vpub_old, pourtx.publicOldValue);
convertIntToBytesVector(vpub_new, pourtx.publicNewValue);

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

Fix indentation (by removing tab chars and using indent level of 4).

pourtx.serialNumber_1 = serials[0];
pourtx.serialNumber_2 = serials[1];
{
CoinCommitment cm;
cm.commitmentValue = commitments[0];
pourtx.cm_1 = cm;
}
{
CoinCommitment cm;
cm.commitmentValue = commitments[1];
pourtx.cm_2 = cm;
}
pourtx.MAC_1 = macs[0];
pourtx.MAC_2 = macs[1];
pourtx.zkSNARK = zkSNARK;

return pourtx.verify(params, pubkeyHash, rt);
}
};


}

#endif /* POURPROVER_H_ */
2 changes: 1 addition & 1 deletion libzerocash/PourTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void PourTransaction::init(uint16_t version_num,
}

bool PourTransaction::verify(ZerocashParams& params,
std::vector<unsigned char> &pubkeyHash,
const std::vector<unsigned char> &pubkeyHash,

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

Nice catch.

const MerkleRootType &merkleRoot) const
{
if(this->version == 0){
Expand Down
48 changes: 47 additions & 1 deletion libzerocash/PourTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "PourInput.h"
#include "PourOutput.h"
#include <stdexcept>
#include <bitset>

#include <boost/array.hpp>

typedef std::vector<unsigned char> CoinCommitmentValue;

Expand All @@ -26,6 +29,7 @@ namespace libzerocash {
/***************************** Pour transaction ******************************/

class PourTransaction {
friend class PourProver;
public:
PourTransaction();
PourTransaction(ZerocashParams& params,
Expand Down Expand Up @@ -104,7 +108,7 @@ class PourTransaction {
* @return ture if correct, false otherwise.
*/
bool verify(ZerocashParams& params,
std::vector<unsigned char> &pubkeyHash,
const std::vector<unsigned char> &pubkeyHash,
const MerkleRootType &merkleRoot) const;

const std::vector<unsigned char>& getSpentSerial1() const;
Expand All @@ -130,6 +134,48 @@ class PourTransaction {

uint64_t getPublicValueOut() const;

std::string unpack(boost::array<std::vector<unsigned char>, 2>& serials,
boost::array<std::vector<unsigned char>, 2>& commitments,
boost::array<std::vector<unsigned char>, 2>& macs,
boost::array<std::string, 2>& ciphertexts

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

Replace 2's with named constants.

) const {
serials[0] = this->serialNumber_1;
serials[1] = this->serialNumber_2;
commitments[0] = this->cm_1.getCommitmentValue();
commitments[1] = this->cm_2.getCommitmentValue();
macs[0] = this->MAC_1;
macs[1] = this->MAC_2;
ciphertexts[0] = this->ciphertext_1;
ciphertexts[1] = this->ciphertext_2;

return this->zkSNARK;
}

// just hashes a few fields to see if integrity is correct.
// useful for debugging since there's such bad error handling
// currently
void debug_print() {
#define DEBUG_PRINT_POUR_FIELD(X, NAME) {\
std::hash<std::string> h; \
std::cout << NAME << ": " << h(std::string(X.begin(), X.end())) << std::endl;\
}

DEBUG_PRINT_POUR_FIELD(publicOldValue, "publicOldValue");
DEBUG_PRINT_POUR_FIELD(publicNewValue, "publicNewValue");
DEBUG_PRINT_POUR_FIELD(serialNumber_1, "serialNumber_1");
DEBUG_PRINT_POUR_FIELD(serialNumber_2, "serialNumber_2");
{
auto v = cm_1.getCommitmentValue();
DEBUG_PRINT_POUR_FIELD(v, "cm_1");
}
{
auto v = cm_2.getCommitmentValue();
DEBUG_PRINT_POUR_FIELD(v, "cm_2");
}
DEBUG_PRINT_POUR_FIELD(MAC_1, "MAC_1");
DEBUG_PRINT_POUR_FIELD(MAC_2, "MAC_2");

}

private:

Expand Down

1 comment on commit e79cd2d

@nathan-at-least
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except for whitespace and renaming 2's to named constants.

Please sign in to comment.