Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devnet-6 fix bls_12_381 #3048

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion nimbus/evm/blscurve.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2020-2024 Status Research & Development GmbH
# Copyright (c) 2020-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
Expand Down Expand Up @@ -60,6 +60,13 @@ template toCC(x: auto): auto =
elif x is BLS_G2P:
toCC(x, cblst_p2_affine)

func isOverModulus(data: openArray[byte]): bool =
const
fieldModulus = StUint[512].fromHex "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
var z: StUint[512]
z.initFromBytesBE(data)
z >= fieldModulus

func fromBytes*(ret: var BLS_SCALAR, raw: openArray[byte]): bool =
const L = 32
if raw.len < L:
Expand All @@ -73,6 +80,8 @@ func fromBytes(ret: var BLS_FP, raw: openArray[byte]): bool =
if raw.len < L:
return false
let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr)
if isOverModulus(pa[]):
return false
blst_fp_from_bendian(toCV(ret), pa[])
true

Expand Down Expand Up @@ -150,6 +159,12 @@ 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(toCV(g)).int == 1

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

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

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

Expand Down
8 changes: 7 additions & 1 deletion nimbus/evm/precompiles.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
Expand Down Expand Up @@ -481,6 +481,9 @@ func blsG1MultiExp(c: Computation): EvmResultVoid =
if not p.decodePoint(input.toOpenArray(off, off+127)):
return err(prcErr(PrcInvalidPoint))

if not p.subgroupCheck:
return err(prcErr(PrcInvalidPoint))

# Decode scalar value
if not s.fromBytes(input.toOpenArray(off+128, off+159)):
return err(prcErr(PrcInvalidParam))
Expand Down Expand Up @@ -546,6 +549,9 @@ func blsG2MultiExp(c: Computation): EvmResultVoid =
if not p.decodePoint(input.toOpenArray(off, off+255)):
return err(prcErr(PrcInvalidPoint))

if not p.subgroupCheck:
return err(prcErr(PrcInvalidPoint))

# Decode scalar value
if not s.fromBytes(input.toOpenArray(off+256, off+287)):
return err(prcErr(PrcInvalidParam))
Expand Down
Loading