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:
Mamy Ratsimbazafy 2020-09-18 16:55:55 +02:00 committed by GitHub
parent e106549efe
commit ea4ec6a785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 16 deletions

View File

@ -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.} =
# We use the init-update-finish interface to avoid ## Apply the Eth2 Hash function
# the expensive burning/clearing memory (20~30% perf) ## Do NOT use for secret data.
# TODO: security implication? when BLS_BACKEND == BLST:
var ctx: sha256 # BLST has a fast assembly optimized SHA256
ctx.init() result.data.bls_sha256_digest(v)
ctx.update(v) else:
ctx.finish() # We use the init-update-finish interface to avoid
# the expensive burning/clearing memory (20~30% perf)
var ctx: Eth2DigestCtx
ctx.init()
ctx.update(v)
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)
var h {.inject.}: sha256 when nimvm:
init(h) # In SSZ, computeZeroHashes require compile-time SHA256
body block:
finish(h) var h {.inject.}: sha256
init(h)
body
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

2
vendor/nim-blscurve vendored

@ -1 +1 @@
Subproject commit da9ae49dab4cbb95b2cdaa68ecc221ee15c67a9a Subproject commit eed30c06231438b87fe25127326e40cb2bf412e3