Skip to content

Commit

Permalink
devnet-6 fix bls_12_381 (#3048)
Browse files Browse the repository at this point in the history
* fix mathematical misconceptions

* fix lint

* change proc to func
  • Loading branch information
advaita-saha authored Feb 5, 2025
1 parent db0a971 commit 7ebede9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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

0 comments on commit 7ebede9

Please sign in to comment.