Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
test_mimc_contract.py
Go to the documentation of this file.
1 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 #
3 # SPDX-License-Identifier: LGPL-3.0+
4 
5 from zeth.core.utils import get_contracts_dir
6 from zeth.core.contracts import InstanceDescription
7 from zeth.core.mimc import MiMCAltBN128, MiMCBLS12_377
8 from zeth.cli.utils import get_eth_network, open_web3_from_network
9 from os.path import join
10 from unittest import TestCase
11 from typing import Any
12 
13 CONTRACT_INSTANCE: Any = None
14 
15 """
16 Test data here matches that used in test_mimc.py, which is also used in the
17 tests of mimc circuits.
18 """
19 
20 
21 class TestMiMCContract(TestCase):
22 
23  @staticmethod
24  def setUpClass() -> None:
25  web3: Any = open_web3_from_network(get_eth_network(None))
26  contracts_dir = get_contracts_dir()
27  contract_instance_desc = InstanceDescription.deploy(
28  web3,
29  join(contracts_dir, "TestMiMC.sol"),
30  "TestMiMC",
31  web3.eth.accounts[0], # pylint: disable=no-member
32  None,
33  500000,
34  {"allow_paths": contracts_dir})
35  global CONTRACT_INSTANCE # pylint: disable=global-statement
36  CONTRACT_INSTANCE = contract_instance_desc.instantiate(web3)
37 
38  def test_mimc_alt_bn128(self) -> None:
39  # pylint: disable=line-too-long
40  x = int(28948022309329048855892746252171976963317496166410141009864396001978282409983).to_bytes(32, 'big') # noqa
41  y = int(14220067918847996031108144435763672811050758065945364308986253046354060608451).to_bytes(32, 'big') # noqa
42  # pylint: enable=line-too-long
43  h = MiMCAltBN128().hash(x, y)
44 
45  result = CONTRACT_INSTANCE.functions.testMimcAltBN128(x, y).call()
46  self.assertEqual(h, result)
47 
48  def test_mimc_bls12_377(self) -> None:
49  # pylint: disable=line-too-long
50  x = int(28948022309329048855892746252171976963317496166410141009864396001978282409983).to_bytes(32, 'big') # noqa
51  y = int(14220067918847996031108144435763672811050758065945364308986253046354060608451).to_bytes(32, 'big') # noqa
52  # pylint: enable=line-too-long
53  h = MiMCBLS12_377().hash(x, y)
54 
55  result = CONTRACT_INSTANCE.functions.testMimcBLS12_377(x, y).call()
56  self.assertEqual(h, result)
zeth.cli.utils.get_eth_network
NetworkConfig get_eth_network(Optional[str] eth_network)
Definition: utils.py:84
zeth.cli.zeth_deploy.int
int
Definition: zeth_deploy.py:27
zeth.core.mimc.MiMCAltBN128
Definition: mimc.py:96
zeth.cli.utils
Definition: utils.py:1
zeth.core.utils
Definition: utils.py:1
zeth.core.mimc.MiMCBLS12_377
Definition: mimc.py:109
zeth.core.contracts
Definition: contracts.py:1
zeth.cli.utils.open_web3_from_network
Any open_web3_from_network(NetworkConfig eth_net)
Definition: utils.py:114
test_contracts.test_mimc_contract.TestMiMCContract.setUpClass
None setUpClass()
Definition: test_mimc_contract.py:24
test_contracts.test_mimc_contract.TestMiMCContract.test_mimc_bls12_377
None test_mimc_bls12_377(self)
Definition: test_mimc_contract.py:48
test_contracts.test_mimc_contract.TestMiMCContract.test_mimc_alt_bn128
None test_mimc_alt_bn128(self)
Definition: test_mimc_contract.py:38
zeth.core.mimc
Definition: mimc.py:1
zeth.core.utils.get_contracts_dir
str get_contracts_dir()
Definition: utils.py:255
test_contracts.test_mimc_contract.TestMiMCContract
Definition: test_mimc_contract.py:21