From bb6c2f58546b442040376574ca75788706c0438a Mon Sep 17 00:00:00 2001 From: Agatha Bahati <58204713+MoigeMatino@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:08:56 +0300 Subject: [PATCH] refactor: resolve BigInt3 structure (#354) * refactor: resolve BigInt3 structure and decouple functionality draft: change file structure * chore: remove bigint3 utils parent directory * refactor: modify ResolveBigInt3 function Modified function definition by replacing reference to VM.VirtualMachine with Memory and performed ci linting * refactor: update function signature with fixed size array * Using ResolveBigInt3 and errors resulting from it * refactor: resolve BigInt3 structure and decouple functionality draft: change file structure * chore: remove bigint3 utils parent directory * refactor: modify ResolveBigInt3 function Modified function definition by replacing reference to VM.VirtualMachine with Memory and performed ci linting * refactor: update function signature with fixed size array * Using ResolveBigInt3 and errors resulting from it * refactor: use ResolveBigInt3 helper function to streamline BigInt3 resolution logic Replaced the embedded code for resolving BigInt3 structures with the ResolveBigInt3 helper function to improve code readability and maintainability. This change reduces code duplication and ensures consistent logic for handling BigInt3 structures. * chore: remove comments on import * refactor: modify parsing of y values in newComputeSlopeV1Hint * refactor: modify parsing of y values in newComputeSlopeV1Hint --- go.mod | 4 +- pkg/hintrunner/zero/zerohint_ec.go | 198 +++++++--------------- pkg/hintrunner/zero/zerohint_signature.go | 62 ++----- pkg/vm/memory/memory_value.go | 18 ++ 4 files changed, 95 insertions(+), 187 deletions(-) diff --git a/go.mod b/go.mod index 7e32afbc..bd356b73 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/joho/godotenv v1.5.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect @@ -18,7 +17,6 @@ require ( github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect - golang.org/x/crypto v0.10.0 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/sys v0.11.0 // indirect golang.org/x/text v0.10.0 // indirect @@ -31,7 +29,9 @@ require ( github.com/consensys/gnark-crypto v0.12.1 github.com/go-playground/validator/v10 v10.15.5 github.com/holiman/uint256 v1.2.3 + github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.8.4 github.com/urfave/cli/v2 v2.25.7 + golang.org/x/crypto v0.10.0 golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb ) diff --git a/pkg/hintrunner/zero/zerohint_ec.go b/pkg/hintrunner/zero/zerohint_ec.go index 65aa239b..208bf553 100644 --- a/pkg/hintrunner/zero/zerohint_ec.go +++ b/pkg/hintrunner/zero/zerohint_ec.go @@ -36,20 +36,14 @@ func newEcNegateHint(point hinter.ResOperander) hinter.Hinter { return err } - pointMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(pointAddr, int16(6)) + pointYAddr, err := pointAddr.AddOffset(3) if err != nil { return err } - // [y.d0, y.d1, y.d2] - var pointYValues [3]*fp.Element - - for i := 0; i < 3; i++ { - pointYValue, err := pointMemoryValues[i+3].FieldElement() - if err != nil { - return err - } - pointYValues[i] = pointYValue + pointYValues, err := vm.Memory.ResolveAsBigInt3(pointYAddr) + if err != nil { + return err } //> y = pack(ids.point.y, PRIME) % SECP_P @@ -161,64 +155,40 @@ func newFastEcAddAssignNewXHint(slope, point0, point1 hinter.ResOperander) hinte if err != nil { return err } - slopeMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(slopeAddr, int16(3)) + + point0Addr, err := point0.GetAddress(vm) if err != nil { return err } - point0Addr, err := point0.GetAddress(vm) + point1Addr, err := point1.GetAddress(vm) if err != nil { return err } - point0MemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(point0Addr, int16(6)) + + point0YAddr, err := point0Addr.AddOffset(3) if err != nil { return err } - point1Addr, err := point1.GetAddress(vm) + slopeValues, err := vm.Memory.ResolveAsBigInt3(slopeAddr) if err != nil { return err } - point1MemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(point1Addr, int16(3)) + + point0XValues, err := vm.Memory.ResolveAsBigInt3(point0Addr) if err != nil { return err } - // [d0, d1, d2] - var slopeValues [3]*fp.Element - // [x.d0, x.d1, x.d2] - var point0XValues [3]*fp.Element - // [y.d0, y.d1, y.d2] - var point0YValues [3]*fp.Element - // [x.d0, x.d1, x.d2] - var point1XValues [3]*fp.Element - - for i := 0; i < 3; i++ { - slopeValue, err := slopeMemoryValues[i].FieldElement() - if err != nil { - return err - } - slopeValues[i] = slopeValue - - point0XValue, err := point0MemoryValues[i].FieldElement() - if err != nil { - return err - } - point0XValues[i] = point0XValue - - point1XValue, err := point1MemoryValues[i].FieldElement() - if err != nil { - return err - } - point1XValues[i] = point1XValue + point1XValues, err := vm.Memory.ResolveAsBigInt3(point1Addr) + if err != nil { + return err } - for i := 3; i < 6; i++ { - point0YValue, err := point0MemoryValues[i].FieldElement() - if err != nil { - return err - } - point0YValues[i-3] = point0YValue + point0YValues, err := vm.Memory.ResolveAsBigInt3(point0YAddr) + if err != nil { + return err } //> slope = pack(ids.slope, PRIME) @@ -353,29 +323,20 @@ func newEcDoubleSlopeV1Hint(point hinter.ResOperander) hinter.Hinter { if err != nil { return err } - pointMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(pointAddr, int16(6)) + + pointYAddr, err := pointAddr.AddOffset(3) if err != nil { return err } - // [x.d0, x.d1, x.d2] - var pointXValues [3]*fp.Element - // [y.d0, y.d1, y.d2] - var pointYValues [3]*fp.Element - - for i := 0; i < 3; i++ { - pointValue, err := pointMemoryValues[i].FieldElement() - if err != nil { - return err - } - pointXValues[i] = pointValue + pointXValues, err := vm.Memory.ResolveAsBigInt3(pointAddr) + if err != nil { + return err } - for i := 3; i < 6; i++ { - pointValue, err := pointMemoryValues[i].FieldElement() - if err != nil { - return err - } - pointYValues[i-3] = pointValue + + pointYValues, err := vm.Memory.ResolveAsBigInt3(pointYAddr) + if err != nil { + return err } //> x = pack(ids.point.x, PRIME) @@ -438,20 +399,11 @@ func newReduceV1Hint(x hinter.ResOperander) hinter.Hinter { return err } - xMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(xAddr, int16(3)) + xValues, err := vm.Memory.ResolveAsBigInt3(xAddr) if err != nil { return err } - var xValues [3]*fp.Element - for i := 0; i < 3; i++ { - xValue, err := xMemoryValues[i].FieldElement() - if err != nil { - return err - } - xValues[i] = xValue - } - xBig, err := secp_utils.SecPPacked(xValues) if err != nil { return err @@ -499,45 +451,30 @@ func newEcDoubleAssignNewXV1Hint(slope, point hinter.ResOperander) hinter.Hinter if err != nil { return err } - slopeMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(slopeAddr, int16(3)) + + pointAddr, err := point.GetAddress(vm) if err != nil { return err } - pointAddr, err := point.GetAddress(vm) + pointYAddr, err := pointAddr.AddOffset(3) if err != nil { return err } - pointMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(pointAddr, int16(6)) + + slopeValues, err := vm.Memory.ResolveAsBigInt3(slopeAddr) if err != nil { return err } - // [d0, d1, d2] - var slopeValues [3]*fp.Element - // [x.d0, x.d1, x.d2] - var pointXValues [3]*fp.Element - // [y.d0, y.d1, y.d2] - var pointYValues [3]*fp.Element - - for i := 0; i < 3; i++ { - slopeValue, err := slopeMemoryValues[i].FieldElement() - if err != nil { - return err - } - slopeValues[i] = slopeValue - - pointXValue, err := pointMemoryValues[i].FieldElement() - if err != nil { - return err - } - pointXValues[i] = pointXValue + pointXValues, err := vm.Memory.ResolveAsBigInt3(pointAddr) + if err != nil { + return err + } - pointYValue, err := pointMemoryValues[i+3].FieldElement() - if err != nil { - return err - } - pointYValues[i] = pointYValue + pointYValues, err := vm.Memory.ResolveAsBigInt3(pointYAddr) + if err != nil { + return err } //> slope = pack(ids.slope, PRIME) @@ -662,59 +599,44 @@ func newComputeSlopeV1Hint(point0, point1 hinter.ResOperander) hinter.Hinter { //> y1 = pack(ids.point1.y, PRIME) //> value = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P) - point0Addr, err := point0.GetAddress(vm) + point0XAddr, err := point0.GetAddress(vm) if err != nil { return err } - point0MemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(point0Addr, int16(6)) + + point1XAddr, err := point1.GetAddress(vm) if err != nil { return err } - point1Addr, err := point1.GetAddress(vm) + point0YAddr, err := point0XAddr.AddOffset(3) if err != nil { return err } - point1MemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(point1Addr, int16(6)) + + point1YAddr, err := point1XAddr.AddOffset(3) if err != nil { return err } - // [x.d0, x.d1, x.d2] - var point0XValues [3]*fp.Element - // [y.d0, y.d1, y.d2] - var point0YValues [3]*fp.Element - // [x.d0, x.d1, x.d2] - var point1XValues [3]*fp.Element - // [y.d0, y.d1, y.d2] - var point1YValues [3]*fp.Element - - for i := 0; i < 3; i++ { - point0XValue, err := point0MemoryValues[i].FieldElement() - if err != nil { - return err - } - point0XValues[i] = point0XValue + point0XValues, err := vm.Memory.ResolveAsBigInt3(point0XAddr) + if err != nil { + return err + } - point1XValue, err := point1MemoryValues[i].FieldElement() - if err != nil { - return err - } - point1XValues[i] = point1XValue + point1XValues, err := vm.Memory.ResolveAsBigInt3(point1XAddr) + if err != nil { + return err } - for i := 3; i < 6; i++ { - point0YValue, err := point0MemoryValues[i].FieldElement() - if err != nil { - return err - } - point0YValues[i-3] = point0YValue + point0YValues, err := vm.Memory.ResolveAsBigInt3(point0YAddr) + if err != nil { + return err + } - point1YValue, err := point1MemoryValues[i].FieldElement() - if err != nil { - return err - } - point1YValues[i-3] = point1YValue + point1YValues, err := vm.Memory.ResolveAsBigInt3(point1YAddr) + if err != nil { + return err } //> x0 = pack(ids.point0.x, PRIME) diff --git a/pkg/hintrunner/zero/zerohint_signature.go b/pkg/hintrunner/zero/zerohint_signature.go index 378f4718..f689a1e1 100644 --- a/pkg/hintrunner/zero/zerohint_signature.go +++ b/pkg/hintrunner/zero/zerohint_signature.go @@ -8,7 +8,7 @@ import ( secp_utils "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/utils" VM "github.com/NethermindEth/cairo-vm-go/pkg/vm" "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" - "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" + mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -32,30 +32,20 @@ func newVerifyZeroHint(val, q hinter.ResOperander) hinter.Hinter { //> ids.q = q % PRIME //> from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack - secPBig, ok := secp_utils.GetSecPBig() - if !ok { - return fmt.Errorf("GetSecPBig failed") - } valAddr, err := val.GetAddress(vm) if err != nil { return err } - valMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(valAddr, int16(3)) + valValues, err := vm.Memory.ResolveAsBigInt3(valAddr) if err != nil { return err } - // [d0, d1, d2] - var valValues [3]*fp.Element - - for i := 0; i < 3; i++ { - valValue, err := valMemoryValues[i].FieldElement() - if err != nil { - return err - } - valValues[i] = valValue + secPBig, ok := secp_utils.GetSecPBig() + if !ok { + return fmt.Errorf("GetSecPBig failed") } //> q, r = divmod(pack(ids.val, PRIME), SECP_P) @@ -78,7 +68,7 @@ func newVerifyZeroHint(val, q hinter.ResOperander) hinter.Hinter { if err != nil { return err } - qMv := memory.MemoryValueFromFieldElement(qFelt) + qMv := mem.MemoryValueFromFieldElement(qFelt) return vm.Memory.WriteToAddress(&qAddr, &qMv) }, } @@ -177,10 +167,6 @@ func newGetPointFromXHint(xCube, v hinter.ResOperander) hinter.Hinter { return err } - xCubeMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(xCubeAddr, int16(3)) - if err != nil { - return err - } v, err := hinter.ResolveAsFelt(vm, v) if err != nil { return err @@ -189,14 +175,11 @@ func newGetPointFromXHint(xCube, v hinter.ResOperander) hinter.Hinter { //> from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack secpBig, _ := secp_utils.GetSecPBig() - //> x_cube_int = pack(ids.x_cube, PRIME) % SECP_P - var xCubeValues [3]*fp.Element - for i := 0; i < 3; i++ { - xCubeValues[i], err = xCubeMemoryValues[i].FieldElement() - if err != nil { - return err - } + xCubeValues, err := vm.Memory.ResolveAsBigInt3(xCubeAddr) + if err != nil { + return err } + xCubeIntBig, err := secp_utils.SecPPacked(xCubeValues) if err != nil { return err @@ -334,35 +317,20 @@ func newDivModNPackedDivmodV1Hint(a, b hinter.ResOperander) hinter.Hinter { if err != nil { return err } - aMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(aAddr, int16(3)) - if err != nil { - return err - } bAddr, err := b.GetAddress(vm) if err != nil { return err } - bMemoryValues, err := vm.Memory.GetConsecutiveMemoryValues(bAddr, int16(3)) + + aValues, err := vm.Memory.ResolveAsBigInt3(aAddr) if err != nil { return err } - var aValues [3]*fp.Element - var bValues [3]*fp.Element - - for i := 0; i < 3; i++ { - aValue, err := aMemoryValues[i].FieldElement() - if err != nil { - return err - } - aValues[i] = aValue - - bValue, err := bMemoryValues[i].FieldElement() - if err != nil { - return err - } - bValues[i] = bValue + bValues, err := vm.Memory.ResolveAsBigInt3(bAddr) + if err != nil { + return err } //> a = pack(ids.a, PRIME) diff --git a/pkg/vm/memory/memory_value.go b/pkg/vm/memory/memory_value.go index 14501586..01e723a1 100644 --- a/pkg/vm/memory/memory_value.go +++ b/pkg/vm/memory/memory_value.go @@ -331,3 +331,21 @@ func (memory *Memory) GetConsecutiveMemoryValues(addr MemoryAddress, size int16) } return values, nil } + +func (memory *Memory) ResolveAsBigInt3(valAddr MemoryAddress) ([3]*f.Element, error) { + valMemoryValues, err := memory.GetConsecutiveMemoryValues(valAddr, int16(3)) + if err != nil { + return [3]*f.Element{}, err + } + + var valValues [3]*f.Element + for i := 0; i < 3; i++ { + valValue, err := valMemoryValues[i].FieldElement() + if err != nil { + return [3]*f.Element{}, err + } + valValues[i] = valValue + } + + return valValues, nil +}