fixed algo direction for respective vdf actions

This commit is contained in:
Kevin Froman 2020-03-21 16:01:47 -05:00
parent 28d1deb301
commit 2e5f7d5907
3 changed files with 14 additions and 15 deletions

View File

@ -25,26 +25,27 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
DEFAULT_ROUNDS = 8000
def _sha3_256_hash(data: bytes) -> str:
def _sha3_256_hash(data: bytes) -> int:
sha3 = sha3_256()
sha3.update(data)
return sha3.hexdigest()
return int.from_bytes(sha3.digest(), byteorder='big')
def vdf_create(data: bytes, rounds: int = DEFAULT_ROUNDS) -> str:
assert rounds > 1
input_hash = int(_sha3_256_hash(data), 16)
return hex(forward_mimc(input_hash, rounds)).replace('0x', '')
input_data: int = _sha3_256_hash(data)
return hex(reverse_mimc(input_data, rounds)).replace('0x', '')
def vdf_verify(
data: bytes,
test_hash: Union[str, bytes],
test_hash: str,
rounds: int = DEFAULT_ROUNDS) -> bool:
"""Verify data for test_hash generated by vdf_create."""
assert rounds > 1
return _sha3_256_hash(data) == \
hex(reverse_mimc(int(test_hash, 16), rounds)).replace('0x', '')
should_match = _sha3_256_hash(data)
test_hash = int(test_hash, 16)
return forward_mimc(test_hash, rounds) == should_match
def profile_cpu_speed(seconds=1) -> float:

View File

@ -1,8 +1,6 @@
"""Mimc hash function."""
"""
Mimc hash function.
"""
"""
This code adapted from https://github.com/OlegJakushkin/deepblockchains/blob/master/vdf/mimc/python/mimc.py by Sourabh Niyogi https://github.com/sourabhniyogi
This module adapted from https://github.com/OlegJakushkin/deepblockchains/blob/master/vdf/mimc/python/mimc.py by Sourabh Niyogi https://github.com/sourabhniyogi
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
@ -21,15 +19,15 @@ modulus = 2**256 - 2**32 * 351 + 1
little_fermat_expt = (modulus*2-1)//3
round_constants = [(i**7) ^ 42 for i in range(64)]
# Forward MiMC
def forward_mimc(inp: int, steps: int) -> int:
for i in range(1,steps):
inp = (inp**3 + round_constants[i % len(round_constants)]) % modulus
return inp
def reverse_mimc(mimc_output: int, steps: int) -> int:
rtrace = mimc_output
def reverse_mimc(input_data: int, steps: int) -> int:
rtrace = input_data
for i in range(steps - 1, 0, -1):
rtrace = pow(rtrace-round_constants[i%len(round_constants)],

View File

@ -1,7 +1,7 @@
from setuptools import setup, find_packages
setup(name='mimcvdf',
version='0.0.0',
version='1.0.0',
description='Generic high level VDF using MiMC',
author='Kevin Froman',
author_email='beardog@mailbox.org',