Skip to content

Commit

Permalink
[FEAT] better init process for multithreaded environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Scratch-net committed Jan 15, 2025
1 parent f9f60f0 commit 6dbf451
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
48 changes: 42 additions & 6 deletions gnark/libraries/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"math"
"math/big"
"os"
"sync"
"testing"

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

func TestInit(t *testing.T) {
assert := test.NewAssert(t)
assert.True(prover.InitAlgorithm(prover.CHACHA20, chachaKey, chachaR1CS))
assert.True(prover.InitAlgorithm(prover.AES_128, aes128Key, aes128r1cs))
assert.True(prover.InitAlgorithm(prover.AES_256, aes256Key, aes256r1cs))
assert.True(prover.InitAlgorithm(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs))
assert.True(prover.InitAlgorithm(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs))
assert.True(prover.InitAlgorithm(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs))

wg1 := &sync.WaitGroup{}
wg1.Add(1)

wg2 := &sync.WaitGroup{}
wg2.Add(24)

f := func(algorithmID uint8, provingKey []byte, r1csData []byte) {
go func() {
wg1.Wait()
assert.True(prover.InitAlgorithm(algorithmID, provingKey, r1csData))
wg2.Done()
}()
}

f(prover.CHACHA20, chachaKey, chachaR1CS)
f(prover.AES_128, aes128Key, aes128r1cs)
f(prover.AES_256, aes256Key, aes256r1cs)
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
f(prover.CHACHA20, chachaKey, chachaR1CS)
f(prover.AES_128, aes128Key, aes128r1cs)
f(prover.AES_256, aes256Key, aes256r1cs)
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
f(prover.CHACHA20, chachaKey, chachaR1CS)
f(prover.AES_128, aes128Key, aes128r1cs)
f(prover.AES_256, aes256Key, aes256r1cs)
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
f(prover.CHACHA20, chachaKey, chachaR1CS)
f(prover.AES_128, aes128Key, aes128r1cs)
f(prover.AES_256, aes256Key, aes256r1cs)
f(prover.CHACHA20_OPRF, chachaOprfKey, chachaOprfr1cs)
f(prover.AES_128_OPRF, aes128OprfKey, aes128Oprfr1cs)
f(prover.AES_256_OPRF, aes256OprfKey, aes256Oprfr1cs)
wg1.Done()
wg2.Wait()
}

func TestPanic(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions gnark/libraries/prover/impl/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"sync"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
Expand Down Expand Up @@ -76,6 +77,7 @@ type ProverParams struct {
KeyHash string
CircuitHash string
initDone bool
initLock sync.Mutex
}

func init() {
Expand All @@ -92,7 +94,10 @@ func InitAlgorithm(algorithmID uint8, provingKey []byte, r1csData []byte) (res b
}()
if alg, ok := algorithmNames[algorithmID]; ok {
proverParams := provers[alg]
proverParams.initLock.Lock()
defer proverParams.initLock.Unlock()
if proverParams.initDone {
fmt.Println("Algorithm already initialized", alg)
return true
}

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

proverParams.SetParams(r1cs, pkey)
proverParams.initDone = true
fmt.Println("Initialized", alg)
return true
}
return false
Expand Down

0 comments on commit 6dbf451

Please sign in to comment.