Skip to content
Permalink
Browse files

wip

  • Loading branch information...
mariogemoll committed Feb 7, 2019
1 parent 1aa1e0c commit 918a13dd8c6090ccc055ac93e1e6c739fcde0494
Showing with 2,128 additions and 55 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 .gitmodules
  3. +3 −0 cpp/CMakeLists.txt
  4. +1 −1 cpp/depends/CMakeLists.txt
  5. +78 −1 cpp/src/CMakeLists.txt
  6. +23 −22 cpp/src/MerkleTree.cpp
  7. +7 −7 cpp/src/MerkleTree.hpp
  8. +161 −0 cpp/src/circuit_test.cpp
  9. +88 −0 cpp/src/circuitry/gadgets/prfs.h
  10. +176 −0 cpp/src/circuits/NoteAdditionCircuit.hpp
  11. +105 −0 cpp/src/circuits/NoteInclusionCircuit.hpp
  12. 0 cpp/src/{examples/merkle_tree → circuits/gadgets}/MTLeafAddition.hpp
  13. 0 cpp/src/{examples/merkle_tree → circuits/gadgets}/MTLeafAddition.tcc
  14. 0 cpp/src/{examples/merkle_tree → circuits/gadgets}/MTLeafAdditionPacked.hpp
  15. 0 cpp/src/{examples/merkle_tree → circuits/gadgets}/MTLeafAdditionPacked.tcc
  16. +78 −0 cpp/src/circuits/gadgets/prfs.hpp
  17. +47 −0 cpp/src/compat/byteswap.h
  18. +196 −0 cpp/src/compat/endian.h
  19. +111 −0 cpp/src/crypto/common.h
  20. +199 −0 cpp/src/crypto/sha256.cpp
  21. +32 −0 cpp/src/crypto/sha256.h
  22. +24 −0 cpp/src/dummy.cpp
  23. +1 −1 cpp/src/examples/merkle_tree/AdditionCircuit.tcc
  24. +6 −5 cpp/src/examples/merkle_tree/MerkleTreeServer.tcc
  25. +0 −12 cpp/src/examples/merkle_tree/server.cpp
  26. 0 cpp/src/{util.hpp → file_output.hpp}
  27. +33 −0 cpp/src/hash/sha256_compression.cpp
  28. +11 −0 cpp/src/hash/sha256_compression.h
  29. +64 −0 cpp/src/playground.cpp
  30. +25 −0 cpp/src/prfs.cpp
  31. +9 −0 cpp/src/prfs.h
  32. +85 −0 cpp/src/util.cpp
  33. +16 −0 cpp/src/util.h
  34. +1 −0 cpp/test.txt
  35. +24 −0 cpp/test/CMakeLists.txt
  36. +263 −0 cpp/test/aux/pypy_sha256.py
  37. +22 −0 cpp/test/aux/sha256_compress.py
  38. +8 −0 cpp/test/dummy.cpp
  39. +8 −0 cpp/test/main.cpp
  40. +127 −0 cpp/test/sha256_compression.cpp
  41. +67 −0 cpp/test/util.cpp
  42. +19 −0 hashtest.py
  43. +6 −6 js/package.json
@@ -1,3 +1,4 @@
.idea
.vscode
*.pyc
/data
@@ -4,3 +4,6 @@
[submodule "cpp/depends/gtest"]
path = cpp/depends/gtest
url = https://github.com/google/googletest.git
[submodule "cpp/depends/zcash"]
path = cpp/depends/zcash
url = https://github.com/zcash/zcash.git
@@ -97,7 +97,10 @@ else()
add_definitions(-DNO_PROCPS)
endif()

add_definitions(-DNDEBUG=0)

include_directories(.)

add_subdirectory(depends)
add_subdirectory(src)
add_subdirectory(test)
@@ -1 +1 @@
add_subdirectory(libsnark)
add_subdirectory(libsnark)
@@ -34,4 +34,81 @@ target_include_directories(
${DEPENDS_DIR}/libsnark/depends/libfqfft
)

