This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Quick way to apply Chia BLS #764
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3e2be61
enshrine chia backend
ChihChengLiang 05cc60d
fix chia bls invalid private key range
ChihChengLiang b65fba3
install blspy
ChihChengLiang b62b92c
tuning inputs
ChihChengLiang 257cd89
add cmake in circle
ChihChengLiang 079ef87
fix mock py_ecc
ChihChengLiang d3e5be0
separate lightchain integration
ChihChengLiang 6160ef3
fix docker
ChihChengLiang 325f18c
fix private key range
ChihChengLiang 59d1624
remove eth2 from install requires
ChihChengLiang b7df842
fix tox
ChihChengLiang 75798e3
don't messup everything else, just add cmake everywhere
ChihChengLiang 89d96fc
fix empty sig and pubkey
ChihChengLiang a52b6b0
make typing happy
ChihChengLiang 85cfee5
skip to be updated test
ChihChengLiang e43d1ba
chia bls likes all 00 empty sig
ChihChengLiang 7bbe855
Fix public key error message
ChihChengLiang df7f6cd
handle privkey error nicer
ChihChengLiang 4328095
handle value error too
ChihChengLiang 74d8fb0
more check, typing, and fixes
ChihChengLiang 296640f
Update eth2/_utils/bls_bindings/chia_network/api.py
ChihChengLiang 6692066
Update eth2/_utils/bls_bindings/chia_network/api.py
ChihChengLiang c6ba90e
PR feedback
ChihChengLiang 34a9c19
add empty public key test
ChihChengLiang aa640b0
raise ValidationError when bls verify False
ChihChengLiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
|
||
from .bls_bindings.chia_network import ( | ||
api as chia_api, | ||
) | ||
from abc import ( | ||
ABC, | ||
abstractmethod, | ||
) | ||
from typing import ( | ||
Sequence, | ||
) | ||
from eth_typing import ( | ||
BLSPubkey, | ||
BLSSignature, | ||
Hash32, | ||
) | ||
from eth_utils import ( | ||
ValidationError, | ||
) | ||
|
||
|
||
class BaseBLSBackend(ABC): | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def privtopub(k: int) -> BLSPubkey: | ||
pass | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def sign(message_hash: Hash32, | ||
privkey: int, | ||
domain: int) -> BLSSignature: | ||
pass | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def _verify(message_hash: Hash32, | ||
pubkey: BLSPubkey, | ||
signature: BLSSignature, | ||
domain: int) -> bool: | ||
pass | ||
|
||
@classmethod | ||
def verify(cls, | ||
message_hash: Hash32, | ||
pubkey: BLSPubkey, | ||
signature: BLSSignature, | ||
domain: int) -> None: | ||
if not cls._verify(message_hash, pubkey, signature, domain): | ||
raise ValidationError(( | ||
"Verification failed:\n" | ||
f"message_hash {message_hash}\n" | ||
f"pubkey {pubkey}\n" | ||
f"signature {signature}\n" | ||
f"domain {domain}" | ||
)) | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def aggregate_signatures(signatures: Sequence[BLSSignature]) -> BLSSignature: | ||
pass | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkey: | ||
pass | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def _verify_multiple(pubkeys: Sequence[BLSPubkey], | ||
message_hashes: Sequence[Hash32], | ||
signature: BLSSignature, | ||
domain: int) -> bool: | ||
pass | ||
|
||
@classmethod | ||
def verify_multiple(cls, | ||
pubkeys: Sequence[BLSPubkey], | ||
message_hashes: Sequence[Hash32], | ||
signature: BLSSignature, | ||
domain: int) -> None: | ||
if not cls._verify_multiple(pubkeys, message_hashes, signature, domain): | ||
raise ValidationError(( | ||
"Verification failed:\n" | ||
f"pubkeys {pubkeys}\n" | ||
f"message_hashes {message_hashes}\n" | ||
f"signature {signature}\n" | ||
f"domain {domain}" | ||
)) | ||
|
||
|
||
class ChiaBackend(BaseBLSBackend): | ||
@staticmethod | ||
def privtopub(k: int) -> BLSPubkey: | ||
return chia_api.privtopub(k) | ||
|
||
@staticmethod | ||
def sign(message_hash: Hash32, | ||
privkey: int, | ||
domain: int) -> BLSSignature: | ||
return chia_api.sign(message_hash, privkey, domain) | ||
|
||
@staticmethod | ||
def _verify(message_hash: Hash32, | ||
pubkey: BLSPubkey, | ||
signature: BLSSignature, | ||
domain: int) -> bool: | ||
return chia_api.verify(message_hash, pubkey, signature, domain) | ||
|
||
@staticmethod | ||
def aggregate_signatures(signatures: Sequence[BLSSignature]) -> BLSSignature: | ||
return chia_api.aggregate_signatures(signatures) | ||
|
||
@staticmethod | ||
def aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkey: | ||
return chia_api.aggregate_pubkeys(pubkeys) | ||
|
||
@staticmethod | ||
def _verify_multiple(pubkeys: Sequence[BLSPubkey], | ||
message_hashes: Sequence[Hash32], | ||
signature: BLSSignature, | ||
domain: int) -> bool: | ||
return chia_api.verify_multiple(pubkeys, message_hashes, signature, domain) | ||
|
||
|
||
eth2_bls = ChiaBackend() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we want the
verify
to return False or raise ValidationError for the invalid inputs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I prefer
ValidationError
, since the caller cannot distinguish whether it is invalid inputs or just the incorrect signatures/wrong private keys ifverify
returnsFalse
.