2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2020-07-02 16:15:27 +00:00
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# Mocking validator public and private keys
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
import
|
2020-07-07 15:51:02 +00:00
|
|
|
bearssl, eth/keys,
|
|
|
|
blscurve/bls_signature_scheme,
|
2020-07-07 23:02:14 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, crypto, presets]
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-07-07 15:51:02 +00:00
|
|
|
proc newKeyPair(rng: var BrHmacDrbgContext): BlsResult[tuple[pub: ValidatorPubKey, priv: ValidatorPrivKey]] =
|
|
|
|
## Generates a new public-private keypair
|
|
|
|
## This requires entropy on the system
|
|
|
|
# The input-keying-material requires 32 bytes at least for security
|
|
|
|
# The generation is deterministic and the input-keying-material
|
|
|
|
# must be protected against side-channel attacks
|
|
|
|
|
|
|
|
var ikm: array[32, byte]
|
|
|
|
brHmacDrbgGenerate(rng, ikm)
|
|
|
|
|
|
|
|
var
|
|
|
|
sk: SecretKey
|
|
|
|
pk: bls_signature_scheme.PublicKey
|
|
|
|
if keyGen(ikm, pk, sk):
|
|
|
|
ok((ValidatorPubKey(kind: Real, blsValue: pk), ValidatorPrivKey(sk)))
|
|
|
|
else:
|
|
|
|
err "bls: cannot generate keypair"
|
|
|
|
|
2019-11-29 09:17:18 +00:00
|
|
|
# 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`
|
2020-07-07 23:02:14 +00:00
|
|
|
proc genMockPrivKeys(privkeys: var openarray[ValidatorPrivKey]) =
|
2020-07-07 15:51:02 +00:00
|
|
|
let rng = newRng()
|
2020-03-11 13:50:08 +00:00
|
|
|
for i in 0 ..< privkeys.len:
|
2020-07-07 15:51:02 +00:00
|
|
|
let pair = newKeyPair(rng[])[]
|
2020-03-11 13:50:08 +00:00
|
|
|
privkeys[i] = pair.priv
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-07-08 12:36:03 +00:00
|
|
|
func genMockPubKeys(pubkeys: var openarray[ValidatorPubKey],
|
|
|
|
privkeys: openarray[ValidatorPrivKey]) =
|
2020-03-11 13:50:08 +00:00
|
|
|
for i in 0 ..< privkeys.len:
|
2020-04-11 08:51:07 +00:00
|
|
|
pubkeys[i] = toPubKey(privkeys[i])
|
2020-03-11 13:50:08 +00:00
|
|
|
|
|
|
|
# Ref array necessary to limit stack usage / binary size
|
2020-07-07 23:02:14 +00:00
|
|
|
var MockPrivKeys* = newSeq[ValidatorPrivKey](defaultRuntimePreset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT)
|
|
|
|
genMockPrivKeys(MockPrivKeys)
|
2020-03-11 13:50:08 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
var MockPubKeys* = newSeq[ValidatorPubKey](defaultRuntimePreset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT)
|
|
|
|
genMockPubKeys(MockPubKeys, MockPrivKeys)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
type MockKey = ValidatorPrivKey or ValidatorPubKey
|
|
|
|
|
|
|
|
template `[]`*[N: static int](a: array[N, MockKey], idx: ValidatorIndex): MockKey =
|
|
|
|
a[idx.int]
|
2020-03-11 13:50:08 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
echo "========================================"
|
|
|
|
echo "Mock keys"
|
2020-07-07 23:02:14 +00:00
|
|
|
for i in 0 ..< defaultRuntimePreset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT:
|
2020-03-11 13:50:08 +00:00
|
|
|
echo " validator ", i
|
|
|
|
echo " seckey: ", MockPrivKeys[i].toHex()
|
|
|
|
echo " pubkey: ", MockPubKeys[i]
|