Skip to content

Commit

Permalink
checksum: Provide more clear name to Tillich-Zémor hash
Browse files Browse the repository at this point in the history
Unlike SHA-256, this hash function is not so widely known.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Jul 9, 2024
1 parent d288541 commit 101f214
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
20 changes: 12 additions & 8 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ type Checksum refs.Checksum
type Type uint32

const (
Unknown Type = iota // Deprecated: use 0 instead.
SHA256 // SHA-256 hash
TZ // Tillich-Zémor homomorphic hash
Unknown Type = iota // Deprecated: use 0 instead.
SHA256 // SHA-256 hash
TillichZemor // Tillich-Zémor homomorphic hash
)

// TZ is a type for Tillich-Zémor homomorphic hash.
// Deprecated: use TillichZemor instead.
const TZ = TillichZemor

func typeToProto(t Type) refs.ChecksumType {
switch t {
default:
return refs.ChecksumType(t)
case SHA256:
return refs.SHA256
case TZ:
case TillichZemor:
return refs.TillichZemor
}
}
Expand All @@ -60,7 +64,7 @@ func NewSHA256(h [sha256.Size]byte) Checksum {

// NewTillichZemor constructs new Checksum from Tillich-Zémor homomorphic hash.
func NewTillichZemor(h [tz.Size]byte) Checksum {
return New(TZ, h[:])
return New(TillichZemor, h[:])
}

// NewFromHash constructs new Checksum of specified type from accumulated
Expand All @@ -78,7 +82,7 @@ func NewFromData(typ Type, data []byte) (Checksum, error) {
return Checksum{}, fmt.Errorf("unsupported checksum type %d", typ)
case SHA256:
return NewSHA256(sha256.Sum256(data)), nil
case TZ:
case TillichZemor:
return NewTillichZemor(tz.Sum(data)), nil
}
}
Expand Down Expand Up @@ -116,7 +120,7 @@ func (c Checksum) Type() Type {
case refs.SHA256:
return SHA256
case refs.TillichZemor:
return TZ
return TillichZemor
default:
return Type(typ)
}
Expand Down Expand Up @@ -147,7 +151,7 @@ func (c *Checksum) SetSHA256(v [sha256.Size]byte) { *c = NewSHA256(v) }
//
// Does nothing if the passed type is not one of the:
// - SHA256;
// - TZ.
// - TillichZemor.
//
// Does not mutate the passed value.
//
Expand Down
22 changes: 11 additions & 11 deletions checksum/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestType_String(t *testing.T) {
}{
{0, "CHECKSUM_TYPE_UNSPECIFIED"},
{checksum.SHA256, "SHA256"},
{checksum.TZ, "TZ"},
{checksum.TillichZemor, "TZ"},
{3, "3"},
} {
require.Equal(t, tc.exp, tc.typ.String())
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestNew(t *testing.T) {
switch actual := m.GetType(); typ {
default:
require.EqualValues(t, typ, actual)
case checksum.TZ:
case checksum.TillichZemor:
require.Equal(t, refs.TillichZemor, actual)
case checksum.SHA256:
require.Equal(t, refs.SHA256, actual)
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestNewSHA256(t *testing.T) {
}

func TestNewTZ(t *testing.T) {
testTypeConstructor(t, checksum.TZ, refs.TillichZemor, checksum.NewTillichZemor)
testTypeConstructor(t, checksum.TillichZemor, refs.TillichZemor, checksum.NewTillichZemor)
}

func TestNewFromHash(t *testing.T) {
Expand All @@ -173,7 +173,7 @@ func TestNewFromHash(t *testing.T) {
switch actual := m.GetType(); typ {
default:
require.EqualValues(t, typ, actual)
case checksum.TZ:
case checksum.TillichZemor:
require.Equal(t, refs.TillichZemor, actual)
case checksum.SHA256:
require.Equal(t, refs.SHA256, actual)
Expand All @@ -192,9 +192,9 @@ func TestNewFromData(t *testing.T) {
var cs checksum.Checksum
checksum.Calculate(&cs, 0, nil)
require.Zero(t, cs)
_, err = checksum.NewFromData(checksum.TZ+1, nil)
_, err = checksum.NewFromData(checksum.TillichZemor+1, nil)
require.EqualError(t, err, "unsupported checksum type 3")
checksum.Calculate(&cs, checksum.TZ+1, nil)
checksum.Calculate(&cs, checksum.TillichZemor+1, nil)
require.Zero(t, cs)

payload := []byte("Hello, world!")
Expand All @@ -215,15 +215,15 @@ func TestNewFromData(t *testing.T) {
})

t.Run("TillichZemor", func(t *testing.T) {
c, err := checksum.NewFromData(checksum.TZ, payload)
c, err := checksum.NewFromData(checksum.TillichZemor, payload)
require.NoError(t, err)
require.Equal(t, hTZ[:], c.Value())
require.Equal(t, checksum.TZ, c.Type())
require.Equal(t, checksum.TillichZemor, c.Type())

c = checksum.Checksum{}
checksum.Calculate(&c, checksum.TZ, payload)
checksum.Calculate(&c, checksum.TillichZemor, payload)
require.Equal(t, hTZ[:], c.Value())
require.Equal(t, checksum.TZ, c.Type())
require.Equal(t, checksum.TillichZemor, c.Type())
})
}

Expand All @@ -232,5 +232,5 @@ func TestChecksum_SetSHA256(t *testing.T) {
}

func TestChecksum_SetTillichZemor(t *testing.T) {
testTypeConstructor(t, checksum.TZ, refs.TillichZemor, func(b [tz.Size]byte) (c checksum.Checksum) { c.SetTillichZemor(b); return })
testTypeConstructor(t, checksum.TillichZemor, refs.TillichZemor, func(b [tz.Size]byte) (c checksum.Checksum) { c.SetTillichZemor(b); return })
}
4 changes: 2 additions & 2 deletions checksum/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExampleNewFromHash() {
newHash func() hash.Hash
}{
{SHA256, sha256.New},
{TZ, tz.New},
{TillichZemor, tz.New},
} {
h := tc.newHash()
h.Write(data)
Expand All @@ -40,7 +40,7 @@ func ExampleNewFromData() {
data := []byte("Hello, world!")
for _, typ := range []Type{
SHA256,
TZ,
TillichZemor,
} {
cs, err := NewFromData(typ, data)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion checksum/test/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/checksum"
)

var allSupportedTypes = []checksum.Type{checksum.SHA256, checksum.TZ}
var allSupportedTypes = []checksum.Type{checksum.SHA256, checksum.TillichZemor}

// Checksum returns random checksum.Checksum.
func Checksum() checksum.Checksum {
Expand Down
2 changes: 1 addition & 1 deletion object/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func flushObjectMetadata(signer neofscrypto.Signer, meta dynamicObjectMetadata,
header.SetPayloadChecksum(checksum.NewFromHash(checksum.SHA256, meta.checksum))

if meta.homomorphicChecksum != nil {
header.SetPayloadHomomorphicHash(checksum.NewFromHash(checksum.TZ, meta.homomorphicChecksum))
header.SetPayloadHomomorphicHash(checksum.NewFromHash(checksum.TillichZemor, meta.homomorphicChecksum))
}

header.SetPayloadSize(meta.length)
Expand Down

0 comments on commit 101f214

Please sign in to comment.