Fix mock signatures/validator keys and stack smashing

This commit is contained in:
Mamy André-Ratsimbazafy 2020-03-11 14:50:08 +01:00 committed by tersec
parent 65d668c3f6
commit 28dc8a6a29
5 changed files with 36 additions and 21 deletions

View File

@ -217,12 +217,12 @@ proc toGaugeValue*(hash: Eth2Digest): int64 =
# ----------------------------------------------------------------------
func `$`*(x: BlsValue): string =
# The prefix must be short
# due to the mechanics of the `shortLog` function.
if x.kind == Real:
"r: 0x" & x.blsValue.toHex()
"real: 0x" & x.blsValue.toHex()
else:
# r: is short for random. The prefix must be short
# due to the mechanics of the `shortLog` function.
"r: 0x" & x.blob.toHex(lowercase = true)
"raw: 0x" & x.blob.toHex(lowercase = true)
func getBytes*(x: BlsValue): auto =
if x.kind == Real:
@ -235,7 +235,7 @@ func initFromBytes[T](val: var BlsValue[T], bytes: openarray[byte]) =
# default-initialized BlsValue without raising an exception
when defined(ssz_testing):
# Only for SSZ parsing tests, everything is an opaque blob
R(kind: OpaqueBlob, blob: toArray(result.blob.len, bytes))
val = BlsValue[T](kind: OpaqueBlob, blob: toArray(result.blob.len, bytes))
else:
# Try if valid BLS value
# TODO: address the side-effects in nim-blscurve

View File

@ -58,16 +58,14 @@ proc get_attestation_signature(
attestation_data: AttestationData,
privkey: ValidatorPrivKey
): ValidatorSig =
let msg = attestation_data.hash_tree_root()
let domain = get_domain(
state = state,
domain_type = DOMAIN_BEACON_ATTESTER,
message_epoch = attestation_data.target.epoch
)
let signing_root = compute_signing_root(msg, domain)
let signing_root = compute_signing_root(attestation_data, domain)
return bls_sign(privkey, signing_root.data)
return blsSign(privkey, signing_root.data)
proc signMockAttestation*(state: BeaconState, attestation: var Attestation) =
var cache = get_empty_per_epoch_cache()

View File

@ -14,20 +14,38 @@ import
# this is being indexed inside "mock_deposits.nim" by a value up to `validatorCount`
# which is `num_validators` which is `MIN_GENESIS_ACTIVE_VALIDATOR_COUNT`
let MockPrivKeys* = block:
var privkeys: array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPrivKey]
for pk in privkeys.mitems():
proc genMockPrivKeys(privkeys: var array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPrivKey]) =
for i in 0 ..< privkeys.len:
let pair = newKeyPair()
pk = pair.priv
privkeys
privkeys[i] = pair.priv
let MockPubKeys* = block:
var pubkeys: array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPubKey]
for idx, privkey in MockPrivKeys:
pubkeys[idx] = pubkey(privkey)
pubkeys
proc genMockPubKeys(
pubkeys: var array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPubKey],
privkeys: array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPrivKey]
) =
for i in 0 ..< privkeys.len:
pubkeys[i] = pubkey(privkeys[i])
# Ref array necessary to limit stack usage / binary size
var MockPrivKeys*: ref array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPrivKey]
new MockPrivKeys
genMockPrivKeys(MockPrivKeys[])
var MockPubKeys*: ref array[MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, ValidatorPubKey]
new MockPubKeys
genMockPubKeys(MockPubKeys[], MockPrivKeys[])
type MockKey = ValidatorPrivKey or ValidatorPubKey
template `[]`*[N: static int](a: array[N, MockKey], idx: ValidatorIndex): MockKey =
a[idx.int]
when isMainModule:
from blscurve import toHex
echo "========================================"
echo "Mock keys"
for i in 0 ..< MIN_GENESIS_ACTIVE_VALIDATOR_COUNT:
echo " validator ", i
echo " seckey: ", MockPrivKeys[i].toHex()
echo " pubkey: ", MockPubKeys[i]

View File

@ -109,4 +109,3 @@ suite "[Unit - Spec - Block processing] Attestations " & preset():
# - bad source root
# - inconsistent custody bits length
# - non-empty custody bits in phase 0

2
vendor/nim-blscurve vendored

@ -1 +1 @@
Subproject commit 9a143350f107b9bbd8c4d9bb4807e3b470aa232b
Subproject commit e6849cd5703fd1e5632432066fef93a97941462d