[Pkg-privacy-commits] [golang-siphash-dev] 01/23: Initial commit
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 12:55:52 UTC 2015
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository golang-siphash-dev.
commit 4c0409ff2d17decc7e5f6a2473a5be7dfb5e0c58
Author: Dmitry Chestnykh <dmitry at codingrobots.com>
Date: Wed Jun 20 22:12:58 2012 +0200
Initial commit
---
siphash.go | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
siphash_test.go | 14 ++++++
2 files changed, 167 insertions(+)
diff --git a/siphash.go b/siphash.go
new file mode 100644
index 0000000..7e21f9f
--- /dev/null
+++ b/siphash.go
@@ -0,0 +1,153 @@
+// Written in 2012 by Dmitry Chestnykh.
+//
+// To the extent possible under law, the author have dedicated all copyright
+// and related and neighboring rights to this software to the public domain
+// worldwide. This software is distributed without any warranty.
+// http://creativecommons.org/publicdomain/zero/1.0/
+
+// Package siphash implements SipHash-2-4, a fast short-input PRF
+// created by Jean-Philippe Aumasson and Daniel J. Bernstein.
+package siphash
+
+import (
+ "encoding/binary"
+ "hash"
+)
+
+const (
+ // The block size of hash algorithm in bytes.
+ BlockSize = 8
+ // The size of hash output in bytes.
+ Size = 8
+ // Number of c iterations.
+ CRounds = 2
+ // Number of d iterations.
+ DRounds = 4
+)
+
+type digest struct {
+ v0, v1, v2, v3 uint64 // state
+ k0, k1 uint64 // two parts of key
+ t uint8 // message bytes counter (mod 256)
+ nx int // number of bytes in buffer x
+ x [8]byte // buffer for unprocessed bytes
+}
+
+// New returns a new hash.Hash64 computing SipHash-2-4.
+func New(key []byte) hash.Hash64 {
+ d := new(digest)
+ d.k0 = binary.LittleEndian.Uint64(key[0:8])
+ d.k1 = binary.LittleEndian.Uint64(key[8:16])
+ d.Reset()
+ return d
+}
+
+func (d *digest) Reset() {
+ d.v0 = d.k0 ^ 0x736f6d6570736575
+ d.v1 = d.k1 ^ 0x646f72616e646f6d
+ d.v2 = d.k0 ^ 0x6c7967656e657261
+ d.v3 = d.k1 ^ 0x7465646279746573
+ d.t = 0
+ d.nx = 0
+}
+
+func (d *digest) Size() int { return Size }
+
+func (d *digest) BlockSize() int { return BlockSize }
+
+func block(d *digest, p []uint8) {
+ v0, v1, v2, v3 := d.v0, d.v1, d.v2, d.v3
+
+ for len(p) >= BlockSize {
+ m := binary.LittleEndian.Uint64(p)
+
+ v3 ^= m
+ for i := 0; i < CRounds; i++ {
+ v0 += v1
+ v2 += v3
+ v1 = v1<<13 | v1>>(64-13)
+ v3 = v3<<16 | v3>>(64-16)
+ v1 ^= v0
+ v3 ^= v2
+ v0 = v0<<32 | v0>>(64-32)
+
+ v2 += v1
+ v0 += v3
+ v1 = v1<<17 | v1>>(64-17)
+ v3 = v3<<21 | v3>>(64-21)
+
+ v1 ^= v2
+ v3 ^= v0
+ v2 = v2<<32 | v2>>(64-32)
+ }
+ v0 ^= m
+
+ p = p[BlockSize:]
+ d.t += BlockSize
+ }
+
+ d.v0, d.v1, d.v2, d.v3 = v0, v1, v2, v3
+}
+
+func (d *digest) Write(p []byte) (nn int, err error) {
+ nn = len(p)
+ if d.nx > 0 {
+ n := len(p)
+ if n > BlockSize-d.nx {
+ n = BlockSize - d.nx
+ }
+ d.nx += copy(d.x[d.nx:], p)
+ if d.nx == BlockSize {
+ block(d, d.x[:])
+ d.nx = 0
+ }
+ p = p[n:]
+ }
+ if len(p) >= BlockSize {
+ n := len(p) &^ (BlockSize - 1)
+ block(d, p[:n])
+ p = p[n:]
+ }
+ if len(p) > 0 {
+ d.nx = copy(d.x[:], p)
+ }
+ return
+}
+
+func (d0 *digest) Sum64() uint64 {
+ // Make a copy of d0 so that caller can keep writing and summing.
+ d := *d0
+
+ var zeros [8]byte
+ d.Write(zeros[:7-d.nx])
+ d.t += uint8(d.nx)
+ d.Write([]byte{d.t})
+
+ v0, v1, v2, v3 := d.v0, d.v1, d.v2, d.v3
+ v2 ^= 0xff
+ for i := 0; i < DRounds; i++ {
+ v0 += v1
+ v2 += v3
+ v1 = v1<<13 | v1>>(64-13)
+ v3 = v3<<16 | v3>>(64-16)
+ v1 ^= v0
+ v3 ^= v2
+ v0 = v0<<32 | v0>>(64-32)
+
+ v2 += v1
+ v0 += v3
+ v1 = v1<<17 | v1>>(64-17)
+ v3 = v3<<21 | v3>>(64-21)
+
+ v1 ^= v2
+ v3 ^= v0
+ v2 = v2<<32 | v2>>(64-32)
+ }
+ return v0 ^ v1 ^ v2 ^ v3
+}
+
+func (d *digest) Sum(in []byte) []byte {
+ var tmp [8]byte
+ binary.LittleEndian.PutUint64(tmp[:], d.Sum64())
+ return append(in, tmp[:]...)
+}
diff --git a/siphash_test.go b/siphash_test.go
new file mode 100644
index 0000000..8ae7ddf
--- /dev/null
+++ b/siphash_test.go
@@ -0,0 +1,14 @@
+package siphash
+
+import "testing"
+
+func TestNew(t *testing.T) {
+ k := []byte{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
+ m := []byte{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e}
+ result := uint64(0xa129ca6149be45e5)
+ h := New(k)
+ h.Write(m)
+ if sum := h.Sum64(); sum != result {
+ t.Errorf("expected %x, got %x", result, sum)
+ }
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/golang-siphash-dev.git
More information about the Pkg-privacy-commits
mailing list