Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyanho committed Sep 15, 2020
1 parent a7bdfe8 commit 2d1fa3b
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 27 deletions.
4 changes: 2 additions & 2 deletions commands/zeth_deploy.py

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ def compile_mixer() -> Interface:

def compile_token():

zeth_dir = "/Users/ruanyang/works/snark-project/zeth/zeth/client"
zeth_dir = "/Users/ruanyang/works/snark-project/zk-client"
print("***********", zeth_dir)
allowed_path = join(
zeth_dir,
"contract")
path_to_token = join(
zeth_dir,
"contract",
"bac/BAC001.sol")
"test1.sol")
# Compilation
set_solc_version(SOL_COMPILER_VERSION)
compiled_sol = compile_files([path_to_token], allow_paths=allowed_path)
token_interface = compiled_sol[path_to_token + ":BAC001"]
fo = open("./contract/bac/abi/BAC001.abi", "w")
fo1 = open("./contract/bac/abi/BAC001.bin", "w")
token_interface = compiled_sol[path_to_token + ":Test1"]
fo = open("./contract/Test1.abi", "w")
fo1 = open("./contract/Test1.bin", "w")
fo.write(str(token_interface["abi"]))
fo.close()
fo1.write(str(token_interface["bin"]))
Expand All @@ -91,4 +91,4 @@ def compile_token():


