-
Notifications
You must be signed in to change notification settings - Fork 1
Avoid reversing the slice #1
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
base: compatibility
Are you sure you want to change the base?
Avoid reversing the slice #1
Conversation
Also enforce naming conventions and fix typos
|
I apparently wasn't subscribed to this repo, so I didn't see your PR. I don't really feel qualified to review this, my knowledge of Go is not limited, it's zero. So whenever @nogoegst get's around reviewing the original PR, he can take a look here too. Thanks for looking into it though! |
|
It is all good, I was learning about balloon hashing and came across your PR. After looking at the password-hashes crate, I'm jealous of Rust devs. I really hope someone can create a Go library with a collection of modern hashing algs. |
| // Int.SetBits. We can unsafely convert otherBytes directly | ||
| // into a big.Word slice to bypass the need to reverse | ||
| // otherBytes. | ||
| header := *(*reflect.SliceHeader)(unsafe.Pointer(&otherBytes)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should not be reflect and unsafe in crypto code. Also, it's hard to read (and to test).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did manage to convert it without unsafe in the original PR, not sure if it can be improved any further though.
otherBytes := h.Sum(nil)
otherWords := make([]big.Word, h.Size() / (bits.UintSize / 8))
for byte, word := 0, 0; byte < len(otherBytes); byte, word = byte+bits.UintSize/8, word+1 {
if bits.UintSize == 64 {
otherWords[word] = big.Word(binary.LittleEndian.Uint64(otherBytes[byte : byte+bits.UintSize/8]))
} else if bits.UintSize == 32 {
otherWords[word] = big.Word(binary.LittleEndian.Uint32(otherBytes[byte : byte+bits.UintSize/8]))
} else {
panic("balloon: unsupported architecture")
}
}
otherInt.SetBits(otherWords)
This PR aims to remove the need to reverse the bytes slice when using math/big. I also made some of the variable/type naming more consistent (Go uses camel-case) and fixed a few typos.
I saw that there was an issue with math/big using primarily big-endian in its package. Oddly, I came across this function https://pkg.go.dev/math/big#Int.SetBits that uses little-endian.
It's possible (in a rather hacky way) to use the unsafe package to type cast the bytes slice into a big.Word slice and use SetBits without the need to reverse a slice.
Here are the given benchmark tests, showing a slight improvement.
With reverse
Without reverse
I think more can be squeezed out of this hashing implementation, but it'll need more looking into.