Zecale - Reconciling Privacy and Scalability on Smart-Contract Chains  0.5
Reference implementation of the Zecale protocol by Clearmatics
test_bw6_761_groth16_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 zecale.core.utils import get_zecale_dir
6 from zecale.cli.utils import load_verification_key, load_aggregated_transaction
7 from zeth.core.pairing import PairingParameters
8 from zeth.core.zksnark import IZKSnarkProvider, Groth16, IVerificationKey
9 from zeth.core.utils import hex_list_to_uint256_list
10 from zeth.core.contracts import InstanceDescription
11 from zeth.cli.utils import get_eth_network, open_web3_from_network
12 from os.path import join
13 import sys
14 from typing import Any
15 
16 
17 ZECALE_DIR = get_zecale_dir()
18 CONTRACTS_DIR = join(ZECALE_DIR, "contracts")
19 DUMMY_APP_DIR = join(ZECALE_DIR, "testdata", "dummy_app")
20 
21 
22 # Pairing parameters for BW6-761
23 # pylint: disable=line-too-long
24 BW6_761_PAIRING_PARAMETERS = PairingParameters.from_json_dict({
25  "name": "bw6-761",
26  "r": "0x01ae3a4617c510eac63b05c06ca1493b1a22d9f300f5138f1ef3622fba094800170b5d44300000008508c00000000001", # noqa
27  "q": "0x0122e824fb83ce0ad187c94004faff3eb926186a81d14688528275ef8087be41707ba638e584e91903cebaff25b423048689c8ed12f9fd9071dcd3dc73ebff2e98a116c25667a8f8160cf8aeeaf0a437e6913e6870000082f49d00000000008b", # noqa
28  "generator_g1": [
29  "0x01075b020ea190c8b277ce98a477beaee6a0cfb7551b27f0ee05c54b85f56fc779017ffac15520ac11dbfcd294c2e746a17a54ce47729b905bd71fa0c9ea097103758f9a280ca27f6750dd0356133e82055928aca6af603f4088f3af66e5b43d", # noqa
30  "0x0058b84e0a6fc574e6fd637b45cc2a420f952589884c9ec61a7348d2a2e573a3265909f1af7e0dbac5b8fa1771b5b806cc685d31717a4c55be3fb90b6fc2cdd49f9df141b3053253b2b08119cad0fb93ad1cb2be0b20d2a1bafc8f2db4e95363" # noqa
31  ],
32  "generator_g2": [
33  "0x0110133241d9b816c852a82e69d660f9d61053aac5a7115f4c06201013890f6d26b41c5dab3da268734ec3f1f09feb58c5bbcae9ac70e7c7963317a300e1b6bace6948cb3cd208d700e96efbc2ad54b06410cf4fe1bf995ba830c194cd025f1c", # noqa
34  "0x0017c3357761369f8179eb10e4b6d2dc26b7cf9acec2181c81a78e2753ffe3160a1d86c80b95a59c94c97eb733293fef64f293dbd2c712b88906c170ffa823003ea96fcd504affc758aa2d3a3c5a02a591ec0594f9eac689eb70a16728c73b61" # noqa
35  ],
36 })
37 # pylint: enable=line-too-long
38 
39 
40 def _test_bw6_761_groth16_contract_with_proof(
41  zksnark: IZKSnarkProvider,
42  instance: Any,
43  vk: IVerificationKey,
44  tx_filename: str) -> bool:
45  # Load proof and extract inputs
46  tx = load_aggregated_transaction(zksnark, join(DUMMY_APP_DIR, tx_filename))
47  ext_proof = tx.ext_proof
48 
49  # Encode the vk, proof and inputs into evm words
50  vk_evm_parameters = zksnark.verification_key_to_contract_parameters(
51  vk, BW6_761_PAIRING_PARAMETERS)
52  proof_evm_parameters = zksnark.proof_to_contract_parameters(
53  ext_proof.proof, BW6_761_PAIRING_PARAMETERS)
54  inputs_evm_parameters = hex_list_to_uint256_list(ext_proof.inputs)
55 
56  # Execute the test contract and return the result
57  evm_parameters = [
58  vk_evm_parameters,
59  proof_evm_parameters,
60  inputs_evm_parameters
61  ]
62 
63  return instance.functions.test_verify(*evm_parameters).call()
64 
65 
67  zksnark: IZKSnarkProvider,
68  instance: Any,
69  aggregator_vk: IVerificationKey) -> None:
70  assert _test_bw6_761_groth16_contract_with_proof(
71  zksnark, instance, aggregator_vk, "batch1.json")
72 
73 
75  zksnark: IZKSnarkProvider,
76  instance: Any,
77  aggregator_vk: IVerificationKey) -> None:
78  assert not _test_bw6_761_groth16_contract_with_proof(
79  zksnark, instance, aggregator_vk, "batch1-invalid.json")
80 
81 
82 def main() -> int:
83  web3: Any = open_web3_from_network(get_eth_network(None))
84  bw6_761_groth16_instance_desc = InstanceDescription.deploy(
85  web3,
86  join(CONTRACTS_DIR, "Groth16BW6_761_test.sol"),
87  "Groth16BW6_761_test",
88  web3.eth.accounts[0], # pylint: disable=no-member
89  None,
90  500000,
91  {"allow_paths": CONTRACTS_DIR})
92 
93  bw6_761_groth16_instance = bw6_761_groth16_instance_desc.instantiate(web3)
94  zksnark = Groth16()
95  aggregator_vk = load_verification_key(
96  zksnark, join(DUMMY_APP_DIR, "aggregator_vk.json"))
97 
98  test_bw6_761_groth16_valid(zksnark, bw6_761_groth16_instance, aggregator_vk)
99  test_bw6_761_groth16_invalid(zksnark, bw6_761_groth16_instance, aggregator_vk)
100 
101  print("========================================")
102  print("== PASSED ==")
103  print("========================================")
104  return 0
105 
106 
107 if __name__ == "__main__":
108  sys.exit(main())
zecale.cli.utils
Definition: utils.py:1
test_commands.test_bw6_761_groth16_contract.test_bw6_761_groth16_valid
None test_bw6_761_groth16_valid(IZKSnarkProvider zksnark, Any instance, IVerificationKey aggregator_vk)
Definition: test_bw6_761_groth16_contract.py:66
zecale.core.utils.get_zecale_dir
str get_zecale_dir()
Definition: utils.py:9
zecale.cli.utils.load_aggregated_transaction
AggregatedTransaction load_aggregated_transaction(IZKSnarkProvider zksnark, str agg_tx_file)
Definition: utils.py:29
zecale.core.utils
Definition: utils.py:1
zecale.cli.utils.load_verification_key
IVerificationKey load_verification_key(IZKSnarkProvider zksnark, str vk_file)
Definition: utils.py:11
test_commands.test_bw6_761_groth16_contract.main
int main()
Definition: test_bw6_761_groth16_contract.py:82
test_commands.test_bw6_761_groth16_contract.test_bw6_761_groth16_invalid
None test_bw6_761_groth16_invalid(IZKSnarkProvider zksnark, Any instance, IVerificationKey aggregator_vk)
Definition: test_bw6_761_groth16_contract.py:74