Skip to content

Commit

Permalink
BLAKE2s Update
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineRondelet authored and rrtoledo committed Sep 11, 2019
2 parents 86ed830 + 564de26 commit 7f886d7
Show file tree
Hide file tree
Showing 36 changed files with 2,342 additions and 575 deletions.
6 changes: 6 additions & 0 deletions pyClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ python -m venv zeth-devenv
source zeth-devenv/bin/activate
```

### Update pip to the latest version

```
pip install --upgrade pip
```

### Install the dependencies

```
Expand Down
5 changes: 3 additions & 2 deletions pyClient/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ eth-hash==0.2.0
eth-keyfile==0.5.1
eth-keys==0.2.1
eth-rlp==0.1.2
eth-typing==2.0.0
eth-typing==2.1.0
eth-utils==1.4.1
grpcio==1.18.0
grpcio-tools==1.18.0
Expand All @@ -17,8 +17,10 @@ idna==2.8
lru-dict==1.1.6
parsimonious==0.8.1
protobuf==3.6.1
py_ecc==1.7.1
py-solc-x==0.1.1
pycryptodome==3.7.3
pynacl==1.3.0
requests==2.21.0
rlp==1.1.0
semantic-version==2.6.0
Expand All @@ -27,4 +29,3 @@ toolz==0.9.0
urllib3==1.24.2
web3==4.8.2
websockets==6.0
py_ecc==1.7.1
31 changes: 21 additions & 10 deletions pyClient/testERCTokenMixing.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ def mint_token(token_instance, spender_address, deployer_address, token_amount):
cm_address_bob_to_bob1 = result_deposit_bob_to_bob[0]
cm_address_bob_to_bob2 = result_deposit_bob_to_bob[1]
new_merkle_root_bob_to_bob = result_deposit_bob_to_bob[2]
ciphertext_bob_to_bob1 = result_deposit_bob_to_bob[3]
ciphertext_bob_to_bob2 = result_deposit_bob_to_bob[4]
pk_sender_bob_to_bob = result_deposit_bob_to_bob[3]
ciphertext_bob_to_bob1 = result_deposit_bob_to_bob[4]
ciphertext_bob_to_bob2 = result_deposit_bob_to_bob[5]

print("- Balances after Bob's deposit: ")
print_token_balances(
Expand All @@ -161,10 +162,14 @@ def mint_token(token_instance, spender_address, deployer_address, token_amount):
mixer_instance.address
)

# Construct sk and pk objects from bytes
alice_sk = zethUtils.get_private_key_from_bytes(keystore["Alice"]["AddrSk"]["encSK"])
pk_sender = zethUtils.get_public_key_from_bytes(pk_sender_bob_to_bob)

# Alice sees a deposit and tries to decrypt the ciphertexts to see if she was the recipient
# But she wasn't the recipient (Bob was), so she fails to decrypt
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_bob1, keystore["Alice"]["AddrSk"]["dk"], "alice")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_bob2, keystore["Alice"]["AddrSk"]["dk"], "alice")
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_bob1, pk_sender, alice_sk, "alice")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_bob2, pk_sender, alice_sk, "alice")
assert (recovered_plaintext1 == ""),"Alice managed to decrypt a ciphertext that was not encrypted with her key!"
assert (recovered_plaintext2 == ""),"Alice managed to decrypt a ciphertext that was not encrypted with her key!"

