Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
fix chia bls invalid private key range
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang committed Jul 3, 2019
1 parent 3e2be61 commit 05cc60d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion eth2/_utils/bls_bindings/chia_network/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
ValidationError,
)

from py_ecc.optimized_bls12_381 import (
curve_order,
)


def _privkey_int_to_bytes(privkey: int) -> bytes:
return privkey.to_bytes(bls_chia.PrivateKey.PRIVATE_KEY_SIZE, "big")
Expand All @@ -35,6 +39,9 @@ def sign(message_hash: Hash32,


def privtopub(k: int) -> BLSPubkey:
if k <= 0 or k >= curve_order:
raise ValidationError(f"Expect integer between 0 and {curve_order}, got {k}")

privkey_chia = bls_chia.PrivateKey.from_bytes(_privkey_int_to_bytes(k))
return cast(BLSPubkey, privkey_chia.get_public_key().serialize())

Expand Down Expand Up @@ -74,7 +81,6 @@ def verify_multiple(pubkeys: Sequence[BLSPubkey],
message_hashes: Sequence[Hash32],
signature: BLSSignature,
domain: int) -> bool:

len_msgs = len(message_hashes)

if len(pubkeys) != len_msgs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def test_validate_block_slot(sample_beacon_state_params,
'slots_per_epoch, shard_count,'
'proposer_privkey, proposer_pubkey, is_valid_signature',
(
(5, 2, 0, bls.privtopub(0), True, ),
(5, 2, 0, bls.privtopub(0)[1:] + b'\x01', False),
(5, 2, 0, bls.privtopub(56), True, ),
(5, 2, 0, bls.privtopub(56)[1:] + b'\x01', False),
(5, 2, 123, bls.privtopub(123), True),
(5, 2, 123, bls.privtopub(123)[1:] + b'\x01', False),
)
Expand Down

0 comments on commit 05cc60d

Please sign in to comment.