From 2b002a7e60bd3fd037f2b4352bb3d4794df6b21d Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Sat, 14 Apr 2018 22:33:41 -0600 Subject: [PATCH] Add implementation of Sapling merkle tree --- src/zcash/IncrementalMerkleTree.cpp | 33 +++++++++++++++++++++++++++++ src/zcash/IncrementalMerkleTree.hpp | 20 +++++++++++++++++ src/zcash/Zcash.h | 2 ++ 3 files changed, 55 insertions(+) diff --git a/src/zcash/IncrementalMerkleTree.cpp b/src/zcash/IncrementalMerkleTree.cpp index 3101cf3f3..f8d5fad61 100644 --- a/src/zcash/IncrementalMerkleTree.cpp +++ b/src/zcash/IncrementalMerkleTree.cpp @@ -5,9 +5,36 @@ #include "zcash/IncrementalMerkleTree.hpp" #include "crypto/sha256.h" #include "zcash/util.h" +#include "librustzcash.h" namespace libzcash { +PedersenHash PedersenHash::combine( + const PedersenHash& a, + const PedersenHash& b, + size_t depth +) +{ + PedersenHash res = PedersenHash(); + + librustzcash_merkle_hash( + depth, + a.begin(), + b.begin(), + res.begin() + ); + + return res; +} + +PedersenHash PedersenHash::uncommitted() { + PedersenHash res = PedersenHash(); + + librustzcash_tree_uncommitted(res.begin()); + + return res; +} + SHA256Compress SHA256Compress::combine( const SHA256Compress& a, const SHA256Compress& b, @@ -327,4 +354,10 @@ template class IncrementalMerkleTree; template class IncrementalWitness; +template class IncrementalMerkleTree; +template class IncrementalMerkleTree; + +template class IncrementalWitness; +template class IncrementalWitness; + } // end namespace `libzcash` diff --git a/src/zcash/IncrementalMerkleTree.hpp b/src/zcash/IncrementalMerkleTree.hpp index ce536fec4..fc476cd29 100644 --- a/src/zcash/IncrementalMerkleTree.hpp +++ b/src/zcash/IncrementalMerkleTree.hpp @@ -224,6 +224,20 @@ public: } }; +class PedersenHash : public uint256 { +public: + PedersenHash() : uint256() {} + PedersenHash(uint256 contents) : uint256(contents) { } + + static PedersenHash combine( + const PedersenHash& a, + const PedersenHash& b, + size_t depth + ); + + static PedersenHash uncommitted(); +}; + template EmptyMerkleRoots IncrementalMerkleTree::emptyroots; @@ -235,4 +249,10 @@ typedef libzcash::IncrementalMerkleTree ZCIncrementalWitness; typedef libzcash::IncrementalWitness ZCTestingIncrementalWitness; +typedef libzcash::IncrementalMerkleTree ZCSaplingIncrementalMerkleTree; +typedef libzcash::IncrementalMerkleTree ZCSaplingTestingIncrementalMerkleTree; + +typedef libzcash::IncrementalWitness ZCSaplingIncrementalWitness; +typedef libzcash::IncrementalWitness ZCSaplingTestingIncrementalWitness; + #endif /* ZC_INCREMENTALMERKLETREE_H_ */ diff --git a/src/zcash/Zcash.h b/src/zcash/Zcash.h index 9e6684475..bb805eef5 100644 --- a/src/zcash/Zcash.h +++ b/src/zcash/Zcash.h @@ -6,6 +6,8 @@ #define INCREMENTAL_MERKLE_TREE_DEPTH 29 #define INCREMENTAL_MERKLE_TREE_DEPTH_TESTING 4 +#define SAPLING_INCREMENTAL_MERKLE_TREE_DEPTH 32 + #define ZC_NOTEPLAINTEXT_LEADING 1 #define ZC_V_SIZE 8 #define ZC_RHO_SIZE 32