/
sc_pl_wrapper.go
120 lines (106 loc) · 2.87 KB
/
sc_pl_wrapper.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package vm
import (
"fmt"
"strconv"
"strings"
"github.com/Venachain/Venachain/common"
"github.com/Venachain/Venachain/params"
)
const (
paillierSuccess CodeType = 0
paillierFailed CodeType = 1
paillierWeightAddType = "paillierWeightAdd"
paillierAddType = "paillierAdd"
paillierMulType = "paillierMul"
paillierResult = "paillierResult:%s"
)
type SCPaillierWrapper struct {
base *PaillierManager
}
func (s SCPaillierWrapper) RequiredGas(input []byte) uint64 {
if common.IsBytesEmpty(input) {
return 0
}
return params.SCPaillierProofGas
}
func (s SCPaillierWrapper) Run(input []byte) ([]byte, error) {
fnName, ret, err := execSC(input, s.AllExportFns())
if err != nil {
if fnName == "" {
fnName = "Notify"
}
s.base.emitEvent(fnName, operateFail, err.Error())
}
return ret, err
}
// for access control
func (s *SCPaillierWrapper) AllExportFns() SCExportFns {
return SCExportFns{
"paillierWeightAdd": s.paillierWeightAdd,
"paillierAdd": s.paillierAdd,
"paillierMul": s.paillierMul,
}
}
func NewPLWrapper(db StateDB) *SCPaillierWrapper {
return &SCPaillierWrapper{NewPL(db)}
}
func (s *SCPaillierWrapper) paillierWeightAdd(args string, arr string, pubKey string) (string, error) {
var intArr []uint
split := strings.Split(arr, ",")
for _, i := range split {
l, err := strconv.Atoi(i)
if err != nil {
return "", err
}
intArr = append(intArr, uint(l))
}
res, err := s.base.paillierWeightAdd(args, intArr, pubKey)
if err != nil {
return "", err
}
data := struct {
Args string
Arr string
}{Args: args, Arr: arr}
if err := s.base.savePaillier(paillierWeightAddType, pubKey, data, res); err != nil {
return "", err
}
s.base.emitNotifyEvent(paillierSuccess, fmt.Sprintf(paillierResult, res))
fmt.Println("the result of PaillierWeightAdd is:", res)
return res, err
}
func (s *SCPaillierWrapper) paillierAdd(args string, pubKey string) (string, error) {
res, err := s.base.paillierAdd(args, pubKey)
if err != nil {
return "", err
}
data := struct {
Args string
}{Args: args}
if err := s.base.savePaillier(paillierAddType, pubKey, data, res); err != nil {
return "", err
}
s.base.emitNotifyEvent(paillierSuccess, fmt.Sprintf(paillierResult, res))
fmt.Println("the result of the PaillierAdd is:", res)
return res, err
}
func (s *SCPaillierWrapper) paillierMul(arg string, scalar string, pubKey string) (string, error) {
intScalar, err := strconv.Atoi(scalar)
if err != nil {
return "", err
}
res, err := s.base.paillierMul(arg, uint(intScalar), pubKey)
if err != nil {
return "", err
}
data := struct {
Args string
Scalar string
}{Args: arg, Scalar: scalar}
if err := s.base.savePaillier(paillierMulType, pubKey, data, res); err != nil {
return "", err
}
s.base.emitNotifyEvent(paillierSuccess, fmt.Sprintf(paillierResult, res))
fmt.Println("the result of the PaillierMul is:", res)
return res, err
}