fixed algo direction for respective vdf actions
This commit is contained in:
parent
28d1deb301
commit
2e5f7d5907
@ -25,26 +25,27 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
DEFAULT_ROUNDS = 8000
|
DEFAULT_ROUNDS = 8000
|
||||||
|
|
||||||
|
|
||||||
def _sha3_256_hash(data: bytes) -> str:
|
def _sha3_256_hash(data: bytes) -> int:
|
||||||
sha3 = sha3_256()
|
sha3 = sha3_256()
|
||||||
sha3.update(data)
|
sha3.update(data)
|
||||||
return sha3.hexdigest()
|
return int.from_bytes(sha3.digest(), byteorder='big')
|
||||||
|
|
||||||
|
|
||||||
def vdf_create(data: bytes, rounds: int = DEFAULT_ROUNDS) -> str:
|
def vdf_create(data: bytes, rounds: int = DEFAULT_ROUNDS) -> str:
|
||||||
assert rounds > 1
|
assert rounds > 1
|
||||||
input_hash = int(_sha3_256_hash(data), 16)
|
input_data: int = _sha3_256_hash(data)
|
||||||
return hex(forward_mimc(input_hash, rounds)).replace('0x', '')
|
return hex(reverse_mimc(input_data, rounds)).replace('0x', '')
|
||||||
|
|
||||||
|
|
||||||
def vdf_verify(
|
def vdf_verify(
|
||||||
data: bytes,
|
data: bytes,
|
||||||
test_hash: Union[str, bytes],
|
test_hash: str,
|
||||||
rounds: int = DEFAULT_ROUNDS) -> bool:
|
rounds: int = DEFAULT_ROUNDS) -> bool:
|
||||||
"""Verify data for test_hash generated by vdf_create."""
|
"""Verify data for test_hash generated by vdf_create."""
|
||||||
assert rounds > 1
|
assert rounds > 1
|
||||||
return _sha3_256_hash(data) == \
|
should_match = _sha3_256_hash(data)
|
||||||
hex(reverse_mimc(int(test_hash, 16), rounds)).replace('0x', '')
|
test_hash = int(test_hash, 16)
|
||||||
|
return forward_mimc(test_hash, rounds) == should_match
|
||||||
|
|
||||||
|
|
||||||
def profile_cpu_speed(seconds=1) -> float:
|
def profile_cpu_speed(seconds=1) -> float:
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
|
"""Mimc hash function."""
|
||||||
"""
|
"""
|
||||||
Mimc hash function.
|
This module adapted from https://github.com/OlegJakushkin/deepblockchains/blob/master/vdf/mimc/python/mimc.py by Sourabh Niyogi https://github.com/sourabhniyogi
|
||||||
"""
|
|
||||||
"""
|
|
||||||
This code 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
|
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
|
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
|
little_fermat_expt = (modulus*2-1)//3
|
||||||
round_constants = [(i**7) ^ 42 for i in range(64)]
|
round_constants = [(i**7) ^ 42 for i in range(64)]
|
||||||
|
|
||||||
# Forward MiMC
|
|
||||||
def forward_mimc(inp: int, steps: int) -> int:
|
def forward_mimc(inp: int, steps: int) -> int:
|
||||||
for i in range(1,steps):
|
for i in range(1,steps):
|
||||||
inp = (inp**3 + round_constants[i % len(round_constants)]) % modulus
|
inp = (inp**3 + round_constants[i % len(round_constants)]) % modulus
|
||||||
return inp
|
return inp
|
||||||
|
|
||||||
|
|
||||||
def reverse_mimc(mimc_output: int, steps: int) -> int:
|
def reverse_mimc(input_data: int, steps: int) -> int:
|
||||||
rtrace = mimc_output
|
rtrace = input_data
|
||||||
|
|
||||||
for i in range(steps - 1, 0, -1):
|
for i in range(steps - 1, 0, -1):
|
||||||
rtrace = pow(rtrace-round_constants[i%len(round_constants)],
|
rtrace = pow(rtrace-round_constants[i%len(round_constants)],
|
||||||
|
2
setup.py
2
setup.py
@ -1,7 +1,7 @@
|
|||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
setup(name='mimcvdf',
|
setup(name='mimcvdf',
|
||||||
version='0.0.0',
|
version='1.0.0',
|
||||||
description='Generic high level VDF using MiMC',
|
description='Generic high level VDF using MiMC',
|
||||||
author='Kevin Froman',
|
author='Kevin Froman',
|
||||||
author_email='beardog@mailbox.org',
|
author_email='beardog@mailbox.org',
|
||||||
|
Loading…
Reference in New Issue
Block a user