Skip to content

Commit

Permalink
refactor: resolve BigInt3 structure (NethermindEth#354)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MoigeMatino authored Jun 4, 2024
1 parent bd2d4dc commit bb6c2f5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 187 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ 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
github.com/pmezard/go-difflib v1.0.0 // indirect
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
Expand All @@ -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
)
198 changes: 60 additions & 138 deletions pkg/hintrunner/zero/zerohint_ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit bb6c2f5

Please sign in to comment.