Bump `milagro_bls_binding` to 1.2.0

Also verify it in BLS test generator
This commit is contained in:
Hsiao-Wei Wang 2020-05-21 01:35:40 +08:00
parent 21a4143d99
commit f72d14a747
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
3 changed files with 30 additions and 7 deletions

View File

@ -503,8 +503,7 @@ setup(
"eth-typing>=2.1.0,<3.0.0", "eth-typing>=2.1.0,<3.0.0",
"pycryptodome==3.9.4", "pycryptodome==3.9.4",
"py_ecc==4.0.0", "py_ecc==4.0.0",
"milagro_bls_binding==1.0.2", "milagro_bls_binding==1.2.0",
"py_ecc==4.0.0",
"dataclasses==0.6", "dataclasses==0.6",
"remerkleable==0.1.13", "remerkleable==0.1.13",
"ruamel.yaml==0.16.5", "ruamel.yaml==0.16.5",

View File

@ -10,7 +10,8 @@ bls = py_ecc_bls
STUB_SIGNATURE = b'\x11' * 96 STUB_SIGNATURE = b'\x11' * 96
STUB_PUBKEY = b'\x22' * 48 STUB_PUBKEY = b'\x22' * 48
STUB_COORDINATES = _signature_to_G2(bls.Sign(0, b"")) Z2_SIGNATURE = b'\xc0' + b'\x00' * 95
STUB_COORDINATES = _signature_to_G2(Z2_SIGNATURE)
def only_with_bls(alt_return=None): def only_with_bls(alt_return=None):
@ -67,7 +68,7 @@ def Sign(SK, message):
if bls == py_ecc_bls: if bls == py_ecc_bls:
return bls.Sign(SK, message) return bls.Sign(SK, message)
else: else:
return bls.Sign(SK.to_bytes(48, 'big'), message) return bls.Sign(SK.to_bytes(32, 'big'), message)
@only_with_bls(alt_return=STUB_COORDINATES) @only_with_bls(alt_return=STUB_COORDINATES)

View File

@ -2,18 +2,22 @@
BLS test vectors generator BLS test vectors generator
""" """
from hashlib import sha256
from typing import Tuple, Iterable, Any, Callable, Dict from typing import Tuple, Iterable, Any, Callable, Dict
from eth_utils import ( from eth_utils import (
encode_hex, encode_hex,
int_to_big_endian, int_to_big_endian,
) )
from gen_base import gen_runner, gen_typing import milagro_bls_binding as milagro_bls
from eth2spec.utils import bls from eth2spec.utils import bls
from hashlib import sha256
from eth2spec.test.context import PHASE0 from eth2spec.test.context import PHASE0
from gen_base import gen_runner, gen_typing
def to_bytes(i):
return i.to_bytes(32, "big")
def hash(x): def hash(x):
@ -70,8 +74,15 @@ def case02_verify():
# Valid signature # Valid signature
signature = bls.Sign(privkey, message) signature = bls.Sign(privkey, message)
pubkey = bls.SkToPk(privkey) pubkey = bls.SkToPk(privkey)
assert milagro_bls.SkToPk(to_bytes(privkey)) == pubkey
assert milagro_bls.Sign(to_bytes(privkey), message) == signature
identifier = f'{encode_hex(pubkey)}_{encode_hex(message)}' identifier = f'{encode_hex(pubkey)}_{encode_hex(message)}'
assert bls.Verify(pubkey, message, signature) assert bls.Verify(pubkey, message, signature)
assert milagro_bls.Verify(pubkey, message, signature)
yield f'verify_valid_case_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', { yield f'verify_valid_case_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', {
'input': { 'input': {
'pubkey': encode_hex(pubkey), 'pubkey': encode_hex(pubkey),
@ -85,6 +96,7 @@ def case02_verify():
wrong_pubkey = bls.SkToPk(PRIVKEYS[(i + 1) % len(PRIVKEYS)]) wrong_pubkey = bls.SkToPk(PRIVKEYS[(i + 1) % len(PRIVKEYS)])
identifier = f'{encode_hex(wrong_pubkey)}_{encode_hex(message)}' identifier = f'{encode_hex(wrong_pubkey)}_{encode_hex(message)}'
assert not bls.Verify(wrong_pubkey, message, signature) assert not bls.Verify(wrong_pubkey, message, signature)
assert not milagro_bls.Verify(wrong_pubkey, message, signature)
yield f'verify_wrong_pubkey_case_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', { yield f'verify_wrong_pubkey_case_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', {
'input': { 'input': {
'pubkey': encode_hex(wrong_pubkey), 'pubkey': encode_hex(wrong_pubkey),
@ -98,6 +110,7 @@ def case02_verify():
tampered_signature = signature[:-4] + b'\xFF\xFF\xFF\xFF' tampered_signature = signature[:-4] + b'\xFF\xFF\xFF\xFF'
identifier = f'{encode_hex(pubkey)}_{encode_hex(message)}' identifier = f'{encode_hex(pubkey)}_{encode_hex(message)}'
assert not bls.Verify(pubkey, message, tampered_signature) assert not bls.Verify(pubkey, message, tampered_signature)
assert not milagro_bls.Verify(pubkey, message, tampered_signature)
yield f'verify_tampered_signature_case_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', { yield f'verify_tampered_signature_case_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', {
'input': { 'input': {
'pubkey': encode_hex(pubkey), 'pubkey': encode_hex(pubkey),
@ -109,6 +122,7 @@ def case02_verify():
# Valid pubkey and signature with the point at infinity # Valid pubkey and signature with the point at infinity
assert bls.Verify(Z1_PUBKEY, message, Z2_SIGNATURE) assert bls.Verify(Z1_PUBKEY, message, Z2_SIGNATURE)
assert milagro_bls.Verify(Z1_PUBKEY, message, Z2_SIGNATURE)
yield f'verify_infinity_pubkey_and_infinity_signature', { yield f'verify_infinity_pubkey_and_infinity_signature', {
'input': { 'input': {
'pubkey': encode_hex(Z1_PUBKEY), 'pubkey': encode_hex(Z1_PUBKEY),
@ -152,6 +166,7 @@ def case04_fast_aggregate_verify():
# Valid signature # Valid signature
identifier = f'{pubkeys_serial}_{encode_hex(message)}' identifier = f'{pubkeys_serial}_{encode_hex(message)}'
assert bls.FastAggregateVerify(pubkeys, message, aggregate_signature) assert bls.FastAggregateVerify(pubkeys, message, aggregate_signature)
assert milagro_bls.FastAggregateVerify(pubkeys, message, aggregate_signature)
yield f'fast_aggregate_verify_valid_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', { yield f'fast_aggregate_verify_valid_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', {
'input': { 'input': {
'pubkeys': pubkeys_serial, 'pubkeys': pubkeys_serial,
@ -166,6 +181,7 @@ def case04_fast_aggregate_verify():
pubkeys_extra_serial = [encode_hex(pubkey) for pubkey in pubkeys_extra] pubkeys_extra_serial = [encode_hex(pubkey) for pubkey in pubkeys_extra]
identifier = f'{pubkeys_extra_serial}_{encode_hex(message)}' identifier = f'{pubkeys_extra_serial}_{encode_hex(message)}'
assert not bls.FastAggregateVerify(pubkeys_extra, message, aggregate_signature) assert not bls.FastAggregateVerify(pubkeys_extra, message, aggregate_signature)
assert not milagro_bls.FastAggregateVerify(pubkeys_extra, message, aggregate_signature)
yield f'fast_aggregate_verify_extra_pubkey_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', { yield f'fast_aggregate_verify_extra_pubkey_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', {
'input': { 'input': {
'pubkeys': pubkeys_extra_serial, 'pubkeys': pubkeys_extra_serial,
@ -179,6 +195,7 @@ def case04_fast_aggregate_verify():
tampered_signature = aggregate_signature[:-4] + b'\xff\xff\xff\xff' tampered_signature = aggregate_signature[:-4] + b'\xff\xff\xff\xff'
identifier = f'{pubkeys_serial}_{encode_hex(message)}' identifier = f'{pubkeys_serial}_{encode_hex(message)}'
assert not bls.FastAggregateVerify(pubkeys, message, tampered_signature) assert not bls.FastAggregateVerify(pubkeys, message, tampered_signature)
assert not milagro_bls.FastAggregateVerify(pubkeys, message, tampered_signature)
yield f'fast_aggregate_verify_tampered_signature_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', { yield f'fast_aggregate_verify_tampered_signature_{(hash(bytes(identifier, "utf-8"))[:8]).hex()}', {
'input': { 'input': {
'pubkeys': pubkeys_serial, 'pubkeys': pubkeys_serial,
@ -190,6 +207,7 @@ def case04_fast_aggregate_verify():
# Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == Z1_SIGNATURE # Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == Z1_SIGNATURE
assert not bls.FastAggregateVerify([], message, Z2_SIGNATURE) assert not bls.FastAggregateVerify([], message, Z2_SIGNATURE)
assert not milagro_bls.FastAggregateVerify([], message, Z2_SIGNATURE)
yield f'fast_aggregate_verify_na_pubkeys_and_infinity_signature', { yield f'fast_aggregate_verify_na_pubkeys_and_infinity_signature', {
'input': { 'input': {
'pubkeys': [], 'pubkeys': [],
@ -201,6 +219,7 @@ def case04_fast_aggregate_verify():
# Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == 0x00... # Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == 0x00...
assert not bls.FastAggregateVerify([], message, NO_SIGNATURE) assert not bls.FastAggregateVerify([], message, NO_SIGNATURE)
assert not milagro_bls.FastAggregateVerify([], message, NO_SIGNATURE)
yield f'fast_aggregate_verify_na_pubkeys_and_na_signature', { yield f'fast_aggregate_verify_na_pubkeys_and_na_signature', {
'input': { 'input': {
'pubkeys': [], 'pubkeys': [],
@ -228,6 +247,7 @@ def case05_aggregate_verify():
aggregate_signature = bls.Aggregate(sigs) aggregate_signature = bls.Aggregate(sigs)
assert bls.AggregateVerify(pubkeys, messages, aggregate_signature) assert bls.AggregateVerify(pubkeys, messages, aggregate_signature)
assert milagro_bls.AggregateVerify(pubkeys, messages, aggregate_signature)
yield f'aggregate_verify_valid', { yield f'aggregate_verify_valid', {
'input': { 'input': {
'pubkeys': pubkeys_serial, 'pubkeys': pubkeys_serial,
@ -239,6 +259,7 @@ def case05_aggregate_verify():
tampered_signature = aggregate_signature[:4] + b'\xff\xff\xff\xff' tampered_signature = aggregate_signature[:4] + b'\xff\xff\xff\xff'
assert not bls.AggregateVerify(pubkey, messages, tampered_signature) assert not bls.AggregateVerify(pubkey, messages, tampered_signature)
assert not milagro_bls.AggregateVerify(pubkeys, messages, tampered_signature)
yield f'aggregate_verify_tampered_signature', { yield f'aggregate_verify_tampered_signature', {
'input': { 'input': {
'pubkeys': pubkeys_serial, 'pubkeys': pubkeys_serial,
@ -250,6 +271,7 @@ def case05_aggregate_verify():
# Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == Z1_SIGNATURE # Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == Z1_SIGNATURE
assert not bls.AggregateVerify([], [], Z2_SIGNATURE) assert not bls.AggregateVerify([], [], Z2_SIGNATURE)
assert not milagro_bls.AggregateVerify([], [], Z2_SIGNATURE)
yield f'aggregate_verify_na_pubkeys_and_infinity_signature', { yield f'aggregate_verify_na_pubkeys_and_infinity_signature', {
'input': { 'input': {
'pubkeys': [], 'pubkeys': [],
@ -261,6 +283,7 @@ def case05_aggregate_verify():
# Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == 0x00... # Invalid pubkeys and signature -- len(pubkeys) == 0 and signature == 0x00...
assert not bls.AggregateVerify([], [], NO_SIGNATURE) assert not bls.AggregateVerify([], [], NO_SIGNATURE)
assert not milagro_bls.AggregateVerify([], [], NO_SIGNATURE)
yield f'aggregate_verify_na_pubkeys_and_na_signature', { yield f'aggregate_verify_na_pubkeys_and_na_signature', {
'input': { 'input': {
'pubkeys': [], 'pubkeys': [],