From 26bad82e06e64fa37eebf9b69bd5127150962796 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 18 Mar 2020 19:50:34 -0500 Subject: [PATCH] improved CPU profiling and added cli utility to calc rounds for given seconds --- README.md | 8 +++++++- mimcvdf/__init__.py | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0e3071f..bda2210 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,15 @@

Simple Verifiable Delay Function using MiMC

+## 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. + ## Security -This code has not been audited for security. It is not currently recommended for protecting against anything but denial of service, even though MiMC is able to do much more. +This code has not been audited for security. It is not currently recommended for protecting against anything but denial of service, even though MiMC is able to do much more. If used, make sure it is not protecting anything critical. + +In addition, since this is a Python implementation, attackers can be faster than typical users of an application by using faster language implementations or even FPGAs. diff --git a/mimcvdf/__init__.py b/mimcvdf/__init__.py index ea0b5b9..1ec393f 100644 --- a/mimcvdf/__init__.py +++ b/mimcvdf/__init__.py @@ -1,7 +1,9 @@ """High level Verifiable Delay Function using keccak (sha3).""" from hashlib import sha3_256 from typing import Union -import timeit +import time +from statistics import mean +from math import ceil from .mimc import forward_mimc, reverse_mimc """ @@ -45,5 +47,17 @@ def vdf_verify( 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) +def profile_cpu_speed(rounds=1000, seconds=1) -> float: + time_results = [] + for _ in range(10000): + start = time.time() + vdf_create(b"t", rounds) + end = time.time() + time_results.append(end - start) + return (seconds / mean(time_results) * 1000) + + +if __name__ == "__main__": + print("Calculate how may rounds are needed for X seconds (influenced by system processes): ") + seconds = int(input("Seconds: ")) + print("Rounds:", ceil(profile_cpu_speed()))