komihash.cr

Direct rough translation, port of komihash to Crystal. For details on the algorithm refer to the original C implementation.

Installation

Add this repository as dependency to your shard.yml:

dependencies:
  komihash:
    git: "https://codeberg.org/coralpink/komihash.cr.git"

Usage

require "komihash"

hash = Komihash.hash "This function is named the way it is named is to honor the Komi Republic (located in Russia), native to the author."
random = Komihash.rand

For streamed usage use Komihash::Stream struct with similar API as the reference library:

require "komihash"

stream = Komihash::Stream.new
stream.update "the territory of the Komi Republic was internationally"
stream.update " known as Condora Regio inhabited by people worshipping a golden"
stream.update " (sun-shining) woman-goddess"

hash = stream.final

There is also API to match digests and RNGs in the Crystal standard library:

require "komihash"

digest = Komihash::Digest.new
digest << "the territory of the Komi Republic was internationally"
digest << " known as Condora Regio inhabited by people worshipping a golden"
digest << " (sun-shining) woman-goddess"

hash = digest.final
require "komihash"

rng = Komihash::Random.new
number = rng.rand

Generated docs are available at coralpink.codeberg.page/komihash.cr

Performance

Nowhere near inlined C, but decent enough when compared to linking against it.

$ shards run --release -Dkomihash_ffi benchmark
[benchmark: 32*4 bytes]
crystal   5.92M (169.02ns) (± 0.74%)  0.0B/op   1.60× slower
  c ffi   9.48M (105.46ns) (± 0.14%)  0.0B/op        fastest
[benchmark: 32*8 bytes]
crystal   4.41M (226.92ns) (± 0.52%)  0.0B/op   1.64× slower
  c ffi   7.22M (138.50ns) (± 0.13%)  0.0B/op        fastest
[benchmark: 32*16 bytes]
crystal   2.92M (342.95ns) (± 0.39%)  0.0B/op   1.65× slower
  c ffi   4.82M (207.45ns) (± 0.27%)  0.0B/op        fastest
[benchmark: 32*32 bytes]
crystal   1.65M (606.09ns) (± 0.32%)  0.0B/op   1.73× slower
  c ffi   2.86M (349.63ns) (± 0.41%)  0.0B/op        fastest
[benchmark: 32*64 bytes]
crystal 933.34k (  1.07µs) (± 0.64%)  0.0B/op   1.71× slower
  c ffi   1.60M (625.71ns) (± 0.70%)  0.0B/op        fastest
[benchmark: 32*256 bytes]
crystal   2.05M (488.51ns) (± 0.38%)  0.0B/op   1.31× slower
  c ffi   2.67M (373.97ns) (± 0.21%)  0.0B/op        fastest
[benchmark: 32*4096 bytes]
crystal 169.36k (  5.90µs) (± 0.36%)  0.0B/op   1.23× slower
  c ffi 207.54k (  4.82µs) (± 0.12%)  0.0B/op        fastest

See for yourself by creating a libkomihash.a inside the ./vendor directory.

License

Licensed under Apache License version 2.0.

Based on the original C implementation - komihash - Copyright (c) Aleksey Vaneev - licensed under MIT.