def test_mimc7_round(self) -> None: mimc = MiMC7("Clearmatics") msg = 340282366920938463463374607431768211456 key = 28948022309329048855892746252171976963317496166410141009864396001978282409983 # noqa round_const = 14220067918847996031108144435763672811050758065945364308986253046354060608451 # noqa expect_result = 7970444205539657036866618419973693567765196138501849736587140180515018751924 # noqa self.assertEqual(expect_result, mimc.mimc_round(msg, key, round_const))
def test_mimc_round() -> None: m = MiMC7("Clearmatics") x = 340282366920938463463374607431768211456 k = 28948022309329048855892746252171976963317496166410141009864396001978282409983 # noqa c = 14220067918847996031108144435763672811050758065945364308986253046354060608451 # noqa assert m.mimc_round(x, k, c) == \ 7970444205539657036866618419973693567765196138501849736587140180515018751924 # noqa
def test_mimc7(instance: Any) -> None: # pylint: disable=line-too-long x = int( 28948022309329048855892746252171976963317496166410141009864396001978282409983 ).to_bytes(32, 'big') # noqa y = int( 14220067918847996031108144435763672811050758065945364308986253046354060608451 ).to_bytes(32, 'big') # noqa # pylint: enable=line-too-long h = MiMC7().hash(x, y) result = instance.functions.test_mimc7(x, y).call() assert result == h
def mimc_mp_utility() -> None: """ Generates test vector for MiMC Hash """ m = MiMC7() x = 3703141493535563179657531719960160174296085208671919316200479060314459804651 # noqa y = 15683951496311901749339509118960676303290224812129752890706581988986633412003 # noqa digest = m.mimc_mp(x, y) print("MiMC MP test vector:") print(f"x = {x}") print(f"y = {y}") print(f"digest = {digest}\n")
def mimc_encrypt_utility() -> None: """ Generates test vector for MiMC encrypt """ m = MiMC7() msg = 3703141493535563179657531719960160174296085208671919316200479060314459804651 # noqa ek = \ 15683951496311901749339509118960676303290224812129752890706581988986633412003 # noqa ct = m.mimc_encrypt(msg, ek) print("MiMC encrypt test vector:") print(f"msg = {msg}") print(f"ek = {ek}") print(f"ct = {ct}\n")
def mimc_tree_utility() -> None: """ # Generates test vectors for testing the MiMC Merkle Tree contract. A # 16 entry (4 level) merkle tree with 0 values everywhere. """ m = MiMC7() level_3 = m.mimc_mp(0, 0) level_2 = m.mimc_mp(level_3, level_3) level_1 = m.mimc_mp(level_2, level_2) root = m.mimc_mp(level_1, level_1) print("MiMC Tree test vector (4 entries, all zero):") print(f"Level 2 = {level_3}") print(f"Level 2 = {level_2}") print(f"Level 1 = {level_1}") print(f"Root = {root}\n")
def _test_partial(num_entries: int, step: int = 1) -> None: """ Take the first 'num_entries' from TEST_VALUES. Cut them at each possible place and submit them as two halves to the contract, receiving back the root for the updated tree. """ leaves = TEST_VALUES[:num_entries] mktree = MerkleTree.empty_with_depth(ZETH_MERKLE_TREE_DEPTH, MiMC7()) for leaf in leaves: mktree.insert(leaf) expected_root = mktree.recompute_root() for cut in range(0, num_entries + 1, step): print(f"_test_partial: num_entries={num_entries}, cut={cut}") first = leaves[:cut] second = leaves[cut:] root = contract.functions.testAddLeaves(first, second).call() assert_root(expected_root, root, f"num_entries: {num_entries}, cut: {cut}: ")
from zeth.core.merkle_tree import MerkleTree, PersistentMerkleTree, ZERO_ENTRY, \ compute_merkle_path from zeth.core.utils import extend_32bytes from zeth.core.mimc import MiMC7 from os.path import exists, join from os import makedirs from shutil import rmtree from unittest import TestCase from typing import List MERKLE_TREE_TEST_DIR = "_merkle_tests" MERKLE_TREE_TEST_DEPTH = 4 MERKLE_TREE_TEST_NUM_LEAVES = pow(2, MERKLE_TREE_TEST_DEPTH) MERKLE_TREE_HASH = MiMC7() TEST_VALUES = [ extend_32bytes(i.to_bytes(1, 'big')) for i in range(1, MERKLE_TREE_TEST_NUM_LEAVES) ] class TestMerkleTree(TestCase): @staticmethod def setUpClass() -> None: TestMerkleTree.tearDownClass() makedirs(MERKLE_TREE_TEST_DIR) @staticmethod def tearDownClass() -> None: if exists(MERKLE_TREE_TEST_DIR):
def test_tree_empty(contract: Any) -> None: mktree = MerkleTree.empty_with_depth(ZETH_MERKLE_TREE_DEPTH, MiMC7()) expected_root = mktree.recompute_root() root = contract.functions.testAddLeaves([], []).call() assert_root(expected_root, root, "test_tree_empty")
def test_mimc7(self) -> None: left = 28948022309329048855892746252171976963317496166410141009864396001978282409983 # noqa right = 14220067918847996031108144435763672811050758065945364308986253046354060608451 # noqa expect_result = 14404914332179247771191118015445305957789480573634910846417052002923707343766 # noqa result = MiMC7().hash(_int_to_bytes(left), _int_to_bytes(right)) self.assertEqual(_int_to_bytes(expect_result), result)
# Copyright (c) 2015-2020 Clearmatics Technologies Ltd # # SPDX-License-Identifier: LGPL-3.0+ from __future__ import annotations from zeth.core.mimc import MiMC7 from os.path import exists import json import math from typing import Dict, List, Tuple, Iterator, cast, Any ZERO_ENTRY = bytes.fromhex( "0000000000000000000000000000000000000000000000000000000000000000") HASH = MiMC7() class MerkleTreeData: """ Simple container to be persisted for a client-side Merkle tree. Does not perform any computation. Layers are ordered from top (smallest) to bottom. """ def __init__(self, depth: int, default_values: List[bytes], layers: List[List[bytes]]): self.depth = depth self.default_values = default_values self.layers = layers @staticmethod def empty_with_depth(depth: int) -> MerkleTreeData: # Compute default values for each layer