Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
youwenbusi committed Aug 21, 2020
2 parents 862fbf0 + bfabf0f commit 171df5b
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 27 deletions.
7 changes: 6 additions & 1 deletion commands/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
DATABASE_DEFAULT_ADDRESS = "39.108.248.156"
DATABASE_DEFAULT_PORT = 3306
DATABASE_DEFAULT_USER = "root"
DATABASE_DEFAULT_PASSWORD = "******"
DATABASE_DEFAULT_DATABASE = "zkclient"
DATABASE_DEFAULT_PASSWORD = "xBj2019"
DATABASE_DEFAULT_DATABASE = "zkclient_test"
"""

DATABASE_DEFAULT_ADDRESS = "127.0.0.1"
DATABASE_DEFAULT_PORT = 3306
DATABASE_DEFAULT_USER = "root"
DATABASE_DEFAULT_PASSWORD = "8614"
DATABASE_DEFAULT_DATABASE = "merkletree"
DATABASE_DEFAULT_DATABASE = "merkletree"


2 changes: 2 additions & 0 deletions commands/zeth_deploy.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion contract/Groth16Mixer.abi
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[{"inputs": [{"internalType": "uint256", "name": "mk_depth", "type": "uint256"}, {"internalType": "address", "name": "token", "type": "address"}, {"internalType": "uint256[2]", "name": "Alpha", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta2", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta2", "type": "uint256[2]"}, {"internalType": "uint256[]", "name": "ABC_coords", "type": "uint256[]"}], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "string", "name": "message", "type": "string"}], "name": "LogDebug", "type": "event"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "message", "type": "bytes32"}], "name": "LogDebug", "type": "event"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "uint256", "name": "mid", "type": "uint256"}, {"indexed": false, "internalType": "bytes32", "name": "root", "type": "bytes32"}, {"indexed": false, "internalType": "bytes32[2]", "name": "nullifiers", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes32[2]", "name": "commitments", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "LogMix", "type": "event"}, {"constant": true, "inputs": [{"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_hsig", "outputs": [{"internalType": "bytes32", "name": "hsig", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256", "name": "index", "type": "uint256"}, {"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_nullifier", "outputs": [{"internalType": "bytes32", "name": "nf", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_public_values", "outputs": [{"internalType": "uint256", "name": "vpub_in", "type": "uint256"}, {"internalType": "uint256", "name": "vpub_out", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [], "name": "get_constants", "outputs": [{"internalType": "uint256", "name": "js_in", "type": "uint256"}, {"internalType": "uint256", "name": "js_out", "type": "uint256"}, {"internalType": "uint256", "name": "num_inputs", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": false, "inputs": [{"internalType": "bytes32", "name": "commitment", "type": "bytes32"}], "name": "insert", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": true, "inputs": [], "name": "mid", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "payable": false, "stateMutability": "view", "type": "function"}, {"constant": false, "inputs": [{"internalType": "uint256[2]", "name": "a", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "b", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "c", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256", "name": "sigma", "type": "uint256"}, {"internalType": "uint256[10]", "name": "input", "type": "uint256[10]"}, {"internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "mix", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function"}, {"constant": false, "inputs": [{"internalType": "address", "name": "", "type": "address"}, {"internalType": "address", "name": "", "type": "address"}, {"internalType": "uint256", "name": "", "type": "uint256"}, {"internalType": "bytes", "name": "", "type": "bytes"}], "name": "onBAC001Received", "outputs": [{"internalType": "bytes4", "name": "", "type": "bytes4"}], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": true, "inputs": [], "name": "token", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "payable": false, "stateMutability": "view", "type": "function"}]
[{"inputs": [{"internalType": "uint256", "name": "mk_depth", "type": "uint256"}, {"internalType": "address", "name": "token", "type": "address"}, {"internalType": "uint256[2]", "name": "Alpha", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta2", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta2", "type": "uint256[2]"}, {"internalType": "uint256[]", "name": "ABC_coords", "type": "uint256[]"}], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "string", "name": "message", "type": "string"}], "name": "LogDebug", "type": "event"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "message", "type": "bytes32"}], "name": "LogDebug", "type": "event"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "uint256", "name": "mid", "type": "uint256"}, {"indexed": false, "internalType": "bytes32", "name": "root", "type": "bytes32"}, {"indexed": false, "internalType": "bytes32[2]", "name": "nullifiers", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes32[2]", "name": "commitments", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "LogMix", "type": "event"}, {"constant": true, "inputs": [{"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_hsig", "outputs": [{"internalType": "bytes32", "name": "hsig", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256", "name": "index", "type": "uint256"}, {"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_nullifier", "outputs": [{"internalType": "bytes32", "name": "nf", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_public_values", "outputs": [{"internalType": "uint256", "name": "vpub_in", "type": "uint256"}, {"internalType": "uint256", "name": "vpub_out", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [], "name": "get_constants", "outputs": [{"internalType": "uint256", "name": "js_in", "type": "uint256"}, {"internalType": "uint256", "name": "js_out", "type": "uint256"}, {"internalType": "uint256", "name": "num_inputs", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": false, "inputs": [{"internalType": "bytes32", "name": "commitment", "type": "bytes32"}], "name": "insert", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": true, "inputs": [], "name": "mid", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "payable": false, "stateMutability": "view", "type": "function"}, {"constant": false, "inputs": [{"internalType": "uint256[2]", "name": "a", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "b", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "c", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256", "name": "sigma", "type": "uint256"}, {"internalType": "uint256[10]", "name": "input", "type": "uint256[10]"}, {"internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "mix", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function"}, {"constant": false, "inputs": [{"internalType": "address", "name": "", "type": "address"}, {"internalType": "address", "name": "", "type": "address"}, {"internalType": "uint256", "name": "", "type": "uint256"}, {"internalType": "bytes", "name": "", "type": "bytes"}], "name": "onBAC001Received", "outputs": [{"internalType": "bytes4", "name": "", "type": "bytes4"}], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": true, "inputs": [], "name": "token", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "payable": false, "stateMutability": "view", "type": "function"}]

2 changes: 1 addition & 1 deletion contract/Groth16Mixer.bin

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions contract/Groth16Mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
from eth_utils import to_checksum_address



class Groth16Mixer: # name of abi
address = None

contract_abi_string = '''[{"inputs": [{"internalType": "uint256", "name": "mk_depth", "type": "uint256"}, {"internalType": "address", "name": "token", "type": "address"}, {"internalType": "uint256[2]", "name": "Alpha", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta2", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta2", "type": "uint256[2]"}, {"internalType": "uint256[]", "name": "ABC_coords", "type": "uint256[]"}], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "string", "name": "message", "type": "string"}], "name": "LogDebug", "type": "event", "topic": "0xd44da6836c8376d1693e8b9cacf1c39b9bed3599164ad6d8e60902515f83938e"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "message", "type": "bytes32"}], "name": "LogDebug", "type": "event", "topic": "0x05e46912c9be87d8a6830598db8544b61884d9d22f3921597a9a6e8a340914b3"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "uint256", "name": "mid", "type": "uint256"}, {"indexed": false, "internalType": "bytes32", "name": "root", "type": "bytes32"}, {"indexed": false, "internalType": "bytes32[2]", "name": "nullifiers", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes32[2]", "name": "commitments", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "LogMix", "type": "event", "topic": "0x5b20d7b970f991ad433adaa73d15ec55f2dc64ddfecb9505eb1f94e330ecddf7"}, {"constant": true, "inputs": [{"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_hsig", "outputs": [{"internalType": "bytes32", "name": "hsig", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256", "name": "index", "type": "uint256"}, {"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_nullifier", "outputs": [{"internalType": "bytes32", "name": "nf", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}], "name": "assemble_public_values", "outputs": [{"internalType": "uint256", "name": "vpub_in", "type": "uint256"}, {"internalType": "uint256", "name": "vpub_out", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [], "name": "get_constants", "outputs": [{"internalType": "uint256", "name": "js_in", "type": "uint256"}, {"internalType": "uint256", "name": "js_out", "type": "uint256"}, {"internalType": "uint256", "name": "num_inputs", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": false, "inputs": [{"internalType": "bytes32", "name": "commitment", "type": "bytes32"}], "name": "insert", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": true, "inputs": [], "name": "mid", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "payable": false, "stateMutability": "view", "type": "function"}, {"constant": false, "inputs": [{"internalType": "uint256[2]", "name": "a", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "b", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "c", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256", "name": "sigma", "type": "uint256"}, {"internalType": "uint256[10]", "name": "input", "type": "uint256[10]"}, {"internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "mix", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function"}, {"constant": false, "inputs": [{"internalType": "address", "name": "", "type": "address"}, {"internalType": "address", "name": "", "type": "address"}, {"internalType": "uint256", "name": "", "type": "uint256"}, {"internalType": "bytes", "name": "", "type": "bytes"}], "name": "onBAC001Received", "outputs": [{"internalType": "bytes4", "name": "", "type": "bytes4"}], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": true, "inputs": [], "name": "token", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "payable": false, "stateMutability": "view", "type": "function"}]'''

contract_abi = None
data_parser = DatatypeParser()
client = None
Expand Down
36 changes: 31 additions & 5 deletions contract/mixer/BaseMerkleTree.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: LGPL-3.0+

pragma solidity ^0.5.0;
import "./MiMC7.sol";

// Adapted from: https://github.com/zcash-hackworks/babyzoe

Expand All @@ -29,9 +30,9 @@ contract BaseMerkleTree {

// Sparse array of populated leaves of the merkle tree. Unpopulated leaves
// have the DEFAULT_LEAF_VALUE.

bytes32[MAX_NUM_NODES] nodes;

uint public mid = 0;
// bytes32[MAX_NUM_NODES] nodes;
mapping(uint => bytes32[MAX_NUM_NODES]) nodesWithMid;
// Number of leaves populated in `nodes`.
uint256 num_leaves;

Expand All @@ -45,6 +46,24 @@ contract BaseMerkleTree {
"Invalid depth in BaseMerkleTree");
}


function initializeTree() internal
{
// First layer
bytes32 default_value = DEFAULT_LEAF_VALUE;

nodesWithMid[mid][2 * MAX_NUM_LEAVES - 2] = default_value;
uint256 layer_size = MAX_NUM_LEAVES / 2;

// Subsequent layers
while (layer_size > 0) {
default_value = MiMC7.hash(default_value, default_value);
uint256 layer_final_entry_idx = 2 * layer_size - 2;
nodesWithMid[mid][layer_final_entry_idx] = default_value;
layer_size = layer_size / 2;
}
}

// Appends a commitment to the tree, and returns its address
function insert(bytes32 commitment) public {

Expand All @@ -58,9 +77,16 @@ contract BaseMerkleTree {
// Address of the next leaf is the current number of leaves (before
// insertion). Compute the next index in the full set of nodes, and
// write.
++num_leaves;
if(num_leaves == MAX_NUM_LEAVES) {
mid = mid+1;

}
num_leaves = num_leaves % MAX_NUM_LEAVES;

uint256 next_address = num_leaves;
++num_leaves;

uint256 next_entry_idx = (MAX_NUM_LEAVES - 1) + next_address;
nodes[next_entry_idx] = commitment;
nodesWithMid[mid][next_entry_idx] = commitment;
}
}
21 changes: 15 additions & 6 deletions contract/mixer/BaseMixer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract BAC001Holder {
contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {

// The roots of the different updated trees
mapping(bytes32 => bool) roots;
mapping(uint => mapping(bytes32 => bool)) roots;

// The public list of nullifiers (prevents double spend)
mapping(bytes32 => bool) nullifiers;
Expand Down Expand Up @@ -84,7 +84,7 @@ contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {
// The number of public inputs is:
// - 1 (the root)
// - jsIn (the nullifiers)
// - jsOut (the commitments)
// - jsOut (the commitments) 2
// - 1 (hsig)
// - JsIn (the message auth. tags)
// - nb_field_residual (the residual bits not fitting in a single field
Expand All @@ -101,6 +101,7 @@ contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {

// solium complains if the parameters here are indented.
event LogMix(
uint256 mid,
bytes32 root,
bytes32[jsIn] nullifiers,
bytes32[jsOut] commitments,
Expand All @@ -112,8 +113,8 @@ contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {
// Constructor
constructor(uint256 depth, address token_address) MerkleTreeMiMC7(depth)
public {
bytes32 initialRoot = nodes[0];
roots[initialRoot] = true;
bytes32 initialRoot = nodesWithMid[0][0];
roots[0][initialRoot] = true;

token = token_address;

Expand Down Expand Up @@ -274,7 +275,7 @@ contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {
internal {
// 1. We re-assemble the full root digest and check it is in the tree
require(
roots[bytes32(primary_inputs[0])],
checkRoot(primary_inputs),
"Invalid root: This root doesn't exist"
);

Expand Down Expand Up @@ -302,6 +303,14 @@ contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {
);
}

function checkRoot(uint256[nbInputs] memory primary_inputs) internal returns (bool){
for(uint i =0 ;i<=mid; i++) {
if(roots[i][bytes32(primary_inputs[0])])
return true;
}
return false;
}

function assemble_commitments_and_append_to_state(
uint256[nbInputs] memory primary_inputs,
bytes32[jsOut] memory comms)
Expand Down Expand Up @@ -353,6 +362,6 @@ contract BaseMixer is MerkleTreeMiMC7, BAC001Holder {
}

function add_merkle_root(bytes32 root) internal {
roots[root] = true;
roots[mid][root] = true;
}
}
1 change: 1 addition & 0 deletions contract/mixer/Groth16Mixer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ contract Groth16Mixer is BaseMixer {

// 5. Emit the all Mix data
emit LogMix(
mid,
new_merkle_root,
nullifiers,
commitments,
Expand Down
Loading

0 comments on commit 171df5b

Please sign in to comment.