refactor: use BaoEncodedSize

This commit is contained in:
Derrick Hammer 2023-05-17 10:12:22 -04:00
parent aa702ffd02
commit ee33da755c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 4 additions and 15 deletions

View File

@ -7,12 +7,8 @@ import (
"lukechampine.com/blake3" "lukechampine.com/blake3"
) )
const (
chunkSize = 1024
)
func ComputeTree(reader io.Reader, size int64) ([]byte, [32]byte, error) { func ComputeTree(reader io.Reader, size int64) ([]byte, [32]byte, error) {
bufSize := baoOutboardSize(int(size)) bufSize := blake3.BaoEncodedSize(int(size), true)
buf := bufferAt{buf: make([]byte, bufSize)} buf := bufferAt{buf: make([]byte, bufSize)}
hash, err := blake3.BaoEncode(&buf, bufio.NewReader(reader), size, true) hash, err := blake3.BaoEncode(&buf, bufio.NewReader(reader), size, true)
@ -23,15 +19,6 @@ func ComputeTree(reader io.Reader, size int64) ([]byte, [32]byte, error) {
return buf.buf, hash, nil return buf.buf, hash, nil
} }
func baoOutboardSize(dataLen int) int {
if dataLen == 0 {
return 8
}
chunks := (dataLen + chunkSize - 1) / chunkSize
cvs := 2*chunks - 2 // no I will not elaborate
return 8 + cvs*32
}
type bufferAt struct { type bufferAt struct {
buf []byte buf []byte
} }

2
go.mod
View File

@ -30,7 +30,7 @@ require (
gorm.io/driver/mysql v1.5.0 gorm.io/driver/mysql v1.5.0
gorm.io/driver/sqlite v1.5.0 gorm.io/driver/sqlite v1.5.0
gorm.io/gorm v1.25.0 gorm.io/gorm v1.25.0
lukechampine.com/blake3 v1.2.0 lukechampine.com/blake3 v1.2.1
lukechampine.com/frand v1.4.2 lukechampine.com/frand v1.4.2
) )

2
go.sum
View File

@ -1953,6 +1953,8 @@ lukechampine.com/blake3 v1.1.8-0.20230515191509-85fa20df84c1 h1:NTVGxhcZ8DAwAZny
lukechampine.com/blake3 v1.1.8-0.20230515191509-85fa20df84c1/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= lukechampine.com/blake3 v1.1.8-0.20230515191509-85fa20df84c1/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA=
lukechampine.com/blake3 v1.2.0 h1:xBP4eLyBtfzoNTBaeECFxxoLyPX7hD1xntcqc1xywQE= lukechampine.com/blake3 v1.2.0 h1:xBP4eLyBtfzoNTBaeECFxxoLyPX7hD1xntcqc1xywQE=
lukechampine.com/blake3 v1.2.0/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k= lukechampine.com/blake3 v1.2.0/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k=
lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI=
lukechampine.com/blake3 v1.2.1/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k=
lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw= lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw=
lukechampine.com/frand v1.4.2/go.mod h1:4S/TM2ZgrKejMcKMbeLjISpJMO+/eZ1zu3vYX9dtj3s= lukechampine.com/frand v1.4.2/go.mod h1:4S/TM2ZgrKejMcKMbeLjISpJMO+/eZ1zu3vYX9dtj3s=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=