Skip to content

Commit

Permalink
Bump nim-blscurve for gcc-14 compatibility (#2365)
Browse files Browse the repository at this point in the history
* Bump nim-blscurve for gcc-14 compatibility

* Fix evm/blscurve.nim pointer works when using blscurve_abi
  • Loading branch information
jangko authored Jun 15, 2024
1 parent 27d7102 commit c5508b8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 29 deletions.
90 changes: 62 additions & 28 deletions nimbus/evm/blscurve.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,63 +24,99 @@ type
BLS_G1P* = blst_p1_affine
BLS_G2P* = blst_p2_affine

template toCV(x: auto): auto =
when x is BLS_G1:
toCV(x, cblst_p1)
elif x is BLS_G2:
toCV(x, cblst_p2)
elif x is BLS_FP:
toCV(x, cblst_fp)
elif x is BLS_FP2:
toCV(x, cblst_fp2)
elif x is BLS_SCALAR:
toCV(x, cblst_scalar)
elif x is BLS_ACC:
toCV(x, cblst_fp12)
elif x is BLS_G1P:
toCV(x, cblst_p1_affine)
elif x is BLS_G2P:
toCV(x, cblst_p2_affine)

template toCC(x: auto): auto =
when x is BLS_G1:
toCC(x, cblst_p1)
elif x is BLS_G2:
toCC(x, cblst_p2)
elif x is BLS_FP:
toCC(x, cblst_fp)
elif x is BLS_FP2:
toCC(x, cblst_fp2)
elif x is BLS_SCALAR:
toCC(x, cblst_scalar)
elif x is BLS_ACC:
toCC(x, cblst_fp12)
elif x is BLS_G1P:
toCC(x, cblst_p1_affine)
elif x is BLS_G2P:
toCC(x, cblst_p2_affine)

func fromBytes*(ret: var BLS_SCALAR, raw: openArray[byte]): bool =
const L = 32
if raw.len < L:
return false
let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr)
blst_scalar_from_bendian(ret, pa[])
blst_scalar_from_bendian(toCV(ret), pa[])
true

func fromBytes(ret: var BLS_FP, raw: openArray[byte]): bool =
const L = 48
if raw.len < L:
return false
let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr)
blst_fp_from_bendian(ret, pa[])
blst_fp_from_bendian(toCV(ret), pa[])
true

func toBytes(fp: BLS_FP, output: var openArray[byte]): bool =
const L = 48
if output.len < L:
return false
let pa = cast[ptr array[L, byte]](output[0].unsafeAddr)
blst_bendian_from_fp(pa[], fp)
blst_bendian_from_fp(pa[], toCC(fp))
true

func pack(g: var BLS_G1, x, y: BLS_FP): bool =
let src = blst_p1_affine(x: x, y: y)
blst_p1_from_affine(g, src)
blst_p1_on_curve(g).int == 1
blst_p1_from_affine(toCV(g), toCC(src))
blst_p1_on_curve(toCV(g)).int == 1

func unpack(g: BLS_G1, x, y: var BLS_FP): bool =
var dst: blst_p1_affine
blst_p1_to_affine(dst, g)
blst_p1_to_affine(toCV(dst), toCC(g))
x = dst.x
y = dst.y
true

func pack(g: var BLS_G2, x0, x1, y0, y1: BLS_FP): bool =
let src = blst_p2_affine(x: blst_fp2(fp: [x0, x1]), y: blst_fp2(fp: [y0, y1]))
blst_p2_from_affine(g, src)
blst_p2_on_curve(g).int == 1
blst_p2_from_affine(toCV(g), toCC(src))
blst_p2_on_curve(toCV(g)).int == 1

func unpack(g: BLS_G2, x0, x1, y0, y1: var BLS_FP): bool =
var dst: blst_p2_affine
blst_p2_to_affine(dst, g)
blst_p2_to_affine(toCV(dst), toCC(g))
x0 = dst.x.fp[0]
x1 = dst.x.fp[1]
y0 = dst.y.fp[0]
y1 = dst.y.fp[1]
true

