fixed profiling
This commit is contained in:
parent
26bad82e06
commit
f86b9e1850
20
README.md
20
README.md
@ -1,12 +1,30 @@
|
|||||||
<h1 align="center">mimcvdf ⏲️</h1>
|
<h1 align="center">mimcvdf ⏲️</h1>
|
||||||
|
|
||||||
<p align="center"><i>Simple <a href="https://eprint.iacr.org/2018/601.pdf">Verifiable Delay Function<a> using <a href="https://eprint.iacr.org/2016/492.pdf">MiMC</a></i></p>
|
<p align="center"><i>Simple <a href="https://eprint.iacr.org/2018/601.pdf">Verifiable Delay Function</a> using <a href="https://eprint.iacr.org/2016/492.pdf">MiMC</a></i></p>
|
||||||
|
|
||||||
## Applications
|
## Applications
|
||||||
|
|
||||||
This module was created for use in reducing spam in a similar manner to [HashCash](https://en.wikipedia.org/wiki/Hashcash). However, some potential uses for VDFs include blockchains and verifiable lotteries.
|
This module was created for use in reducing spam in a similar manner to [HashCash](https://en.wikipedia.org/wiki/Hashcash). However, some potential uses for VDFs include blockchains and verifiable lotteries.
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
from mimchash import vdf_create, vdf_verify
|
||||||
|
|
||||||
|
|
||||||
|
# Get a mimc hash of a byte sequence
|
||||||
|
|
||||||
|
vdf_create(byte_data, round_count) # Returns hex string
|
||||||
|
|
||||||
|
|
||||||
|
# Verify a mimc hash (must use same round count)
|
||||||
|
|
||||||
|
vdf_verify(same_bytes_data, vdf_create_result, rounds)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ def _sha3_256_hash(data: bytes) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def vdf_create(data: bytes, rounds: int = DEFAULT_ROUNDS) -> str:
|
def vdf_create(data: bytes, rounds: int = DEFAULT_ROUNDS) -> str:
|
||||||
assert rounds > 0
|
assert rounds > 1
|
||||||
input_hash = int(_sha3_256_hash(data), 16)
|
input_hash = int(_sha3_256_hash(data), 16)
|
||||||
return hex(forward_mimc(input_hash, rounds)).replace('0x', '')
|
return hex(forward_mimc(input_hash, rounds)).replace('0x', '')
|
||||||
|
|
||||||
@ -42,22 +42,33 @@ def vdf_verify(
|
|||||||
test_hash: Union[str, bytes],
|
test_hash: Union[str, bytes],
|
||||||
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 > 0
|
assert rounds > 1
|
||||||
return _sha3_256_hash(data) == \
|
return _sha3_256_hash(data) == \
|
||||||
hex(reverse_mimc(int(test_hash, 16), rounds)).replace('0x', '')
|
hex(reverse_mimc(int(test_hash, 16), rounds)).replace('0x', '')
|
||||||
|
|
||||||
|
|
||||||
def profile_cpu_speed(rounds=1000, seconds=1) -> float:
|
def profile_cpu_speed(seconds=1) -> float:
|
||||||
time_results = []
|
n = 2
|
||||||
for _ in range(10000):
|
start = time.time()
|
||||||
start = time.time()
|
done = False
|
||||||
vdf_create(b"t", rounds)
|
results = []
|
||||||
end = time.time()
|
try:
|
||||||
time_results.append(end - start)
|
for _ in range(20):
|
||||||
return (seconds / mean(time_results) * 1000)
|
done = False
|
||||||
|
n = 2
|
||||||
|
start = time.time()
|
||||||
|
while not done:
|
||||||
|
vdf_create(b't', n)
|
||||||
|
if time.time() - start >= seconds:
|
||||||
|
break
|
||||||
|
n += 1
|
||||||
|
results.append(n)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
return ceil(mean(results))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Calculate how may rounds are needed for X seconds (influenced by system processes): ")
|
print("Calculate how may rounds are needed for X seconds (influenced by system processes): ")
|
||||||
seconds = int(input("Seconds: "))
|
seconds = int(input("Seconds: "))
|
||||||
print("Rounds:", ceil(profile_cpu_speed()))
|
print("Rounds:", profile_cpu_speed(seconds))
|
||||||
|
27
tests/test_mimc.py
Normal file
27
tests/test_mimc.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import sys
|
||||||
|
import os
|
||||||
|
sys.path.append('..')
|
||||||
|
import unittest
|
||||||
|
import time
|
||||||
|
from hashlib import sha3_256
|
||||||
|
|
||||||
|
import mimcvdf.mimc
|
||||||
|
|
||||||
|
class TestMimc(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_bytes(self):
|
||||||
|
start = time.time()
|
||||||
|
data = b"a" * 6000000
|
||||||
|
h = sha3_256()
|
||||||
|
h.update(data)
|
||||||
|
data = int(h.hexdigest(), 16)
|
||||||
|
|
||||||
|
forw = mimcvdf.forward_mimc(data, 2000)
|
||||||
|
|
||||||
|
rev = mimcvdf.reverse_mimc(forw, 2000)
|
||||||
|
|
||||||
|
print(data)
|
||||||
|
print(forw, rev)
|
||||||
|
self.assertEqual(rev, data)
|
||||||
|
|
||||||
|
unittest.main()
|
@ -2,7 +2,7 @@ import sys
|
|||||||
import os
|
import os
|
||||||
sys.path.append('..')
|
sys.path.append('..')
|
||||||
import unittest
|
import unittest
|
||||||
import timeit
|
from time import time
|
||||||
|
|
||||||
import mimcvdf
|
import mimcvdf
|
||||||
|
|
||||||
@ -19,11 +19,8 @@ class TestVDF(unittest.TestCase):
|
|||||||
def test_above_zero_rounds(self):
|
def test_above_zero_rounds(self):
|
||||||
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", 0)
|
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", 0)
|
||||||
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", -1)
|
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", -1)
|
||||||
|
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", 1)
|
||||||
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", -10000)
|
self.assertRaises(AssertionError, mimcvdf.vdf_create, b"test", -10000)
|
||||||
|
|
||||||
def test_profile(self):
|
|
||||||
rand = os.urandom(1000)
|
|
||||||
self.assertAlmostEqual(timeit.timeit(lambda: mimcvdf.vdf_create(b"test", 1000), number=100), mimcvdf.profile_cpu_speed(1000), places=2)
|
|
||||||
self.assertAlmostEqual(timeit.timeit(lambda: mimcvdf.vdf_create(rand, 1000), number=100), mimcvdf.profile_cpu_speed(1000), places=2)
|
|
||||||
|
|
||||||
unittest.main()
|
unittest.main()
|
Loading…
Reference in New Issue
Block a user