2019-05-23 07:41:17 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard libs
|
2019-09-03 03:12:09 +00:00
|
|
|
os, unittest, endians,
|
2019-06-17 09:29:23 +00:00
|
|
|
# Status libs
|
2019-07-07 09:53:58 +00:00
|
|
|
blscurve, stew/byteutils,
|
2019-05-23 07:41:17 +00:00
|
|
|
# Beacon chain internals
|
|
|
|
../../beacon_chain/spec/crypto,
|
|
|
|
# Test utilities
|
2019-09-03 03:12:09 +00:00
|
|
|
./fixtures_utils
|
2019-05-23 07:41:17 +00:00
|
|
|
|
2019-06-17 09:29:23 +00:00
|
|
|
type
|
|
|
|
BLSPrivToPub* = object
|
|
|
|
input*: ValidatorPrivKey
|
|
|
|
output*: ValidatorPubKey
|
|
|
|
|
|
|
|
BLSSignMsgInput = object
|
|
|
|
privkey*: ValidatorPrivKey
|
|
|
|
message*: seq[byte]
|
|
|
|
domain*: Domain
|
|
|
|
|
|
|
|
BLSSignMsg* = object
|
|
|
|
input*: BLSSignMsgInput
|
|
|
|
output*: Signature
|
|
|
|
|
|
|
|
BLSAggSig* = object
|
|
|
|
input*: seq[Signature]
|
|
|
|
output*: Signature
|
|
|
|
|
|
|
|
BLSAggPubKey* = object
|
|
|
|
input*: seq[ValidatorPubKey]
|
|
|
|
output*: ValidatorPubKey
|
|
|
|
|
|
|
|
proc readValue*(r: var JsonReader, a: var Domain) {.inline.} =
|
|
|
|
## Custom deserializer for Domain
|
|
|
|
# Furthermore Nim parseHex doesn't support uint
|
|
|
|
# until https://github.com/nim-lang/Nim/pull/11067
|
|
|
|
# (0.20)
|
2019-09-25 17:07:08 +00:00
|
|
|
a = hexToPaddedByteArray[8](r.readValue(string))
|
2019-06-17 09:29:23 +00:00
|
|
|
|
2019-09-03 03:12:09 +00:00
|
|
|
const BLSDir = JsonTestsDir/"general"/"phase0"/"bls"
|
2019-05-23 07:41:17 +00:00
|
|
|
|
|
|
|
suite "Official - BLS tests":
|
|
|
|
test "Private to public key conversion":
|
2019-09-03 03:12:09 +00:00
|
|
|
for file in walkDirRec(BLSDir/"priv_to_pub"):
|
2019-11-01 08:46:50 +00:00
|
|
|
let t = parseTest(file, Json, BLSPrivToPub)
|
|
|
|
let implResult = t.input.pubkey()
|
|
|
|
check: implResult == t.output
|
2019-05-23 07:41:17 +00:00
|
|
|
|
|
|
|
test "Message signing":
|
2019-09-03 03:12:09 +00:00
|
|
|
for file in walkDirRec(BLSDir/"sign_msg"):
|
|
|
|
let t = parseTest(file, Json, BLSSignMsg)
|
2019-05-23 07:41:17 +00:00
|
|
|
let implResult = t.input.privkey.bls_sign(
|
|
|
|
t.input.message,
|
2019-09-25 17:07:08 +00:00
|
|
|
t.input.domain
|
2019-09-03 03:12:09 +00:00
|
|
|
)
|
2019-05-23 07:41:17 +00:00
|
|
|
check: implResult == t.output
|
|
|
|
|
|
|
|
test "Aggregating signatures":
|
2019-09-03 03:12:09 +00:00
|
|
|
for file in walkDirRec(BLSDir/"aggregate_sigs"):
|
|
|
|
let t = parseTest(file, Json, BLSAggSig)
|
2019-05-23 07:41:17 +00:00
|
|
|
let implResult = t.input.combine()
|
|
|
|
check: implResult == t.output
|
|
|
|
|
|
|
|
test "Aggregating public keys":
|
2019-09-03 03:12:09 +00:00
|
|
|
for file in walkDirRec(BLSDir/"aggregate_pubkeys"):
|
|
|
|
let t = parseTest(file, Json, BLSAggPubKey)
|
2019-05-23 07:41:17 +00:00
|
|
|
let implResult = t.input.combine()
|
|
|
|
check: implResult == t.output
|
2019-09-03 03:12:09 +00:00
|
|
|
|
|
|
|
# TODO: msg_hash_compressed and uncompressed
|