Skip to content

Commit

Permalink
Merge pull request #6 from ingonyama-zk/feat/add-point-copy
Browse files Browse the repository at this point in the history
[FEAT]: Add copy points functions
  • Loading branch information
jeremyfelder authored Sep 18, 2023
2 parents c314861 + b18f425 commit 7c37237
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
16 changes: 8 additions & 8 deletions curves/bls12377/ntt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestNttBN254CompareToGnarkDIF(t *testing.T) {
copy(nttResult, scalars)

assert.Equal(t, nttResult, scalars)
icicle.Ntt(&nttResult, false, icicle.DIF, 0)
icicle.Ntt(&nttResult, false, 0)
assert.NotEqual(t, nttResult, scalars)

domain := fft.NewDomain(uint64(len(scalars)))
Expand All @@ -83,7 +83,7 @@ func TestNttBN254CompareToGnarkDIT(t *testing.T) {
copy(nttResult, scalars)

assert.Equal(t, nttResult, scalars)
icicle.Ntt(&nttResult, false, icicle.DIT, 0)
icicle.Ntt(&nttResult, false, 0)
assert.NotEqual(t, nttResult, scalars)

domain := fft.NewDomain(uint64(len(scalars)))
Expand All @@ -108,7 +108,7 @@ func TestINttBN254CompareToGnarkDIT(t *testing.T) {
copy(nttResult, scalars)

assert.Equal(t, nttResult, scalars)
icicle.Ntt(&nttResult, true, icicle.DIT, 0)
icicle.Ntt(&nttResult, true, 0)
assert.NotEqual(t, nttResult, scalars)

frResScalars := make([]fr.Element, len(frScalars)) // Make a new slice with the same length
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestINttBN254CompareToGnarkDIF(t *testing.T) {
copy(nttResult, scalars)

assert.Equal(t, nttResult, scalars)
icicle.Ntt(&nttResult, true, icicle.DIF, 0)
icicle.Ntt(&nttResult, true, 0)
assert.NotEqual(t, nttResult, scalars)

domain := fft.NewDomain(uint64(len(scalars)))
Expand All @@ -160,14 +160,14 @@ func TestNttBN254(t *testing.T) {
copy(nttResult, scalars)

assert.Equal(t, nttResult, scalars)
icicle.Ntt(&nttResult, false, icicle.NONE, 0)
icicle.Ntt(&nttResult, false, 0)
assert.NotEqual(t, nttResult, scalars)

inttResult := make([]icicle.G1ScalarField, len(nttResult))
copy(inttResult, nttResult)

assert.Equal(t, inttResult, nttResult)
icicle.Ntt(&inttResult, true, icicle.NONE, 0)
icicle.Ntt(&inttResult, true, 0)
assert.Equal(t, inttResult, scalars)
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func TestNttBatchBN254(t *testing.T) {
nttResultVecOfVec = append(nttResultVecOfVec, clone)

// Call the ntt_bls12377 function
icicle.Ntt(&nttResultVecOfVec[i], false, icicle.NONE, 0)
icicle.Ntt(&nttResultVecOfVec[i], false, 0)
}

assert.NotEqual(t, nttBatchResult, scalars)
Expand All @@ -227,7 +227,7 @@ func BenchmarkNTT(b *testing.B) {
nttResult := make([]icicle.G1ScalarField, len(scalars)) // Make a new slice with the same length
copy(nttResult, scalars)
for n := 0; n < b.N; n++ {
icicle.Ntt(&nttResult, false, icicle.NONE, 0)
icicle.Ntt(&nttResult, false, 0)
}
})
}
Expand Down
10 changes: 2 additions & 8 deletions curves/bn254/gnark.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

type OnDeviceData struct {
p unsafe.Pointer
size int
P unsafe.Pointer
Size int
}

func INttOnDevice(scalars_d, twiddles_d, cosetPowers_d unsafe.Pointer, size, sizeBytes int, isCoset bool) unsafe.Pointer {
Expand All @@ -35,8 +35,6 @@ func NttOnDevice(scalars_out, scalars_d, twiddles_d, coset_powers_d unsafe.Point
}

icicle.ReverseScalars(scalars_out, size)

return
}

func MsmOnDevice(scalars_d, points_d unsafe.Pointer, count int, convert bool) (curve.G1Jac, unsafe.Pointer, error) {
Expand Down Expand Up @@ -84,8 +82,6 @@ func PolyOps(a_d, b_d, c_d, den_d unsafe.Pointer, size int) {
if ret != 0 {
fmt.Print("Vector mult a*den issue")
}

return
}

func MontConvOnDevice(scalars_d unsafe.Pointer, size int, is_into bool) {
Expand All @@ -94,8 +90,6 @@ func MontConvOnDevice(scalars_d unsafe.Pointer, size int, is_into bool) {
} else {
icicle.FromMontgomery(scalars_d, size)
}

return
}

func CopyToDevice(scalars []fr.Element, bytes int, copyDone chan unsafe.Pointer) {
Expand Down
16 changes: 8 additions & 8 deletions curves/bn254/ntt_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions curves/bn254/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@ package bn254

import (
"fmt"
"unsafe"

"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
icicle "github.com/ingonyama-zk/icicle/goicicle/curves/bn254"
goicicle "github.com/ingonyama-zk/icicle/goicicle"
)

func CopyPointsToDevice(points []bn254.G1Affine, pointsBytes int, copyDone chan unsafe.Pointer) {
if pointsBytes == 0 {
copyDone <- nil
} else {
devicePtr, _ := goicicle.CudaMalloc(pointsBytes)
iciclePoints := BatchConvertFromG1Affine(points)
goicicle.CudaMemCpyHtoD[icicle.G1PointAffine](devicePtr, iciclePoints, pointsBytes)

copyDone <- devicePtr
}
}

func CopyG2PointsToDevice(points []bn254.G2Affine, pointsBytes int, copyDone chan unsafe.Pointer) {
if pointsBytes == 0 {
copyDone <- nil
} else {
devicePtr, _ := goicicle.CudaMalloc(pointsBytes)
iciclePoints := BatchConvertFromG2Affine(points)
goicicle.CudaMemCpyHtoD[icicle.G2PointAffine](devicePtr, iciclePoints, pointsBytes)

copyDone <- devicePtr
}
}


func ScalarToGnarkFr(f *icicle.G1ScalarField) *fr.Element {
fb := f.ToBytesLe()
var b32 [32]byte
Expand Down

0 comments on commit 7c37237

Please sign in to comment.