From 18ecc51ae61e777d1f855aa4351541b664e21734 Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:51:22 +0200 Subject: [PATCH] Bump dependencies, allow preallocated encode --- address.go | 4 ++-- base58.go | 27 ++++++++++++++++++--------- go.mod | 4 ++-- go.sum | 8 ++++---- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/address.go b/address.go index 87a39f0..d6712f8 100644 --- a/address.go +++ b/address.go @@ -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 diff --git a/base58.go b/base58.go index b774ffb..f69db28 100644 --- a/base58.go +++ b/base58.go @@ -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 diff --git a/go.mod b/go.mod index 134e738..7846b7b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 883a5a2..7f80147 100644 --- a/go.sum +++ b/go.sum @@ -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=