Skip to content

Commit

Permalink
Revert "Use another method."
Browse files Browse the repository at this point in the history
This reverts commit cc0231c.
  • Loading branch information
cheatfate committed May 23, 2024
1 parent cc0231c commit 4792220
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
27 changes: 8 additions & 19 deletions blscurve/blst/blst_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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|
Expand Down
38 changes: 24 additions & 14 deletions blscurve/blst/blst_min_pubkey_sig_core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 4792220

Please sign in to comment.