Digest::SipHash v0.0.1リリース

ksss/digest-siphash · GitHub

digest-siphash | RubyGems.org | your community gem host

作る

SipHash(https://131002.net/siphash/)のRuby実装がpureRuby版はあったのですが(https://github.com/emboss/siphash-ruby)、C拡張版かつDigest系のAPIを使用しているものがなかったので作成しました。

悩んだのはseedの扱いぐらいで、ほとんどがMurmurHashを実装した時と同じように書けたので楽に作れました。

CRuby内では既にsiphashは使用されているのですが、あくまでHash関数の裏側として使用しているのでAPIは公開していないようです。

使う

Digest::SipHashを使用することでバージョンを気にすることなくSipHash関数をRubyで使用することができます。

gem install digest-siphash
irb -r digest/siphash
irb(main):001:0> Digest::SipHash.digest "siphash"
"\x59\xca\xae\xb9\x0d\x54\x24\x64"
irb(main):002:0> Digest::SipHash.hexdigest "siphash"
"59caaeb90d542464"

測る

10万回の単純なSipHashを計算するBenchmarkはこんな感じでした。(pure Ruby版はsiphash gemを使用しました)

#! /usr/bin/env ruby

require 'siphash'
require 'digest/siphash'
require 'benchmark'

n = 100000
seed = (0..0x0f).to_a.pack "C16"
Benchmark.bm do |x|
  x.report("siphash"){ n.times{ SipHash.digest seed, "bench" } }
  x.report("digest/siphash"){ n.times{ Digest::SipHash.rawdigest "bench", seed } }
end
       user     system      total        real
siphash  4.580000   0.010000   4.590000 (  4.596224)
digest/siphash  0.030000   0.000000   0.030000 (  0.034023)

挨拶

新年早々親戚回りの合間にgemを作ったりしていますが、今年もひたすらOSSを書きたいものですね。

参考

SipHash: a fast short-input PRF

404 Blog Not Found:perl - Digest::SipHash released