Skip to content

Commit

Permalink
bindings/go/*: resolve golint nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Sep 18, 2024
1 parent 38fc663 commit 8431c16
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 101 deletions.
8 changes: 4 additions & 4 deletions bindings/go/blst.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ func initMaxProcs() int {
return maxProcs
}

func SetMaxProcs(max int) {
if max <= 0 {
max = 1
func SetMaxProcs(procs int) {
if procs <= 0 {
procs = 1
}
maxProcs = max
maxProcs = procs
}

func numThreads(maxThreads int) int {
Expand Down
8 changes: 4 additions & 4 deletions bindings/go/blst.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ func initMaxProcs() int {
return maxProcs
}

func SetMaxProcs(max int) {
if max <= 0 {
max = 1
func SetMaxProcs(procs int) {
if procs <= 0 {
procs = 1
}
maxProcs = max
maxProcs = procs
}

func numThreads(maxThreads int) int {
Expand Down
57 changes: 37 additions & 20 deletions bindings/go/blst_htoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -35,30 +34,50 @@ func decodeP1(m map[string]interface{}) *P1Affine {
return &p1
}

func readAll(file *os.File) ([]byte, error) {
defer file.Close()

stat, err := file.Stat()
if err != nil {
return nil, err //nolint:wrapcheck
}

buf := make([]byte, stat.Size())
total := 0
for total < len(buf) {
read, err := file.Read(buf[total:])
if err != nil {
return nil, err //nolint:wrapcheck
}
total += read
}

return buf, nil
}

func jsonG1HashToCurve(t *testing.T, fname string) {
t.Helper()
vfile, err := os.Open(fname)
if err != nil {
t.Skipf("%.16s... not found", fname)
}
defer vfile.Close()
buf, err := ioutil.ReadAll(vfile)
buf, err := readAll(vfile)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

var vectors map[string]interface{}
err = json.Unmarshal(buf, &vectors)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

dst := []byte(vectors["dst"].(string))
hash_or_encode := vectors["randomOracle"].(bool)

vectorsArr, ok := vectors["vectors"].([]interface{})
if !ok {
t.Errorf("Could not cast vectors to an array")
t.Error("Could not cast vectors to an array")
}

for _, v := range vectorsArr {
Expand Down Expand Up @@ -125,24 +144,23 @@ func jsonG2HashToCurve(t *testing.T, fname string) {
if err != nil {
t.Skipf("%.16s... not found", fname)
}
defer vfile.Close()
buf, err := ioutil.ReadAll(vfile)
buf, err := readAll(vfile)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

var vectors map[string]interface{}
err = json.Unmarshal(buf, &vectors)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

dst := []byte(vectors["dst"].(string))
hash_or_encode := vectors["randomOracle"].(bool)

vectorsArr, ok := vectors["vectors"].([]interface{})
if !ok {
t.Errorf("Could not cast vectors to an array")
t.Error("Could not cast vectors to an array")
}

for _, v := range vectorsArr {
Expand Down Expand Up @@ -178,44 +196,43 @@ func jsonExpandMessageXmd(t *testing.T, fname string) {
if err != nil {
t.Skipf("%.16s... not found", fname)
}
defer vfile.Close()
buf, err := ioutil.ReadAll(vfile)
buf, err := readAll(vfile)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

var vectors map[string]interface{}
err = json.Unmarshal(buf, &vectors)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

DST := []byte(vectors["DST"].(string))

tests, ok := vectors["tests"].([]interface{})
if !ok {
t.Errorf("Could not cast 'tests' to an array")
t.Error("Could not cast 'tests' to an array")
}

for _, v := range tests {
test, ok := v.(map[string]interface{})
if !ok {
t.Errorf("Could not map 'tests[]' element")
t.Error("Could not map 'tests[]' element")
}

len_in_bytes, err := strconv.ParseInt(test["len_in_bytes"].(string), 0, 0)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}
msg := []byte(test["msg"].(string))
expected, err := hex.DecodeString(test["uniform_bytes"].(string))
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

hashed := expandMessageXmd(msg, DST, int(len_in_bytes))
if !bytes.Equal(hashed, expected) {
t.Errorf("hashed != expected")
t.Error("hashed != expected")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/go/blst_miller_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestMillerLoopN(t *testing.T) {
scalars := make([]byte, npoints*8)
_, err := rand.Read(scalars)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
return
}

Expand Down
Loading

0 comments on commit 8431c16

Please sign in to comment.