add_subdirectory(examples)
add_executable(
circuit_test

circuit_test.cpp
serialization.cpp
MerkleTree.cpp
../depends/zcash/src/zcash/Address.cpp
../depends/zcash/src/zcash/prf.cpp
../depends/zcash/src/zcash/NoteEncryption.cpp
../depends/zcash/src/zcash/Note.cpp
../depends/zcash/src/zcash/util.cpp
../depends/zcash/src/support/cleanse.cpp
../depends/zcash/src/crypto/sha256.cpp
../depends/zcash/src/uint256.cpp
../depends/zcash/src/utilstrencodings.cpp
)
target_link_libraries(
circuit_test

snark
sodium
)
target_include_directories(
circuit_test

PUBLIC
${DEPENDS_DIR}/libsnark
${DEPENDS_DIR}/libsnark/depends/libfqfft
)

add_executable(
playground

playground.cpp
crypto/sha256.cpp
)
target_link_libraries(
playground

snark
)
target_include_directories(
playground

PUBLIC
${DEPENDS_DIR}/libsnark
${DEPENDS_DIR}/libsnark/depends/libff
${DEPENDS_DIR}/libsnark/depends/libfqfft
)

add_subdirectory(examples)
include_directories(.)
add_library(zktrade "")

target_sources(
zktrade

PRIVATE
util.cpp
crypto/sha256.cpp
hash/sha256_compression.cpp
prfs.cpp
PUBLIC
prfs.h
util.h
circuitry/gadgets/prfs.h
hash/sha256_compression.h
)

