Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
test_groth16_bls12_377_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, hex_list_to_uint256_list
6 from zeth.core.zksnark import Groth16
7 from zeth.core.contracts import InstanceDescription
8 from zeth.cli.utils import get_eth_network, open_web3_from_network
9 from tests.test_pairing import BLS12_377_PAIRING
10 from os.path import join
11 from unittest import TestCase
12 from typing import List, Any
13 
14 # pylint: disable=line-too-long
15 VERIFICATION_KEY = Groth16.VerificationKey.from_json_dict({
16  "alpha": [
17  "0x002a586f4738bdc629cfeffae788183586416cf2255ca2dfdca0866fa39a0ff27a75ab2fc8230815a3263da828707bb6", # noqa
18  "0x009797441005cca3c8e1b0ec78e7d5c5da95091ae0d9ca7396fb2245ab99101b34d7c5f0175aafd35ede91dc09f9532d" # noqa
19  ],
20  "beta": [
21  ["0x003d46509bd7f4e68ce267343ee8e555570ef1ab465bfe36756ba76f8059f2714573e6a2833e15a03c5c078c80ed06d5", # noqa
22  "0x010f8805ac1719b1c76cb7e12f0d548a53f30a0caad7f6f9b0eb1ed676aae1faa6476daa60f60916bfdd1fa63f389728"], # noqa
23  ["0x003a0ac1e41533af971fb2faca7e2cf662142c8b329b01efd299bc5ec263617127d5599f36b2038c58dfee7cc009b0ec", # noqa
24  "0x01595cb8a7e1401cd60c4b132ac611affec72d56c20624c1237fa318fb9c007dee7f92c08db7d910022b0ef1c7f95ff4"] # noqa
25  ],
26  "delta": [
27  ["0x001e2716cb3ffbfe3cc1e76c6fa548018b3f9b12bfc2aa07640c9071fb7a95b4bdaf0ee15a0a4f6ddddc85834efc9009", # noqa
28  "0x0098df94239036766b474b3a800d6edbc561e0cc37b251fa94f7cbcaeb01388646016163cd3d6a784a5c63f1879f0cf6"], # noqa
29  ["0x008fd1d23577ded8f806c31e5d34b863b534382c83030c4382025cf3a06bdc14534f79861e9d8c17c98c3ad75d4bd46c", # noqa
30  "0x003b5aeb59778b91048df1827f71209950b0e397cb9430632e2f194d37f51318bff10a37d88c2c86f2c21f71126bc909"] # noqa
31  ],
32  "ABC": [
33  ["0x01768261e5d9e312768c75fde3c50cd800a3ab94572c55f3482993bc406ea7cac091c0a450c4a2f1a5ad7aa9e3f6eb10", # noqa
34  "0x012b1728cf8b05ebfb588d307f432deebf428c202a8572cec70ab403559b2999abfdbe830ffe51558876936facfc967b"], # noqa
35  ["0x00ecfad9e9ebc8a46b3a5664abdcea154d30565f04c90e75e8bdf157a019ddda540d58621a6543745b62e6d856b5a962", # noqa
36  "0x0042421dd9c1f425afd48f2b1814712cc8a5238b96b3db9edc24f80e25e370351c80aa0cd2c79bdf1fc4a4319fea6209"] # noqa
37  ]
38 })
39 
40 PROOF = Groth16.Proof.from_json_dict({
41  "a": [
42  "0x0122d4c743e0b0a9a1bc920d070c536390096eedc60ac427943f01acf8f95011d0d9ee51539c338f5f31b74e9138646c", # noqa
43  "0x0117f63edf1996607168c711e5d6c9add5dbad4df22c344bcb8f5a20026148a2cf1a3b905388ab952e05903944ed1f47" # noqa
44  ],
45  "b": [
46  ["0x019df27cf0dae60f1a6107cd36838b2379af2da7765e9a0257f424db9d58224f45b133d7085bf350885afe7ffcae16b9", # noqa
47  "0x019cc9fec349ada5065075e7d42355f9bdc413d2b481f62b71ad9cf7af560f555714291ff3ed277ac973d1a83b4fc193"], # noqa
48  ["0x009db56d5960ce13e15e93cb3985eb94d8b4ca033917757bf362baccac5462668cd900800f4de4861a5a92a542b81aef", # noqa
49  "0x019e188316b82cc8a95ae6e7fb27ea5c6abaed28610152d421fc6fa090e406299cbb40e5f25c18339a5bc04c23bd6822"] # noqa
50  ],
51  "c": [
52  "0x00ae39a12b92b09e1ec1e256f7bf0522c067806f1ed46e959f018ec859b3a200506c7a7993243df77cc911ff35c1a0c8", # noqa
53  "0x0016b89bd7d30c7a34d25780ef14fd027e2515c7449afe9d7dbf21f183673aefffb196e5959fcedd0dcf6064f1186d4c" # noqa
54  ]
55 })
56 
57 INPUTS_VALID = [
58  "0x0000000000000000000000000000000000000000000000000000000000000007"
59 ]
60 
61 INPUTS_INVALID = [
62  "0x0000000000000000000000000000000000000000000000000000000000000008"
63 ]
64 # pylint: enable=line-too-long
65 
66 CONTRACT_INSTANCE: Any = None
67 
68 
70 
71  @staticmethod
72  def setUpClass() -> None:
73  web3: Any = open_web3_from_network(get_eth_network(None))
74  contracts_dir = get_contracts_dir()
75  contract_instance_desc = InstanceDescription.deploy(
76  web3,
77  join(contracts_dir, "TestGroth16BLS12_377.sol"),
78  "TestGroth16BLS12_377",
79  web3.eth.accounts[0], # pylint: disable=no-member
80  None,
81  500000,
82  {"allow_paths": contracts_dir})
83  global CONTRACT_INSTANCE # pylint: disable=global-statement
84  CONTRACT_INSTANCE = contract_instance_desc.instantiate(web3)
85 
86  @staticmethod
87  def _invoke_groth16_bls12_377_verify(
88  vk: Groth16.VerificationKey,
89  proof: Groth16.Proof,
90  inputs: List[str]) -> bool:
91  vk_evm = Groth16.verification_key_to_contract_parameters(
92  vk, BLS12_377_PAIRING)
93  proof_evm = Groth16.proof_to_contract_parameters(proof, BLS12_377_PAIRING)
94  inputs_evm = hex_list_to_uint256_list(inputs)
95  return CONTRACT_INSTANCE.functions.testVerify(
96  vk_evm, proof_evm, inputs_evm).call()
97 
98  def test_groth16_bls12_377_valid(self) -> None:
99  self.assertTrue(self._invoke_groth16_bls12_377_verify(
100  VERIFICATION_KEY, PROOF, INPUTS_VALID))
101 
103  self.assertFalse(self._invoke_groth16_bls12_377_verify(
104  VERIFICATION_KEY, PROOF, INPUTS_INVALID))
zeth.core.zksnark.Groth16.Proof
Definition: zksnark.py:184
zeth.cli.utils.get_eth_network
NetworkConfig get_eth_network(Optional[str] eth_network)
Definition: utils.py:84
zeth.core.utils.hex_list_to_uint256_list
List[int] hex_list_to_uint256_list(Sequence[Union[str, List[str]]] elements)
Definition: utils.py:189
test_contracts.test_groth16_bls12_377_contract.TestGroth16BLS12_377Contract._invoke_groth16_bls12_377_verify
bool _invoke_groth16_bls12_377_verify(Groth16.VerificationKey vk, Groth16.Proof proof, List[str] inputs)
Definition: test_groth16_bls12_377_contract.py:87
zeth.cli.utils
Definition: utils.py:1
test_contracts.test_groth16_bls12_377_contract.TestGroth16BLS12_377Contract.setUpClass
None setUpClass()
Definition: test_groth16_bls12_377_contract.py:72
zeth.core.utils
Definition: utils.py:1
zeth.core.contracts
Definition: contracts.py:1
test_contracts.test_groth16_bls12_377_contract.TestGroth16BLS12_377Contract.test_groth16_bls12_377_valid
None test_groth16_bls12_377_valid(self)
Definition: test_groth16_bls12_377_contract.py:98
test_contracts.test_groth16_bls12_377_contract.TestGroth16BLS12_377Contract
Definition: test_groth16_bls12_377_contract.py:69
zeth.cli.utils.open_web3_from_network
Any open_web3_from_network(NetworkConfig eth_net)
Definition: utils.py:114
test_contracts.test_groth16_bls12_377_contract.TestGroth16BLS12_377Contract.test_groth16_bls12_377_invalid
None test_groth16_bls12_377_invalid(self)
Definition: test_groth16_bls12_377_contract.py:102
zeth.core.zksnark
Definition: zksnark.py:1
zeth.core.utils.get_contracts_dir
str get_contracts_dir()
Definition: utils.py:255