Skip to content

Commit

Permalink
keccak: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Dec 22, 2024
1 parent d0834ca commit b63bd61
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
88 changes: 88 additions & 0 deletions benchmarks/bench_h_keccak.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import
# Internals
constantine/hashes,
# Helpers
helpers/prng_unsafe,
./bench_blueprint

proc separator*() = separator(69)

# Deal with platform mess
# --------------------------------------------------------------------
when defined(windows):
when sizeof(int) == 8:
const DLLSSLName* = "(libssl-1_1-x64|ssleay64|libssl64).dll"
else:
const DLLSSLName* = "(libssl-1_1|ssleay32|libssl32).dll"
else:
when defined(macosx) or defined(macos) or defined(ios):
const versions = "(.1.1|.38|.39|.41|.43|.44|.45|.46|.47|.48|.10|.1.0.2|.1.0.1|.1.0.0|.0.9.9|.0.9.8|)"
else:
const versions = "(.1.1|.1.0.2|.1.0.1|.1.0.0|.0.9.9|.0.9.8|.48|.47|.46|.45|.44|.43|.41|.39|.38|.10|)"

when defined(macosx) or defined(macos) or defined(ios):
const DLLSSLName* = "libssl" & versions & ".dylib"
elif defined(genode):
const DLLSSLName* = "libssl.lib.so"
else:
const DLLSSLName* = "libssl.so" & versions

# OpenSSL wrapper
# --------------------------------------------------------------------

proc EVP_Q_digest[T: byte|char](
ossl_libctx: pointer,
algoName: cstring,
propq: cstring,
data: openArray[T],
digest: var array[32, byte],
size: ptr uint): int32 {.noconv, dynlib: DLLSSLName, importc.}

proc SHA3_256_OpenSSL[T: byte|char](
digest: var array[32, byte],
s: openArray[T]) =
discard EVP_Q_digest(nil, "SHA3-256", nil, s, digest, nil)

# --------------------------------------------------------------------

proc report(op: string, bytes: int, startTime, stopTime: MonoTime, startClk, stopClk: int64, iters: int) =
let ns = inNanoseconds((stopTime-startTime) div iters)
let throughput = 1e9 / float64(ns)
when SupportsGetTicks:
let cycles = (stopClk - startClk) div iters
let cyclePerByte = cycles.float64 / bytes.float64
echo &"{op:<50} {throughput:>15.3f} ops/s {ns:>9} ns/op {cycles:>10} cycles {cyclePerByte:>5.2f} cycles/byte"
else:
echo &"{op:<50} {throughput:>15.3f} ops/s {ns:>9} ns/op"

template bench(op: string, bytes: int, iters: int, body: untyped): untyped =
measure(iters, startTime, stopTime, startClk, stopClk, body)
report(op, bytes, startTime, stopTime, startClk, stopClk, iters)

proc benchKeccak256_constantine[T](msg: openarray[T], msgComment: string, iters: int) =
var digest: array[32, byte]
bench("Keccak256 - Constantine - " & msgComment, msg.len, iters):
keccak256.hash(digest, msg)

proc benchSHA3_256_openssl[T](msg: openarray[T], msgComment: string, iters: int) =
var digest: array[32, byte]
bench("SHA3-256 - OpenSSL - " & msgComment, msg.len, iters):
SHA3_256_OpenSSL(digest, msg)

when isMainModule:
proc main() =
const sizes = [
32, 64, 128, 256,
1024, 4096, 16384, 65536,
1_000_000, 10_000_000
]

const target_cycles = 1_000_000_000'i64
const worst_cycles_per_bytes = 25'i64
for s in sizes:
let msg = rng.random_byte_seq(s)
let iters = int(target_cycles div (s.int64 * worst_cycles_per_bytes))
benchKeccak256_constantine(msg, $s & "B", iters)
benchSHA3_256_openssl(msg, $s & "B", iters)

main()
File renamed without changes.
8 changes: 6 additions & 2 deletions constantine.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ const benchDesc = [
"bench_summary_bn254_snarks",
"bench_summary_pasta",
"bench_poly1305",
"bench_sha256",
"bench_h_sha256",
"bench_h_keccak",
"bench_hash_to_curve",
"bench_gmp_modexp",
"bench_gmp_modmul",
Expand Down Expand Up @@ -1119,7 +1120,10 @@ task bench_summary_pasta, "Run summary benchmarks for the Pasta curves - CC comp
# ------------------------------------------

task bench_sha256, "Run SHA256 benchmarks":
runBench("bench_sha256")
runBench("bench_h_sha256")

task bench_keccak, "Run Keccak256 benchmarks":
runBench("bench_h_keccak")

# Hash-to-curve
# ------------------------------------------
Expand Down

0 comments on commit b63bd61

Please sign in to comment.