mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-09 02:35:08 +00:00
Fix ecMul, ecPairing, ecAdd.
This commit is contained in:
parent
6e4616e443
commit
0b15b86195
@ -20,7 +20,7 @@ requires "nim >= 0.18.1",
|
|||||||
"eth_p2p",
|
"eth_p2p",
|
||||||
"eth_keyfile",
|
"eth_keyfile",
|
||||||
"eth_keys",
|
"eth_keys",
|
||||||
"https://github.com/status-im/nim-bncurve"
|
"https://github.com/status-im/nim-bncurve >= 1.0.1"
|
||||||
|
|
||||||
proc buildBinary(name: string, srcDir = ".", lang = "c") =
|
proc buildBinary(name: string, srcDir = ".", lang = "c") =
|
||||||
if not dirExists "build": mkDir "build"
|
if not dirExists "build": mkDir "build"
|
||||||
|
@ -41,9 +41,9 @@ proc getPoint[T: G1|G2](t: typedesc[T], data: openarray[byte]): Point[T] =
|
|||||||
else:
|
else:
|
||||||
const nextOffset = 64
|
const nextOffset = 64
|
||||||
var px, py: FQ2
|
var px, py: FQ2
|
||||||
if not px.fromBytes(data.toOpenArray(0, nextOffset - 1)):
|
if not px.fromBytes2(data.toOpenArray(0, nextOffset - 1)):
|
||||||
raise newException(ValidationError, "Could not get point value")
|
raise newException(ValidationError, "Could not get point value")
|
||||||
if not py.fromBytes(data.toOpenArray(nextOffset, nextOffset * 2 - 1)):
|
if not py.fromBytes2(data.toOpenArray(nextOffset, nextOffset * 2 - 1)):
|
||||||
raise newException(ValidationError, "Could not get point value")
|
raise newException(ValidationError, "Could not get point value")
|
||||||
if px.isZero() and py.isZero():
|
if px.isZero() and py.isZero():
|
||||||
result = T.zero()
|
result = T.zero()
|
||||||
@ -54,7 +54,7 @@ proc getPoint[T: G1|G2](t: typedesc[T], data: openarray[byte]): Point[T] =
|
|||||||
result = ap.toJacobian()
|
result = ap.toJacobian()
|
||||||
|
|
||||||
proc getFR(data: openarray[byte]): FR =
|
proc getFR(data: openarray[byte]): FR =
|
||||||
if not result.fromBytes(data):
|
if not result.fromBytes2(data):
|
||||||
raise newException(ValidationError, "Could not get FR value")
|
raise newException(ValidationError, "Could not get FR value")
|
||||||
|
|
||||||
proc ecRecover*(computation: var BaseComputation) =
|
proc ecRecover*(computation: var BaseComputation) =
|
||||||
@ -171,20 +171,17 @@ proc bn256ecAdd*(computation: var BaseComputation) =
|
|||||||
var
|
var
|
||||||
input: array[128, byte]
|
input: array[128, byte]
|
||||||
output: array[64, byte]
|
output: array[64, byte]
|
||||||
|
|
||||||
# Padding data
|
# Padding data
|
||||||
let msglen = len(computation.msg.data)
|
let msglen = len(computation.msg.data)
|
||||||
let tocopy = if msglen < 128: msglen else: 128
|
let tocopy = if msglen < 128: msglen else: 128
|
||||||
if tocopy > 0:
|
if tocopy > 0:
|
||||||
copyMem(addr input[0], addr computation.msg.data[0], tocopy)
|
copyMem(addr input[0], addr computation.msg.data[0], tocopy)
|
||||||
|
|
||||||
var p1 = G1.getPoint(input.toOpenArray(0, 63))
|
var p1 = G1.getPoint(input.toOpenArray(0, 63))
|
||||||
var p2 = G1.getPoint(input.toOpenArray(64, 127))
|
var p2 = G1.getPoint(input.toOpenArray(64, 127))
|
||||||
var apo = (p1 + p2).toAffine()
|
var apo = (p1 + p2).toAffine()
|
||||||
if isSome(apo):
|
if isSome(apo):
|
||||||
let p = apo.get()
|
|
||||||
# we can discard here because we supply proper buffer
|
# we can discard here because we supply proper buffer
|
||||||
discard p.toBytes(output)
|
discard apo.get().toBytes(output)
|
||||||
|
|
||||||
# TODO: gas computation
|
# TODO: gas computation
|
||||||
# computation.gasMeter.consumeGas(gasFee, reason = "ecAdd Precompile")
|
# computation.gasMeter.consumeGas(gasFee, reason = "ecAdd Precompile")
|
||||||
@ -205,9 +202,8 @@ proc bn256ecMul*(computation: var BaseComputation) =
|
|||||||
var fr = getFR(input.toOpenArray(64, 95))
|
var fr = getFR(input.toOpenArray(64, 95))
|
||||||
var apo = (p1 * fr).toAffine()
|
var apo = (p1 * fr).toAffine()
|
||||||
if isSome(apo):
|
if isSome(apo):
|
||||||
let p = apo.get()
|
|
||||||
# we can discard here because we supply buffer of proper size
|
# we can discard here because we supply buffer of proper size
|
||||||
discard p.toBytes(output)
|
discard apo.get().toBytes(output)
|
||||||
|
|
||||||
# TODO: gas computation
|
# TODO: gas computation
|
||||||
# computation.gasMeter.consumeGas(gasFee, reason="ecMul Precompile")
|
# computation.gasMeter.consumeGas(gasFee, reason="ecMul Precompile")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user