Skip to content

Commit

Permalink
Use another method.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed May 23, 2024
1 parent fd653ec commit cc0231c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
27 changes: 19 additions & 8 deletions blscurve/blst/blst_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,21 @@ 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: var blst_pairing,
hash_or_encode: HashOrEncode,
domainSepTag: openArray[T])

proc blst_pairing_init*[T: byte|char](new_ctx: ptr blst_opaque,
hash_or_encode: HashOrEncode,
domainSepTag: openArray[T])
proc blst_pairing_init*[T: byte|char](new_ctx: var blst_pairing,
hash_or_encode: HashOrEncode,
domainSepTag: openArray[T]) =
blst_pairing_init(cast[ptr blst_opaque](addr new_ctx), hash_or_encode,
domainSepTag)

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 @@ -417,7 +423,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: var blst_pairing,
ctx: ptr blst_opaque,
PK: ptr blst_p1_affine,
pk_grpchk: bool,
signature: ptr blst_p2_affine,
Expand All @@ -426,14 +432,17 @@ 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: ptr blst_opaque,
ctx: var blst_pairing,
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_ERROR =
blst_pairing_chk_n_aggr_pk_in_g1(
cast[ptr blst_opaque](addr ctx), PK, pk_grpchk, signature, sig_grpchk, msg,
aug)
proc blst_pairing_mul_n_aggregate_pk_in_g1*[T,U: byte|char](
ctx: var blst_pairing;
PK: ptr blst_p1_affine;
Expand All @@ -454,8 +463,10 @@ 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: 14 additions & 24 deletions blscurve/blst/blst_min_pubkey_sig_core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,11 @@ 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
blst_pairing_init(
cast[ptr blst_opaque](addr ctx),
ctx.blst_pairing_init(
hash_or_encode = kHash,
domainSepTag
)
let ok = BLST_SUCCESS == blst_pairing_chk_n_aggr_pk_in_g1(
cast[ptr blst_opaque](addr ctx),
let ok = BLST_SUCCESS == ctx.blst_pairing_chk_n_aggr_pk_in_g1(
publicKey.point.unsafeAddr,
pk_grpchk = false, # Already grouped checked
sig_or_proof.point.unsafeAddr,
Expand All @@ -279,8 +277,8 @@ func coreVerifyNoGroupCheck*[T: byte|char](
if not ok:
return false

blst_pairing_commit(cast[ptr blst_opaque](addr ctx))
bool blst_pairing_finalverify(cast[ptr blst_opaque](addr ctx), nil)
ctx.blst_pairing_commit()
bool ctx.blst_pairing_finalverify(nil)

# Core aggregate operations
# Aggregate Batch of (Publickeys, Messages, Signatures)
Expand All @@ -306,8 +304,7 @@ type

func init*(ctx: var ContextCoreAggregateVerify) {.inline.} =
## initialize an aggregate verification context
blst_pairing_init(
cast[ptr blst_opaque](addr ctx.c),
ctx.blst_pairing_init(
hash_or_encode = kHash,
ctx.DomainSepTag
) # C1 = 1 (identity element)
Expand All @@ -316,8 +313,7 @@ func update*[T: char|byte](
ctx: var ContextCoreAggregateVerify,
publicKey: PublicKey,
message: openArray[T]): bool {.inline.} =
BLST_SUCCESS == blst_pairing_chk_n_aggr_pk_in_g1(
cast[ptr blst_opaque](addr ctx.c),
BLST_SUCCESS == ctx.c.blst_pairing_chk_n_aggr_pk_in_g1(
publicKey.point.unsafeAddr,
pk_grpchk = false, # Already grouped checked
signature = nil,
Expand All @@ -332,11 +328,11 @@ func commit(ctx: var ContextCoreAggregateVerify) {.inline.} =
## This MUST be done:
## - before merging 2 pairing contexts (for example when distributing computation)
## - before finalVerify
blst_pairing_commit(cast[ptr blst_opaque](addr ctx.c))
ctx.c.blst_pairing_commit()

func finalVerify(ctx: var ContextCoreAggregateVerify): bool {.inline.} =
## Verify a whole batch of (PublicKey, message, Signature) triplets.
bool blst_pairing_finalverify(cast[ptr blst_opaque](addr ctx.c), nil)
bool ctx.c.blst_pairing_finalverify(nil)

func finish*(ctx: var ContextCoreAggregateVerify, signature: Signature or AggregateSignature): bool =
# Implementation strategy
Expand Down Expand Up @@ -369,8 +365,7 @@ 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 == blst_pairing_chk_n_aggr_pk_in_g1(
cast[ptr blst_opaque](addr ctx.c),
result = BLST_SUCCESS == ctx.c.blst_pairing_chk_n_aggr_pk_in_g1(
PK = nil,
pk_grpchk = false, # Already grouped checked
signature.point.unsafeAddr,
Expand All @@ -382,8 +377,7 @@ 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 == blst_pairing_chk_n_aggr_pk_in_g1(
cast[ptr blst_opaque](addr ctx.c),
result = BLST_SUCCESS == ctx.c.blst_pairing_chk_n_aggr_pk_in_g1(
PK = nil,
pk_grpchk = false, # Already grouped checked
sig.point.unsafeAddr,
Expand Down Expand Up @@ -474,8 +468,7 @@ 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
blst_pairing_init(
cast[ptr blst_opaque](addr ctx.c)
ctx.c.blst_pairing_init(
hash_or_encode = kHash,
ctx.DomainSepTag
) # C1 = 1 (identity element)
Expand Down Expand Up @@ -537,8 +530,7 @@ func update*[T: char|byte](
ctx.secureBlinding.bls_sha256_digest(ctx.secureBlinding)
blindingScalar.blst_scalar_from_lendian(blindingAsArray[])

BLST_SUCCESS == blst_pairing_chk_n_mul_n_aggr_pk_in_g1(
cast[ptr blst_opaque](addr ctx.c),
BLST_SUCCESS == ctx.c.blst_pairing_chk_n_mul_n_aggr_pk_in_g1(
publicKey.point.unsafeAddr,
pk_grpchk = false, # Already grouped checked
signature.point.unsafeAddr,
Expand All @@ -555,7 +547,7 @@ func commit*(ctx: var ContextMultiAggregateVerify) {.inline.} =
## This MUST be done:
## - before merging 2 pairing contexts (for example when distributing computation)
## - before finalVerify
blst_pairing_commit(cast[ptr blst_opaque](addr ctx.c))
ctx.c.blst_pairing_commit()

func merge*(
ctx_into: var ContextMultiAggregateVerify,
Expand All @@ -570,9 +562,7 @@ func merge*(

func finalVerify*(ctx: var ContextMultiAggregateVerify): bool {.inline.} =
## Verify a whole batch of (PublicKey, message, Signature) triplets.
result = bool blst_pairing_finalverify(
cast[ptr blst_opaque](addr ctx.c),
nil)
result = bool ctx.c.blst_pairing_finalverify(nil)

func getScalar*(sk: SecretKey): blst_scalar =
return sk.scalar
Expand Down

0 comments on commit cc0231c

Please sign in to comment.