From 4792220b45f37832f76179038f2af8c2971907ab Mon Sep 17 00:00:00 2001 From: cheatfate Date: Thu, 23 May 2024 17:18:01 +0300 Subject: [PATCH] Revert "Use another method." This reverts commit cc0231c85bc657ec9c57fb090ae5839f90de0a48. --- blscurve/blst/blst_abi.nim | 27 +++++---------- blscurve/blst/blst_min_pubkey_sig_core.nim | 38 ++++++++++++++-------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/blscurve/blst/blst_abi.nim b/blscurve/blst/blst_abi.nim index 8d8d26e..8923a5d 100644 --- a/blscurve/blst/blst_abi.nim +++ b/blscurve/blst/blst_abi.nim @@ -368,21 +368,15 @@ proc blst_final_exp*(ret: var blst_fp12; f: blst_fp12) proc blst_precompute_lines*(Qlines: var array[68, blst_fp6]; Q: blst_p2_affine) proc blst_miller_loop_lines*(ret: var blst_fp12; Qlines: array[68, blst_fp6]; P: blst_p1_affine) proc blst_pairing_sizeof*(): uint - -proc blst_pairing_init*[T: byte|char](new_ctx: ptr blst_opaque, +proc blst_pairing_init*[T: byte|char](new_ctx: var blst_pairing, hash_or_encode: HashOrEncode, domainSepTag: openArray[T]) -proc blst_pairing_init*[T: byte|char](new_ctx: var blst_pairing, +proc blst_pairing_init*[T: byte|char](new_ctx: ptr blst_opaque, hash_or_encode: HashOrEncode, - domainSepTag: openArray[T]) = - blst_pairing_init(cast[ptr blst_opaque](addr new_ctx), hash_or_encode, - domainSepTag) - + domainSepTag: openArray[T]) proc blst_pairing_get_dst*(ctx: blst_pairing): ptr UncheckedArray[byte] +proc blst_pairing_commit*(ctx: var blst_pairing) proc blst_pairing_commit*(ctx: ptr blst_opaque) -proc blst_pairing_commit*(ctx: var blst_pairing) = - cast[ptr blst_opaque](addr ctx) - proc blst_pairing_aggregate_pk_in_g2*[T,U: byte|char]( ctx: var blst_pairing; PK: ptr blst_p2_affine; @@ -423,7 +417,7 @@ proc blst_pairing_aggregate_pk_in_g1*[T,U: byte|char]( msg: openArray[T]; aug: openArray[U]): BLST_ERROR proc blst_pairing_chk_n_aggr_pk_in_g1*[T,U: byte|char]( - ctx: ptr blst_opaque, + ctx: var blst_pairing, PK: ptr blst_p1_affine, pk_grpchk: bool, signature: ptr blst_p2_affine, @@ -432,17 +426,14 @@ proc blst_pairing_chk_n_aggr_pk_in_g1*[T,U: byte|char]( aug: openArray[U] ): BLST_ERROR proc blst_pairing_chk_n_aggr_pk_in_g1*[T,U: byte|char]( - ctx: var blst_pairing, + ctx: ptr blst_opaque, PK: ptr blst_p1_affine, pk_grpchk: bool, signature: ptr blst_p2_affine, sig_grpchk: bool, msg: openArray[T], aug: openArray[U] - ): BLST_ERROR = - blst_pairing_chk_n_aggr_pk_in_g1( - cast[ptr blst_opaque](addr ctx), PK, pk_grpchk, signature, sig_grpchk, msg, - aug) + ): BLST_ERROR proc blst_pairing_mul_n_aggregate_pk_in_g1*[T,U: byte|char]( ctx: var blst_pairing; PK: ptr blst_p1_affine; @@ -463,10 +454,8 @@ proc blst_pairing_chk_n_mul_n_aggr_pk_in_g1*[T,U: byte|char]( aug: openArray[U] ): BLST_ERROR proc blst_pairing_merge*(ctx: var blst_pairing; ctx1: blst_pairing): BLST_ERROR +proc blst_pairing_finalverify*(ctx: var blst_pairing; gtsig: ptr blst_fp12): CTbool proc blst_pairing_finalverify*(ctx: ptr blst_opaque; gtsig: ptr blst_fp12): CTbool -proc blst_pairing_finalverify*(ctx: var blst_pairing; - gtsig: ptr blst_fp12): CTbool = - blst_pairing_finalverify(cast[ptr blst_opaque](addr ctx), gtsig) # Customarily applications aggregate signatures separately. # In which case application would have to pass NULLs for |signature| diff --git a/blscurve/blst/blst_min_pubkey_sig_core.nim b/blscurve/blst/blst_min_pubkey_sig_core.nim index cc047a0..0af4a4f 100644 --- a/blscurve/blst/blst_min_pubkey_sig_core.nim +++ b/blscurve/blst/blst_min_pubkey_sig_core.nim @@ -262,11 +262,13 @@ func coreVerifyNoGroupCheck*[T: byte|char]( ## This assumes that the Public Key and Signatures ## have been pre group checked (likely on deserialization) var ctx{.noinit.}: blst_pairing - ctx.blst_pairing_init( + blst_pairing_init( + cast[ptr blst_opaque](addr ctx), hash_or_encode = kHash, domainSepTag ) - let ok = BLST_SUCCESS == ctx.blst_pairing_chk_n_aggr_pk_in_g1( + let ok = BLST_SUCCESS == blst_pairing_chk_n_aggr_pk_in_g1( + cast[ptr blst_opaque](addr ctx), publicKey.point.unsafeAddr, pk_grpchk = false, # Already grouped checked sig_or_proof.point.unsafeAddr, @@ -277,8 +279,8 @@ func coreVerifyNoGroupCheck*[T: byte|char]( if not ok: return false - ctx.blst_pairing_commit() - bool ctx.blst_pairing_finalverify(nil) + blst_pairing_commit(cast[ptr blst_opaque](addr ctx)) + bool blst_pairing_finalverify(cast[ptr blst_opaque](addr ctx), nil) # Core aggregate operations # Aggregate Batch of (Publickeys, Messages, Signatures) @@ -304,7 +306,8 @@ type func init*(ctx: var ContextCoreAggregateVerify) {.inline.} = ## initialize an aggregate verification context - ctx.blst_pairing_init( + blst_pairing_init( + cast[ptr blst_opaque](addr ctx.c), hash_or_encode = kHash, ctx.DomainSepTag ) # C1 = 1 (identity element) @@ -313,7 +316,8 @@ func update*[T: char|byte]( ctx: var ContextCoreAggregateVerify, publicKey: PublicKey, message: openArray[T]): bool {.inline.} = - BLST_SUCCESS == ctx.c.blst_pairing_chk_n_aggr_pk_in_g1( + BLST_SUCCESS == blst_pairing_chk_n_aggr_pk_in_g1( + cast[ptr blst_opaque](addr ctx.c), publicKey.point.unsafeAddr, pk_grpchk = false, # Already grouped checked signature = nil, @@ -328,11 +332,11 @@ func commit(ctx: var ContextCoreAggregateVerify) {.inline.} = ## This MUST be done: ## - before merging 2 pairing contexts (for example when distributing computation) ## - before finalVerify - ctx.c.blst_pairing_commit() + blst_pairing_commit(cast[ptr blst_opaque](addr ctx.c)) func finalVerify(ctx: var ContextCoreAggregateVerify): bool {.inline.} = ## Verify a whole batch of (PublicKey, message, Signature) triplets. - bool ctx.c.blst_pairing_finalverify(nil) + bool blst_pairing_finalverify(cast[ptr blst_opaque](addr ctx.c), nil) func finish*(ctx: var ContextCoreAggregateVerify, signature: Signature or AggregateSignature): bool = # Implementation strategy @@ -365,7 +369,8 @@ func finish*(ctx: var ContextCoreAggregateVerify, signature: Signature or Aggreg # use a Miller loop internally and Miller loops are **very** costly. when signature is Signature: - result = BLST_SUCCESS == ctx.c.blst_pairing_chk_n_aggr_pk_in_g1( + result = BLST_SUCCESS == blst_pairing_chk_n_aggr_pk_in_g1( + cast[ptr blst_opaque](addr ctx.c), PK = nil, pk_grpchk = false, # Already grouped checked signature.point.unsafeAddr, @@ -377,7 +382,8 @@ func finish*(ctx: var ContextCoreAggregateVerify, signature: Signature or Aggreg block: var sig{.noinit.}: blst_p2_affine sig.blst_p2_to_affine(signature.point) - result = BLST_SUCCESS == ctx.c.blst_pairing_chk_n_aggr_pk_in_g1( + result = BLST_SUCCESS == blst_pairing_chk_n_aggr_pk_in_g1( + cast[ptr blst_opaque](addr ctx.c), PK = nil, pk_grpchk = false, # Already grouped checked sig.point.unsafeAddr, @@ -468,7 +474,8 @@ func init*[T: char|byte]( ## so that from a single source of randomness ## each thread is seeded with a different state when ## used in a multithreading context - ctx.c.blst_pairing_init( + blst_pairing_init( + cast[ptr blst_opaque](addr ctx.c) hash_or_encode = kHash, ctx.DomainSepTag ) # C1 = 1 (identity element) @@ -530,7 +537,8 @@ func update*[T: char|byte]( ctx.secureBlinding.bls_sha256_digest(ctx.secureBlinding) blindingScalar.blst_scalar_from_lendian(blindingAsArray[]) - BLST_SUCCESS == ctx.c.blst_pairing_chk_n_mul_n_aggr_pk_in_g1( + BLST_SUCCESS == blst_pairing_chk_n_mul_n_aggr_pk_in_g1( + cast[ptr blst_opaque](addr ctx.c), publicKey.point.unsafeAddr, pk_grpchk = false, # Already grouped checked signature.point.unsafeAddr, @@ -547,7 +555,7 @@ func commit*(ctx: var ContextMultiAggregateVerify) {.inline.} = ## This MUST be done: ## - before merging 2 pairing contexts (for example when distributing computation) ## - before finalVerify - ctx.c.blst_pairing_commit() + blst_pairing_commit(cast[ptr blst_opaque](addr ctx.c)) func merge*( ctx_into: var ContextMultiAggregateVerify, @@ -562,7 +570,9 @@ func merge*( func finalVerify*(ctx: var ContextMultiAggregateVerify): bool {.inline.} = ## Verify a whole batch of (PublicKey, message, Signature) triplets. - result = bool ctx.c.blst_pairing_finalverify(nil) + result = bool blst_pairing_finalverify( + cast[ptr blst_opaque](addr ctx.c), + nil) func getScalar*(sk: SecretKey): blst_scalar = return sk.scalar