mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-22 08:48:24 +00:00
devnet-6 fix bls_12_381 (#3048)
* fix mathematical misconceptions * fix lint * change proc to func
This commit is contained in:
parent
db0a971416
commit
7ebede9e1e
@ -1,5 +1,5 @@
|
|||||||
# Nimbus
|
# Nimbus
|
||||||
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
# Copyright (c) 2020-2025 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0)
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
@ -60,6 +60,13 @@ template toCC(x: auto): auto =
|
|||||||
elif x is BLS_G2P:
|
elif x is BLS_G2P:
|
||||||
toCC(x, cblst_p2_affine)
|
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 =
|
func fromBytes*(ret: var BLS_SCALAR, raw: openArray[byte]): bool =
|
||||||
const L = 32
|
const L = 32
|
||||||
if raw.len < L:
|
if raw.len < L:
|
||||||
@ -73,6 +80,8 @@ func fromBytes(ret: var BLS_FP, raw: openArray[byte]): bool =
|
|||||||
if raw.len < L:
|
if raw.len < L:
|
||||||
return false
|
return false
|
||||||
let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr)
|
let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr)
|
||||||
|
if isOverModulus(pa[]):
|
||||||
|
return false
|
||||||
blst_fp_from_bendian(toCV(ret), pa[])
|
blst_fp_from_bendian(toCV(ret), pa[])
|
||||||
true
|
true
|
||||||
|
|
||||||
@ -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]))
|
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
|
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.} =
|
func subgroupCheck*(P: BLS_G1P): bool {.inline.} =
|
||||||
blst_p1_affine_in_g1(toCC(P)).int == 1
|
blst_p1_affine_in_g1(toCC(P)).int == 1
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# Nimbus
|
# Nimbus
|
||||||
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
# Copyright (c) 2018-2025 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0)
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
@ -481,6 +481,9 @@ func blsG1MultiExp(c: Computation): EvmResultVoid =
|
|||||||
if not p.decodePoint(input.toOpenArray(off, off+127)):
|
if not p.decodePoint(input.toOpenArray(off, off+127)):
|
||||||
return err(prcErr(PrcInvalidPoint))
|
return err(prcErr(PrcInvalidPoint))
|
||||||
|
|
||||||
|
if not p.subgroupCheck:
|
||||||
|
return err(prcErr(PrcInvalidPoint))
|
||||||
|
|
||||||
# Decode scalar value
|
# Decode scalar value
|
||||||
if not s.fromBytes(input.toOpenArray(off+128, off+159)):
|
if not s.fromBytes(input.toOpenArray(off+128, off+159)):
|
||||||
return err(prcErr(PrcInvalidParam))
|
return err(prcErr(PrcInvalidParam))
|
||||||
@ -546,6 +549,9 @@ func blsG2MultiExp(c: Computation): EvmResultVoid =
|
|||||||
if not p.decodePoint(input.toOpenArray(off, off+255)):
|
if not p.decodePoint(input.toOpenArray(off, off+255)):
|
||||||
return err(prcErr(PrcInvalidPoint))
|
return err(prcErr(PrcInvalidPoint))
|
||||||
|
|
||||||
|
if not p.subgroupCheck:
|
||||||
|
return err(prcErr(PrcInvalidPoint))
|
||||||
|
|
||||||
# Decode scalar value
|
# Decode scalar value
|
||||||
if not s.fromBytes(input.toOpenArray(off+256, off+287)):
|
if not s.fromBytes(input.toOpenArray(off+256, off+287)):
|
||||||
return err(prcErr(PrcInvalidParam))
|
return err(prcErr(PrcInvalidParam))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user