-
Notifications
You must be signed in to change notification settings - Fork 10
/
operators_arithmetic_gas.go
111 lines (101 loc) · 3.88 KB
/
operators_arithmetic_gas.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
package fhevm
import "encoding/hex"
func fheAddSubRequiredGas(environment EVMEnvironment, input []byte) uint64 {
input = input[:minInt(65, len(input))]
logger := environment.GetLogger()
isScalar, err := isScalarOp(input)
if err != nil {
logger.Error("fheAdd/Sub RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input))
return 0
}
var lhs, rhs *verifiedCiphertext
if !isScalar {
lhs, rhs, err = get2VerifiedOperands(environment, input)
if err != nil {
logger.Error("fheAdd/Sub RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
if lhs.fheUintType() != rhs.fheUintType() {
logger.Error("fheAdd/Sub RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType())
return 0
}
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheAdd/Sub RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
}
return environment.FhevmParams().GasCosts.FheAddSub[lhs.fheUintType()]
}
func fheMulRequiredGas(environment EVMEnvironment, input []byte) uint64 {
input = input[:minInt(65, len(input))]
logger := environment.GetLogger()
isScalar, err := isScalarOp(input)
if err != nil {
logger.Error("fheMul RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input))
return 0
}
var lhs, rhs *verifiedCiphertext
if !isScalar {
lhs, rhs, err = get2VerifiedOperands(environment, input)
if err != nil {
logger.Error("fheMul RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
if lhs.fheUintType() != rhs.fheUintType() {
logger.Error("fheMul RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType())
return 0
}
return environment.FhevmParams().GasCosts.FheMul[lhs.fheUintType()]
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheMul RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarMul[lhs.fheUintType()]
}
}
func fheDivRequiredGas(environment EVMEnvironment, input []byte) uint64 {
input = input[:minInt(65, len(input))]
logger := environment.GetLogger()
isScalar, err := isScalarOp(input)
if err != nil {
logger.Error("fheDiv RequiredGas() cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input))
return 0
}
var lhs *verifiedCiphertext
if !isScalar {
logger.Error("fheDiv RequiredGas() only scalar in division is supported, two ciphertexts received", "input", hex.EncodeToString(input))
return 0
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheDiv RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarDiv[lhs.fheUintType()]
}
}
func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 {
input = input[:minInt(65, len(input))]
logger := environment.GetLogger()
isScalar, err := isScalarOp(input)
if err != nil {
logger.Error("fheRem RequiredGas() cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input))
return 0
}
var lhs *verifiedCiphertext
if !isScalar {
logger.Error("fheRem RequiredGas() only scalar in division is supported, two ciphertexts received", "input", hex.EncodeToString(input))
return 0
} else {
lhs, _, err = getScalarOperands(environment, input)
if err != nil {
logger.Error("fheRem RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input))
return 0
}
return environment.FhevmParams().GasCosts.FheScalarRem[lhs.fheUintType()]
}
}