target_include_directories(
zktrade

PUBLIC
.
${DEPENDS_DIR}/libsnark
${DEPENDS_DIR}/libsnark/depends/libff
${DEPENDS_DIR}/libsnark/depends/libfqfft
)
@@ -41,11 +41,12 @@ bool returnFalse() {
MerkleTree::MerkleTree(size_t d) :
depth{d}
{
cout << "MT CONSTRUCTOR" << endl;
max_size = exp2(depth);
empty_tree_roots = new bit_vector[depth];
node_levels = new vector<bit_vector>[depth];
leaves = new vector<bit_vector>();
for (size_t i = 0; i < depth; i++) {
for (uint i = 0; i < depth; i++) {
bit_vector prev;
if (i == 0) {
prev = all_zeros;
@@ -61,25 +62,25 @@ bit_vector MerkleTree::root() {
return node_levels[depth - 1][0];
}

size_t MerkleTree::num_elements() {
uint MerkleTree::num_elements() {
return leaves->size();
}

bit_vector MerkleTree::operator[](size_t i) {
bit_vector MerkleTree::operator[](uint i) {
return leaves->at(i);
}

size_t MerkleTree::add(bit_vector leaf) {
const size_t address = leaves->size();
uint MerkleTree::add(bit_vector leaf) {
const uint address = leaves->size();
if (leaves->size() >= max_size) {
throw overflow_error("Tree is full");
}
leaves->push_back(leaf);
size_t prev_level_pos = address;
for (size_t level = 0; level < depth; level++) {
uint prev_level_pos = address;
for (uint level = 0; level < depth; level++) {
// First we need to find out which element at the current level gets updated
// (position at previous level divided by 2).
const size_t pos = prev_level_pos / 2;
const uint pos = prev_level_pos / 2;
// We store the values of all the nodes, however most of them will only have
// empty subtrees and therefore have the standard values. We only need to
// store the actually computed hash values. On each level these will grow
@@ -115,10 +116,10 @@ size_t MerkleTree::add(bit_vector leaf) {
return address;
}

vector<bit_vector> MerkleTree::path(size_t address) {
vector<bit_vector> MerkleTree::path(uint address) {
vector<bit_vector> p;
size_t prev_pos;
for (size_t row = 0; row < depth; row++) {
uint prev_pos;
for (uint row = 0; row < depth; row++) {
bit_vector sibling;
if (row == 0) {
if (address % 2) {
@@ -129,7 +130,7 @@ vector<bit_vector> MerkleTree::path(size_t address) {
}
prev_pos = address;
} else {
size_t pos = prev_pos / 2;
uint pos = prev_pos / 2;
if (pos % 2) {
// right side
sibling = internal_node_at(row, pos - 1);
@@ -143,12 +144,12 @@ vector<bit_vector> MerkleTree::path(size_t address) {
return p;
}

tuple<size_t, bit_vector, vector<bit_vector>> MerkleTree::simulate_add(bit_vector leaf) {
size_t address = leaves->size();
tuple<uint, bit_vector, vector<bit_vector>> MerkleTree::simulate_add(bit_vector leaf) {
uint address = leaves->size();
vector<bit_vector> p;
size_t prev_pos;
uint prev_pos;
bit_vector prev_hash;
for (size_t row = 0; row < depth; row++) {
for (uint row = 0; row < depth; row++) {
bit_vector sibling;
bit_vector hash;
if (row == 0) {
@@ -162,7 +163,7 @@ tuple<size_t, bit_vector, vector<bit_vector>> MerkleTree::simulate_add(bit_vecto
}
prev_pos = address;
} else {
size_t pos = prev_pos / 2;
uint pos = prev_pos / 2;
if (pos % 2) {
// right side
sibling = internal_node_at(row, pos - 1);
@@ -180,7 +181,7 @@ tuple<size_t, bit_vector, vector<bit_vector>> MerkleTree::simulate_add(bit_vecto
return make_tuple(address, prev_hash, p);
}

bit_vector MerkleTree::internal_node_at(size_t row, size_t pos) {
bit_vector MerkleTree::internal_node_at(uint row, uint pos) {
if (row == 0) {
if (pos + 1 <= leaves->size()) {
return leaves->at(pos);
@@ -189,7 +190,7 @@ bit_vector MerkleTree::internal_node_at(size_t row, size_t pos) {
}
} else {
// row 0 is the leaves, row 1 is (node_level) level 0 etc.
size_t level = row - 1;
uint level = row - 1;
if (pos + 1 <= node_levels[level].size()) {
return node_levels[level][pos];
} else {
@@ -200,8 +201,8 @@ bit_vector MerkleTree::internal_node_at(size_t row, size_t pos) {

void MerkleTree::print() {
for (int level = depth - 1; level >= 0; level--) {
size_t num_elems = exp2(depth - level - 1);
for (size_t pos = 0; pos < num_elems; pos++) {
uint num_elems = exp2(depth - level - 1);
for (uint pos = 0; pos < num_elems; pos++) {
if (node_levels[level].size() - 1 >= pos) {
printnode(node_levels[level][pos]);
} else {
@@ -210,7 +211,7 @@ void MerkleTree::print() {
}
cout << endl;
}
for (size_t pos = 0; pos < max_size; pos++) {
for (uint pos = 0; pos < max_size; pos++) {
if (leaves->size() - 1 >= pos) {
printnode(leaves->at(pos));
} else {
@@ -4,17 +4,17 @@ class MerkleTree {
public:
MerkleTree(size_t depth);
libff::bit_vector root();
libff::bit_vector operator[](size_t i);
size_t add(libff::bit_vector leaf);
std::tuple<size_t, libff::bit_vector, std::vector<libff::bit_vector>> simulate_add(libff::bit_vector leaf);
size_t num_elements();
std::vector<libff::bit_vector> path(size_t address);
libff::bit_vector operator[](uint i);
uint add(libff::bit_vector leaf);
std::tuple<uint, libff::bit_vector, std::vector<libff::bit_vector>> simulate_add(libff::bit_vector leaf);
uint num_elements();
std::vector<libff::bit_vector> path(uint address);
void print();
private:
std::vector<libff::bit_vector>* node_levels;
libff::bit_vector* empty_tree_roots;
std::vector<libff::bit_vector>* leaves;
size_t depth;
size_t max_size;
libff::bit_vector internal_node_at(size_t level, size_t position);
uint max_size;
libff::bit_vector internal_node_at(uint level, uint position);
};

0 comments on commit 918a13d

Please sign in to comment.
You can’t perform that action at this time.