Expand All @@ -174,7 +179,8 @@ def mint_token(token_instance, spender_address, deployer_address, token_amount):
mk_byte_tree = get_merkle_tree(mixer_instance)
mk_path = zethUtils.compute_merkle_path(cm_address_bob_to_bob1, mk_tree_depth, mk_byte_tree)
# Bob decrypts one of the note he previously received (useless here but useful if the payment came from someone else)
input_note_json = json.loads(zethUtils.decrypt(ciphertext_bob_to_bob1, keystore["Bob"]["AddrSk"]["dk"]))
bob_sk = zethUtils.get_private_key_from_bytes(keystore["Bob"]["AddrSk"]["encSK"])
input_note_json = json.loads(zethUtils.decrypt(ciphertext_bob_to_bob1, pk_sender, bob_sk))
input_note_bob_to_charlie = zethGRPC.zethNoteObjFromParsed(input_note_json)
# Execution of the transfer
result_transfer_bob_to_charlie = zethTest.bob_to_charlie(
Expand All @@ -192,9 +198,10 @@ def mint_token(token_instance, spender_address, deployer_address, token_amount):
cm_address_bob_to_charlie1 = result_transfer_bob_to_charlie[0] # Bob -> Bob (Change)
cm_address_bob_to_charlie2 = result_transfer_bob_to_charlie[1] # Bob -> Charlie (payment to Charlie)
new_merkle_root_bob_to_charlie = result_transfer_bob_to_charlie[2]
ciphertext_bob_to_charlie1 = result_transfer_bob_to_charlie[3]
ciphertext_bob_to_charlie2 = result_transfer_bob_to_charlie[4]

pk_sender_bob_to_charlie = result_transfer_bob_to_charlie[3]
ciphertext_bob_to_charlie1 = result_transfer_bob_to_charlie[4]
ciphertext_bob_to_charlie2 = result_transfer_bob_to_charlie[5]

# Bob tries to spend `input_note_bob_to_charlie` twice
result_double_spending = ""
try:
Expand Down Expand Up @@ -222,9 +229,13 @@ def mint_token(token_instance, spender_address, deployer_address, token_amount):
mixer_instance.address
)

# Construct sk and pk objects from bytes
charlie_sk = zethUtils.get_private_key_from_bytes(keystore["Charlie"]["AddrSk"]["encSK"])
pk_sender = zethUtils.get_public_key_from_bytes(pk_sender_bob_to_charlie)

# Charlie tries to decrypt the ciphertexts from Bob's previous transaction
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_charlie1, keystore["Charlie"]["AddrSk"]["dk"], "charlie")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_charlie2, keystore["Charlie"]["AddrSk"]["dk"], "charlie")
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_charlie1, pk_sender, charlie_sk, "charlie")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_charlie2, pk_sender, charlie_sk, "charlie")
assert (recovered_plaintext1 == ""),"Charlie managed to decrypt a ciphertext that was not encrypted with his key!"
assert (recovered_plaintext2 != ""),"Charlie should have been able to decrypt the ciphertext that was obtained with his key!"

Expand Down
31 changes: 21 additions & 10 deletions pyClient/testEtherMixing.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def get_merkle_tree(mixer_instance):
cm_address_bob_to_bob1 = result_deposit_bob_to_bob[0]
cm_address_bob_to_bob2 = result_deposit_bob_to_bob[1]
new_merkle_root_bob_to_bob = result_deposit_bob_to_bob[2]
ciphertext_bob_to_bob1 = result_deposit_bob_to_bob[3]
ciphertext_bob_to_bob2 = result_deposit_bob_to_bob[4]
pk_sender_ciphertext_bob_to_bob = result_deposit_bob_to_bob[3]
ciphertext_bob_to_bob1 = result_deposit_bob_to_bob[4]
ciphertext_bob_to_bob2 = result_deposit_bob_to_bob[5]

print("- Balances after Bob's deposit: ")
print_balances(
Expand All @@ -101,10 +102,14 @@ def get_merkle_tree(mixer_instance):
mixer_instance.address
)

# Construct sk and pk objects from bytes
sk_alice = zethUtils.get_private_key_from_bytes(keystore["Alice"]["AddrSk"]["encSK"])
pk_sender = zethUtils.get_public_key_from_bytes(pk_sender_ciphertext_bob_to_bob)

# Alice sees a deposit and tries to decrypt the ciphertexts to see if she was the recipient
# But she wasn't the recipient (Bob was), so she fails to decrypt
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_bob1, keystore["Alice"]["AddrSk"]["dk"], "alice")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_bob2, keystore["Alice"]["AddrSk"]["dk"], "alice")
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_bob1, pk_sender, sk_alice, "alice")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_bob2, pk_sender, sk_alice, "alice")
assert (recovered_plaintext1 == ""),"Alice managed to decrypt a ciphertext that was not encrypted with her key!"
assert (recovered_plaintext2 == ""),"Alice managed to decrypt a ciphertext that was not encrypted with her key!"

Expand All @@ -115,7 +120,8 @@ def get_merkle_tree(mixer_instance):
mk_path = zethUtils.compute_merkle_path(cm_address_bob_to_bob1, mk_tree_depth, mk_byte_tree)

