Skip to content

Commit

Permalink
working kzg and rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jul 7, 2024
1 parent da40cb9 commit 4102710
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 147 deletions.
9 changes: 0 additions & 9 deletions encoding/kzg/prover/cpu/multiframe_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,6 @@ func (p *CpuComputer) ComputeMultiFrameProof(polyFr []fr.Element, numChunks, chu

t0 := time.Now()

fmt.Println("Transposed FFT")
for i := 0; i < len(coeffStore); i++ {
vec := coeffStore[i]
for j := 0; j < len(vec); j++ {
fmt.Printf("%v ", vec[j].String())
}
fmt.Println()
}

// compute proof by multi scaler multiplication
msmErrors := make(chan error, dimE*2)
for i := uint64(0); i < dimE*2; i++ {
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions encoding/kzg/prover/gpu/ecntt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gpu
import (
"fmt"

"github.com/Layr-Labs/eigenda/encoding/utils/gpu_utils"
"github.com/consensys/gnark-crypto/ecc/bn254"
cr "github.com/ingonyama-zk/icicle/v2/wrappers/golang/cuda_runtime"
icicle_bn254 "github.com/ingonyama-zk/icicle/v2/wrappers/golang/curves/bn254"
Expand All @@ -15,19 +16,19 @@ func (c *GpuComputer) ECNtt(batchPoints []bn254.G1Affine, isInverse bool) ([]bn2
totalNumSym := len(batchPoints)

// convert gnark affine to icicle projective on slice
pointsIcileProjective := BatchConvertGnarkAffineToIcicleProjective(batchPoints)
pointsIcileProjective := gpu_utils.BatchConvertGnarkAffineToIcicleProjective(batchPoints)
pointsCopy := core.HostSliceFromElements[icicle_bn254.Projective](pointsIcileProjective)

output := make(core.HostSlice[icicle_bn254.Projective], int(totalNumSym))

// compute
if isInverse {
err := ecntt.ECNtt(pointsCopy, core.KInverse, &c.cfg, output)
err := ecntt.ECNtt(pointsCopy, core.KInverse, &c.NttCfg, output)
if err.CudaErrorCode != cr.CudaSuccess || err.IcicleErrorCode != core.IcicleSuccess {
return nil, fmt.Errorf("inverse ecntt failed")
}
} else {
err := ecntt.ECNtt(pointsCopy, core.KForward, &c.cfg, output)
err := ecntt.ECNtt(pointsCopy, core.KForward, &c.NttCfg, output)
if err.CudaErrorCode != cr.CudaSuccess || err.IcicleErrorCode != core.IcicleSuccess {
return nil, fmt.Errorf("forward ecntt failed")
}
Expand All @@ -36,7 +37,7 @@ func (c *GpuComputer) ECNtt(batchPoints []bn254.G1Affine, isInverse bool) ([]bn2
// convert icicle projective to gnark affine
gpuFFTBatch := make([]bn254.G1Affine, len(batchPoints))
for j := 0; j < totalNumSym; j++ {
gpuFFTBatch[j] = IcicleProjectiveToGnarkAffine(output[j])
gpuFFTBatch[j] = gpu_utils.IcicleProjectiveToGnarkAffine(output[j])
}

return gpuFFTBatch, nil
Expand Down
7 changes: 4 additions & 3 deletions encoding/kzg/prover/gpu/msm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gpu
import (
"fmt"

"github.com/Layr-Labs/eigenda/encoding/utils/gpu_utils"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/ingonyama-zk/icicle/v2/wrappers/golang/core"
Expand All @@ -20,13 +21,13 @@ func (c *GpuComputer) MsmBatch(rowsFr [][]fr.Element, rowsG1 [][]bn254.G1Affine)

// Prepare scalar fields
for _, row := range rowsFr {
rowsSfIcicle = append(rowsSfIcicle, ConvertFrToScalarFieldsBytes(row)...)
rowsSfIcicle = append(rowsSfIcicle, gpu_utils.ConvertFrToScalarFieldsBytes(row)...)
}
rowsFrIcicleCopy := core.HostSliceFromElements[icicle_bn254.ScalarField](rowsSfIcicle)

// Prepare icicle g1 affines
for _, row := range rowsG1 {
rowsAffineIcicle = append(rowsAffineIcicle, BatchConvertGnarkAffineToIcicleAffine(row)...)
rowsAffineIcicle = append(rowsAffineIcicle, gpu_utils.BatchConvertGnarkAffineToIcicleAffine(row)...)
}
rowsG1IcicleCopy := core.HostSliceFromElements[icicle_bn254.Affine](rowsAffineIcicle)

Expand All @@ -52,7 +53,7 @@ func (c *GpuComputer) MsmBatch(rowsFr [][]fr.Element, rowsG1 [][]bn254.G1Affine)
// convert data back to gnark format
gnarkOuts := make([]bn254.G1Affine, numBatchEle)
for i := 0; i < numBatchEle; i++ {
gnarkOuts[i] = IcicleProjectiveToGnarkAffine(outHost[i])
gnarkOuts[i] = gpu_utils.IcicleProjectiveToGnarkAffine(outHost[i])
}

return gnarkOuts, nil
Expand Down
111 changes: 30 additions & 81 deletions encoding/kzg/prover/gpu/multiframe_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type GpuComputer struct {
SFs *fft.FFTSettings
Srs *kzg.SRS
G2Trailing []bn254.G2Affine
cfg core.NTTConfig[[bn254_icicle.SCALAR_LIMBS]uint32]
NttCfg core.NTTConfig[[bn254_icicle.SCALAR_LIMBS]uint32]
}

// benchmarks shows cpu commit on 2MB blob only takes 24.165562ms. For now, use cpu
Expand Down Expand Up @@ -67,33 +67,26 @@ func (p *GpuComputer) ComputeLengthCommitment(coeffs []fr.Element) (*bn254.G2Aff
return &lengthCommitment, nil
}

// This function supports batching over multiple blobs.
// All blobs must have same size and concatenated passed as polyFr
func (p *GpuComputer) ComputeMultiFrameProof(polyFr []fr.Element, numChunks, chunkLen, numWorker uint64) ([]bn254.G1Affine, error) {
// Robert: Standardizing this to use the same math used in precomputeSRS
dimE := numChunks
l := chunkLen
numPoly := uint64(len(polyFr)) / dimE / chunkLen

p.cfg = SetupNTT()

begin := time.Now()

// create storage for intermediate coefficients matrix
jobChan := make(chan uint64, numWorker)
results := make(chan WorkerResult, numWorker)

// create storage for intermediate coefficients
coeffStore := make([][]fr.Element, l*numPoly)
for i := range coeffStore {
coeffStore[i] = make([]fr.Element, dimE*2)
}

fmt.Println("numPoly", numPoly)
fmt.Println("numChunks, ", numChunks)
fmt.Println("chunkLen", chunkLen)

for j := 0; j < len(polyFr); j++ {
fmt.Printf("%v ", polyFr[j].String())
}
fmt.Println("len(polyFr)", len(polyFr))

// Preprpcessing use CPU to compute those coefficients based on polyFr
for w := uint64(0); w < numWorker; w++ {
go p.proofWorkerGPU(polyFr, jobChan, l, dimE, coeffStore, results)
}
Expand All @@ -111,37 +104,26 @@ func (p *GpuComputer) ComputeMultiFrameProof(polyFr []fr.Element, numChunks, chu
err = wr.err
}
}
t_prepare := time.Now()

// Preprpcessing Completed
if err != nil {
return nil, fmt.Errorf("proof worker error: %v", err)
}
preprocessDone := time.Now()

fmt.Println("coeffStore")
for i := 0; i < len(coeffStore); i++ {
a := coeffStore[i]
for j := 0; j < len(a); j++ {
fmt.Printf("%v ", a[j].String())
}
fmt.Println()
}

fmt.Println("NTT")
// setup batch size for NTT
p.cfg.BatchSize = int32(dimE * 2)
// Compute NTT on the coeff matrix
p.NttCfg.BatchSize = int32(l)
coeffStoreFFT, e := p.NTT(coeffStore)
if e != nil {
return nil, e
}
nttDone := time.Now()

// transpose it
// transpose the FFT tranformed matrix
coeffStoreFFTT := make([][]fr.Element, dimE*2*numPoly)
for i := range coeffStoreFFTT {
coeffStoreFFTT[i] = make([]fr.Element, l)
}

t_ntt := time.Now()

for k := uint64(0); k < numPoly; k++ {
step := int(k * dimE * 2)
for i := 0; i < int(l); i++ {
Expand All @@ -151,61 +133,49 @@ func (p *GpuComputer) ComputeMultiFrameProof(polyFr []fr.Element, numChunks, chu
}
}
}
transposingDone := time.Now()

fmt.Println("Transposed FFT")
for i := 0; i < len(coeffStoreFFTT); i++ {
vec := coeffStoreFFTT[i]
for j := 0; j < len(vec); j++ {
fmt.Printf("%v ", vec[j].String())
}
fmt.Println()
}

t0 := time.Now()
fmt.Println("MsmBatch")
// compute msm on each rows of the transposed matrix
sumVec, err := p.MsmBatch(coeffStoreFFTT, p.FFTPointsT)
if err != nil {
return nil, err
}
msmDone := time.Now()

t1 := time.Now()

fmt.Println("ECNTT inverse")
// set new batch size for ntt, this equals to number of blobs
p.cfg.BatchSize = int32(numPoly)
// compute the first ecntt, and set new batch size for ntt
p.NttCfg.BatchSize = int32(numPoly)
sumVecInv, err := p.ECNtt(sumVec, true)
if err != nil {
return nil, err
}

t2 := time.Now()
firstECNttDone := time.Now()

// remove half points per poly
batchInv := make([]bn254.G1Affine, len(sumVecInv)/2)
// outputs is out of order - buttefly
k := 0
for i := 0; i < int(numPoly); i++ {
for j := 0; j < int(dimE); j++ {
batchInv[k] = sumVecInv[i*int(dimE)*2+j]
k += 1
}
}
fmt.Println("ECNTT last")

// compute the second ecntt on the reduced size array
flatProofsBatch, err := p.ECNtt(batchInv, false)
if err != nil {
return nil, fmt.Errorf("second ECNtt error: %w", err)
}
secondECNttDone := time.Now()

t3 := time.Now()

fmt.Println("flatProofsBatch")

for j := 0; j < len(flatProofsBatch); j++ {
fmt.Printf("%v ", flatProofsBatch[j].String())
}

fmt.Printf("prepare %v, ntt %v,\n", t_prepare.Sub(begin), t_ntt.Sub(t_prepare))
fmt.Printf("total %v mult-th %v, msm %v,fft1 %v, fft2 %v,\n", t3.Sub(begin), t0.Sub(begin), t1.Sub(t0), t2.Sub(t1), t3.Sub(t2))
fmt.Printf("Multiproof Time Decomp \n\t\ttotal %-20v \n\t\tpreproc %-20v \n\t\tntt %-20v \n\t\ttranspose %-20v \n\t\tmsm %-v \n\t\tfft1 %-v \n\t\tfft2 %-v,\n",
secondECNttDone.Sub(begin),
preprocessDone.Sub(begin),
nttDone.Sub(preprocessDone),
transposingDone.Sub(nttDone),
msmDone.Sub(transposingDone),
firstECNttDone.Sub(msmDone),
secondECNttDone.Sub(firstECNttDone),
)

return flatProofsBatch, nil
}
Expand Down Expand Up @@ -237,26 +207,6 @@ func (p *GpuComputer) proofWorkerGPU(
}
}

func (p *GpuComputer) GetSlicesCoeffWithoutFFT(polyFr []fr.Element, dimE, j, l uint64) ([]fr.Element, error) {
// there is a constant term
m := uint64(len(polyFr)) - 1
dim := (m - j) / l

toeV := make([]fr.Element, 2*dimE-1)
for i := uint64(0); i < dim; i++ {

toeV[i].Set(&polyFr[m-(j+i*l)])
}

// use precompute table
tm, err := toeplitz.NewToeplitz(toeV, p.SFs)
if err != nil {
return nil, err
}
return tm.GetCoeff()
}

/*
// capable of batching blobs
func (p *GpuComputer) GetSlicesCoeffWithoutFFT(polyFr []fr.Element, dimE, j, l uint64) ([]fr.Element, error) {
// there is a constant term
Expand All @@ -277,4 +227,3 @@ func (p *GpuComputer) GetSlicesCoeffWithoutFFT(polyFr []fr.Element, dimE, j, l u
}
return tm.GetCoeff()
}
*/
32 changes: 4 additions & 28 deletions encoding/kzg/prover/gpu/ntt.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
package gpu

import (
"github.com/Layr-Labs/eigenda/encoding/utils/gpu_utils"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/consensys/gnark-crypto/ecc/bn254/fr/fft"
"github.com/ingonyama-zk/icicle/v2/wrappers/golang/core"
bn254_icicle "github.com/ingonyama-zk/icicle/v2/wrappers/golang/curves/bn254"
bn254_icicle_ntt "github.com/ingonyama-zk/icicle/v2/wrappers/golang/curves/bn254/ntt"
)

// batchSize is number of batches
func SetupNTT() core.NTTConfig[[bn254_icicle.SCALAR_LIMBS]uint32] {
cfg := bn254_icicle_ntt.GetDefaultNttConfig()

cfg.Ordering = core.KNN
cfg.NttAlgorithm = core.Radix2

// batchSize will change later when used
cfg.BatchSize = int32(1)
cfg.NttAlgorithm = core.Radix2

// maximally possible
exp := 28

rouMont, _ := fft.Generator(uint64(1 << exp))
rou := rouMont.Bits()
rouIcicle := bn254_icicle.ScalarField{}
limbs := core.ConvertUint64ArrToUint32Arr(rou[:])
rouIcicle.FromLimbs(limbs)
bn254_icicle_ntt.InitDomain(rouIcicle, cfg.Ctx, false)

return cfg
}

func (c *GpuComputer) NTT(batchFr [][]fr.Element) ([][]fr.Element, error) {
numSymbol := len(batchFr[0])
batchSize := len(batchFr)
Expand All @@ -43,13 +19,13 @@ func (c *GpuComputer) NTT(batchFr [][]fr.Element) ([][]fr.Element, error) {
for i := 0; i < len(batchFr); i++ {
flattenBatchFr = append(flattenBatchFr, batchFr[i]...)
}
flattenBatchSf := ConvertFrToScalarFieldsBytes(flattenBatchFr)
flattenBatchSf := gpu_utils.ConvertFrToScalarFieldsBytes(flattenBatchFr)
scalarsCopy := core.HostSliceFromElements[bn254_icicle.ScalarField](flattenBatchSf)

// run ntt
output := make(core.HostSlice[bn254_icicle.ScalarField], totalSize)
bn254_icicle_ntt.Ntt(scalarsCopy, core.KForward, &c.cfg, output)
flattenBatchFrOutput := ConvertScalarFieldsToFrBytes(output)
bn254_icicle_ntt.Ntt(scalarsCopy, core.KForward, &c.NttCfg, output)
flattenBatchFrOutput := gpu_utils.ConvertScalarFieldsToFrBytes(output)

// convert ntt output from icicle to gnark
nttOutput := make([][]fr.Element, len(batchFr))
Expand Down
15 changes: 9 additions & 6 deletions encoding/kzg/prover/parametrized_prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ func (g *ParametrizedProver) Encode(inputFr []fr.Element) (*bn254.G1Affine, *bn2
multiProofDone := time.Now()

if g.Verbose {
log.Printf(" RS encode takes %v\n", rsEncodeDone.Sub(startTime))
log.Printf(" Commiting takes %v\n", commitDone.Sub(rsEncodeDone))
log.Printf(" LengthCommit takes %v\n", lengthCommitDone.Sub(commitDone))
log.Printf(" lengthProof takes %v\n", lengthProofDone.Sub(lengthCommitDone))
log.Printf(" multiProof takes %v\n", multiProofDone.Sub(lengthProofDone))
log.Printf("Meta infro. order %v. shift %v\n", len(g.Srs.G2), g.SRSOrder-uint64(len(inputFr)))
log.Printf("\n\t\tRS encode %-v\n\t\tCommiting %-v\n\t\tLengthCommit %-v\n\t\tlengthProof %-v\n\t\tmultiProof %-v\n\t\tMetaInfo. order %-v shift %v\n",
rsEncodeDone.Sub(startTime),
commitDone.Sub(rsEncodeDone),
lengthCommitDone.Sub(commitDone),
lengthProofDone.Sub(lengthCommitDone),
multiProofDone.Sub(lengthProofDone),
len(g.Srs.G2),
g.SRSOrder-uint64(len(inputFr)),
)
}

// assemble frames
Expand Down
Loading

0 comments on commit 4102710

Please sign in to comment.