forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 3
/
icicle_wrapper_gpu.go
105 lines (76 loc) · 2.59 KB
/
icicle_wrapper_gpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//go:build gpu
package groth16
import (
"fmt"
"unsafe"
curve "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
cudawrapper "github.com/ingonyama-zk/icicle/goicicle"
icicle "github.com/ingonyama-zk/icicle/goicicle/curves/bn254"
)
type OnDeviceData struct {
p unsafe.Pointer
size int
}
func INttOnDevice(scalars_d, twiddles_d, cosetPowers_d unsafe.Pointer, size, sizeBytes int, isCoset bool) unsafe.Pointer {
icicle.ReverseScalars(scalars_d, size)
scalarsInterp := icicle.Interpolate(scalars_d, twiddles_d, cosetPowers_d, size, isCoset)
return scalarsInterp
}
func NttOnDevice(scalars_out, scalars_d, twiddles_d, coset_powers_d unsafe.Pointer, size, twid_size, size_bytes int, isCoset bool) {
res := icicle.Evaluate(scalars_out, scalars_d, twiddles_d, coset_powers_d, size, twid_size, isCoset)
if res != 0 {
fmt.Print("Issue evaluating")
}
icicle.ReverseScalars(scalars_out, size)
return
}
func MsmOnDevice(scalars_d, points_d unsafe.Pointer, count int, convert bool) (curve.G1Jac, unsafe.Pointer, error) {
out_d, _ := cudawrapper.CudaMalloc(96)
icicle.Commit(out_d, scalars_d, points_d, count, 10)
if convert {
outHost := make([]icicle.PointBN254, 1)
cudawrapper.CudaMemCpyDtoH[icicle.PointBN254](outHost, out_d, 96)
return *outHost[0].ToGnarkJac(), nil, nil
}
return curve.G1Jac{}, out_d, nil
}
func MsmG2OnDevice(scalars_d, points_d unsafe.Pointer, count int, convert bool) (curve.G2Jac, unsafe.Pointer, error) {
out_d, _ := cudawrapper.CudaMalloc(192)
icicle.CommitG2(out_d, scalars_d, points_d, count, 10)
if convert {
outHost := make([]icicle.G2Point, 1)
cudawrapper.CudaMemCpyDtoH[icicle.G2Point](outHost, out_d, 192)
return *outHost[0].ToGnarkJac(), nil, nil
}
return curve.G2Jac{}, out_d, nil
}
func PolyOps(a_d, b_d, c_d, den_d unsafe.Pointer, size int) {
ret := icicle.VecScalarMulMod(a_d, b_d, size)
if ret != 0 {
fmt.Print("Vector mult a*b issue")
}
ret = icicle.VecScalarSub(a_d, c_d, size)
if ret != 0 {
fmt.Print("Vector sub issue")
}
ret = icicle.VecScalarMulMod(a_d, den_d, size)
if ret != 0 {
fmt.Print("Vector mult a*den issue")
}
return
}
func MontConvOnDevice(scalars_d unsafe.Pointer, size int, is_into bool) {
if is_into {
icicle.ToMontgomery(scalars_d, size)
} else {
icicle.FromMontgomery(scalars_d, size)
}
return
}
func CopyToDevice(scalars []fr.Element, bytes int, copyDone chan unsafe.Pointer) {
devicePtr, _ := cudawrapper.CudaMalloc(bytes)
cudawrapper.CudaMemCpyHtoD[fr.Element](devicePtr, scalars, bytes)
MontConvOnDevice(devicePtr, len(scalars), false)
copyDone <- devicePtr
}