# Bob decrypts one of the note he previously received (useless here but useful if the payment came from someone else)
input_note_json = json.loads(zethUtils.decrypt(ciphertext_bob_to_bob1, keystore["Bob"]["AddrSk"]["dk"]))
sk_bob = zethUtils.get_private_key_from_bytes(keystore["Bob"]["AddrSk"]["encSK"])
input_note_json = json.loads(zethUtils.decrypt(ciphertext_bob_to_bob1, pk_sender, sk_bob))
input_note_bob_to_charlie = zethGRPC.zethNoteObjFromParsed(input_note_json)
# Execution of the transfer
result_transfer_bob_to_charlie = zethTest.bob_to_charlie(
Expand All @@ -133,9 +139,10 @@ def get_merkle_tree(mixer_instance):
cm_address_bob_to_charlie1 = result_transfer_bob_to_charlie[0] # Bob -> Bob (Change)
cm_address_bob_to_charlie2 = result_transfer_bob_to_charlie[1] # Bob -> Charlie (payment to Charlie)
new_merkle_root_bob_to_charlie = result_transfer_bob_to_charlie[2]
ciphertext_bob_to_charlie1 = result_transfer_bob_to_charlie[3]
ciphertext_bob_to_charlie2 = result_transfer_bob_to_charlie[4]

pk_sender_ciphertext_bob_to_charlie = result_transfer_bob_to_charlie[3]
ciphertext_bob_to_charlie1 = result_transfer_bob_to_charlie[4]
ciphertext_bob_to_charlie2 = result_transfer_bob_to_charlie[5]

# Bob tries to spend `input_note_bob_to_charlie` twice
result_double_spending = ""
try:
Expand Down Expand Up @@ -163,9 +170,13 @@ def get_merkle_tree(mixer_instance):
mixer_instance.address
)

# Construct sk and pk objects from bytes
sk_charlie = zethUtils.get_private_key_from_bytes(keystore["Charlie"]["AddrSk"]["encSK"])
pk_sender = zethUtils.get_public_key_from_bytes(pk_sender_ciphertext_bob_to_charlie)

# Charlie tries to decrypt the ciphertexts from Bob's previous transaction
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_charlie1, keystore["Charlie"]["AddrSk"]["dk"], "charlie")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_charlie2, keystore["Charlie"]["AddrSk"]["dk"], "charlie")
recovered_plaintext1 = zethUtils.receive(ciphertext_bob_to_charlie1, pk_sender, sk_charlie, "charlie")
recovered_plaintext2 = zethUtils.receive(ciphertext_bob_to_charlie2, pk_sender, sk_charlie, "charlie")
assert (recovered_plaintext1 == ""),"Charlie managed to decrypt a ciphertext that was not encrypted with his key!"
assert (recovered_plaintext2 != ""),"Charlie should have been able to decrypt the ciphertext that was obtained with his key!"

Expand Down
32 changes: 32 additions & 0 deletions pyClient/testZethUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import zethUtils

from nacl.public import PrivateKey, PublicKey

# Tests the correct encrypt-decrypt flow: decrypt(encrypt(m)) == m
def test_encrypt_decrypt():
message = "Join Clearmatics, we are hiring!"

keypair_alice_bytes, keypair_bob_bytes, _ = zethUtils.gen_keys_utility()

pk_alice = zethUtils.get_public_key_from_bytes(keypair_alice_bytes[0])
sk_alice = zethUtils.get_private_key_from_bytes(keypair_alice_bytes[1])

pk_bob = zethUtils.get_public_key_from_bytes(keypair_bob_bytes[0])
sk_bob = zethUtils.get_private_key_from_bytes(keypair_bob_bytes[1])

# Subtest 1: Alice to Alice
ciphertext_alice_alice = zethUtils.encrypt(message, pk_alice, sk_alice)
plaintext_alice_alice = zethUtils.decrypt(ciphertext_alice_alice, pk_alice, sk_alice)
assert plaintext_alice_alice == message, "Error in Alice to Alice test"

# Subest 2: Bob to Alice
ciphertext_bob_alice = zethUtils.encrypt(message, pk_alice, sk_bob)
plaintext_bob_alice = zethUtils.decrypt(ciphertext_bob_alice, pk_alice, sk_bob)
assert plaintext_bob_alice == message, "Error in Bob to Alice test: pk_alice, sk_bob"
plaintext_bob_alice = zethUtils.decrypt(ciphertext_bob_alice, pk_bob, sk_alice)
assert plaintext_bob_alice == message, "Error in Bob to Alice test: pk_bob, sk_alice"

