Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inclusion of Skein #52

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
44 changes: 39 additions & 5 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"math"

b58 "github.com/jbenet/go-base58"
b58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
)

// errors
Expand Down Expand Up @@ -53,10 +53,17 @@ const (
SHAKE_128 = 0x18
SHAKE_256 = 0x19

BLAKE2B_MIN = 0xb201
BLAKE2B_MAX = 0xb240
BLAKE2S_MIN = 0xb241
BLAKE2S_MAX = 0xb260
BLAKE2B_MIN = 0xB201
BLAKE2B_MAX = 0xB240
BLAKE2S_MIN = 0xB241
BLAKE2S_MAX = 0xB260

SKEIN256_MIN = 0xB301
SKEIN256_MAX = 0xB320
SKEIN512_MIN = 0xB321
SKEIN512_MAX = 0xB360
SKEIN1024_MIN = 0xB361
SKEIN1024_MAX = 0xB3E0

DBL_SHA2_256 = 0x56

Expand All @@ -81,6 +88,33 @@ func init() {
Codes[c] = name
DefaultLengths[c] = int(n)
}

// Add skein256 (32 codes)
for c := uint64(SKEIN256_MIN); c <= SKEIN256_MAX; c++ {
n := c - SKEIN256_MIN + 1
name := fmt.Sprintf("skein256-%d", n*8)
Names[name] = c
Codes[c] = name
DefaultLengths[c] = int(n)
}

// Add skein512 (64 codes)
for c := uint64(SKEIN512_MIN); c <= SKEIN512_MAX; c++ {
n := c - SKEIN512_MIN + 1
name := fmt.Sprintf("skein512-%d", n*8)
Names[name] = c
Codes[c] = name
DefaultLengths[c] = int(n)
}

// Add skein1024 (128 codes)
for c := uint64(SKEIN1024_MIN); c <= SKEIN1024_MAX; c++ {
n := c - SKEIN1024_MIN + 1
name := fmt.Sprintf("skein1024-%d", n*8)
Names[name] = c
Codes[c] = name
DefaultLengths[c] = int(n)
}
}

// Names maps the name of a hash to the code
Expand Down
2 changes: 1 addition & 1 deletion opts/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/hex"
"fmt"

base58 "github.com/jbenet/go-base58"
mh "github.com/multiformats/go-multihash"
base58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
)

func Decode(encoding, digest string) (mh.Multihash, error) {
Expand Down
52 changes: 47 additions & 5 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"errors"
"fmt"

"github.com/spaolacci/murmur3"
blake2b "golang.org/x/crypto/blake2b"
blake2s "golang.org/x/crypto/blake2s"
sha3 "golang.org/x/crypto/sha3"
keccak "leb.io/hashland/keccakpg"
keccak "gx/ipfs/QmQPWTeQJnJE7MYu6dJTiNTQRNuqBr41dis6UgY6Uekmgd/keccakpg"
blake2b "gx/ipfs/QmaPHkZLbQQbvcyavn8q1GFHg6o6yeceyHFSJ3Pjf3p3TQ/go-crypto/blake2b"
blake2s "gx/ipfs/QmaPHkZLbQQbvcyavn8q1GFHg6o6yeceyHFSJ3Pjf3p3TQ/go-crypto/blake2s"
sha3 "gx/ipfs/QmaPHkZLbQQbvcyavn8q1GFHg6o6yeceyHFSJ3Pjf3p3TQ/go-crypto/sha3"
"gx/ipfs/QmfJHywXQu98UeZtGJBQrPAR6AtmDjjbe3qjTo9piXHPnx/murmur3"
skein "leb.io/hashland/skein" // could use https://github.com/wernerd/Skein3Fish/tree/master/go as well
)

// ErrSumNotSupported is returned when the Sum function code is not implemented
Expand Down Expand Up @@ -44,6 +45,13 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
out := blake2s.Sum256(data)
d = out[:]
default:

// var sum [32]byte
// var tmp [olen]byte
// checkSum(&sum, olen, data)
// copy(tmp[:], sum[:olen])
// d = tmp

return nil, fmt.Errorf("unsupported length for blake2s: %d", olen)
}
case isBlake2b(code):
Expand All @@ -59,8 +67,30 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
out := blake2b.Sum512(data)
d = out[:]
default:

// var sum [64]byte
// var tmp [olen]byte
// checkSum(&sum, olen, data)
// copy(tmp[:], sum[:olen])
// d = tmp

return nil, fmt.Errorf("unsupported length for blake2b: %d", olen)
}
case isSkein256(code):
olen := code - SKEIN256_MIN + 1
state, _ := skein.New(256, int(olen))
out := state.Sum(data)
d = out[:]
case isSkein512(code):
olen := code - SKEIN512_MIN + 1
state, _ := skein.New(512, int(olen))
out := state.Sum(data)
d = out[:]
case isSkein1024(code):
olen := code - SKEIN1024_MIN + 1
state, _ := skein.New(1024, int(olen))
out := state.Sum(data)
d = out[:]
default:
switch code {
case ID:
Expand Down Expand Up @@ -112,6 +142,18 @@ func isBlake2b(code uint64) bool {
return code >= BLAKE2B_MIN && code <= BLAKE2B_MAX
}

func isSkein256(code uint64) bool {
return code >= SKEIN256_MIN && code <= SKEIN256_MAX
}

func isSkein512(code uint64) bool {
return code >= SKEIN512_MIN && code <= SKEIN512_MAX
}

func isSkein1024(code uint64) bool {
return code >= SKEIN1024_MIN && code <= SKEIN1024_MAX
}

func sumID(data []byte) []byte {
return data
}
Expand Down
Loading