Optimize code to not perform useless subgroup checks (#11546)

* Optimize code to not perform useless subgroup checks

* Revert less important optimizations
This commit is contained in:
Mariano Sorgente 2022-05-18 12:09:56 -04:00 committed by GitHub
parent 99275e6e37
commit 31ed32628f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -122,7 +122,8 @@ def batch_pre_validate_blocks(
if npc_result is not None and block.transactions_info is not None:
assert npc_result.conds
pairs_pks, pairs_msgs = pkm_pairs(npc_result.conds, constants.AGG_SIG_ME_ADDITIONAL_DATA)
pks_objects: List[G1Element] = [G1Element.from_bytes(pk) for pk in pairs_pks]
# Using AugSchemeMPL.aggregate_verify, so it's safe to use from_bytes_unchecked
pks_objects: List[G1Element] = [G1Element.from_bytes_unchecked(pk) for pk in pairs_pks]
if not AugSchemeMPL.aggregate_verify(
pks_objects, pairs_msgs, block.transactions_info.aggregated_signature
):

View File

@ -33,6 +33,8 @@ def get_pairings(cache: LRUCache, pks: List[bytes48], msgs: Sequence[bytes], for
pk_parsed: Optional[G1Element] = pk_bytes_to_g1.get(pks[i])
if pk_parsed is None:
# In this case, we use from_bytes instead of from_bytes_unchecked, because we will not be using
# the bls_signatures aggregate_verify method which performs the subgroup checks
pk_parsed = G1Element.from_bytes(pks[i])
pk_bytes_to_g1[pks[i]] = pk_parsed
@ -53,7 +55,8 @@ def aggregate_verify(
):
pairings: List[GTElement] = get_pairings(cache, pks, msgs, force_cache)
if len(pairings) == 0:
pks_objects: List[G1Element] = [G1Element.from_bytes(pk) for pk in pks]
# Using AugSchemeMPL.aggregate_verify, so it's safe to use from_bytes_unchecked
pks_objects: List[G1Element] = [G1Element.from_bytes_unchecked(pk) for pk in pks]
return AugSchemeMPL.aggregate_verify(pks_objects, msgs, sig)
pairings_prod: GTElement = functools.reduce(GTElement.__mul__, pairings)