Add {common, prover, verifier}.py

This commit is contained in:
Benjamin Loison 2023-04-10 17:41:23 +02:00
commit 11daae87ad
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8
3 changed files with 51 additions and 0 deletions

1
common.py Normal file
View File

@ -0,0 +1 @@
SECURITY_PARAMETER = 256

39
prover.py Normal file
View File

@ -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

11
verifier.py Normal file
View File

@ -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.