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

feat: Support generics. #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License

Copyright (c) 2021 Prysmatic Labs
# Copyright (c) 2021 Prysmatic Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,4 +25,4 @@ package gohashtree

// Export internal functions for testing.

var Sha256_1_generic = sha256_1_generic
var Sha256_1_generic = sha256_1_generic[[32]byte, [32]byte]
8 changes: 4 additions & 4 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func _hash(digests *byte, p [][32]byte, count uint32)

// Hash hashes the chunks two at the time and outputs the digests on the first
// argument. It does check for lengths on the inputs.
func Hash(digests [][32]byte, chunks [][32]byte) error {
func Hash[D, C ~[32]byte](digests []D, chunks []C) error {
if len(chunks) == 0 {
return nil
}
Expand All @@ -44,17 +44,17 @@ func Hash(digests [][32]byte, chunks [][32]byte) error {
return fmt.Errorf("not enough digest length, need at least %v, got %v", len(chunks)/2, len(digests))
}
if supportedCPU {
_hash(&digests[0][0], chunks, uint32(len(chunks)/2))
_hash(&digests[0][0], *(*[][32]byte)(unsafe.Pointer(&chunks)), uint32(len(chunks)/2))
} else {
sha256_1_generic(digests, chunks)
}
return nil
}

// HashChunks is the same as Hash, but does not do error checking on the lengths of the slices
func HashChunks(digests [][32]byte, chunks [][32]byte) {
func HashChunks[D, C ~[32]byte](digests []D, chunks []C) {
if supportedCPU {
_hash(&digests[0][0], chunks, uint32(len(chunks)/2))
_hash(&digests[0][0], *(*[][32]byte)(unsafe.Pointer(&chunks[0])), uint32(len(chunks)/2))
} else {
sha256_1_generic(digests, chunks)
}
Expand Down
6 changes: 3 additions & 3 deletions sha256_1_generic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License

Copyright (c) 2021-2022 Prysmatic Labs
# Copyright (c) 2021-2022 Prysmatic Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -125,7 +125,7 @@ var _K = []uint32{
0xc67178f2,
}

func sha256_1_generic(digests [][32]byte, p [][32]byte) {
func sha256_1_generic[D, P ~[32]byte](digests []D, p []P) {
var w [16]uint32
for k := 0; k < len(p)/2; k++ {
// First 16 rounds
Expand Down Expand Up @@ -217,7 +217,7 @@ func sha256_1_generic(digests [][32]byte, p [][32]byte) {
h6 += g
h7 += h

var dig [32]byte
var dig D
binary.BigEndian.PutUint32(dig[0:4], h0)
binary.BigEndian.PutUint32(dig[4:8], h1)
binary.BigEndian.PutUint32(dig[8:12], h2)
Expand Down
Loading