Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fast-verification-of-multiple-bls-signatures #67

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove groupby
ChihChengLiang committed May 28, 2019
commit 59c319d8baba775c403a06572fda99910ad555b8
39 changes: 13 additions & 26 deletions py_ecc/bls/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from operator import (
itemgetter,
)

from secrets import (
randbelow,
)
@@ -9,10 +7,6 @@
Sequence,
Tuple,
)

from cytoolz.itertoolz import (
groupby,
)
from eth_typing import (
BLSPubkey,
BLSSignature,
@@ -92,19 +86,15 @@ def aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkey:
return G1_to_pubkey(o)


def _group_by_messages(
message_hashes: Sequence[Hash32],
pubkeys: Sequence[BLSPubkey]) -> Iterator[Tuple[Hash32, Tuple[BLSPubkey, ...]]]:
def _zip(pubkeys: Sequence[BLSPubkey],
message_hashes: Sequence[Hash32])-> Iterator[Tuple[BLSPubkey, Hash32]]:
if len(pubkeys) != len(message_hashes):
raise ValidationError(
"len(pubkeys) (%s) should be equal to len(message_hashes) (%s)" % (
len(pubkeys), len(message_hashes)
)
)
groups = groupby(itemgetter(0), zip(message_hashes, pubkeys)).items()
for message_hash, group in groups:
group_pubkeys = tuple(BLSPubkey(pubkey) for _, pubkey in group)
yield Hash32(message_hash), group_pubkeys
return zip(pubkeys, message_hashes)


def verify_multiple(pubkeys: Sequence[BLSPubkey],
@@ -113,11 +103,12 @@ def verify_multiple(pubkeys: Sequence[BLSPubkey],
domain: int) -> bool:

o = FQ12.one()
for message_hash, group_pubkeys in _group_by_messages(message_hashes, pubkeys):
agg_pub = Z1
for key in group_pubkeys:
agg_pub = add(agg_pub, pubkey_to_G1(key))
o *= pairing(hash_to_G2(message_hash, domain), agg_pub, final_exponentiate=False)
for pubkey, message_hash in _zip(pubkeys, message_hashes):
o *= pairing(
hash_to_G2(message_hash, domain),
pubkey_to_G1(pubkey),
final_exponentiate=False,
)
o *= pairing(signature_to_G2(signature), neg(G1), final_exponentiate=False)
final_exponentiation = final_exponentiate(o)
return final_exponentiation == FQ12.one()
@@ -139,15 +130,11 @@ def verify_multiple_multiple(

random_ints = (1,) + tuple(2**randbelow(64) for _ in signatures[:-1])
o = FQ12.one()
for r_i, pm in zip(random_ints, pubkeys_and_messages):
pubkeys, message_hashes = pm
for message_hash, group_pubkeys in _group_by_messages(message_hashes, pubkeys):
agg_pub = Z1
for key in group_pubkeys:
agg_pub = add(agg_pub, pubkey_to_G1(key))
for r_i, (pubkeys, message_hashes) in zip(random_ints, pubkeys_and_messages):
for pubkey, message_hash in _zip(pubkeys, message_hashes):
o *= pairing(
multiply(hash_to_G2(message_hash, domain), r_i),
agg_pub,
pubkey_to_G1(pubkey),
final_exponentiate=False,
)
agg_sig = Z2