Added support for decimal output

This commit is contained in:
Kevin Froman 2020-04-22 01:50:54 -05:00
parent 2e5f7d5907
commit 727714f6b3
4 changed files with 28 additions and 6 deletions

View File

@ -2,6 +2,7 @@
This project uses Semantic Versioning
## 0.0.0
## 1.1.0
Added support for decimal output
Initial release

View File

@ -31,10 +31,12 @@ def _sha3_256_hash(data: bytes) -> int:
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, dec=False) -> str:
assert rounds > 1
input_data: int = _sha3_256_hash(data)
return hex(reverse_mimc(input_data, rounds)).replace('0x', '')
if not dec:
return hex(reverse_mimc(input_data, rounds)).replace('0x', '')
return reverse_mimc(input_data, rounds)
def vdf_verify(
@ -44,7 +46,10 @@ def vdf_verify(
"""Verify data for test_hash generated by vdf_create."""
assert rounds > 1
should_match = _sha3_256_hash(data)
test_hash = int(test_hash, 16)
try:
test_hash = int(test_hash, 16)
except TypeError:
pass
return forward_mimc(test_hash, rounds) == should_match

View File

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

View File

@ -22,5 +22,21 @@ class TestVDF(unittest.TestCase):
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", 1)
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", -10000)
def test_dec_false(self):
for i in range(3):
h = mimcvdf.vdf_create(os.urandom(i), 2, dec=False)
self.assertTrue(h.isalnum())
self.assertTrue(len(h) == 63 or len(h) == 64)
self.assertIs(type(h), str)
def test_dec_true(self):
for i in range(3):
h = mimcvdf.vdf_create(os.urandom(i), 2, dec=True)
self.assertIs(type(h), int)
def test_dec_true_verify(self):
h = mimcvdf.vdf_create(b"test", dec=True)
self.assertTrue(mimcvdf.vdf_verify(b"test", h))
unittest.main()