From 28dc8a6a29b65005062e4371446b0ba388114a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Wed, 11 Mar 2020 14:50:08 +0100 Subject: [PATCH] Fix mock signatures/validator keys and stack smashing --- beacon_chain/spec/crypto.nim | 10 ++--- tests/mocking/mock_attestations.nim | 6 +-- tests/mocking/mock_validator_keys.nim | 38 ++++++++++++++----- .../test_process_attestation.nim | 1 - vendor/nim-blscurve | 2 +- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/beacon_chain/spec/crypto.nim b/beacon_chain/spec/crypto.nim index aa9357f76..82c7c2d5f 100644 --- a/beacon_chain/spec/crypto.nim +++ b/beacon_chain/spec/crypto.nim @@ -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 diff --git a/tests/mocking/mock_attestations.nim b/tests/mocking/mock_attestations.nim index 6feb63bd9..1130096c2 100644 --- a/tests/mocking/mock_attestations.nim +++ b/tests/mocking/mock_attestations.nim @@ -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() diff --git a/tests/mocking/mock_validator_keys.nim b/tests/mocking/mock_validator_keys.nim index 07424083f..1421c676a 100644 --- a/tests/mocking/mock_validator_keys.nim +++ b/tests/mocking/mock_validator_keys.nim @@ -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] diff --git a/tests/spec_block_processing/test_process_attestation.nim b/tests/spec_block_processing/test_process_attestation.nim index 685be33fa..3d044ff12 100644 --- a/tests/spec_block_processing/test_process_attestation.nim +++ b/tests/spec_block_processing/test_process_attestation.nim @@ -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 - diff --git a/vendor/nim-blscurve b/vendor/nim-blscurve index 9a143350f..e6849cd57 160000 --- a/vendor/nim-blscurve +++ b/vendor/nim-blscurve @@ -1 +1 @@ -Subproject commit 9a143350f107b9bbd8c4d9bb4807e3b470aa232b +Subproject commit e6849cd5703fd1e5632432066fef93a97941462d