Skip to content

Commit 6dbf451

Browse files
author
Alex E
committed
[FEAT] better init process for multithreaded environment
1 parent f9f60f0 commit 6dbf451

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

gnark/libraries/core_test.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"math"
1616
"math/big"
1717
"os"
18+
"sync"
1819
"testing"
1920

2021
"github.com/consensys/gnark-crypto/ecc/bn254/twistededwards"
@@ -45,12 +46,47 @@ func init() {
4546

4647
func TestInit(t *testing.T) {
4748
assert := test.NewAssert(t)
48-
assert.True(prover.InitAlgorithm(prover.CHACHA20, chachaKey, chachaR1CS))
49-
assert.True(prover.InitAlgorithm(prover.AES_128, aes128Key, aes128r1cs))
50-
assert.True(prover.InitAlgorithm(prover.AES_256, aes256Key, aes256r1cs))
51-
assert.True(prover.InitAlgorithm(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs))
52-
assert.True(prover.InitAlgorithm(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs))
53-
assert.True(prover.InitAlgorithm(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs))
49+
50+
wg1 := &sync.WaitGroup{}
51+
wg1.Add(1)
52+
53+
wg2 := &sync.WaitGroup{}
54+
wg2.Add(24)
55+
56+
f := func(algorithmID uint8, provingKey []byte, r1csData []byte) {
57+
go func() {
58+
wg1.Wait()
59+
assert.True(prover.InitAlgorithm(algorithmID, provingKey, r1csData))
60+
wg2.Done()
61+
}()
62+
}
63+
64+
f(prover.CHACHA20, chachaKey, chachaR1CS)
65+
f(prover.AES_128, aes128Key, aes128r1cs)
66+
f(prover.AES_256, aes256Key, aes256r1cs)
67+
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
68+
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
69+
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
70+
f(prover.CHACHA20, chachaKey, chachaR1CS)
71+
f(prover.AES_128, aes128Key, aes128r1cs)
72+
f(prover.AES_256, aes256Key, aes256r1cs)
73+
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
74+
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
75+
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
76+
f(prover.CHACHA20, chachaKey, chachaR1CS)
77+
f(prover.AES_128, aes128Key, aes128r1cs)
78+
f(prover.AES_256, aes256Key, aes256r1cs)
79+
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
80+
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
81+
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
82+
f(prover.CHACHA20, chachaKey, chachaR1CS)
83+
f(prover.AES_128, aes128Key, aes128r1cs)
84+
f(prover.AES_256, aes256Key, aes256r1cs)
85+
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
86+
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
87+
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
88+
wg1.Done()
89+
wg2.Wait()
5490
}
5591

5692
func TestPanic(t *testing.T) {

gnark/libraries/prover/impl/library.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"encoding/json"
99
"fmt"
10+
"sync"
1011

1112
"github.com/consensys/gnark-crypto/ecc"
1213
"github.com/consensys/gnark/backend/groth16"
@@ -76,6 +77,7 @@ type ProverParams struct {
7677
KeyHash string
7778
CircuitHash string
7879
initDone bool
80+
initLock sync.Mutex
7981
}
8082

8183
func init() {
@@ -92,7 +94,10 @@ func InitAlgorithm(algorithmID uint8, provingKey []byte, r1csData []byte) (res b
9294
}()
9395
if alg, ok := algorithmNames[algorithmID]; ok {
9496
proverParams := provers[alg]
97+
proverParams.initLock.Lock()
98+
defer proverParams.initLock.Unlock()
9599
if proverParams.initDone {
100+
fmt.Println("Algorithm already initialized", alg)
96101
return true
97102
}
98103

@@ -129,6 +134,7 @@ func InitAlgorithm(algorithmID uint8, provingKey []byte, r1csData []byte) (res b
129134

130135
proverParams.SetParams(r1cs, pkey)
131136
proverParams.initDone = true
137+
fmt.Println("Initialized", alg)
132138
return true
133139
}
134140
return false

0 commit comments

Comments
 (0)