Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
test_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 test_commands import mock
6 from unittest import TestCase
7 from typing import Any
8 
9 # Data generated by libzeth/tests/core/ec_operation_data_test. Statements to be
10 # checked match those in that test.
11 
12 
13 # Internal convenience function to create an EVM bytes32
14 def _b32(hex_value: str) -> bytes:
15  assert len(hex_value) == 64
16  return bytes.fromhex(hex_value)
17 
18 
19 # BLS-377 test values
20 
21 FR_MINUS_2 = [
22  _b32("12ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a117fffffffffff")
23 ]
24 
25 G1_2 = [
26  _b32("0000000000000000000000000000000000ed453141939e91056edb5a4b5452ed"),
27  _b32("7e61f7f3dd2a4b7ee90e97c9a2301955880661656781dc90857aed6d6a416390"),
28  _b32("0000000000000000000000000000000000cfb0b9717bc8e5ae04601813171337"),
29  _b32("ad99cdae42c561cae80b12f135c64479d6a23f5675ed5ca7e2dd5e8727d7c7ed"),
30 ]
31 G1_3 = [
32  _b32("0000000000000000000000000000000001252b781171f507db36291b433a1f91"),
33  _b32("1a46543890a20ca9712e11f66a5d216e63d817bd8d96cef715abc604dcf6ec2e"),
34  _b32("00000000000000000000000000000000014a00fa77c727e8987cc438b51bbe01"),
35  _b32("2c823a19955ae692c54ce572a61f0ea1fe5cd981533df419fd1330d1f6e6d802"),
36 ]
37 G1_4 = [
38  _b32("0000000000000000000000000000000000c1090c0750892fa132043a3ec67b5d"),
39  _b32("fb5aa24b4772bef1724153a759353c27a216259fecad4b19be1f169b2739bd26"),
40  _b32("00000000000000000000000000000000003f820683c042b943bfb056956d1c8d"),
41  _b32("bc16939f19bd1131ab15caced9f05b7fd009962b3d13907fde8175d86f72e3a4"),
42 ]
43 G1_6 = [
44  _b32("00000000000000000000000000000000010c65c0fb9e6c6ef4cbb27fdc55a07e"),
45  _b32("474df11c564bd91e3fa162c32b7fc3dabba5fc508cfdd8938fb4a30f7de5ad9c"),
46  _b32("000000000000000000000000000000000149a58ced619866b242313876fe2df3"),
47  _b32("188f33b77566a9ddc966ff4d4d5c42d515be862c348f51cc91f1c45a74110ba6"),
48 ]
49 G1_MINUS_8 = [
50  _b32("00000000000000000000000000000000018aff632c0048f5afb5c07fd197a44a"),
51  _b32("127c829be3ff6170c6cebc1154bc72633b45de2ac855e0da30cebfa33672e7f3"),
52  _b32("0000000000000000000000000000000000be8218b8b1ba93705bf17a91027476"),
53  _b32("8383d037cf28b27f9b084d9efb929cc7ff8ad0f1567f8b0edb78c769fed58756"),
54 ]
55 
56 G2_4 = [
57  _b32("00000000000000000000000000000000008deeab95264cf16b9e9b87ce615b9c"),
58  _b32("8ccff86332b286332c00d4f0f0bb4c9c0fc0923d83231e5e3a3a961c8e266651"),
59  _b32("0000000000000000000000000000000000a54cee57ec21b53167f636a9bb1d81"),
60  _b32("691edf259b4164867760b250049bbd722021968ab0b927ee5b0d44c70db106f5"),
61  _b32("000000000000000000000000000000000113d2333add9290fe17235e413f2d8c"),
62  _b32("7f448406f586aefaad39284ac6d80fb7f1741fb37f91c5c6299038063a34bb25"),
63  _b32("000000000000000000000000000000000045e1d96afde76075237a8c3472473f"),
64  _b32("1c4debf2bf79abf2c8b22fd4ccc5c3da30ee409c293094115bddd5da7de0da8a"),
65 ]
66 G2_8 = [
67  _b32("00000000000000000000000000000000019ece89be3c82b561d87e7898ddfc92"),
68  _b32("8d2e86fafa21febc222d8ff38a752fe95b25e19b25410205800385fb047ca1cf"),
69  _b32("00000000000000000000000000000000016251c985d96e59d07cc363290ec714"),
70  _b32("6682da9bde87ed3d64ccad05403992cc9f3704c7f9a1df34116840d220edba27"),
71  _b32("00000000000000000000000000000000012c74d25dd0ca3264d6141945dc309b"),
72  _b32("94be879fc67835ab3db6b1faad807bca3e21e122184678fe4ea38affe654e15f"),
73  _b32("00000000000000000000000000000000002f302a2d6f3c2a5b53edb53d73c1e3"),
74  _b32("f1771c3b929563c62e74e3330adb0f75b1b164fef915d00aa35b5e668524ef2b"),
75 ]
76 
77 
78 BLS12_INSTANCE: Any = None
79 
80 
81 class TestBLS12_377Contract(TestCase):
82 
83  @staticmethod
84  def setUpClass() -> None:
85  print("Deploying TestBLS12_377.sol")
86  _web3, eth = mock.open_test_web3()
87  _bls12_interface, bls12_instance = mock.deploy_contract(
88  eth,
89  eth.accounts[0],
90  "TestBLS12_377",
91  {})
92  global BLS12_INSTANCE # pylint: disable=global-statement
93  BLS12_INSTANCE = bls12_instance
94 
95  def test_bls12_ecadd(self) -> None:
96  """
97  Check that [6] == [2] + [4]
98  """
99  result = BLS12_INSTANCE.functions.testECAdd(G1_2 + G1_4).call()
100  self.assertEqual(G1_6, result)
101 
102  def test_bls12_ecmul(self) -> None:
103  """
104  Check that [-8] == -2 * [4]
105  """
106  result = BLS12_INSTANCE.functions.testECMul(G1_4 + FR_MINUS_2).call()
107  self.assertEqual(G1_MINUS_8, result)
108 
109  def test_bls12_ecpairing(self) -> None:
110  """
111  Check that e([6], [4]) * e([3],[8]) * e([4],[4]) * e([-8], [8]) == 1
112  """
113  # Note, return result here is uint256(1) or uint256(0) depending on the
114  # pairing check result.
115  points = G1_6 + G2_4 + G1_3 + G2_8 + G1_4 + G2_4 + G1_MINUS_8 + G2_8
116  result = BLS12_INSTANCE.functions.testECPairing(points).call()
117  self.assertEqual(1, result)
118 
119  points = G1_6 + G2_4 + G1_3 + G2_8 + G1_4 + G2_4 + G1_MINUS_8 + G2_4
120  result = BLS12_INSTANCE.functions.testECPairing(points).call()
121  self.assertEqual(0, result)
test_contracts.test_bls12_377_contract.TestBLS12_377Contract.test_bls12_ecadd
None test_bls12_ecadd(self)
Definition: test_bls12_377_contract.py:95
test_contracts.test_bls12_377_contract.TestBLS12_377Contract
Definition: test_bls12_377_contract.py:81
test_contracts.test_bls12_377_contract.TestBLS12_377Contract.setUpClass
None setUpClass()
Definition: test_bls12_377_contract.py:84
test_contracts.test_bls12_377_contract.TestBLS12_377Contract.test_bls12_ecpairing
None test_bls12_ecpairing(self)
Definition: test_bls12_377_contract.py:109
test_contracts.test_bls12_377_contract.TestBLS12_377Contract.test_bls12_ecmul
None test_bls12_ecmul(self)
Definition: test_bls12_377_contract.py:102