opt for constrained errgroups to reduce parallelization factor so memory isn't overspent

This commit is contained in:
Cassandra Heart 2024-01-03 02:45:58 -06:00
parent d02ef18174
commit c5c36c4491
No known key found for this signature in database
GPG Key ID: 6352152859385958

View File

@ -8,10 +8,12 @@ import (
"hash" "hash"
"math/big" "math/big"
"os" "os"
"runtime"
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"golang.org/x/sync/errgroup"
"source.quilibrium.com/quilibrium/monorepo/nekryptology/pkg/core/curves" "source.quilibrium.com/quilibrium/monorepo/nekryptology/pkg/core/curves"
"source.quilibrium.com/quilibrium/monorepo/nekryptology/pkg/core/curves/native/bls48581" "source.quilibrium.com/quilibrium/monorepo/nekryptology/pkg/core/curves/native/bls48581"
) )
@ -307,12 +309,12 @@ func Init() {
g1s := make([]curves.PairingPoint, 65536) g1s := make([]curves.PairingPoint, 65536)
g2s := make([]curves.PairingPoint, 257) g2s := make([]curves.PairingPoint, 257)
g1ffts := make([]curves.PairingPoint, 65536) g1ffts := make([]curves.PairingPoint, 65536)
wg := sync.WaitGroup{} wg := errgroup.Group{}
wg.Add(65536) wg.SetLimit(runtime.NumCPU())
for i := 0; i < 65536; i++ { for i := 0; i < 65536; i++ {
i := i i := i
go func() { wg.Go(func() error {
b, err := hex.DecodeString(cs.PowersOfTau.G1Affines[i][2:]) b, err := hex.DecodeString(cs.PowersOfTau.G1Affines[i][2:])
if err != nil { if err != nil {
panic(err) panic(err)
@ -346,17 +348,14 @@ func Init() {
} }
g2s[i] = g2.(curves.PairingPoint) g2s[i] = g2.(curves.PairingPoint)
} }
wg.Done() return nil
}() })
} }
wg.Wait()
wg.Add(len(cs.Witness.RunningProducts))
CeremonyRunningProducts = make([]curves.PairingPoint, len(cs.Witness.RunningProducts)) CeremonyRunningProducts = make([]curves.PairingPoint, len(cs.Witness.RunningProducts))
for i, s := range cs.Witness.RunningProducts { for i, s := range cs.Witness.RunningProducts {
i, s := i, s i, s := i, s
go func() { wg.Go(func() error {
b, err := hex.DecodeString(s[2:]) b, err := hex.DecodeString(s[2:])
if err != nil { if err != nil {
panic(err) panic(err)
@ -367,16 +366,14 @@ func Init() {
panic(err) panic(err)
} }
CeremonyRunningProducts[i] = g1.(curves.PairingPoint) CeremonyRunningProducts[i] = g1.(curves.PairingPoint)
wg.Done() return nil
}() })
} }
wg.Wait()
wg.Add(len(cs.Witness.PotPubKeys))
CeremonyPotPubKeys = make([]curves.PairingPoint, len(cs.Witness.PotPubKeys)) CeremonyPotPubKeys = make([]curves.PairingPoint, len(cs.Witness.PotPubKeys))
for i, s := range cs.Witness.PotPubKeys { for i, s := range cs.Witness.PotPubKeys {
i, s := i, s i, s := i, s
go func() { wg.Go(func() error {
b, err := hex.DecodeString(s[2:]) b, err := hex.DecodeString(s[2:])
if err != nil { if err != nil {
panic(err) panic(err)
@ -387,16 +384,14 @@ func Init() {
panic(err) panic(err)
} }
CeremonyPotPubKeys[i] = g2.(curves.PairingPoint) CeremonyPotPubKeys[i] = g2.(curves.PairingPoint)
wg.Done() return nil
}() })
} }
wg.Wait()
wg.Add(len(cs.VoucherPubKeys))
CeremonySignatories = make([]curves.Point, len(cs.VoucherPubKeys)) CeremonySignatories = make([]curves.Point, len(cs.VoucherPubKeys))
for i, s := range cs.VoucherPubKeys { for i, s := range cs.VoucherPubKeys {
i, s := i, s i, s := i, s
go func() { wg.Go(func() error {
b, err := hex.DecodeString(s[2:]) b, err := hex.DecodeString(s[2:])
if err != nil { if err != nil {
panic(err) panic(err)
@ -406,8 +401,8 @@ func Init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
wg.Done() return nil
}() })
} }
wg.Wait() wg.Wait()
@ -421,7 +416,8 @@ func Init() {
q := new(big.Int).SetBytes(modulus) q := new(big.Int).SetBytes(modulus)
sizes := []int64{16, 128, 1024, 65536} sizes := []int64{16, 128, 1024, 65536}
wg.Add(len(sizes)) wg = errgroup.Group{}
wg.SetLimit(runtime.NumCPU())
root := make([]curves.PairingScalar, 4) root := make([]curves.PairingScalar, 4)
roots := make([][]curves.PairingScalar, 4) roots := make([][]curves.PairingScalar, 4)
reverseRoots := make([][]curves.PairingScalar, 4) reverseRoots := make([][]curves.PairingScalar, 4)
@ -430,7 +426,7 @@ func Init() {
for idx, i := range sizes { for idx, i := range sizes {
i := i i := i
idx := idx idx := idx
go func() { wg.Go(func() error {
exp := new(big.Int).Quo( exp := new(big.Int).Quo(
new(big.Int).Sub(q, big.NewInt(1)), new(big.Int).Sub(q, big.NewInt(1)),
big.NewInt(i), big.NewInt(i),
@ -463,19 +459,20 @@ func Init() {
wg2.Wait() wg2.Wait()
roots[idx][i] = roots[idx][0] roots[idx][i] = roots[idx][0]
reverseRoots[idx][0] = reverseRoots[idx][i] reverseRoots[idx][0] = reverseRoots[idx][i]
wg.Done() return nil
}() })
} }
wg.Wait() wg.Wait()
wg.Add(len(sizes)) wg = errgroup.Group{}
wg.SetLimit(runtime.NumCPU())
for i := range root { for i := range root {
i := i i := i
RootOfUnityBLS48581[uint64(sizes[i])] = root[i] RootOfUnityBLS48581[uint64(sizes[i])] = root[i]
RootsOfUnityBLS48581[uint64(sizes[i])] = roots[i] RootsOfUnityBLS48581[uint64(sizes[i])] = roots[i]
ReverseRootsOfUnityBLS48581[uint64(sizes[i])] = reverseRoots[i] ReverseRootsOfUnityBLS48581[uint64(sizes[i])] = reverseRoots[i]
go func() { wg.Go(func() error {
// We precomputed 65536, others are cheap and will be fully precomputed // We precomputed 65536, others are cheap and will be fully precomputed
// post-ceremony // post-ceremony
if sizes[i] < 65536 { if sizes[i] < 65536 {
@ -495,8 +492,8 @@ func Init() {
} else { } else {
ffts[i] = g1ffts ffts[i] = g1ffts
} }
wg.Done() return nil
}() })
} }
wg.Wait() wg.Wait()