commit 11daae87ada421e286a9261ca4758b96e0c00262 Author: Benjamin Loison Date: Mon Apr 10 17:41:23 2023 +0200 Add `{common, prover, verifier}.py` diff --git a/common.py b/common.py new file mode 100644 index 0000000..85f18f7 --- /dev/null +++ b/common.py @@ -0,0 +1 @@ +SECURITY_PARAMETER = 256 \ No newline at end of file diff --git a/prover.py b/prover.py new file mode 100644 index 0000000..c43ebcb --- /dev/null +++ b/prover.py @@ -0,0 +1,39 @@ +# Proof of Space-Time prover + +import math, common +from hashlib import sha256 + +SPACE_TO_PROVE_IN_BITS = 1_000 +MAXIMUM_HASH_DIFFICULTY = 0.1 + +# 1. Receive a random bitstring from the verifier. + +protocolExecutionId = input('protocolExecutionId: ') + +# 2. Generate in a table the data that the prover has to store. +# To ensure that this data is cheaper to store than to generate and to allow public verifiability by requiring the stored data to be Proof of Works. +# For the ease of this implementation, the data are stored in the memory instead of the disk. + +STORED_DATA = [] +STORED_DATA_IN_BITS = 0 +counter = 0 + +LEAST_DIFFICULT_HASH = int('F' * (common.SECURITY_PARAMETER // 4), 16) + +# We enforce `STORED_DATA` to have its length being a power of 2, to ease Merkle tree implementation. +while STORED_DATA_IN_BITS < SPACE_TO_PROVE_IN_BITS or not math.log2(len(STORED_DATA)).is_integer(): + print(STORED_DATA_IN_BITS, len(STORED_DATA)) + # Note that for a production ready system hashing speedup (as designed for Bitcoin in [AsicBoost - A Speedup for Bitcoin Mining, Dr. Timo Hanke, March 31, 2016 (rev. 5)](https://arxiv.org/pdf/1604.00575.pdf)) should be considered. + hash = sha256((protocolExecutionId + str(counter)).encode('ascii')).hexdigest() + hashInteger = int(hash, 16) + hashDifficulty = hashInteger / LEAST_DIFFICULT_HASH + if hashDifficulty <= MAXIMUM_HASH_DIFFICULTY: + lastCounter = STORED_DATA[-1] if STORED_DATA != [] else -1 + deltaCounter = counter - lastCounter + deltaCounterBits = math.ceil(math.log2(deltaCounter)) + STORED_DATA += [deltaCounter] + STORED_DATA_IN_BITS += deltaCounterBits + print(hash, hashDifficulty, counter, lastCounter, deltaCounterBits) + counter += 1 + + diff --git a/verifier.py b/verifier.py new file mode 100644 index 0000000..95ab792 --- /dev/null +++ b/verifier.py @@ -0,0 +1,11 @@ +# Proof of Space-Time verifier + +import secrets, common + +# 1. Generate a random bitstring to send to the prover. +# To make sure that the following protocol execution can't be used partially or totally by the prover to reduce the cost of another protocol execution. + +protocolExecutionId = ''.join(secrets.choice('0123456789abcdef') for _ in range(common.SECURITY_PARAMETER // 4)) +print(protocolExecutionId) + +# 2. \ No newline at end of file