Skip to content

Commit

Permalink
F function code moved to compression package
Browse files Browse the repository at this point in the history
This will let us to import only F code from projects that require only F
compression function and use govendor to manage dependencies (hello,
go-ethereum!)
  • Loading branch information
pdyraga committed Jul 1, 2019
1 parent 8a82dcc commit 8a47bb5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
11 changes: 8 additions & 3 deletions f.go → compression/f.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
//
// Modified by KEEP SEZC to expose F compression function.
//

package blake2b
package compression

import (
"math/bits"
)

// IV is an initialization vector for BLAKE2b
var IV = [8]uint64{
0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
}

// the precomputed values for BLAKE2b
// there are 10 16-byte arrays - one for each round
// the entries are calculated from the sigma constants.
Expand All @@ -35,7 +40,7 @@ func F(h *[8]uint64, m [16]uint64, c [2]uint64, f bool, rounds uint32) {
c0, c1 := c[0], c[1]

v0, v1, v2, v3, v4, v5, v6, v7 := h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]
v8, v9, v10, v11, v12, v13, v14, v15 := iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]
v8, v9, v10, v11, v12, v13, v14, v15 := IV[0], IV[1], IV[2], IV[3], IV[4], IV[5], IV[6], IV[7]
v12 ^= c0
v13 ^= c1

Expand Down
2 changes: 1 addition & 1 deletion f_test.go → compression/f_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package blake2b
package compression

import (
"encoding/binary"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/keep-network/blake2
module github.com/keep-network/blake2b

go 1.12
19 changes: 5 additions & 14 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"encoding/binary"
"errors"
"hash"

"github.com/keep-network/blake2b/compression"
)

const (
Expand All @@ -35,22 +37,11 @@ const (
Rounds = 12
)

var (
useAVX2 bool
useAVX bool
useSSE4 bool
)

var (
errKeySize = errors.New("blake2b: invalid key size")
errHashSize = errors.New("blake2b: invalid hash size")
)

var iv = [8]uint64{
0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
}

// Sum512 returns the BLAKE2b-512 checksum of the data.
func Sum512(data []byte) [Size]byte {
var sum [Size]byte
Expand Down Expand Up @@ -115,7 +106,7 @@ func newDigest(hashSize int, key []byte) (*digest, error) {
}

func checkSum(sum *[Size]byte, hashSize int, data []byte) {
h := iv
h := compression.IV
h[0] ^= uint64(hashSize) | (1 << 16) | (1 << 24)
var c [2]uint64

Expand Down Expand Up @@ -203,7 +194,7 @@ func (d *digest) BlockSize() int { return BlockSize }
func (d *digest) Size() int { return d.size }

func (d *digest) Reset() {
d.h = iv
d.h = compression.IV
d.h[0] ^= uint64(d.size) | (uint64(d.keyLen) << 8) | (1 << 16) | (1 << 24)
d.offset, d.c[0], d.c[1] = 0, 0, 0
if d.keyLen > 0 {
Expand Down Expand Up @@ -283,7 +274,7 @@ func hashBlocks(h *[8]uint64, blocks []byte, c *[2]uint64, f bool) {
i += 8
}

F(h, m, [2]uint64{c0, c1}, f, Rounds)
compression.F(h, m, [2]uint64{c0, c1}, f, Rounds)
}
c[0], c[1] = c0, c1
}
Expand Down
4 changes: 3 additions & 1 deletion xof.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"encoding/binary"
"errors"
"io"

"github.com/keep-network/blake2b/compression"
)

// XOF defines the interface to hash functions that
Expand Down Expand Up @@ -172,6 +174,6 @@ func (x *xof) Read(p []byte) (n int, err error) {
func (d *digest) initConfig(cfg *[Size]byte) {
d.offset, d.c[0], d.c[1] = 0, 0, 0
for i := range d.h {
d.h[i] = iv[i] ^ binary.LittleEndian.Uint64(cfg[i*8:])
d.h[i] = compression.IV[i] ^ binary.LittleEndian.Uint64(cfg[i*8:])
}
}

0 comments on commit 8a47bb5

Please sign in to comment.