2018-11-28 19:49:03 +00:00
|
|
|
# beacon_chain
|
2020-03-14 21:54:45 +00:00
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
2018-11-28 19:49:03 +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).
|
2018-11-28 19:49:03 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# At the time of writing, the exact definitions of what should be used for
|
|
|
|
# cryptography in the spec is in flux, with sizes and test vectors still being
|
|
|
|
# hashed out. This layer helps isolate those chagnes.
|
|
|
|
|
2019-12-10 14:20:40 +00:00
|
|
|
# BLS signatures can be combined such that multiple signatures are aggregated.
|
|
|
|
# Each time a new signature is added, the corresponding public key must be
|
|
|
|
# added to the verification key as well - if a key signs twice, it must be added
|
|
|
|
# twice to the verification key. Aggregated signatures can be combined
|
|
|
|
# arbitrarily (like addition) as long as public keys are aggregated in the same
|
|
|
|
# way.
|
2018-12-27 20:14:37 +00:00
|
|
|
#
|
2019-12-10 14:20:40 +00:00
|
|
|
# In eth2, we use a single bit to record which keys have signed, thus we cannot
|
|
|
|
# combined overlapping aggregates - ie if we have an aggregate of signatures of
|
|
|
|
# A, B and C, and another with B, C and D, we cannot practically combine them
|
|
|
|
# even if in theory it is possible to allow this in BLS.
|
2018-12-27 20:14:37 +00:00
|
|
|
|
2018-11-28 19:49:03 +00:00
|
|
|
import
|
2020-03-04 21:27:11 +00:00
|
|
|
# Internal
|
|
|
|
./digest,
|
|
|
|
# Status
|
|
|
|
stew/[endians2, objects, byteutils],
|
|
|
|
nimcrypto/[utils, sysrand],
|
2019-03-25 16:46:31 +00:00
|
|
|
blscurve, json_serialization,
|
2020-03-04 21:27:11 +00:00
|
|
|
chronicles,
|
|
|
|
# Standard library
|
|
|
|
hashes
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
export
|
2019-03-01 13:34:37 +00:00
|
|
|
json_serialization
|
2019-02-05 16:13:29 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
# export
|
|
|
|
# blscurve.init, blscurve.getBytes, blscurve.combine,
|
|
|
|
# blscurve.`$`, blscurve.`==`,
|
|
|
|
# blscurve.Signature
|
|
|
|
|
|
|
|
# Type definitions
|
|
|
|
# ----------------------------------------------------------------------
|
2018-11-28 19:49:03 +00:00
|
|
|
|
|
|
|
type
|
2019-07-03 07:35:05 +00:00
|
|
|
BlsValueType* = enum
|
|
|
|
Real
|
|
|
|
OpaqueBlob
|
|
|
|
|
|
|
|
BlsValue*[T] = object
|
|
|
|
# TODO This is a temporary type needed until we sort out the
|
|
|
|
# issues with invalid BLS values appearing in the SSZ test suites.
|
|
|
|
case kind*: BlsValueType
|
|
|
|
of Real:
|
|
|
|
blsValue*: T
|
|
|
|
of OpaqueBlob:
|
|
|
|
when T is blscurve.Signature:
|
|
|
|
blob*: array[96, byte]
|
|
|
|
else:
|
|
|
|
blob*: array[48, byte]
|
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
ValidatorPubKey* = BlsValue[blscurve.PublicKey]
|
|
|
|
# Alternatives
|
|
|
|
# ValidatorPubKey* = blscurve.PublicKey
|
|
|
|
# ValidatorPubKey* = array[48, byte]
|
|
|
|
# The use of byte arrays proved to be a dead end pretty quickly.
|
|
|
|
# Plenty of code needs to be modified for a successful build and
|
|
|
|
# the changes will negatively affect the performance.
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
ValidatorPrivKey* = blscurve.SecretKey
|
|
|
|
# ValidatorPrivKey* = BlsValue[blscurve.SecretKey]
|
2019-07-03 07:35:05 +00:00
|
|
|
|
|
|
|
ValidatorSig* = BlsValue[blscurve.Signature]
|
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
BlsCurveType* = PublicKey|SecretKey|Signature
|
2019-02-05 16:13:29 +00:00
|
|
|
ValidatorPKI* = ValidatorPrivKey|ValidatorPubKey|ValidatorSig
|
2018-11-29 01:08:34 +00:00
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
func `==`*(a, b: BlsValue): bool =
|
|
|
|
if a.kind != b.kind: return false
|
|
|
|
if a.kind == Real:
|
|
|
|
return a.blsValue == b.blsValue
|
|
|
|
else:
|
|
|
|
return a.blob == b.blob
|
|
|
|
|
|
|
|
template `==`*[T](a: BlsValue[T], b: T): bool =
|
|
|
|
a.blsValue == b
|
|
|
|
|
|
|
|
template `==`*[T](a: T, b: BlsValue[T]): bool =
|
|
|
|
a == b.blsValue
|
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
# API
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/specs/phase0/beacon-chain.md#bls-signatures
|
|
|
|
|
|
|
|
func pubKey*(privkey: ValidatorPrivKey): ValidatorPubKey =
|
|
|
|
## Create a private key from a public key
|
|
|
|
# Un-specced in either hash-to-curve or Eth2
|
|
|
|
# TODO: Test suite should use `keyGen` instead
|
2019-07-03 07:35:05 +00:00
|
|
|
when ValidatorPubKey is BlsValue:
|
2020-03-04 21:27:11 +00:00
|
|
|
ValidatorPubKey(kind: Real, blsValue: privkey.privToPub())
|
2019-07-03 07:35:05 +00:00
|
|
|
elif ValidatorPubKey is array:
|
2020-03-04 21:27:11 +00:00
|
|
|
privkey.getKey.getBytes
|
2019-07-03 07:35:05 +00:00
|
|
|
else:
|
2020-03-04 21:27:11 +00:00
|
|
|
privkey.getKey
|
|
|
|
|
2020-03-30 23:40:24 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#bls-signatures
|
2020-03-04 21:27:11 +00:00
|
|
|
func aggregate*[T](values: openarray[ValidatorSig]): ValidatorSig =
|
|
|
|
## Aggregate arrays of sequences of Validator Signatures
|
|
|
|
## This assumes that they are real signatures
|
|
|
|
|
|
|
|
result = BlsValue[T](kind: Real, blsValue: values[0].BlsValue)
|
|
|
|
|
|
|
|
for i in 1 ..< values.len:
|
|
|
|
result.blsValue.aggregate(values[i].blsValue)
|
|
|
|
|
|
|
|
func aggregate*(x: var ValidatorSig, other: ValidatorSig) =
|
|
|
|
## Aggregate 2 Validator Signatures
|
|
|
|
## This assumes that they are real signatures
|
|
|
|
x.blsValue.aggregate(other.blsValue)
|
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/specs/phase0/beacon-chain.md#bls-signatures
|
|
|
|
func blsVerify*(
|
|
|
|
pubkey: ValidatorPubKey, message: openArray[byte],
|
|
|
|
signature: ValidatorSig): bool =
|
|
|
|
## Check that a signature is valid for a message
|
|
|
|
## under the provided public key.
|
|
|
|
## returns `true` if the signature is valid, `false` otherwise.
|
|
|
|
##
|
|
|
|
## The proof-of-possession MUST be verified before calling this function.
|
|
|
|
## It is recommended to use the overload that accepts a proof-of-possession
|
|
|
|
## to enforce correct usage.
|
|
|
|
if signature.kind != Real:
|
|
|
|
# Invalid signatures are possible in deposits (discussed with Danny)
|
|
|
|
return false
|
|
|
|
if pubkey.kind != Real:
|
|
|
|
# TODO: chronicles warning
|
|
|
|
return false
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
# TODO: remove fully if the comment below is not true anymore and
|
|
|
|
# and we don't need this workaround
|
|
|
|
# # TODO bls_verify_multiple(...) used to have this workaround, and now it
|
|
|
|
# # lives here. No matter the signature, there's also no meaningful way to
|
|
|
|
# # verify it -- it's a kind of vacuous truth. No pubkey/sig pairs. Sans a
|
|
|
|
# # getBytes() or similar mechanism, pubKey == default(ValidatorPubKey) is
|
|
|
|
# # a way to create many false positive matches. This seems odd.
|
|
|
|
# if pubkey.getBytes() == default(ValidatorPubKey).getBytes():
|
|
|
|
# return true
|
|
|
|
pubkey.blsValue.verify(message, signature.blsValue)
|
|
|
|
|
|
|
|
func blsSign*(privkey: ValidatorPrivKey, message: openarray[byte]): ValidatorSig =
|
|
|
|
## Computes a signature from a secret key and a message
|
|
|
|
ValidatorSig(kind: Real, blsValue: privkey.sign(message))
|
|
|
|
|
|
|
|
func blsFastAggregateVerify*[T: byte|char](
|
|
|
|
publicKeys: openarray[ValidatorPubKey],
|
|
|
|
message: openarray[T],
|
|
|
|
signature: ValidatorSig
|
|
|
|
): bool =
|
|
|
|
## Verify the aggregate of multiple signatures on the same message
|
|
|
|
## This function is faster than AggregateVerify
|
|
|
|
##
|
|
|
|
## The proof-of-possession MUST be verified before calling this function.
|
|
|
|
## It is recommended to use the overload that accepts a proof-of-possession
|
|
|
|
## to enforce correct usage.
|
|
|
|
# TODO: Note: `invalid` in the following paragraph means invalid by construction
|
|
|
|
# The keys/signatures are not even points on the elliptic curves.
|
|
|
|
# To respect both the IETF API and the fact that
|
|
|
|
# we can have invalid public keys (as in not point on the elliptic curve),
|
|
|
|
# requiring a wrapper indirection,
|
|
|
|
# we need a first pass to extract keys from the wrapper
|
|
|
|
# and then call fastAggregateVerify.
|
|
|
|
# Instead:
|
|
|
|
# - either we expose a new API: context + init-update-finish
|
|
|
|
# in blscurve which already exists internally
|
|
|
|
# - or at network/databases/serialization boundaries we do not
|
|
|
|
# allow invalid BLS objects to pollute consensus routines
|
|
|
|
if signature.kind != Real:
|
|
|
|
return false
|
|
|
|
var unwrapped: seq[PublicKey]
|
|
|
|
for pubkey in publicKeys:
|
|
|
|
if pubkey.kind != Real:
|
|
|
|
return false
|
|
|
|
unwrapped.add pubkey.blsValue
|
|
|
|
return fastAggregateVerify(unwrapped, message, signature.blsValue)
|
2019-09-10 22:03:06 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
proc newKeyPair*(): tuple[pub: ValidatorPubKey, priv: ValidatorPrivKey] {.noInit.}=
|
|
|
|
## 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
|
2019-12-06 12:05:00 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
var ikm: array[32, byte]
|
|
|
|
let written = randomBytes(ikm)
|
|
|
|
doAssert written >= 32, "Key generation failure"
|
2019-09-10 22:03:06 +00:00
|
|
|
|
2020-03-04 22:26:39 +00:00
|
|
|
result.pub = ValidatorPubKey(kind: Real)
|
2020-03-04 21:27:11 +00:00
|
|
|
doAssert keyGen(ikm, result.pub.blsValue, result.priv), "Key generation failure"
|
2018-11-29 01:08:34 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
# Logging
|
|
|
|
# ----------------------------------------------------------------------
|
2018-12-05 13:58:41 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
func shortLog*(x: BlsValue): string =
|
2020-03-19 16:18:48 +00:00
|
|
|
## Logging for wrapped BLS types
|
|
|
|
## that may contain valid or non-validated data
|
|
|
|
# The prefix must be short
|
|
|
|
# due to the mechanics of the `shortLog` function.
|
|
|
|
if x.kind == Real:
|
|
|
|
x.blsValue.toHex()[0..7]
|
|
|
|
else:
|
|
|
|
"raw: " & x.blob.toHex(lowercase = true)[0..7]
|
2018-12-11 17:55:45 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
func shortLog*(x: BlsCurveType): string =
|
2020-03-19 16:18:48 +00:00
|
|
|
## Logging for raw unwrapped BLS types
|
2020-03-04 21:27:11 +00:00
|
|
|
($x)[0..7]
|
2019-12-06 12:05:00 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
proc toGaugeValue*(hash: Eth2Digest): int64 =
|
|
|
|
# Only the last 8 bytes are taken into consideration in accordance
|
|
|
|
# to the ETH2 metrics spec:
|
|
|
|
# https://github.com/ethereum/eth2.0-metrics/blob/6a79914cb31f7d54858c7dd57eee75b6162ec737/metrics.md#interop-metrics
|
|
|
|
cast[int64](uint64.fromBytesLE(hash.data[24..31]))
|
|
|
|
|
|
|
|
# Codecs
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
func `$`*(x: BlsValue): string =
|
2020-03-11 13:50:08 +00:00
|
|
|
# The prefix must be short
|
|
|
|
# due to the mechanics of the `shortLog` function.
|
2020-03-04 21:27:11 +00:00
|
|
|
if x.kind == Real:
|
2020-03-19 16:18:48 +00:00
|
|
|
x.blsValue.toHex()
|
2019-07-03 07:35:05 +00:00
|
|
|
else:
|
2020-03-19 16:18:48 +00:00
|
|
|
"raw: " & x.blob.toHex(lowercase = true)
|
2020-03-04 21:27:11 +00:00
|
|
|
|
|
|
|
func getBytes*(x: BlsValue): auto =
|
|
|
|
if x.kind == Real:
|
|
|
|
x.blsValue.exportRaw()
|
|
|
|
else:
|
|
|
|
x.blob
|
|
|
|
|
|
|
|
func initFromBytes[T](val: var BlsValue[T], bytes: openarray[byte]) =
|
2019-09-05 14:27:28 +00:00
|
|
|
# This is a workaround, so that we can deserialize the serialization of a
|
|
|
|
# default-initialized BlsValue without raising an exception
|
2019-09-05 19:52:34 +00:00
|
|
|
when defined(ssz_testing):
|
2019-09-09 03:33:24 +00:00
|
|
|
# Only for SSZ parsing tests, everything is an opaque blob
|
2020-03-11 16:06:06 +00:00
|
|
|
val = BlsValue[T](kind: OpaqueBlob, blob: toArray(val.blob.len, bytes))
|
2019-07-03 07:35:05 +00:00
|
|
|
else:
|
2019-09-09 03:33:24 +00:00
|
|
|
# Try if valid BLS value
|
2019-11-09 10:46:34 +00:00
|
|
|
# TODO: address the side-effects in nim-blscurve
|
2020-03-04 21:27:11 +00:00
|
|
|
val = BlsValue[T](kind: Real)
|
|
|
|
let success = val.blsValue.fromBytes(bytes)
|
2019-09-09 03:33:24 +00:00
|
|
|
if not success:
|
|
|
|
# TODO: chronicles trace
|
2020-03-04 21:27:11 +00:00
|
|
|
val = BlsValue[T](kind: OpaqueBlob)
|
|
|
|
val.blob[val.blob.low .. val.blob.high] = bytes
|
|
|
|
|
|
|
|
func initFromBytes*(val: var ValidatorPrivKey, bytes: openarray[byte]) {.inline.} =
|
|
|
|
discard val.fromBytes(bytes)
|
|
|
|
|
|
|
|
func fromBytes[T](R: type BlsValue[T], bytes: openarray[byte]): R {.inline.}=
|
|
|
|
result.initFromBytes(bytes)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-04-06 18:18:42 +00:00
|
|
|
func fromBytes[T](R: var BlsValue[T], bytes: openarray[byte]) {.inline.}=
|
|
|
|
# This version is only to support tests/test_interop.nim
|
|
|
|
R.initFromBytes(bytes)
|
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
func fromHex*[T](R: var BlsValue[T], hexStr: string) {.inline.} =
|
|
|
|
## Initialize a BLSValue from its hex representation
|
|
|
|
R.fromBytes(hexStr.hexToSeqByte())
|
2019-10-01 13:44:38 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
# Hashing
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
func hash*(x: BlsValue): Hash {.inline.} =
|
|
|
|
# TODO: we can probably just slice the BlsValue
|
|
|
|
if x.kind == Real:
|
|
|
|
hash x.blsValue.exportRaw()
|
|
|
|
else:
|
|
|
|
hash x.blob
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
template hash*(x: BlsCurveType): Hash =
|
|
|
|
# TODO: prevent using secret keys
|
|
|
|
bind getBytes
|
|
|
|
hash(getBytes(x))
|
|
|
|
|
|
|
|
# Serialization
|
|
|
|
# ----------------------------------------------------------------------
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter, value: ValidatorPubKey) {.inline.} =
|
2020-03-12 01:11:48 +00:00
|
|
|
doAssert value.kind == Real
|
|
|
|
writer.writeValue(value.blsValue.toHex())
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var ValidatorPubKey) {.inline.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
value.initFromBytes(fromHex reader.readValue(string))
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter, value: ValidatorSig) {.inline.} =
|
2020-03-12 01:11:48 +00:00
|
|
|
if value.kind == Real:
|
|
|
|
writer.writeValue(value.blsValue.toHex())
|
2019-07-03 07:35:05 +00:00
|
|
|
else:
|
2020-03-12 01:11:48 +00:00
|
|
|
# Workaround: https://github.com/status-im/nim-beacon-chain/issues/374
|
|
|
|
let asHex = value.blob.toHex(lowercase = true)
|
|
|
|
# echo "[Warning] writing raw opaque signature: ", asHex
|
|
|
|
writer.writeValue(asHex)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var ValidatorSig) {.inline.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
value.initFromBytes(fromHex reader.readValue(string))
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter, value: ValidatorPrivKey) {.inline.} =
|
2020-03-12 01:11:48 +00:00
|
|
|
writer.writeValue(value.toHex())
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var ValidatorPrivKey) {.inline.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
value.initFromBytes(fromHex reader.readValue(string))
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter, value: PublicKey) {.inline.} =
|
2020-03-12 01:11:48 +00:00
|
|
|
writer.writeValue(value.toHex())
|
2019-08-07 03:09:26 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
proc readValue*(reader: var JsonReader, value: var PublicKey) {.inline.} =
|
|
|
|
let hex = reader.readValue(string)
|
|
|
|
let ok = value.fromHex(hex)
|
|
|
|
doAssert ok, "Invalid public key: " & hex
|
2019-08-07 03:09:26 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter, value: Signature) {.inline.} =
|
2020-03-12 01:11:48 +00:00
|
|
|
writer.writeValue(value.toHex())
|
2019-08-07 03:09:26 +00:00
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var Signature) {.inline.} =
|
2020-03-04 21:27:11 +00:00
|
|
|
let hex = reader.readValue(string)
|
|
|
|
let ok = value.fromHex(hex)
|
|
|
|
doAssert ok, "Invalid signature: " & hex
|
2019-11-09 10:46:34 +00:00
|
|
|
|
|
|
|
template fromSszBytes*(T: type BlsValue, bytes: openarray[byte]): auto =
|
|
|
|
fromBytes(T, bytes)
|
2020-03-04 21:27:11 +00:00
|
|
|
|
2020-03-04 22:13:37 +00:00
|
|
|
# Initialization
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
# For confutils
|
2020-03-04 22:41:21 +00:00
|
|
|
func init*(T: typedesc[ValidatorPrivKey], hex: string): T {.noInit, inline.} =
|
2020-03-04 21:27:11 +00:00
|
|
|
let success = result.fromHex(hex)
|
|
|
|
doAssert success, "Private key is invalid" # Don't display private keys even if invalid
|
2020-03-04 22:13:37 +00:00
|
|
|
|
|
|
|
# For mainchain monitor
|
2020-03-04 22:41:21 +00:00
|
|
|
func init*(T: typedesc[ValidatorPubKey], data: array[48, byte]): T {.noInit, inline.} =
|
|
|
|
result.initFromBytes(data)
|
|
|
|
|
|
|
|
# For mainchain monitor
|
|
|
|
func init*(T: typedesc[ValidatorSig], data: array[96, byte]): T {.noInit, inline.} =
|
|
|
|
result.initFromBytes(data)
|