print("Tests encrypt_decrypt passed")

if __name__ == "__main__":
test_encrypt_decrypt()
24 changes: 17 additions & 7 deletions pyClient/zethContracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def deploy_mixer(
# Deploy the Mixer contract once the Verifier is successfully deployed
mixer = w3.eth.contract(
abi=mixer_interface['abi'], bytecode=mixer_interface['bin'])

tx_hash = mixer.constructor(
snark_ver = proof_verifier_address,
sig_ver = otsig_verifier_address,
Expand Down Expand Up @@ -228,6 +229,7 @@ def deploy_tree_contract(interface, depth, hasher_address):
# Call to the mixer's mix function to do zero knowledge payments
def mix_pghr13(
mixer_instance,
pk_sender,
ciphertext1,
ciphertext2,
parsed_proof,
Expand All @@ -238,8 +240,6 @@ def mix_pghr13(
call_gas
):
tx_hash = mixer_instance.functions.mix(
ciphertext1,
ciphertext2,
zethGRPC.hex2int(parsed_proof["a"]),
zethGRPC.hex2int(parsed_proof["a_p"]),
[zethGRPC.hex2int(parsed_proof["b"][0]), zethGRPC.hex2int(parsed_proof["b"][1])],
Expand All @@ -250,14 +250,18 @@ def mix_pghr13(
zethGRPC.hex2int(parsed_proof["k"]),
[ [int(vk[0][0]), int(vk[0][1])], [int(vk[1][0]), int(vk[1][1])] ],
int(sigma),
zethGRPC.hex2int(parsed_proof["inputs"])
zethGRPC.hex2int(parsed_proof["inputs"]),
pk_sender,
ciphertext1,
ciphertext2,
).transact({'from': sender_address, 'value': wei_pub_value, 'gas': call_gas})

tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash, 10000)
return parse_mix_call(mixer_instance, tx_receipt)

def mix_groth16(
mixer_instance,
pk_sender,
ciphertext1,
ciphertext2,
parsed_proof,
Expand All @@ -268,21 +272,23 @@ def mix_groth16(
call_gas
):
tx_hash = mixer_instance.functions.mix(
ciphertext1,
ciphertext2,
zethGRPC.hex2int(parsed_proof["a"]),
[zethGRPC.hex2int(parsed_proof["b"][0]), zethGRPC.hex2int(parsed_proof["b"][1])],
zethGRPC.hex2int(parsed_proof["c"]),
[ [int(vk[0][0]), int(vk[0][1])], [int(vk[1][0]), int(vk[1][1])] ],
int(sigma),
zethGRPC.hex2int(parsed_proof["inputs"])
zethGRPC.hex2int(parsed_proof["inputs"]),
pk_sender,
ciphertext1,
ciphertext2,
).transact({'from': sender_address, 'value': wei_pub_value, 'gas': call_gas})

tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash, 10000)
return parse_mix_call(mixer_instance, tx_receipt)

def mix(
mixer_instance,
pk_sender,
ciphertext1,
ciphertext2,
parsed_proof,
Expand All @@ -296,6 +302,7 @@ def mix(
if zksnark == constants.PGHR13_ZKSNARK:
return mix_pghr13(
mixer_instance,
pk_sender,
ciphertext1,
ciphertext2,
parsed_proof,
Expand All @@ -308,6 +315,7 @@ def mix(
elif zksnark == constants.GROTH16_ZKSNARK:
return mix_groth16(
mixer_instance,
pk_sender,
ciphertext1,
ciphertext2,
parsed_proof,
Expand Down Expand Up @@ -338,7 +346,9 @@ def parse_mix_call(mixer_instance, tx_receipt):
new_mk_root = w3.toHex(event_logs_logMerkleRoot[0].args.root)[2:] # [2:] to strip the '0x' prefix
ciphertext1 = event_logs_logSecretCiphers[0].args.ciphertext
ciphertext2 = event_logs_logSecretCiphers[1].args.ciphertext
return (commitment_address1, commitment_address2, new_mk_root, ciphertext1, ciphertext2)
pk_sender = event_logs_logSecretCiphers[0].args.pk_sender

return (commitment_address1, commitment_address2, new_mk_root, pk_sender, ciphertext1, ciphertext2)

# Call the hash method of MiMC contract
def mimcHash(instance, m, k, seed):
Expand Down
Loading

0 comments on commit 7f886d7

Please sign in to comment.