From 28ea2d811374704278169c800b9cff09bc1ff601 Mon Sep 17 00:00:00 2001 From: Ido Atlas Date: Thu, 18 Apr 2024 11:41:42 +0300 Subject: [PATCH 1/2] MSM Bases moved to host --- backend/groth16/bn254/icicle/icicle.go | 44 +++++++--------------- backend/groth16/bn254/icicle/provingkey.go | 14 +++---- testgpu/large_circuit_test.go | 7 ++-- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/backend/groth16/bn254/icicle/icicle.go b/backend/groth16/bn254/icicle/icicle.go index f46708cbbc..486251e99a 100644 --- a/backend/groth16/bn254/icicle/icicle.go +++ b/backend/groth16/bn254/icicle/icicle.go @@ -9,7 +9,6 @@ import ( "time" curve "github.com/consensys/gnark-crypto/ecc/bn254" - "github.com/consensys/gnark-crypto/ecc/bn254/fp" "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark-crypto/ecc/bn254/fr/fft" "github.com/consensys/gnark-crypto/ecc/bn254/fr/hash_to_field" @@ -64,44 +63,29 @@ func (pk *ProvingKey) setupDevicePointers() error { /************************* Start G1 Device Setup ***************************/ /************************* A ***************************/ - copyADone := make(chan core.DeviceSlice, 1) - go iciclegnark.CopyPointsToDevice(pk.G1.A, copyADone) // Make a function for points + icicleA := iciclegnark.BatchConvertFromG1Affine(pk.G1.A) + pk.G1Device.A = core.HostSliceFromElements(icicleA) /************************* B ***************************/ - copyBDone := make(chan core.DeviceSlice, 1) - go iciclegnark.CopyPointsToDevice(pk.G1.B, copyBDone) // Make a function for points + icicleB := iciclegnark.BatchConvertFromG1Affine(pk.G1.B) + pk.G1Device.B = core.HostSliceFromElements(icicleB) /************************* K ***************************/ - var pointsNoInfinity []curve.G1Affine - for i, gnarkPoint := range pk.G1.K { - if gnarkPoint.IsInfinity() { - pk.InfinityPointIndicesK = append(pk.InfinityPointIndicesK, i) - } else { - pointsNoInfinity = append(pointsNoInfinity, gnarkPoint) - } - } - - copyKDone := make(chan core.DeviceSlice, 1) - go iciclegnark.CopyPointsToDevice(pointsNoInfinity, copyKDone) // Make a function for points + icicleK := iciclegnark.BatchConvertFromG1Affine(pk.G1.K) + pk.G1Device.K = core.HostSliceFromElements(icicleK) /************************* Z ***************************/ - copyZDone := make(chan core.DeviceSlice, 1) padding := make([]curve.G1Affine, 1) // padding[0] = curve.G1Affine.generator() Z_plus_point := append(pk.G1.Z, padding...) - go iciclegnark.CopyPointsToDevice(Z_plus_point, copyZDone) // Make a function for points - - /************************* End G1 Device Setup ***************************/ - pk.G1Device.A = <-copyADone - pk.G1Device.B = <-copyBDone - pk.G1Device.K = <-copyKDone - pk.G1Device.Z = <-copyZDone + icicleZ := iciclegnark.BatchConvertFromG1Affine(Z_plus_point) + pk.G1Device.Z = core.HostSliceFromElements(icicleZ) /************************* Start G2 Device Setup ***************************/ - pointsBytesB2 := len(pk.G2.B) * fp.Bytes * 4 - copyG2BDone := make(chan core.DeviceSlice, 1) - go iciclegnark.CopyG2PointsToDevice(pk.G2.B, pointsBytesB2, copyG2BDone) // Make a function for points - pk.G2Device.B = <-copyG2BDone + icicleB2 := iciclegnark.BatchConvertFromG2Affine(pk.G2.B) + pk.G2Device.B = core.HostSliceFromElements(icicleB2) + + fmt.Println("Size of domain: ", 2*pk.Domain.Cardinality, "; Size of points:", pk.G1Device.A.Len(), pk.G1Device.B.Len(), pk.G1Device.K.Len(), pk.G1Device.Z.Len(), pk.G2Device.B.Len()) lg.Info().Msg("end setupDevicePointers") @@ -184,7 +168,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b for i := range commitmentInfo { copy(commitmentsSerialized[fr.Bytes*i:], wireValues[commitmentInfo[i].CommitmentIndex].Marshal()) } - + var errPedersen error chPedersenDone := make(chan struct{}, 1) go func() { @@ -506,4 +490,4 @@ func computeHonDevice(a, b, c []fr.Element, domain *fft.Domain, stream cuda_runt b_device.FreeAsync(stream) c_device.FreeAsync(stream) return a_device -} \ No newline at end of file +} diff --git a/backend/groth16/bn254/icicle/provingkey.go b/backend/groth16/bn254/icicle/provingkey.go index d61b1faf68..84fea0df73 100644 --- a/backend/groth16/bn254/icicle/provingkey.go +++ b/backend/groth16/bn254/icicle/provingkey.go @@ -1,9 +1,10 @@ package icicle_bn254 import ( - "github.com/ingonyama-zk/icicle/wrappers/golang/core" "io" - "unsafe" + + "github.com/ingonyama-zk/icicle/wrappers/golang/core" + "github.com/ingonyama-zk/icicle/wrappers/golang/curves/bn254" groth16_bn254 "github.com/consensys/gnark/backend/groth16/bn254" cs "github.com/consensys/gnark/constraint/bn254" @@ -11,17 +12,14 @@ import ( type deviceInfo struct { G1Device struct { - A, B, K, Z core.DeviceSlice + A, B, K, Z core.HostSlice[bn254.Affine] } DomainDevice struct { - Twiddles, TwiddlesInv core.DeviceSlice - CosetTable, CosetTableInv unsafe.Pointer + Twiddles, TwiddlesInv core.DeviceSlice } G2Device struct { - B core.DeviceSlice + B core.HostSlice[bn254.G2Affine] } - DenDevice core.DeviceSlice - InfinityPointIndicesK []int } type ProvingKey struct { diff --git a/testgpu/large_circuit_test.go b/testgpu/large_circuit_test.go index 4eb38c7f8a..786eb6aa42 100644 --- a/testgpu/large_circuit_test.go +++ b/testgpu/large_circuit_test.go @@ -1,6 +1,9 @@ package testgpu import ( + "os" + "testing" + "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/backend/groth16" "github.com/consensys/gnark/frontend" @@ -8,11 +11,9 @@ import ( "github.com/consensys/gnark/logger" "github.com/consensys/gnark/test" "github.com/rs/zerolog" - "os" - "testing" ) -const TEST_SIZE = 1 * 10000 +const TEST_SIZE = 1 * 10000000 type LargeCircuitCommitment struct { P, Q [TEST_SIZE]frontend.Variable From 9ce8df802bea081e27389d0cf2da154b41ed6045 Mon Sep 17 00:00:00 2001 From: Ido Atlas Date: Thu, 18 Apr 2024 19:19:31 +0300 Subject: [PATCH 2/2] Temporary NTT fix --- backend/groth16/bn254/icicle/icicle.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/groth16/bn254/icicle/icicle.go b/backend/groth16/bn254/icicle/icicle.go index 486251e99a..95bbc1e81f 100644 --- a/backend/groth16/bn254/icicle/icicle.go +++ b/backend/groth16/bn254/icicle/icicle.go @@ -452,14 +452,11 @@ func computeHonDevice(a, b, c []fr.Element, domain *fft.Domain, stream cuda_runt b_host.CopyToDeviceAsync(&b_device, stream, true) c_host.CopyToDeviceAsync(&c_device, stream, true) - cfg.Ordering = core.KNM - bn254.Ntt(a_device, core.KInverse, &cfg, a_device) bn254.Ntt(b_device, core.KInverse, &cfg, b_device) bn254.Ntt(c_device, core.KInverse, &cfg, c_device) cfg.CosetGen = configCosetGen - cfg.Ordering = core.KMN bn254.Ntt(a_device, core.KForward, &cfg, a_device) bn254.Ntt(b_device, core.KForward, &cfg, b_device)