nimbus-eth2/beacon_chain/randao.nim

50 lines
1.2 KiB
Nim
Raw Normal View History

import spec/[digest, helpers]
2018-12-08 14:17:47 +00:00
type Randao* = object
seed*: Eth2Digest
2018-12-08 14:17:47 +00:00
2018-12-09 08:25:02 +00:00
const MaxRandaoLevels = 10000 # TODO: This number is arbitrary
2018-12-08 14:17:47 +00:00
proc initRandao*(seed: Eth2Digest): Randao =
result.seed = seed
proc initRandao*(bytes: openarray[byte]): Randao =
if bytes.len != sizeof(Eth2Digest):
raise newException(Exception, "Wrong randao size")
var s: Eth2Digest
s.data[0 .. ^1] = bytes
2019-01-25 14:17:35 +00:00
initRandao(s)
2018-12-08 14:17:47 +00:00
proc initialCommitment*(r: Randao): Eth2Digest =
2018-12-09 08:25:02 +00:00
repeatHash(r.seed, MaxRandaoLevels)
2018-12-08 14:17:47 +00:00
proc reveal*(r: Randao, commitment: Eth2Digest): Eth2Digest =
if commitment == r.seed:
raise newException(Exception, "Randao: cannot reveal for seed")
result = r.seed
2018-12-09 08:25:02 +00:00
for i in 0 .. MaxRandaoLevels:
let h = eth2hash(result.data)
2018-12-08 14:17:47 +00:00
if h == commitment:
return
result = h
raise newException(Exception, "Randao: commitment not found")
when isMainModule:
import times, nimcrypto
var seed: Eth2Digest
let r = initRandao(seed)
var s = epochTime()
var ic = r.initialCommitment()
var e = epochTime()
echo "initialCommitment: ", ic
echo "Took time: ", e - s
s = epochTime()
let rev = r.reveal(ic)
e = epochTime()
echo "reveal: ", rev
echo "Took time: ", e - s
2018-12-09 08:25:02 +00:00
echo r.reveal(eth2hash([1.byte, 2, 3])) # Should raise