func nbits(s: BLS_SCALAR): uint =
var k = sizeof(s.l) - 1
while k >= 0 and s.l[k] == 0: dec k
var k = sizeof(s.b) - 1
while k >= 0 and s.b[k] == 0: dec k
if k < 0: return 0
var
bts = k shl 3
c = s.l[k]
c = s.b[k]

while c != 0:
c = c shr 1
Expand All @@ -89,49 +125,47 @@ func nbits(s: BLS_SCALAR): uint =
result = bts.uint

func add*(a: var BLS_G1, b: BLS_G1) {.inline.} =
blst_p1_add_or_double(a, a, b)
blst_p1_add_or_double(toCV(a), toCC(a), toCC(b))

func mul*(a: var BLS_G1, b: BLS_SCALAR) {.inline.} =
blst_p1_mult(a, a, b, b.nbits)
blst_p1_mult(toCV(a), toCV(a), b.b[0].unsafeAddr, b.nbits)

func add*(a: var BLS_G2, b: BLS_G2) {.inline.} =
blst_p2_add_or_double(a, a, b)
blst_p2_add_or_double(toCV(a), toCC(a), toCC(b))

func mul*(a: var BLS_G2, b: BLS_SCALAR) {.inline.} =
blst_p2_mult(a, a, b, b.nbits)
blst_p2_mult(toCV(a), toCV(a), b.b[0].unsafeAddr, b.nbits)

func mapFPToG1*(fp: BLS_FE): BLS_G1 {.inline.} =
let z: ptr blst_fp = nil
blst_map_to_g1(result, fp, z[])
blst_map_to_g1(toCV(result), toCC(fp), nil)

func mapFPToG2*(fp: BLS_FE2): BLS_G2 {.inline.} =
let z: ptr blst_fp2 = nil
blst_map_to_g2(result, fp, z[])
blst_map_to_g2(toCV(result), toCC(fp), nil)

func pack(g: var BLS_G1P, x, y: BLS_FP): bool =
g = blst_p1_affine(x: x, y: y)
blst_p1_affine_on_curve(g).int == 1
blst_p1_affine_on_curve(toCV(g)).int == 1

func pack(g: var BLS_G2P, x0, x1, y0, y1: BLS_FP): bool =
g = blst_p2_affine(x: blst_fp2(fp: [x0, x1]), y: blst_fp2(fp: [y0, y1]))
blst_p2_affine_on_curve(g).int == 1
blst_p2_affine_on_curve(toCV(g)).int == 1

func subgroupCheck*(P: BLS_G1P): bool {.inline.} =
blst_p1_affine_in_g1(P).int == 1
blst_p1_affine_in_g1(toCC(P)).int == 1

func subgroupCheck*(P: BLS_G2P): bool {.inline.} =
blst_p2_affine_in_g2(P).int == 1
blst_p2_affine_in_g2(toCC(P)).int == 1

func millerLoop*(P: BLS_G1P, Q: BLS_G2P): BLS_ACC {.inline.} =
blst_miller_loop(result, Q, P)
blst_miller_loop(toCV(result), toCC(Q), toCC(P))

proc mul*(a: var BLS_ACC, b: BLS_ACC) {.inline.} =
blst_fp12_mul(a, a, b)
blst_fp12_mul(toCV(a), toCC(a), toCC(b))

func check*(x: BLS_ACC): bool {.inline.} =
var ret: BLS_ACC
ret.blst_final_exp(x)
ret.blst_fp12_is_one().int == 1
blst_final_exp(toCV(ret), toCC(x))
blst_fp12_is_one(toCV(ret)).int == 1

# decodeFieldElement expects 64 byte input with zero top 16 bytes,
# returns lower 48 bytes.
Expand Down

0 comments on commit c5508b8

Please sign in to comment.