Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Apr 3, 2024
1 parent 4c3f9f0 commit 247f9ee
Show file tree
Hide file tree
Showing 15 changed files with 894 additions and 652 deletions.
10 changes: 5 additions & 5 deletions blake256/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func uint32sToBytes(w []uint32) []byte {
return dst
}

func ror(x uint32, n int) uint32 {
func rotr(x uint32, n int) uint32 {
return bits.RotateLeft32(x, 32 - n)
}

func G(v *[16]uint32, m []uint32, i int, a, b, c, d, e int) {
v[a] += (m[sigma[i][e]] ^ u256[sigma[i][e+1]]) + v[b]
v[d] = ror(v[d] ^ v[a], 16)
v[d] = rotr(v[d] ^ v[a], 16)
v[c] += v[d]
v[b] = ror(v[b] ^ v[c], 12)
v[b] = rotr(v[b] ^ v[c], 12)
v[a] += (m[sigma[i][e+1]] ^ u256[sigma[i][e]])+v[b]
v[d] = ror(v[d] ^ v[a], 8)
v[d] = rotr(v[d] ^ v[a], 8)
v[c] += v[d]
v[b] = ror(v[b] ^ v[c], 7)
v[b] = rotr(v[b] ^ v[c], 7)
}
127 changes: 125 additions & 2 deletions cubehash/cubehash.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,96 @@
package cubehash

import (
"hash"
)

// NewCubehash returns a new hash.Hash.
func NewCubehash(hashSize, blockSize, r, ir, fr int) hash.Hash {
return NewDigest(hashSize, blockSize, r, ir, fr)
}

// New returns a new hash.Hash.
func New() hash.Hash {
return NewCubehash(512, 32, 16, 16, 32)
}

// New512x returns a new hash.Hash.
func New512x() hash.Hash {
return NewCubehash(512, 1, 16, 16, 32)
}

// New384 returns a new hash.Hash.
func New384() hash.Hash {
return NewCubehash(384, 32, 16, 16, 32)
}

// New256 returns a new hash.Hash.
func New256() hash.Hash {
return NewCubehash(256, 32, 16, 16, 32)
}

// New224 returns a new hash.Hash.
func New224() hash.Hash {
return NewCubehash(224, 32, 16, 16, 32)
}

// New192 returns a new hash.Hash.
func New192() hash.Hash {
return NewCubehash(192, 32, 16, 16, 32)
}

// New160 returns a new hash.Hash.
func New160() hash.Hash {
return NewCubehash(160, 32, 16, 16, 32)
}

// New128 returns a new hash.Hash.
func New128() hash.Hash {
return NewCubehash(128, 32, 16, 16, 32)
}

// =======

// NewSH512 returns a new hash.Hash.
func NewSH512() hash.Hash {
return NewCubehash(512, 32, 16, 160, 160)
}

// NewSH256 returns a new hash.Hash.
func NewSH256() hash.Hash {
return NewCubehash(256, 32, 16, 160, 160)
}

// NewSH224 returns a new hash.Hash.
func NewSH224() hash.Hash {
return NewCubehash(224, 32, 16, 160, 160)
}

// NewSH192 returns a new hash.Hash.
func NewSH192() hash.Hash {
return NewCubehash(192, 32, 16, 160, 160)
}

// =======

// Sum returns the cubehash checksum of the data.
// Sum as Sum512
func Sum(data []byte) (out [Size]byte) {
h := New()
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum[:Size])
copy(out[:], sum)
return
}

// Sum384 returns the cubehash checksum of the data.
func Sum384(data []byte) (out [Size384]byte) {
h := New384()
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum)
return
}

Expand All @@ -17,6 +100,46 @@ func Sum256(data []byte) (out [Size256]byte) {
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum[:Size256])
copy(out[:], sum)
return
}

// Sum224 returns the cubehash checksum of the data.
func Sum224(data []byte) (out [Size224]byte) {
h := New224()
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum)
return
}

// Sum192 returns the cubehash checksum of the data.
func Sum192(data []byte) (out [Size192]byte) {
h := New192()
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum)
return
}

// Sum160 returns the cubehash checksum of the data.
func Sum160(data []byte) (out [Size160]byte) {
h := New160()
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum)
return
}

