mirror of
https://github.com/logos-storage/constantine.git
synced 2026-01-02 21:23:11 +00:00
* [testsuite] Rework parallel test runner to buffer beyond 65536 chars and properly wait for process exit * [testsuite] improve error reporting * rework openArray[byte/char] for BLS signature C API * Prepare for optimized library and bindings * properly link to constantine * Compiler fixes, global sanitizers, GCC bug with --opt:size * workaround/fix #229: don't inline field reduction in Fp2 * fix clang running out of registers with LTO * [C API] missed length parameters for ctt_eth_bls_fast_aggregate_verify * double-precision asm is too large for inlining, try to fix Linux and MacOS woes at https://github.com/mratsim/constantine/pull/228#issuecomment-1512773460 * Use FORTIFY_SOURCE for testing * Fix #230 - gcc miscompiles Fp6 mul with LTO * disable LTO for now, PR is too long
70 lines
2.2 KiB
Nim
70 lines
2.2 KiB
Nim
# Constantine
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
|
|
# Licensed and distributed under either of
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
import platforms/views
|
|
|
|
# ############################################################
|
|
#
|
|
# Hash Function concept
|
|
#
|
|
# ############################################################
|
|
|
|
type
|
|
CryptoHash* = concept h, var ctx, type H
|
|
## Interface of a cryptographic hash function
|
|
##
|
|
## - digestSizeInBytes is the hash output size in bytes
|
|
## - internalBlockSize, in bits:
|
|
## hash functions are supposed to ingest fixed block size
|
|
## that are padded if necessary
|
|
## - SHA256 block size is 64 bits
|
|
## - SHA512 block size is 128 bits
|
|
## - SHA3-512 block size is 72 bits
|
|
|
|
# should we avoid int to avoid exception? But they are compile-time
|
|
H.digestSize is static int
|
|
H.internalBlockSize is static int
|
|
|
|
# Context
|
|
# -------------------------------------------
|
|
ctx.init()
|
|
ctx.update(openarray[byte])
|
|
ctx.finish(var array[H.digestSize, byte])
|
|
ctx.clear()
|
|
|
|
func hash*[DigestSize: static int](
|
|
HashKind: type CryptoHash,
|
|
digest: var array[DigestSize, byte],
|
|
message: openArray[byte],
|
|
clearMem = false) {.genCharAPI.} =
|
|
## Produce a digest from a message
|
|
static: doAssert DigestSize == HashKind.type.digestSize
|
|
|
|
var ctx {.noInit.}: HashKind
|
|
ctx.init()
|
|
ctx.update(message)
|
|
ctx.finish(digest)
|
|
|
|
if clearMem:
|
|
ctx.clear()
|
|
|
|
func hash*(
|
|
HashKind: type CryptoHash,
|
|
message: openArray[byte],
|
|
clearmem = false): array[HashKind.digestSize, byte] {.noInit, genCharAPI.} =
|
|
## Produce a digest from a message
|
|
HashKind.hash(result, message, clearMem)
|
|
|
|
# Exports
|
|
# -----------------------------------------------------------------------
|
|
|
|
import ./hashes/h_sha256
|
|
export h_sha256
|
|
|
|
static: doAssert sha256 is CryptoHash
|