Skip to content

Commit

Permalink
Merge branch 'main' into 520
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 authored Aug 6, 2024
2 parents 1e6c86d + 148a8f3 commit c79b7f7
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 95 deletions.
8 changes: 4 additions & 4 deletions cmd/casm-inspect/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/NethermindEth/cairo-vm-go/pkg/disasm"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"

"github.com/urfave/cli/v2"
)
Expand All @@ -19,7 +19,7 @@ type disasmProgram struct {

rawCasm map[string]any

bytecode []*f.Element
bytecode []*fp.Element

disassembled *disasm.Program
}
Expand Down Expand Up @@ -78,13 +78,13 @@ func (p *disasmProgram) loadBytecodeStep() error {
return fmt.Errorf("%q: expected a slice of strings", p.bytecodeKey)
}

p.bytecode = make([]*f.Element, 0, len(slice))
p.bytecode = make([]*fp.Element, 0, len(slice))
for i, s := range slice {
s, ok := s.(string)
if !ok {
return fmt.Errorf("%q: expected a slice of strings, found %T", p.bytecodeKey, slice[i])
}
felt, err := new(f.Element).SetString(s)
felt, err := new(fp.Element).SetString(s)
if err != nil {
return fmt.Errorf("%q[%d]: parse %q: %w", p.bytecodeKey, i, s, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/casm-inspect/inst_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/urfave/cli/v2"
)

Expand All @@ -18,7 +18,7 @@ func (p *instFieldsProgram) Action(ctx *cli.Context) error {
return errors.New("expected 1 non-empty positional argument")
}

felt, err := new(f.Element).SetString(s)
felt, err := new(fp.Element).SetString(s)
if err != nil {
return fmt.Errorf("parsing %q argument: %w", s, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/disasm/casm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package disasm

import (
"github.com/NethermindEth/cairo-vm-go/pkg/assembler"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

type casmInstruction struct {
*assembler.Instruction

arg *f.Element
arg *fp.Element
bytecodeOffset int64
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/disasm/disasm.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package disasm

import (
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

type Program struct {
Expand All @@ -14,7 +14,7 @@ type Line struct {
}

type Config struct {
Bytecode []*f.Element
Bytecode []*fp.Element

Indent int
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/disasm/disassembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"

"github.com/NethermindEth/cairo-vm-go/pkg/assembler"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

type disassembler struct {
bytecode []*f.Element
bytecode []*fp.Element

config Config

Expand Down Expand Up @@ -161,7 +161,7 @@ func (d *disassembler) collectInstComments(inst casmInstruction) ([]string, erro
// > assert [fp+1] = [fp] * 2894802230932904970957858226476056084498485772265277359978473644908697616385 // div 5
imm := inst.arg
if inst.Res == assembler.MulOperands && !imm.IsUint64() {
dividend := f.NewElement(0)
dividend := fp.NewElement(0)
dividend.Inverse(imm)
// If divident is a very large number, then we could got it wrong.
if dividend.IsUint64() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/disasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package disasm
import (
"strconv"

f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

func feltToInt64(felt *f.Element) int64 {
func feltToInt64(felt *fp.Element) int64 {
// This would not be correct: int64(felt.Uint64)
// since signed values will reside in more than one 64-bit word.
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/hintrunner/zero/zerohint_blake.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ func newBlake2sFinalizeV3Hint(blake2sPtrEnd hinter.ResOperander) hinter.Hinter {
return err
}
*blake2sPtrEnd = temp

}

return nil
},
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/hintrunner/zero/zerohint_ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"github.com/holiman/uint256"
)

// getHighLen calculates the highest bit length of `scalar_u.d2` and `scalar_v.d2`,
// subtracts 1 from the result, and assigns it to `ids.len_hi`.
// GetHighLen hint calculates the highest bit length between `scalar_u.d2` and `scalar_v.d2`,
// subtracts 1 from the result, and assigns it to `ids.len_hi`
//
// `newGetHighLenHint` takes three operanders as arguments:
// - `len_hi`: the variable that will store the result of the bit-length calculation.
// - `scalar_u.d2`: the first scalar value.
// - `scalar_v.d2`: the second scalar value.
// `newGetHighLenHint` takes 3 operanders as arguments:
// - `len_hi`: the variable that will store the result of the bit-length calculation
// - `scalar_u.d2`: the first scalar value
// - `scalar_v.d2`: the second scalar value
func newGetHighLenHint(len_hi, scalar_u, scalar_v hinter.ResOperander) hinter.Hinter {
return &GenericZeroHinter{
Name: "GetHighLen",
Expand Down Expand Up @@ -47,10 +47,10 @@ func newGetHighLenHint(len_hi, scalar_u, scalar_v hinter.ResOperander) hinter.Hi
}

var scalarUD2 big.Int
scalarUD2 = *scalarUValues[2].BigInt(&scalarUD2)
_ = *scalarUValues[2].BigInt(&scalarUD2)

var scalarVD2 big.Int
scalarVD2 = *scalarVValues[2].BigInt(&scalarVD2)
_ = *scalarVValues[2].BigInt(&scalarVD2)

bitLenU := scalarUD2.BitLen()
bitLenV := scalarVD2.BitLen()
Expand All @@ -74,18 +74,21 @@ func createGetHighLenHinter(resolver hintReferenceResolver) (hinter.Hinter, erro
if err != nil {
return nil, err
}

scalar_u, err := resolver.GetResOperander("scalar_u")
if err != nil {
return nil, err
}

scalar_v, err := resolver.GetResOperander("scalar_v")
if err != nil {
return nil, err
}

return newGetHighLenHint(len_hi, scalar_u, scalar_v), nil
}

// BigIntToUint256 hint guesses the low part of the result uint256 variable
// BigIntToUint256 hint guesses the low part of the `x` uint256 variable
//
// `newBigIntToUint256Hint` takes 2 operanders as arguments
// - `low` is the variable that will store the low part of the uint256 result
Expand Down Expand Up @@ -221,6 +224,8 @@ func createEcNegateHinter(resolver hintReferenceResolver) (hinter.Hinter, error)
// the multiplication of `res` and `b` subtracted by `a`, by `N`. It then adds 1
// to the final result to ensure safety and prevent division by zero errors.
//
// `newDivModNSafeDivPlusOneHint` doens't take any operander as argument
//
// `DivModeNSafeDivPlusOne` assigns the result as `value` in the current scope.
func newDivModNSafeDivPlusOneHint() hinter.Hinter {
return &GenericZeroHinter{
Expand Down
15 changes: 15 additions & 0 deletions pkg/hintrunner/zero/zerohint_keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func newCairoKeccakFinalizeHint(keccakPtrEnd hinter.ResOperander) hinter.Hinter
for i := uint64(0); i < blockSizeVal; i++ {
result = append(result, padding...)
}

keccakPtrEnd, err := hinter.ResolveAsAddress(vm, keccakPtrEnd)
if err != nil {
return err
}

for i := 0; i < len(result); i++ {
resultMV := memory.MemoryValueFromUint(result[i])
err = vm.Memory.WriteToAddress(keccakPtrEnd, &resultMV)
Expand All @@ -61,6 +63,7 @@ func newCairoKeccakFinalizeHint(keccakPtrEnd hinter.ResOperander) hinter.Hinter
}
keccakPtrEnd = &keccakPtrEndIncremented
}

return nil
},
}
Expand All @@ -71,6 +74,7 @@ func createCairoKeccakFinalizeHinter(resolver hintReferenceResolver) (hinter.Hin
if err != nil {
return nil, err
}

return newCairoKeccakFinalizeHint(keccakPtrEnd), nil
}

Expand Down Expand Up @@ -149,6 +153,7 @@ func newUnsafeKeccakHint(data, length, high, low hinter.ResOperander) hinter.Hin
return err
}
}

hash := sha3.NewLegacyKeccak256()
hash.Write(keccakInput)
//> hashed = keccak(keccak_input)
Expand All @@ -160,15 +165,18 @@ func newUnsafeKeccakHint(data, length, high, low hinter.ResOperander) hinter.Hin
return err
}
hashedHighMV := memory.MemoryValueFromFieldElement(hashedHigh)

//> ids.high = int.from_bytes(hashed[:16], 'big')
err = vm.Memory.WriteToAddress(&highAddr, &hashedHighMV)
if err != nil {
return err
}

lowAddr, err := low.GetAddress(vm)
if err != nil {
return err
}

hashedLowMV := memory.MemoryValueFromFieldElement(hashedLow)
//> ids.low = int.from_bytes(hashed[16:32], 'big')
return vm.Memory.WriteToAddress(&lowAddr, &hashedLowMV)
Expand All @@ -181,18 +189,22 @@ func createUnsafeKeccakHinter(resolver hintReferenceResolver) (hinter.Hinter, er
if err != nil {
return nil, err
}

length, err := resolver.GetResOperander("length")
if err != nil {
return nil, err
}

high, err := resolver.GetResOperander("high")
if err != nil {
return nil, err
}

low, err := resolver.GetResOperander("low")
if err != nil {
return nil, err
}

return newUnsafeKeccakHint(data, length, high, low), nil
}

Expand Down Expand Up @@ -302,14 +314,17 @@ func createUnsafeKeccakFinalizeHinter(resolver hintReferenceResolver) (hinter.Hi
if err != nil {
return nil, err
}

high, err := resolver.GetResOperander("high")
if err != nil {
return nil, err
}

low, err := resolver.GetResOperander("low")
if err != nil {
return nil, err
}

return newUnsafeKeccakFinalizeHint(keccak_state, high, low), nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/parsers/starknet/starknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"strconv"

f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

type Builtin uint8
Expand Down Expand Up @@ -80,9 +80,9 @@ func (b *Builtin) UnmarshalJSON(data []byte) error {
}

type EntryPointInfo struct {
Selector f.Element `json:"selector"`
Offset f.Element `json:"offset"`
Builtins []Builtin `json:"builtins"`
Selector fp.Element `json:"selector"`
Offset fp.Element `json:"offset"`
Builtins []Builtin `json:"builtins"`
}

type EntryPointByType struct {
Expand Down Expand Up @@ -133,7 +133,7 @@ func (hints *Hints) MarshalJSON() ([]byte, error) {

type StarknetProgram struct {
// Prime is fixed to be 0x800000000000011000000000000000000000000000000000000000000000001 and wont fit in a f.Felt
Bytecode []f.Element `json:"bytecode"`
Bytecode []fp.Element `json:"bytecode"`
CompilerVersion string `json:"compiler_version"`
EntryPoints EntryPointByType `json:"entry_points_by_type"`
Hints []Hints `json:"hints" validate:"required"`
Expand Down
1 change: 0 additions & 1 deletion pkg/parsers/zero/integration_test.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/runners/cairo/cairo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cairo
1 change: 1 addition & 0 deletions pkg/runners/cairo/cairo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cairo
1 change: 1 addition & 0 deletions pkg/runners/cairo/program.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cairo
1 change: 1 addition & 0 deletions pkg/runners/cairo/program_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cairo
8 changes: 4 additions & 4 deletions pkg/runners/zero/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (

sn "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet"
"github.com/NethermindEth/cairo-vm-go/pkg/parsers/zero"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

type Program struct {
// the bytecode in string format
Bytecode []*f.Element
Bytecode []*fp.Element
// given a string it returns the pc for that function call
Entrypoints map[string]uint64
// it stores the start and end label pcs
Expand All @@ -21,9 +21,9 @@ type Program struct {

func LoadCairoZeroProgram(cairoZeroJson *zero.ZeroProgram) (*Program, error) {
// bytecode
bytecode := make([]*f.Element, len(cairoZeroJson.Data))
bytecode := make([]*fp.Element, len(cairoZeroJson.Data))
for i := range cairoZeroJson.Data {
felt, err := new(f.Element).SetString(cairoZeroJson.Data[i])
felt, err := new(fp.Element).SetString(cairoZeroJson.Data[i])
if err != nil {
return nil, fmt.Errorf(
"cannot read bytecode %s at position %d: %w",
Expand Down
11 changes: 6 additions & 5 deletions pkg/runners/zero/program_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package zero

import (
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/stretchr/testify/require"
"testing"

"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/stretchr/testify/require"

zero "github.com/NethermindEth/cairo-vm-go/pkg/parsers/zero"
)

Expand Down Expand Up @@ -33,8 +34,8 @@ func TestLoadCairoZeroProgram(t *testing.T) {
}
`)

stringToFelt := func(bytecode string) *f.Element {
felt, err := new(f.Element).SetString(bytecode)
stringToFelt := func(bytecode string) *fp.Element {
felt, err := new(fp.Element).SetString(bytecode)
if err != nil {
panic(err)
}
Expand All @@ -50,7 +51,7 @@ func TestLoadCairoZeroProgram(t *testing.T) {
require.NoError(t, err)

require.Equal(t, &Program{
Bytecode: []*f.Element{
Bytecode: []*fp.Element{
stringToFelt("0x01"),
stringToFelt("0x02"),
stringToFelt("0x03"),
Expand Down
Loading

0 comments on commit c79b7f7

Please sign in to comment.