// Sum128 returns the cubehash checksum of the data.
func Sum128(data []byte) (out [Size128]byte) {
h := New128()
h.Write(data)
sum := h.Sum(nil)

copy(out[:], sum)
return
}
103 changes: 82 additions & 21 deletions cubehash/cubehash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ func Test_Interfaces(t *testing.T) {
var _ hash.Hash = (*digest)(nil)
var _ encoding.BinaryMarshaler = (*digest)(nil)
var _ encoding.BinaryUnmarshaler = (*digest)(nil)

// digest256
var _ hash.Hash = (*digest256)(nil)
var _ encoding.BinaryMarshaler = (*digest256)(nil)
var _ encoding.BinaryUnmarshaler = (*digest256)(nil)
}

func Test_Sum(t *testing.T) {
Expand Down Expand Up @@ -100,31 +95,97 @@ func Test_Marshal(t *testing.T) {
}
}

func Test_Sum256(t *testing.T) {
msg := "78AECC1F4DBF27AC146780EEA8DCC56B"
check := "df8c13ad710ba02a0a293b94e144d3b212bbf37cbf51c17e0716f65126a23621"
func Test_EmptyMessage(t *testing.T) {
msg := ""

{
check := "44c6de3ac6c73c391bf0906cb7482600ec06b216c7c54a2a8688a6a42676577d"

dst := Sum256([]byte(msg))
c := NewCubehash(256, 32, 16, 160, 160)
c.Reset()
c.Write([]byte(msg))
dst := c.Sum(nil)

if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
}
}

{
check := "4a1d00bbcfcb5a9562fb981e7f7db3350fe2658639d948b9d57452c22328bb32f468b072208450bad5ee178271408be0b16e5633ac8a1e3cf9864cfbfc8e043a"

c := NewCubehash(512, 32, 16, 160, 160)
c.Reset()
c.Write([]byte(msg))
dst := c.Sum(nil)

if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
}
}

}

func Test_Marshal256(t *testing.T) {
a := New256()
a.Write([]byte{1, 2, 3})
save, _ := a.(encoding.BinaryMarshaler).MarshalBinary()
func Test_ShortMessage(t *testing.T) {
msg := "Hello"

b := New256()
b.(encoding.BinaryUnmarshaler).UnmarshalBinary(save)
{
check := "e712139e3b892f2f5fe52d0f30d78a0cb16b51b217da0e4acb103dd0856f2db0"

asum := a.Sum(nil)
bsum := b.Sum(nil)
if !bytes.Equal(asum, bsum) {
t.Errorf("UnmarshalBinary(...), got %x, want %x", bsum, asum)
c := NewCubehash(256, 32, 16, 160, 160)
c.Reset()
c.Write([]byte(msg))
dst := c.Sum(nil)

if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
}
}

{
check := "dcc0503aae279a3c8c95fa1181d37c418783204e2e3048a081392fd61bace883a1f7c4c96b16b4060c42104f1ce45a622f1a9abaeb994beb107fed53a78f588c"

c := NewCubehash(512, 32, 16, 160, 160)
c.Reset()
c.Write([]byte(msg))
dst := c.Sum(nil)

if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
}
}

}

func Test_LongerMessage(t *testing.T) {
msg := "The quick brown fox jumps over the lazy dog"

{
check := "5151e251e348cbbfee46538651c06b138b10eeb71cf6ea6054d7ca5fec82eb79"

c := NewCubehash(256, 32, 16, 160, 160)
c.Reset()
c.Write([]byte(msg))
dst := c.Sum(nil)

if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
}
}

{
check := "bdba44a28cd16b774bdf3c9511def1a2baf39d4ef98b92c27cf5e37beb8990b7cdb6575dae1a548330780810618b8a5c351c1368904db7ebdf8857d596083a86"

c := NewCubehash(512, 32, 16, 160, 160)
c.Reset()
c.Write([]byte(msg))
dst := c.Sum(nil)

if fmt.Sprintf("%x", dst) != check {
t.Errorf("fail, got %x, want %s", dst, check)
}
}

}

func BenchmarkSum(b *testing.B) {
Expand Down
Loading

0 comments on commit 247f9ee

Please sign in to comment.