mimcvdf/mimcvdf/__init__.py

50 lines
1.6 KiB
Python

"""High level Verifiable Delay Function using keccak (sha3)."""
from hashlib import sha3_256
from typing import Union
import timeit
from .mimc import forward_mimc, reverse_mimc
"""
Kevin Froman 2020
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
DEFAULT_ROUNDS = 8000
def _sha3_256_hash(data: bytes) -> str:
sha3 = sha3_256()
sha3.update(data)
return sha3.hexdigest()
def vdf_create(data: bytes, rounds: int = DEFAULT_ROUNDS) -> str:
assert rounds > 0
input_hash = int(_sha3_256_hash(data), 16)
return hex(forward_mimc(input_hash, rounds)).replace('0x', '')
def vdf_verify(
data: bytes,
test_hash: Union[str, bytes],
rounds: int = DEFAULT_ROUNDS) -> bool:
"""Verify data for test_hash generated by vdf_create."""
assert rounds > 0
return _sha3_256_hash(data) == \
hex(reverse_mimc(int(test_hash, 16), rounds)).replace('0x', '')
def profile_cpu_speed(rounds=1000) -> float:
return timeit.timeit(lambda: vdf_create(b"test", rounds), number=100)