Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions bbs/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ var (
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
}
f2192Cache = make(map[*ml.Curve]*ml.Zr)
//nolint:gochecknoglobals
f2192Cache = func() map[*ml.Curve]*ml.Zr {
m := make(map[*ml.Curve]*ml.Zr, len(ml.Curves))
for _, c := range ml.Curves {
m[c] = c.NewZrFromBytes(f2192Bytes)
}

return m
}()
)

func f2192(curve *ml.Curve) *ml.Zr {
if cached, ok := f2192Cache[curve]; ok {
return cached
}
val := curve.NewZrFromBytes(f2192Bytes)
f2192Cache[curve] = val

return val
return curve.NewZrFromBytes(f2192Bytes)
}

func FrFromOKM(message []byte, curve *ml.Curve) *ml.Zr {
Expand Down
39 changes: 39 additions & 0 deletions bbs/fr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package bbs

import (
"sync"
"testing"

ml "github.com/IBM/mathlib"
"github.com/stretchr/testify/require"
)

func TestFrFromOKMConcurrent(t *testing.T) {
message := []byte("test message")

for i, curve := range ml.Curves {
const workers = 16
results := make([]*ml.Zr, workers)

var wg sync.WaitGroup
wg.Add(workers)
for w := range workers {
go func(w int) {
defer wg.Done()
results[w] = FrFromOKM(message, curve)
}(w)
}
wg.Wait()

expected := FrFromOKM(message, curve)
for w, r := range results {
require.True(t, expected.Equals(r), "curve %d, worker %d", i, w)
}
}
}
Loading