Skip to content

Commit 3b55583

Browse files
committed
fixes after zk review
1 parent fcf853a commit 3b55583

File tree

1 file changed

+95
-156
lines changed

1 file changed

+95
-156
lines changed

vmhost/vmhooks/zkcryptoei.go

Lines changed: 95 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vmhooks
33
import (
44
"github.com/multiversx/mx-chain-crypto-go/zk/groth16"
55
"github.com/multiversx/mx-chain-crypto-go/zk/lowLevelFeatures"
6+
"github.com/multiversx/mx-chain-crypto-go/zk/plonk"
67
"github.com/multiversx/mx-chain-vm-go/math"
78
"github.com/multiversx/mx-chain-vm-go/vmhost"
89
)
@@ -17,19 +18,6 @@ const (
1718
managedPairingCheckEC = "ManagedPairingCheckEC"
1819
)
1920

20-
/*
21-
BN254
22-
BLS12_377
23-
BLS12_381
24-
BLS24_315
25-
BLS24_317
26-
BW6_761
27-
BW6_633
28-
STARK_CURVE
29-
SECP256K1
30-
GRUMPKIN
31-
*/
32-
3321
// ManagedVerifyGroth16 VMHooks implementation.
3422
// @autogenerate(VMHooks)
3523
func (context *VMHooksImpl) ManagedVerifyGroth16(
@@ -104,7 +92,7 @@ func ManagedVerifyZKFunctionWithHost(
10492
case managedVerifyGroth16:
10593
verified, invalidSigErr = groth16.VerifyGroth16(uint16(curveID), proofBytes, vkBytes, pubWitnessBytes)
10694
case managedVerifyPlonk:
107-
verified, invalidSigErr = groth16.VerifyGroth16(uint16(curveID), proofBytes, vkBytes, pubWitnessBytes)
95+
verified, invalidSigErr = plonk.VerifyPlonk(uint16(curveID), proofBytes, vkBytes, pubWitnessBytes)
10896
}
10997

11098
if invalidSigErr != nil || !verified {
@@ -115,59 +103,52 @@ func ManagedVerifyZKFunctionWithHost(
115103
return 0
116104
}
117105

118-
// ManagedAddEC VMHooks implementation.
119-
// @autogenerate(VMHooks)
120-
func (context *VMHooksImpl) ManagedAddEC(
121-
curveID int32,
122-
groupID int32,
123-
point1Handle, point2Handle int32,
124-
resultHandle int32,
125-
) int32 {
126-
host := context.GetVMHost()
127-
return ManagedAddECWithHost(host, curveID, groupID, point1Handle, point2Handle, resultHandle)
128-
}
129-
130-
// ManagedAddECWithHost implements the Add elliptic curves operation on the set of defined curves and group
131-
func ManagedAddECWithHost(
106+
func managedECOperationWithHost(
132107
host vmhost.VMHost,
108+
operationName string,
109+
gasCost uint64,
110+
failureError error,
133111
curveID int32,
134112
groupID int32,
135-
point1Handle, point2Handle int32,
113+
inputHandles []int32,
136114
resultHandle int32,
115+
execute func(definedEC lowLevelFeatures.ECGroup, inputs [][]byte) ([]byte, error),
137116
) int32 {
138117
metering := host.Metering()
139118
managedType := host.ManagedTypes()
140119

141-
err := metering.UseGasBoundedAndAddTracedGas(managedAddEC, metering.GasSchedule().CryptoAPICost.AddECC)
120+
err := metering.UseGasBoundedAndAddTracedGas(operationName, gasCost)
142121
if err != nil {
143122
FailExecution(host, err)
144123
return -1
145124
}
146125

147-
point1Bytes, err := getBytesAndConsumeGas(managedType, point1Handle)
148-
if err != nil {
149-
FailExecution(host, err)
150-
return -1
151-
}
152-
153-
point2Bytes, err := getBytesAndConsumeGas(managedType, point2Handle)
154-
if err != nil {
155-
FailExecution(host, err)
156-
return -1
126+
var inputsBytes [][]byte
127+
for _, handle := range inputHandles {
128+
bytes, err := getBytesAndConsumeGas(managedType, handle)
129+
if err != nil {
130+
FailExecution(host, err)
131+
return -1
132+
}
133+
inputsBytes = append(inputsBytes, bytes)
157134
}
158135

159136
definedECParam := lowLevelFeatures.ECParams{Curve: lowLevelFeatures.ID(curveID), Group: lowLevelFeatures.GroupID(groupID)}
160137
definedEC, ok := lowLevelFeatures.EcRegistry[definedECParam]
161138
if !ok {
162-
FailExecution(host, vmhost.ErrNoEllipticCurveUnderThisHandle)
139+
FailExecutionConditionally(host, vmhost.ErrNoEllipticCurveUnderThisHandle)
163140
return -1
164141
}
165142

166-
// TODO: use more gas depending on type
143+
// TODO: use more gas depending on scalar and curve type. This would require changes to the gas schedule and the VM's core logic.
144+
// The gas cost should be dependent on the curve type and other parameters. To implement this, we would need to:
145+
// 1. Define new gas cost parameters in the `CryptoAPICost` struct for each curve type and operation.
146+
// 2. Update the `FillGasMapCryptoAPICosts` function to initialize these new parameters.
147+
// 3. Update this function to use the new gas cost parameters based on the curve ID.
167148

168-
result, err := definedEC.Add(point1Bytes, point2Bytes)
149+
result, err := execute(definedEC, inputsBytes)
169150
if err != nil {
170-
FailExecutionConditionally(host, vmhost.ErrEllipticCurveAddFailed)
151+
FailExecutionConditionally(host, failureError)
171152
return -1
172153
}
173154

@@ -178,10 +159,52 @@ func ManagedAddECWithHost(
178159
}
179160

180161
managedType.SetBytes(resultHandle, result)
181-
182162
return 0
183163
}
184164

165+
func addEC(definedEC lowLevelFeatures.ECGroup, inputs [][]byte) ([]byte, error) {
166+
if len(inputs) != 2 {
167+
return nil, vmhost.ErrArgIndexOutOfRange
168+
}
169+
return definedEC.Add(inputs[0], inputs[1])
170+
}
171+
172+
func mulEC(definedEC lowLevelFeatures.ECGroup, inputs [][]byte) ([]byte, error) {
173+
if len(inputs) != 2 {
174+
return nil, vmhost.ErrArgIndexOutOfRange
175+
}
176+
return definedEC.Mul(inputs[0], inputs[1])
177+
}
178+
179+
func mapToCurveEC(definedEC lowLevelFeatures.ECGroup, inputs [][]byte) ([]byte, error) {
180+
if len(inputs) != 1 {
181+
return nil, vmhost.ErrArgIndexOutOfRange
182+
}
183+
return definedEC.MapToCurve(inputs[0])
184+
}
185+
186+
// ManagedAddEC VMHooks implementation.
187+
// @autogenerate(VMHooks)
188+
func (context *VMHooksImpl) ManagedAddEC(
189+
curveID int32,
190+
groupID int32,
191+
point1Handle, point2Handle int32,
192+
resultHandle int32,
193+
) int32 {
194+
host := context.GetVMHost()
195+
return managedECOperationWithHost(
196+
host,
197+
managedAddEC,
198+
host.Metering().GasSchedule().CryptoAPICost.AddECC,
199+
vmhost.ErrEllipticCurveAddFailed,
200+
curveID,
201+
groupID,
202+
[]int32{point1Handle, point2Handle},
203+
resultHandle,
204+
addEC,
205+
)
206+
}
207+
185208
// ManagedMulEC VMHooks implementation.
186209
// @autogenerate(VMHooks)
187210
func (context *VMHooksImpl) ManagedMulEC(
@@ -191,62 +214,39 @@ func (context *VMHooksImpl) ManagedMulEC(
191214
resultHandle int32,
192215
) int32 {
193216
host := context.GetVMHost()
194-
return ManagedMulECWithHost(host, curveID, groupID, pointHandle, scalarHandle, resultHandle)
217+
return managedECOperationWithHost(
218+
host,
219+
managedMulEC,
220+
host.Metering().GasSchedule().CryptoAPICost.ScalarMultECC,
221+
vmhost.ErrEllipticCurveMulFailed,
222+
curveID,
223+
groupID,
224+
[]int32{pointHandle, scalarHandle},
225+
resultHandle,
226+
mulEC,
227+
)
195228
}
196229

197-
// ManagedMulECWithHost implements the Multiply elliptic curves operation on the set of defined curves and group
198-
func ManagedMulECWithHost(
199-
host vmhost.VMHost,
230+
// ManagedMapToCurveEC VMHooks implementation.
231+
// @autogenerate(VMHooks)
232+
func (context *VMHooksImpl) ManagedMapToCurveEC(
200233
curveID int32,
201234
groupID int32,
202-
pointHandle, scalarHandle int32,
235+
elementHandle int32,
203236
resultHandle int32,
204237
) int32 {
205-
metering := host.Metering()
206-
managedType := host.ManagedTypes()
207-
208-
err := metering.UseGasBoundedAndAddTracedGas(managedMulEC, metering.GasSchedule().CryptoAPICost.AddECC)
209-
if err != nil {
210-
FailExecution(host, err)
211-
return -1
212-
}
213-
214-
pointBytes, err := getBytesAndConsumeGas(managedType, pointHandle)
215-
if err != nil {
216-
FailExecution(host, err)
217-
return -1
218-
}
219-
220-
scalarBytes, err := getBytesAndConsumeGas(managedType, scalarHandle)
221-
if err != nil {
222-
FailExecution(host, err)
223-
return -1
224-
}
225-
226-
definedECParam := lowLevelFeatures.ECParams{Curve: lowLevelFeatures.ID(curveID), Group: lowLevelFeatures.GroupID(groupID)}
227-
definedEC, ok := lowLevelFeatures.EcRegistry[definedECParam]
228-
if !ok {
229-
FailExecution(host, vmhost.ErrNoEllipticCurveUnderThisHandle)
230-
return -1
231-
}
232-
233-
// TODO: use more gas depending on scalar and curve type
234-
235-
result, err := definedEC.Mul(pointBytes, scalarBytes)
236-
if err != nil {
237-
FailExecutionConditionally(host, vmhost.ErrEllipticCurveMulFailed)
238-
return -1
239-
}
240-
241-
err = managedType.ConsumeGasForBytes(result)
242-
if err != nil {
243-
FailExecution(host, err)
244-
return -1
245-
}
246-
247-
managedType.SetBytes(resultHandle, result)
248-
249-
return 0
238+
host := context.GetVMHost()
239+
return managedECOperationWithHost(
240+
host,
241+
managedMapToCurveEC,
242+
host.Metering().GasSchedule().CryptoAPICost.AddECC,
243+
vmhost.ErrEllipticCurveMapToCurveFailed,
244+
curveID,
245+
groupID,
246+
[]int32{elementHandle},
247+
resultHandle,
248+
mapToCurveEC,
249+
)
250250
}
251251

252252
// ManagedMultiExpEC VMHooks implementation.
@@ -323,67 +323,6 @@ func ManagedMultiExpECWithHost(
323323
return 0
324324
}
325325

326-
// ManagedMapToCurveEC VMHooks implementation.
327-
// @autogenerate(VMHooks)
328-
func (context *VMHooksImpl) ManagedMapToCurveEC(
329-
curveID int32,
330-
groupID int32,
331-
elementHandle int32,
332-
resultHandle int32,
333-
) int32 {
334-
host := context.GetVMHost()
335-
return ManagedMapToCurveECWithHost(host, curveID, groupID, elementHandle, resultHandle)
336-
}
337-
338-
// ManagedMapToCurveECWithHost implements the map to curve elliptic curves operation on the set of defined curves and group
339-
func ManagedMapToCurveECWithHost(
340-
host vmhost.VMHost,
341-
curveID int32,
342-
groupID int32,
343-
elementHandle int32,
344-
resultHandle int32,
345-
) int32 {
346-
metering := host.Metering()
347-
managedType := host.ManagedTypes()
348-
349-
err := metering.UseGasBoundedAndAddTracedGas(managedMapToCurveEC, metering.GasSchedule().CryptoAPICost.AddECC)
350-
if err != nil {
351-
FailExecution(host, err)
352-
return -1
353-
}
354-
355-
element, err := getBytesAndConsumeGas(managedType, elementHandle)
356-
if err != nil {
357-
FailExecution(host, err)
358-
return -1
359-
}
360-
361-
definedECParam := lowLevelFeatures.ECParams{Curve: lowLevelFeatures.ID(curveID), Group: lowLevelFeatures.GroupID(groupID)}
362-
definedEC, ok := lowLevelFeatures.EcRegistry[definedECParam]
363-
if !ok {
364-
FailExecution(host, vmhost.ErrNoEllipticCurveUnderThisHandle)
365-
return -1
366-
}
367-
368-
// TODO: use more gas depending on scalar and curve type
369-
370-
result, err := definedEC.MapToCurve(element)
371-
if err != nil {
372-
FailExecutionConditionally(host, vmhost.ErrEllipticCurveMapToCurveFailed)
373-
return -1
374-
}
375-
376-
err = managedType.ConsumeGasForBytes(result)
377-
if err != nil {
378-
FailExecution(host, err)
379-
return -1
380-
}
381-
382-
managedType.SetBytes(resultHandle, result)
383-
384-
return 0
385-
}
386-
387326
// ManagedPairingChecksEC VMHooks implementation.
388327
// @autogenerate(VMHooks)
389328
func (context *VMHooksImpl) ManagedPairingChecksEC(

0 commit comments

Comments
 (0)