Bump dependencies, allow preallocated encode

This commit is contained in:
DataHoarder 2023-07-22 23:51:22 +02:00
parent 7b24ed2d11
commit 18ecc51ae6
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
4 changed files with 26 additions and 17 deletions

View file

@ -13,12 +13,12 @@ type Address struct {
func (a *Address) Base58() (result string) {
prefix := []byte{byte(a.network)}
checksum := GetChecksum(prefix, a.spendingKey, a.viewingKey)
result = EncodeMoneroBase58(prefix, a.spendingKey, a.viewingKey, checksum[:])
result = string(EncodeMoneroBase58(prefix, a.spendingKey, a.viewingKey, checksum[:]))
return
}
func NewAddress(address string) (result *Address, err string) {
raw := DecodeMoneroBase58(address)
raw := DecodeMoneroBase58([]byte(address))
if len(raw) != 69 {
err = "Address is the wrong length"
return

View file

@ -59,7 +59,7 @@ func encodeChunkTail(raw []byte, buf []byte) []byte {
return buf
}
func decodeChunk(buf []byte, encoded string) []byte {
func decodeChunk(buf []byte, encoded []byte) []byte {
var intResult uint64
currentMultiplier := uint64(1)
for i := len(encoded) - 1; i >= 0; i-- {
@ -94,32 +94,41 @@ func decodeChunk(buf []byte, encoded string) []byte {
return nil
}
func EncodeMoneroBase58(data ...[]byte) string {
func EncodeMoneroBase58(data ...[]byte) []byte {
return EncodeMoneroBase58PreAllocated(make([]byte, 0, func() (result int) {
for _, v := range data {
result += len(v)
}
return
}()), data...)
}
func EncodeMoneroBase58PreAllocated(buf []byte, data ...[]byte) []byte {
//preallocate common case
combined := make([]byte, 0, 96)
for _, item := range data {
combined = append(combined, item...)
}
result := make([]byte, 0, len(combined)*2)
buf := make([]byte, 0, len(combined)*2)
result := buf
tmpBuf := make([]byte, 0, len(combined)*2)
length := len(combined)
rounds := length / 8
for i := 0; i < rounds; i++ {
result = append(result, encodeChunk(combined[i*8:(i+1)*8], buf[:0])...)
result = append(result, encodeChunk(combined[i*8:(i+1)*8], tmpBuf[:0])...)
}
if length%8 > 0 {
result = append(result, encodeChunkTail(combined[rounds*8:], buf[:0])...)
result = append(result, encodeChunkTail(combined[rounds*8:], tmpBuf[:0])...)
}
return string(result)
return result
}
func DecodeMoneroBase58(data string) (result []byte) {
func DecodeMoneroBase58(data []byte) (result []byte) {
//common case
return DecodeMoneroBase58PreAllocated(make([]byte, 0, 69), data)
}
func DecodeMoneroBase58PreAllocated(buf []byte, data string) (result []byte) {
func DecodeMoneroBase58PreAllocated(buf []byte, data []byte) (result []byte) {
result = buf
length := len(data)
rounds := length / 11

4
go.mod
View file

@ -2,6 +2,6 @@ module git.gammaspectra.live/P2Pool/moneroutil
go 1.19
require golang.org/x/crypto v0.9.0
require golang.org/x/crypto v0.11.0
require golang.org/x/sys v0.8.0 // indirect
require golang.org/x/sys v0.10.0 // indirect

8
go.sum
View file

@ -1,4 +1,4 @@
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=