Comments addressed, cosmetics

This commit is contained in:
Yuriy Glukhov 2018-12-09 10:25:02 +02:00 committed by zah
parent 61f81fb0c4
commit 10ed2bd5b9
2 changed files with 15 additions and 20 deletions

View File

@ -25,11 +25,14 @@ type
"Nimbus will automatically add the extensions .privkey and .pubkey.", "Nimbus will automatically add the extensions .privkey and .pubkey.",
shorthand: "v".}: seq[ValidatorKeyPath] shorthand: "v".}: seq[ValidatorKeyPath]
proc readFileBytes(path: string): seq[byte] =
cast[seq[byte]](readFile(path))
proc loadPrivKey*(p: ValidatorKeyPath): ValidatorPrivKey = proc loadPrivKey*(p: ValidatorKeyPath): ValidatorPrivKey =
initSigKey(cast[seq[byte]](readFile(string(p) & ".privkey"))) initSigKey(readFileBytes(string(p) & ".privkey"))
proc loadRandao*(p: ValidatorKeyPath): Randao = proc loadRandao*(p: ValidatorKeyPath): Randao =
initRandao(cast[seq[byte]](readFile(string(p) & ".randao"))) initRandao(readFileBytes(string(p) & ".randao"))
proc parse*(T: type ValidatorKeyPath, input: TaintedString): T = proc parse*(T: type ValidatorKeyPath, input: TaintedString): T =
result = T(input) result = T(input)

View File

@ -3,7 +3,7 @@ import spec/digest
type Randao* = object type Randao* = object
seed: Eth2Digest seed: Eth2Digest
const MaxRandaoLevels = 10000 const MaxRandaoLevels = 10000 # TODO: This number is arbitrary
proc initRandao*(seed: Eth2Digest): Randao = proc initRandao*(seed: Eth2Digest): Randao =
result.seed = seed result.seed = seed
@ -16,29 +16,21 @@ proc initRandao*(bytes: openarray[byte]): Randao =
initRandao(bytes) initRandao(bytes)
func repeatHash*(h: Eth2Digest, n: int): Eth2Digest = func repeatHash*(h: Eth2Digest, n: int): Eth2Digest =
if n == 0: h result = h
else: repeatHash(eth2Hash(h.data), n - 1) var n = n
while n != 0:
iterator items(r: Randao): Eth2Digest = result = eth2hash(result.data)
var h = r.seed dec n
for i in 0 .. MaxRandaoLevels:
yield h
h = eth2hash(h.data)
proc initialCommitment*(r: Randao): Eth2Digest = proc initialCommitment*(r: Randao): Eth2Digest =
var i = 0 repeatHash(r.seed, MaxRandaoLevels)
for h in r:
if i == MaxRandaoLevels:
return h
inc i
assert(false, "Unreachable")
proc reveal*(r: Randao, commitment: Eth2Digest): Eth2Digest = proc reveal*(r: Randao, commitment: Eth2Digest): Eth2Digest =
if commitment == r.seed: if commitment == r.seed:
raise newException(Exception, "Randao: cannot reveal for seed") raise newException(Exception, "Randao: cannot reveal for seed")
result = r.seed result = r.seed
for h in r: for i in 0 .. MaxRandaoLevels:
let h = eth2hash(result.data)
if h == commitment: if h == commitment:
return return
result = h result = h
@ -61,4 +53,4 @@ when isMainModule:
echo "reveal: ", rev echo "reveal: ", rev
echo "Took time: ", e - s echo "Took time: ", e - s
echo r.reveal(eth2hash([1.byte, 2, 3])) echo r.reveal(eth2hash([1.byte, 2, 3])) # Should raise