Upgrade blst (#1682)
* Bump BLST * Test for https://github.com/supranational/blst/issues/22 regression * Use SHA256 from BLST + bump nim-blscurve to reenable fno-tree-vectorize * SHA256 on non-blst platforms import fixes * import fixes again * can't prefix with nimcrypto * address review comment [skip ci] * {.noInit.} on the digests
This commit is contained in:
parent
e106549efe
commit
ea4ec6a785
|
@ -22,18 +22,27 @@
|
||||||
{.push raises: [Defect].}
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import
|
import
|
||||||
|
# Standard library
|
||||||
|
std/hashes,
|
||||||
|
#Status libraries
|
||||||
chronicles,
|
chronicles,
|
||||||
nimcrypto/[sha2, hash],
|
nimcrypto/[sha2, hash],
|
||||||
stew/byteutils,
|
stew/byteutils,
|
||||||
hashes,
|
hashes,
|
||||||
eth/common/eth_types_json_serialization
|
eth/common/eth_types_json_serialization,
|
||||||
|
blscurve
|
||||||
|
|
||||||
export
|
export
|
||||||
hash.`$`, sha2, readValue, writeValue
|
hash.`$`, sha2, readValue, writeValue
|
||||||
|
|
||||||
type
|
type
|
||||||
Eth2Digest* = MDigest[32 * 8] ## `hash32` from spec
|
Eth2Digest* = MDigest[32 * 8] ## `hash32` from spec
|
||||||
Eth2Hash* = sha256 ## Context for hash function
|
|
||||||
|
when BLS_BACKEND == BLST:
|
||||||
|
export blscurve.update
|
||||||
|
type Eth2DigestCtx* = BLST_SHA256_CTX
|
||||||
|
else:
|
||||||
|
type Eth2DigestCtx* = sha2.sha256
|
||||||
|
|
||||||
func shortLog*(x: Eth2Digest): string =
|
func shortLog*(x: Eth2Digest): string =
|
||||||
x.data.toOpenArray(0, 3).toHex()
|
x.data.toOpenArray(0, 3).toHex()
|
||||||
|
@ -44,26 +53,52 @@ chronicles.formatIt Eth2Digest:
|
||||||
# TODO: expose an in-place digest function
|
# TODO: expose an in-place digest function
|
||||||
# when hashing in loop or into a buffer
|
# when hashing in loop or into a buffer
|
||||||
# See: https://github.com/cheatfate/nimcrypto/blob/b90ba3abd/nimcrypto/sha2.nim#L570
|
# See: https://github.com/cheatfate/nimcrypto/blob/b90ba3abd/nimcrypto/sha2.nim#L570
|
||||||
func eth2digest*(v: openArray[byte]): Eth2Digest {.inline.} =
|
func eth2digest*(v: openArray[byte]): Eth2Digest {.noInit.} =
|
||||||
|
## Apply the Eth2 Hash function
|
||||||
|
## Do NOT use for secret data.
|
||||||
|
when BLS_BACKEND == BLST:
|
||||||
|
# BLST has a fast assembly optimized SHA256
|
||||||
|
result.data.bls_sha256_digest(v)
|
||||||
|
else:
|
||||||
# We use the init-update-finish interface to avoid
|
# We use the init-update-finish interface to avoid
|
||||||
# the expensive burning/clearing memory (20~30% perf)
|
# the expensive burning/clearing memory (20~30% perf)
|
||||||
# TODO: security implication?
|
var ctx: Eth2DigestCtx
|
||||||
var ctx: sha256
|
|
||||||
ctx.init()
|
ctx.init()
|
||||||
ctx.update(v)
|
ctx.update(v)
|
||||||
ctx.finish()
|
ctx.finish()
|
||||||
|
|
||||||
func update*(ctx: var Sha2Context; digest: Eth2Digest) =
|
when BLS_BACKEND == BLST:
|
||||||
|
func update*(ctx: var BLST_SHA256_CTX; digest: Eth2Digest) =
|
||||||
|
ctx.update digest.data
|
||||||
|
func update*(ctx: var sha256; digest: Eth2Digest) =
|
||||||
ctx.update digest.data
|
ctx.update digest.data
|
||||||
|
|
||||||
template withEth2Hash*(body: untyped): Eth2Digest =
|
template withEth2Hash*(body: untyped): Eth2Digest =
|
||||||
## This little helper will init the hash function and return the sliced
|
## This little helper will init the hash function and return the sliced
|
||||||
## hash:
|
## hash:
|
||||||
## let hashOfData = withHash: h.update(data)
|
## let hashOfData = withHash: h.update(data)
|
||||||
|
when nimvm:
|
||||||
|
# In SSZ, computeZeroHashes require compile-time SHA256
|
||||||
|
block:
|
||||||
var h {.inject.}: sha256
|
var h {.inject.}: sha256
|
||||||
init(h)
|
init(h)
|
||||||
body
|
body
|
||||||
finish(h)
|
finish(h)
|
||||||
|
else:
|
||||||
|
when BLS_BACKEND == BLST:
|
||||||
|
block:
|
||||||
|
var h {.inject, noInit.}: Eth2DigestCtx
|
||||||
|
init(h)
|
||||||
|
body
|
||||||
|
var res {.noInit.}: Eth2Digest
|
||||||
|
finalize(res.data, h)
|
||||||
|
res
|
||||||
|
else:
|
||||||
|
block:
|
||||||
|
var h {.inject, noInit.}: Eth2DigestCtx
|
||||||
|
init(h)
|
||||||
|
body
|
||||||
|
finish(h)
|
||||||
|
|
||||||
func hash*(x: Eth2Digest): Hash =
|
func hash*(x: Eth2Digest): Hash =
|
||||||
## Hash for digests for Nim hash tables
|
## Hash for digests for Nim hash tables
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit da9ae49dab4cbb95b2cdaa68ecc221ee15c67a9a
|
Subproject commit eed30c06231438b87fe25127326e40cb2bf412e3
|
Loading…
Reference in New Issue