-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from clearmatics/input-hashing-prepare
Changes in preparation for proof input hasing
- Loading branch information
Showing
24 changed files
with
527 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright (c) 2015-2021 Clearmatics Technologies Ltd | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0+ | ||
|
||
from zeth.core.mimc import MiMC7, MiMC31 | ||
from zeth.core.input_hasher import InputHasher | ||
from unittest import TestCase | ||
|
||
DUMMY_INPUT_VALUES = [-1, 0, 1] | ||
|
||
|
||
class TestInputHasher(TestCase): | ||
|
||
def test_input_hasher_simple(self) -> None: | ||
# Some very simple cases | ||
mimc = MiMC7() | ||
input_hasher = InputHasher(mimc, 7) | ||
self.assertEqual(mimc.hash_int(7, 0), input_hasher.hash([])) | ||
self.assertEqual( | ||
mimc.hash_int(mimc.hash_int(7, 1), 1), input_hasher.hash([1])) | ||
self.assertEqual( | ||
mimc.hash_int( | ||
mimc.hash_int( | ||
mimc.hash_int(7, 1), 2), | ||
2), | ||
input_hasher.hash([1, 2])) | ||
|
||
def test_input_hasher_mimc7(self) -> None: | ||
mimc = MiMC7() | ||
input_hasher = InputHasher(mimc) | ||
values = [x % mimc.prime for x in DUMMY_INPUT_VALUES] | ||
# pylint:disable=line-too-long | ||
expect = 5568471640435576440988459485125198359192118312228711462978763973844457667180 # noqa | ||
# pylint:enable=line-too-long | ||
self.assertEqual(expect, input_hasher.hash(values)) | ||
|
||
def test_input_hasher_mimc31(self) -> None: | ||
mimc = MiMC31() | ||
input_hasher = InputHasher(mimc) | ||
values = [x % mimc.prime for x in DUMMY_INPUT_VALUES] | ||
# pylint: disable=line-too-long | ||
expect = 1029772481427643815119825324071277815354972734622711297984795198139876181749 # noqa | ||
# pylint: enable=line-too-long | ||
self.assertEqual(expect, input_hasher.hash(values)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright (c) 2015-2021 Clearmatics Technologies Ltd | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0+ | ||
|
||
from zeth.core.mimc import MiMCBase | ||
from typing import List | ||
|
||
|
||
# Default seed, generated as: | ||
# zeth.core.mimc._keccak_256( | ||
# zeth.core.mimc._str_to_bytes("clearmatics_hash_seed")) | ||
DEFAULT_IV_UINT256 = \ | ||
13196537064117388418196223856311987714388543839552400408340921397545324034315 | ||
|
||
|
||
class InputHasher: | ||
""" | ||
Note that this is currently experimental code. Hash a series of field | ||
elements via the Merkle-Damgard construction on a MiMC compression | ||
function. Note that since this function only accepts whole numbers of | ||
scalar field elements, there is no ambiguity w.r.t to padding and we could | ||
technically omit the finalization step. It has been kept for now, to allow | ||
time for further consideration, and in case the form of the hasher changes | ||
(e.g. in case we want to be able to hash arbitrary bit strings in the | ||
future). | ||
""" | ||
def __init__(self, compression_fn: MiMCBase, iv: int = DEFAULT_IV_UINT256): | ||
assert compression_fn.prime < (2 << 256) | ||
self._compression_fn = compression_fn | ||
self._iv = iv % compression_fn.prime | ||
|
||
def hash(self, values: List[int]) -> int: | ||
current = self._iv | ||
for m in values: | ||
current = self._compression_fn.hash_int(current, m) | ||
return self._compression_fn.hash_int(current, len(values)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule libsnark
updated
from 39bb0b to fa306c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.