if __name__ == "__main__":
compile_mixer() # pylint: disable=no-value-for-parameter
compile_token() # pylint: disable=no-value-for-parameter
11 changes: 9 additions & 2 deletions contract/Groth16Mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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": "address", "name": "poseidonAddress", "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_string = '''[{"inputs": [{"internalType": "uint256", "name": "mk_depth", "type": "uint256"}, {"internalType": "address", "name": "token", "type": "address"}, {"internalType": "address", "name": "poseidonAddress", "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": false, "inputs": [{"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256[10]", "name": "primary_inputs", "type": "uint256[10]"}, {"internalType": "bytes32[2]", "name": "nfs", "type": "bytes32[2]"}], "name": "check_mkroot_nullifiers_hsig_append_nullifiers_state", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "payable": false, "stateMutability": "nonpayable", "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 Expand Up @@ -45,6 +45,14 @@ def assemble_public_values(self, primary_inputs):
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result

# ------------------------------------------
def check_mkroot_nullifiers_hsig_append_nullifiers_state(self, vk, primary_inputs, nfs):
func_name = 'check_mkroot_nullifiers_hsig_append_nullifiers_state'
args = [vk, primary_inputs, nfs]
receipt = self.client.sendRawTransactionGetReceipt(self.address, self.contract_abi, func_name, args)
outputresult = self.data_parser.parse_receipt_output(func_name, receipt['output'])
return outputresult, receipt

# ------------------------------------------
def get_constants(self):
func_name = 'get_constants'
Expand Down Expand Up @@ -75,7 +83,6 @@ def mix(self, a, b, c, vk, sigma, input, ciphertexts):
outputresult = self.data_parser.parse_receipt_output(func_name, receipt['output'])
return outputresult, receipt

# ------------------------------------------
def token(self):
func_name = 'token'
args = []
Expand Down
1 change: 1 addition & 0 deletions contract/Test1.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"constant": false, "inputs": [{"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "nfs", "type": "uint256[2]"}], "name": "check_mkroot_nullifiers_hsig_append_nullifiers_state", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "payable": false, "stateMutability": "nonpayable", "type": "function"}]
1 change: 1 addition & 0 deletions contract/Test1.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b50610313806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80639cab510014610030575b600080fd5b6100d4600480360360c081101561004657600080fd5b8101908080608001906004806020026040519081016040528092919082600460200280828437600081840152601f19601f820116905080830192505050505050919291929080604001906002806020026040519081016040528092919082600260200280828437600081840152601f19601f82011690508083019250505050505091929192905050506100ea565b6040518082815260200191505060405180910390f35b60006060600260405190808252806020026020018201604052801561011e5781602001602082028038833980820191505090505b5090508260006002811061012e57fe5b602002015160001b8160008151811061014357fe5b6020026020010181815250508260016002811061015c57fe5b602002015160001b8160018151811061017157fe5b60200260200101818152505060006002828660405160200180838051906020019060200280838360005b838110156101b657808201518184015260208101905061019b565b5050505090500182600460200280838360005b838110156101e45780820151818401526020810190506101c9565b50505050905001925050506040516020818303038152906040526040518082805190602001908083835b60208310610231578051825260208201915060208101905060208303925061020e565b6001836020036101000a038019825116818451168082178552505050505050905001915050602060405180830381855afa158015610273573d6000803e3d6000fd5b5050506040513d602081101561028857600080fd5b8101908080519060200190929190505050905060007f30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f000000190506000818360001c816102ce57fe5b069050809450505050509291505056fea265627a7a72315820bf74a6ac9ceb0fa0d11c95916dd42c3e08f27f8a94c9a4169e1f000465c7089164736f6c63430005110032
33 changes: 33 additions & 0 deletions contract/Test1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# template for codegen
from python_web3.client.bcosclient import (
BcosClient
)
from python_web3.client.datatype_parser import DatatypeParser
import json


class Test1: # name of abi
address = None
contract_abi_string = '''[{"constant": false, "inputs": [{"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "nfs", "type": "uint256[2]"}], "name": "check_mkroot_nullifiers_hsig_append_nullifiers_state", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "payable": false, "stateMutability": "nonpayable", "type": "function"}]'''
contract_abi = None
data_parser = DatatypeParser()
client = None

def __init__(self, address):
self.client = BcosClient()
self.address = address
self.contract_abi = json.loads(self.contract_abi_string)
self.data_parser.set_abi(self.contract_abi)

def deploy(self, contract_bin_file):
result = self.client.deployFromFile(contract_bin_file)
self.address = result["contractAddress"]
return result

# ------------------------------------------
def check_mkroot_nullifiers_hsig_append_nullifiers_state(self, vk, nfs):
func_name = 'check_mkroot_nullifiers_hsig_append_nullifiers_state'
args = [vk, nfs]
receipt = self.client.sendRawTransactionGetReceipt(self.address, self.contract_abi, func_name, args)
outputresult = self.data_parser.parse_receipt_output(func_name, receipt['output'])
return outputresult, receipt
18 changes: 4 additions & 14 deletions contract/mixer/BaseMixer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,12 @@ contract BaseMixer is MerkleTreePos, BAC001Holder {
// combine them with the nullifier field element
function assemble_nullifier(
uint256 index, uint256[nbInputs] memory primary_inputs)
public pure
returns (bytes32 nf) {

public pure returns (bytes32 nf) {
// We first check that the nullifier we want to retrieve exists
require(
index < jsIn,
"nullifier index overflow"
);

// We compute the nullifier's residual bits index and check the 1st
// f.e. indeed comprises it. See the way the residual bits are ordered
// in the extended proof
// uint256 nf_bit_index =
// 2*public_value_length + (1 + index) * packing_residue_length;
// require(
Expand All @@ -256,12 +250,7 @@ contract BaseMixer is MerkleTreePos, BAC001Holder {
// bytes32(primary_inputs[2 + jsOut + nb_hash_digests]);
// << (padding_size + nf_bit_index)) >> field_capacity;

// We offset the nullifier index by the number of values preceding the
// nullifiers in the primary inputs: the root (1) and the cms (jsOut)
// We retrieve the field element corresponding to the `field_capacity`
// most significant bits of nf. We remove the left padding due to
// casting `field_capacity` bits into a bytes32. We reassemble nf by
// adding the values.

// uint256 high_bits = uint(
// primary_inputs[2 + jsOut + index] << (digest_length - field_capacity));

Expand All @@ -278,7 +267,7 @@ contract BaseMixer is MerkleTreePos, BAC001Holder {
uint256[4] memory vk,
uint256[nbInputs] memory primary_inputs,
bytes32[jsIn] memory nfs)
internal {
public returns (uint256 ){
// 1. We re-assemble the full root digest and check it is in the tree
require(
checkRoot(primary_inputs),
Expand Down Expand Up @@ -312,6 +301,7 @@ contract BaseMixer is MerkleTreePos, BAC001Holder {
expected_hsig_mod == uint256(hsig),
"Invalid hsig: This hsig does not correspond to the hash of vk and the nfs"
);
return expected_hsig_mod;
}

function checkRoot(uint256[nbInputs] memory primary_inputs) internal returns (bool){
Expand Down
Loading

0 comments on commit 2d1fa3b

Please sign in to comment.