Proof Of Elapsed Time
User computes VDF and submits on-chain.
import hashlib
import time
class VerifiableDelayFunction:
def __init__(self, x, C, T_target):
"""
Initialize the VDF parameters.
:param x: Initial input value for the VDF function (string).
:param C: Random challenge issued (string).
:param T_target: Target time in seconds for the VDF computation (float).
"""
self.x = x
self.C = C
self.T_target = T_target
self.T_prime = self.calibrate_steps()
def hash_function(self, input_value):
"""
Compute SHA-256 hash of the input value.
:param input_value: Input value to hash (string).
:return: Hexadecimal hash digest (string).
"""
return hashlib.sha256(input_value.encode()).hexdigest()
def calibrate_steps(self):
"""
Calibrate the number of sequential steps (T') to approximate T_target time.
:return: Number of sequential steps T' (int).
"""
test_iterations = 1000
start_time = time.time()
# Perform a test run of 1000 hash computations
v = self.hash_function(self.x + self.C)
for _ in range(test_iterations):
v = self.hash_function(v)
end_time = time.time()
time_per_hash = (end_time - start_time) / test_iterations
# Calculate T' based on the target time and average time per hash
T_prime = int(self.T_target / time_per_hash)
print(f"Calibrated T' to {T_prime} steps for target time {self.T_target} seconds.")
return T_prime
def compute_sequence(self):
"""
Perform the sequential hash computations to obtain the final value (timestamp).
:return: The final value v_T' (string), and a list of intermediate values (list).
"""
v = self.hash_function(self.x + self.C)
sequence = [v]
# Perform T' sequential steps
for _ in range(1, self.T_prime):
v = self.hash_function(v)
sequence.append(v)
v_T_prime = sequence[-1]
return v_T_prime, sequence
def prove(self):
"""
Generate the proof by computing the final value and selected intermediate values.
:return: Proof containing the final value v_T' and selected checkpoints (dict).
"""
v_T_prime, sequence = self.compute_sequence()
# Select checkpoints for verification (e.g., every 10% of T')
checkpoints = {}
step_interval = max(1, self.T_prime // 10)
for i in range(0, self.T_prime, step_interval):
checkpoints[i] = sequence[i]
proof = {
"v_T_prime": v_T_prime,
"checkpoints": checkpoints
}
return proof
def verify(self, proof):
"""
Verify the proof by recomputing the sequence up to the final value.
:param proof: The proof containing v_T' and checkpoints (dict).
:return: Boolean indicating whether the proof is valid (bool).
"""
v_T_prime = proof["v_T_prime"]
checkpoints = proof["checkpoints"]
# Recompute the sequence and verify checkpoints
v = self.hash_function(self.x + self.C)
for i in range(1, self.T_prime):
v = self.hash_function(v)
# Check against the provided checkpoints
if i in checkpoints and checkpoints[i] != v:
print(f"Verification failed at step {i}.")
return False
# Check the final value
if v != v_T_prime:
print("Final value does not match the proof.")
return False
print("Proof successfully verified.")
return True
# Example usage
if __name__ == "__main__":
# Parameters
x = "example_input_value"
C = "random_challenge_string"
T_target = 5.0 # Target time in seconds
# Initialize VDF
vdf = VerifiableDelayFunction(x, C, T_target)
# Generate proof
proof = vdf.prove()
print("Proof generated:", proof)
# Verify proof
is_valid = vdf.verify(proof)
print("Verification result:", is_valid)Last updated