Skip to content

Commit

Permalink
Fix GCC-14 [-Wincompatible-pointer-types] issue (one more version). (#…
Browse files Browse the repository at this point in the history
…173)

* One more way of fix.

* POC blst sha256 issue

---------

Co-authored-by: jangko <jangko128@gmail.com>
  • Loading branch information
cheatfate and jangko authored Jun 6, 2024
1 parent c53d3f6 commit f29698d
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 321 deletions.
48 changes: 26 additions & 22 deletions benchmarks/bls12381_curve.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,50 @@ var benchRNG = initRand(0xFACADE)
proc benchScalarMultG1*(iters: int) =
when BLS_BACKEND == BLST:
var x{.noinit.}: blst_p1
x.blst_p1_from_affine(BLS12_381_G1) # init from generator
blst_p1_from_affine(toCV(x, cblst_p1), toCC(BLS12_381_G1, cblst_p1_affine)) # init from generator

var scal{.noinit.}: array[32, byte]
for val in scal.mitems:
val = byte benchRNG.rand(0xFF)

var scalar{.noinit.}: blst_scalar
scalar.blst_scalar_from_bendian(scal)
blst_scalar_from_bendian(toCV(scalar, cblst_scalar), scal)

bench("Scalar multiplication G1 (255-bit, constant-time)", iters):
x.blst_p1_mult(x, scalar, 255)
blst_p1_mult(toCV(x, cblst_p1), toCC(x, cblst_p1), addr scalar.b[0], 255)

proc benchScalarMultG2*(iters: int) =
when BLS_BACKEND == BLST:
var x{.noinit.}: blst_p2
x.blst_p2_from_affine(BLS12_381_G2) # init from generator
blst_p2_from_affine(toCV(x, cblst_p2), toCC(BLS12_381_G2, cblst_p2_affine)) # init from generator

var scal{.noinit.}: array[32, byte]
for val in scal.mitems:
val = byte benchRNG.rand(0xFF)

var scalar{.noinit.}: blst_scalar
scalar.blst_scalar_from_bendian(scal)
blst_scalar_from_bendian(toCV(scalar, cblst_scalar), scal)

bench("Scalar multiplication G2 (255-bit, constant-time)", iters):
x.blst_p2_mult(x, scalar, 255)
blst_p2_mult(toCV(x, cblst_p2), toCC(x, cblst_p2), addr scalar.b[0], 255)

proc benchECAddG1*(iters: int) =
when BLS_BACKEND == BLST:
var x{.noinit.}, y{.noinit.}: blst_p1
x.blst_p1_from_affine(BLS12_381_G1) # init from generator
blst_p1_from_affine(toCV(x, cblst_p1), toCC(BLS12_381_G1, cblst_p1_affine)) # init from generator
y = x

bench("EC add G1 (constant-time)", iters):
x.blst_p1_add_or_double(x, y)
blst_p1_add_or_double(toCV(x, cblst_p1), toCC(x, cblst_p1), toCC(y, cblst_p1))

proc benchECAddG2*(iters: int) =
when BLS_BACKEND == BLST:
var x{.noinit.}, y{.noinit.}: blst_p2
x.blst_p2_from_affine(BLS12_381_G2) # init from generator
blst_p2_from_affine(toCV(x, cblst_p2), toCC(BLS12_381_G2, cblst_p2_affine)) # init from generator
y = x

bench("EC add G2 (constant-time)", iters):
x.blst_p2_add_or_double(x, y)
blst_p2_add_or_double(toCV(x, cblst_p2), toCC(x, cblst_p2), toCC(y, cblst_p2))

when BLS_BACKEND == BLST:

Expand All @@ -90,30 +90,34 @@ when BLS_BACKEND == BLST:
var sig = block:
var sig {.noinit.}: blst_p2_affine
var s {.noinit.}: blst_p2
s.blst_hash_to_g2(
blst_hash_to_g2(
toCV(s, cblst_p2),
msg,
domainSepTag,
aug = ""
)
s.blst_sign_pk_in_g1(s, seckey)
sig.blst_p2_to_affine(s)
blst_sign_pk_in_g1(toCV(s, cblst_p2), toCC(s, cblst_p2), toCC(seckey, cblst_scalar))
blst_p2_to_affine(toCV(sig, cblst_p2_affine), toCC(s, cblst_p2))
sig

# Verification
let ctx = createU(blst_pairing) # Heap to avoid stack smashing
ctx[].blst_pairing_init(
blst_pairing_init(
cast[ptr cblst_pairing](ctx),
hash_or_encode = kHash,
domainSepTag
)
doAssert BLST_SUCCESS == ctx[].blst_pairing_aggregate_pk_in_g1(
PK = pubkey.unsafeAddr,
doAssert BLST_SUCCESS == blst_pairing_aggregate_pk_in_g1(
cast[ptr cblst_pairing](ctx),
PK = toCC(pubkey, cblst_p1_affine),
signature = nil,
msg,
aug = ""
)
doAssert BLST_SUCCESS == ctx[].blst_pairing_aggregate_pk_in_g1(
doAssert BLST_SUCCESS == blst_pairing_aggregate_pk_in_g1(
cast[ptr cblst_pairing](ctx),
PK = nil,
signature = sig.unsafeAddr,
signature = toCC(sig, cblst_p2_affine),
msg = "",
aug = ""
)
Expand All @@ -122,15 +126,15 @@ when BLS_BACKEND == BLST:
let ctxSave = createU(blst_pairing)
ctxSave[] = ctx[]

ctx[].blst_pairing_commit() # Miller loop
let valid = ctx[].blst_pairing_finalverify(nil) # Final Exponentiation
blst_pairing_commit(cast[ptr cblst_pairing](ctx)) # Miller loop
let valid = blst_pairing_finalverify(cast[ptr cblst_pairing](ctx), nil) # Final Exponentiation
doAssert bool valid

# Pairing: e(Q, xP) == e(R, P)
bench("Pairing (Miller loop + Final Exponentiation)", iters):
ctx[] = ctxSave[]
ctx[].blst_pairing_commit() # Miller loop
let valid = ctx[].blst_pairing_finalverify(nil) # Final Exponentiation
blst_pairing_commit(cast[ptr cblst_pairing](ctx)) # Miller loop
let valid = blst_pairing_finalverify(cast[ptr cblst_pairing](ctx), nil) # Final Exponentiation
# doAssert bool valid

when isMainModule:
Expand Down
5 changes: 3 additions & 2 deletions benchmarks/hash_to_curve.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ proc benchHashToG2*(iters: int) =
var Paff: blst_p2_affine

bench("Hash to G2 (Draft #9) + affine conversion", iters):
P.blst_hash_to_g2(
blst_hash_to_g2(
toCV(P, cblst_p2),
msg,
dst,
aug = ""
)
Paff.blst_p2_to_affine(P)
blst_p2_to_affine(toCV(Paff, cblst_p2_affine), toCC(P, cblst_p2))

when isMainModule:
benchHashToG2(1000)
52 changes: 26 additions & 26 deletions blscurve/blst/bls_sig_io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ func toHex*(
when obj is SecretKey:
const size = 32
var bytes{.noinit.}: array[size, byte]
bytes.blst_bendian_from_scalar(obj.scalar)
bytes.blst_bendian_from_scalar(toCC(obj.scalar, cblst_scalar))
elif obj is PublicKey:
const size = 48
var bytes{.noinit.}: array[size, byte]
bytes.blst_p1_affine_compress(obj.point)
bytes.blst_p1_affine_compress(toCC(obj.point, cblst_p1_affine))
elif obj is (Signature or ProofOfPossession):
const size = 96
var bytes{.noinit.}: array[size, byte]
bytes.blst_p2_affine_compress(obj.point)
bytes.blst_p2_affine_compress(toCC(obj.point, cblst_p2_affine))
elif obj is AggregateSignature:
const size = 96
var bytes{.noinit.}: array[size, byte]
bytes.blst_p2_compress(obj.point)
bytes.blst_p2_compress(toCC(obj.point, cblst_p2))

bytes.toHex()

Expand All @@ -48,14 +48,14 @@ func fromBytes*(
## Returns true on success and false otherwise
result =
when raw.len == 96:
obj.point.blst_p2_uncompress(raw) == BLST_SUCCESS
blst_p2_uncompress(toCV(obj.point, cblst_p2_affine), raw) == BLST_SUCCESS
elif raw.len == 192:
obj.point.blst_p2_deserialize(raw) == BLST_SUCCESS
blst_p2_deserialize(toCV(obj.point, cblst_p2_affine), raw) == BLST_SUCCESS
else: false

# Infinity signatures are allowed if we receive an empty aggregated signature
if result:
result = bool obj.point.blst_p2_affine_in_g2()
result = bool blst_p2_affine_in_g2(toCV(obj.point, cblst_p2_affine))

func fromBytesKnownOnCurve*(
obj: var (Signature|ProofOfPossession),
Expand All @@ -68,9 +68,9 @@ func fromBytesKnownOnCurve*(
## The point is known to be on curve and is not group checked
result =
when raw.len == 96:
obj.point.blst_p2_uncompress(raw) == BLST_SUCCESS
toCV(obj.point, cblst_p2_affine).blst_p2_uncompress(raw) == BLST_SUCCESS
elif raw.len == 192:
obj.point.blst_p2_deserialize(raw) == BLST_SUCCESS
toCV(obj.point, cblst_p2_affine).blst_p2_deserialize(raw) == BLST_SUCCESS
else: false
# Infinity signatures are allowed if we receive an empty aggregated signature

Expand All @@ -87,16 +87,16 @@ func fromBytes*(
## Returns true on success and false otherwise
result =
when raw.len == 48:
obj.point.blst_p1_uncompress(raw) == BLST_SUCCESS
toCV(obj.point, cblst_p1_affine).blst_p1_uncompress(raw) == BLST_SUCCESS
elif raw.len == 96:
obj.point.blst_p1_deserialize(raw) == BLST_SUCCESS
toCV(obj.point, cblst_p1_affine).blst_p1_deserialize(raw) == BLST_SUCCESS
else: false

# Infinity public keys are not allowed
if result:
result = not bool obj.point.blst_p1_affine_is_inf()
result = not bool toCV(obj.point, cblst_p1_affine).blst_p1_affine_is_inf()
if result:
result = bool obj.point.blst_p1_affine_in_g1()
result = bool toCV(obj.point, cblst_p1_affine).blst_p1_affine_in_g1()

func fromBytesKnownOnCurve*(
obj: var PublicKey,
Expand All @@ -107,14 +107,14 @@ func fromBytesKnownOnCurve*(
## Returns true on success and false otherwise
result =
when raw.len == 48:
obj.point.blst_p1_uncompress(raw) == BLST_SUCCESS
toCV(obj.point, cblst_p1_affine).blst_p1_uncompress(raw) == BLST_SUCCESS
elif raw.len == 96:
obj.point.blst_p1_deserialize(raw) == BLST_SUCCESS
toCV(obj.point, cblst_p1_affine).blst_p1_deserialize(raw) == BLST_SUCCESS
else: false

# Infinity public keys are not allowed
if result:
result = not bool obj.point.blst_p1_affine_is_inf()
result = not bool blst_p1_affine_is_inf(toCC(obj.point, cblst_p1_affine))

# Skipped - Known on curve
# if result:
Expand Down Expand Up @@ -155,15 +155,15 @@ func fromBytes*(
## Returns true on success and false otherwise
const L = 32
when raw is array:
obj.scalar.blst_scalar_from_bendian(raw)
blst_scalar_from_bendian(toCV(obj.scalar, cblst_scalar), raw)
else:
if raw.len != 32:
return false
let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr)
obj.scalar.blst_scalar_from_bendian(pa[])
blst_scalar_from_bendian(toCV(obj.scalar, cblst_scalar), pa[])
if obj.vec_is_zero():
return false
if not obj.scalar.blst_sk_check().bool:
if not blst_sk_check(toCC(obj.scalar, cblst_scalar)).bool:
return false
return true

Expand Down Expand Up @@ -197,8 +197,8 @@ func serialize*(
## Serialize the input `obj` in raw binary form and write it
## in `dst`.
## Returns `true` if the export is succesful, `false` otherwise
blst_bendian_from_scalar(dst, obj.scalar)
return true
blst_bendian_from_scalar(dst, toCC(obj.scalar, cblst_scalar))
true

func serialize*(
dst: var array[48, byte],
Expand All @@ -208,8 +208,8 @@ func serialize*(
## Returns `true` if the export is successful, `false` otherwise
## Note: this overload will serialize to the compressed format most commonly
## used.
blst_p1_affine_compress(dst, obj.point)
return true
blst_p1_affine_compress(dst, toCC(obj.point, cblst_p1_affine))
true

func serialize*(
dst: var array[96, byte],
Expand All @@ -219,7 +219,7 @@ func serialize*(
## Returns `true` if the export is successful, `false` otherwise
## Note: this overload willl serialize to an uncompressed format that is
## faster to deserialize but takes up more space.
blst_p1_affine_serialize(dst, obj.point)
blst_p1_affine_serialize(dst, toCC(obj.point, cblst_p1_affine))
return true

func serialize*(
Expand All @@ -230,7 +230,7 @@ func serialize*(
## Returns `true` if the export is successful, `false` otherwise
## Note: this overload will serialize to the compressed format most commonly
## used.
blst_p2_affine_compress(dst, obj.point)
blst_p2_affine_compress(dst, toCC(obj.point, cblst_p2_affine))
return true

func serialize*(
Expand All @@ -241,7 +241,7 @@ func serialize*(
## Returns `true` if the export is successful, `false` otherwise
## Note: this overload willl serialize to an uncompressed format that is
## faster to deserialize but takes up more space.
blst_p2_affine_serialize(dst, obj.point)
blst_p2_affine_serialize(dst, toCC(obj.point, cblst_p2_affine))
return true

func exportRaw*(secretKey: SecretKey): array[32, byte] {.inline, noinit.}=
Expand Down
Loading

0 comments on commit f29698d

Please # to comment.