diff --git a/pkg/hintrunner/zero/zerohint_math_test.go b/pkg/hintrunner/zero/zerohint_math_test.go index 2dbb63dac..97681fb70 100755 --- a/pkg/hintrunner/zero/zerohint_math_test.go +++ b/pkg/hintrunner/zero/zerohint_math_test.go @@ -6,8 +6,8 @@ import ( "testing" "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" - "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "github.com/NethermindEth/cairo-vm-go/pkg/utils" + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -445,8 +445,8 @@ func TestZeroHintMath(t *testing.T) { "Assert250bits": { { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: apRelative, Value: feltInt64(3042)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -459,8 +459,8 @@ func TestZeroHintMath(t *testing.T) { }, { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: fpRelative, Value: feltInt64(4938538853994)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -473,8 +473,8 @@ func TestZeroHintMath(t *testing.T) { }, { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: apRelative, Value: feltString("348329493943842849393993999999231222222222")}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -487,8 +487,8 @@ func TestZeroHintMath(t *testing.T) { }, { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: apRelative, Value: feltString("348329493943842849393124453993999999231222222222")}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -501,8 +501,8 @@ func TestZeroHintMath(t *testing.T) { }, { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: apRelative, Value: feltInt64(-233)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -578,8 +578,8 @@ func TestZeroHintMath(t *testing.T) { "SplitFelt": { { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: apRelative, Value: feltString("100000000000000000000000000000000000000")}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -592,8 +592,8 @@ func TestZeroHintMath(t *testing.T) { }, { operanders: []*hintOperander{ - {Name: "low", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "high", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "low", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "high", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, {Name: "value", Kind: apRelative, Value: &utils.FeltMax128}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { @@ -611,8 +611,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: &utils.FeltZero}, {Name: "div", Kind: apRelative, Value: &utils.FeltMax128}, {Name: "bound", Kind: apRelative, Value: &utils.Felt127}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -624,8 +624,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: &utils.FeltZero}, {Name: "div", Kind: apRelative, Value: &utils.FeltZero}, {Name: "bound", Kind: apRelative, Value: &utils.Felt127}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -637,8 +637,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: &utils.FeltZero}, {Name: "div", Kind: apRelative, Value: &utils.FeltOne}, {Name: "bound", Kind: apRelative, Value: new(fp.Element).SetBigInt(new(big.Int).Lsh(big.NewInt(1), 130))}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -650,8 +650,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: feltInt64(-6)}, {Name: "div", Kind: apRelative, Value: feltInt64(2)}, {Name: "bound", Kind: apRelative, Value: feltInt64(2)}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -663,8 +663,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: feltInt64(6)}, {Name: "div", Kind: apRelative, Value: feltInt64(2)}, {Name: "bound", Kind: apRelative, Value: feltInt64(3)}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -676,8 +676,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: feltInt64(5)}, {Name: "div", Kind: apRelative, Value: feltInt64(2)}, {Name: "bound", Kind: apRelative, Value: &utils.Felt127}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -692,8 +692,8 @@ func TestZeroHintMath(t *testing.T) { {Name: "value", Kind: apRelative, Value: feltInt64(-3)}, {Name: "div", Kind: apRelative, Value: feltInt64(2)}, {Name: "bound", Kind: apRelative, Value: &utils.Felt127}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "biased_q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "biased_q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newSignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["bound"], ctx.operanders["r"], ctx.operanders["biased_q"]) @@ -751,8 +751,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(100)}, {Name: "div", Kind: fpRelative, Value: feltUint64(6)}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) @@ -766,8 +766,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(450326666)}, {Name: "div", Kind: fpRelative, Value: feltUint64(136310839)}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) @@ -781,8 +781,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(0)}, {Name: "div", Kind: fpRelative, Value: feltUint64(10)}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) @@ -796,8 +796,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(10)}, {Name: "div", Kind: fpRelative, Value: feltUint64(0)}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) @@ -808,8 +808,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(10)}, {Name: "div", Kind: fpRelative, Value: feltString("10633823966279327296825105735305134079")}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) @@ -823,8 +823,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(10)}, {Name: "div", Kind: fpRelative, Value: feltString("10633823966279327296825105735305134080")}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) @@ -838,8 +838,8 @@ func TestZeroHintMath(t *testing.T) { operanders: []*hintOperander{ {Name: "value", Kind: fpRelative, Value: feltUint64(10)}, {Name: "div", Kind: fpRelative, Value: feltString("10633823966279327296825105735305134081")}, - {Name: "r", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 0)}, - {Name: "q", Kind: reference, Value: addrBuiltin(starknet.RangeCheck, 1)}, + {Name: "r", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 0)}, + {Name: "q", Kind: reference, Value: addrBuiltin(builtins.RangeCheckType, 1)}, }, makeHinter: func(ctx *hintTestContext) hinter.Hinter { return newUnsignedDivRemHint(ctx.operanders["value"], ctx.operanders["div"], ctx.operanders["q"], ctx.operanders["r"]) diff --git a/pkg/hintrunner/zero/zerohint_signature_test.go b/pkg/hintrunner/zero/zerohint_signature_test.go index 49ece7c67..f29417352 100644 --- a/pkg/hintrunner/zero/zerohint_signature_test.go +++ b/pkg/hintrunner/zero/zerohint_signature_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" - "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "github.com/NethermindEth/cairo-vm-go/pkg/utils" + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" "github.com/stretchr/testify/require" ) @@ -54,7 +54,7 @@ func TestVerifyZeroHint(t *testing.T) { "VerifyECDSASignature": { { operanders: []*hintOperander{ - {Name: "ecdsaPtr", Kind: reference, Value: addrBuiltin(starknet.ECDSA, 0)}, + {Name: "ecdsaPtr", Kind: reference, Value: addrBuiltin(builtins.ECDSAType, 0)}, {Name: "signature_r", Kind: apRelative, Value: feltString("3086480810278599376317923499561306189851900463386393948998357832163236918254")}, {Name: "signature_s", Kind: apRelative, Value: feltString("598673427589502599949712887611119751108407514580626464031881322743364689811")}, }, diff --git a/pkg/hintrunner/zero/zerohint_test.go b/pkg/hintrunner/zero/zerohint_test.go index 638755fbc..a71b0b7a9 100644 --- a/pkg/hintrunner/zero/zerohint_test.go +++ b/pkg/hintrunner/zero/zerohint_test.go @@ -6,7 +6,6 @@ import ( "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" runnerutil "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/utils" - "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" 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" @@ -57,7 +56,7 @@ type hintOperander struct { } type builtinReference struct { - builtin starknet.Builtin + builtin builtins.BuiltinType offset uint64 } @@ -135,7 +134,7 @@ func runHinterTests(t *testing.T, tests map[string][]hintTestCase) { offset uint64 addr memory.MemoryAddress } - builtinsAllocated := map[starknet.Builtin]allocatedBuiltin{} + builtinsAllocated := map[builtins.BuiltinType]allocatedBuiltin{} for _, o := range tc.operanders { if o.Kind != reference { continue diff --git a/pkg/hintrunner/zero/zerohint_utils_test.go b/pkg/hintrunner/zero/zerohint_utils_test.go index c4af2df1b..d817f7a13 100644 --- a/pkg/hintrunner/zero/zerohint_utils_test.go +++ b/pkg/hintrunner/zero/zerohint_utils_test.go @@ -8,8 +8,8 @@ import ( "testing" runnerutil "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/utils" - "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "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" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" "github.com/stretchr/testify/assert" @@ -30,7 +30,7 @@ func addrWithSegment(segment, offset uint64) *memory.MemoryAddress { } } -func addrBuiltin(builtin starknet.Builtin, offset uint64) *builtinReference { +func addrBuiltin(builtin builtins.BuiltinType, offset uint64) *builtinReference { return &builtinReference{ builtin: builtin, offset: offset, diff --git a/pkg/parsers/starknet/starknet.go b/pkg/parsers/starknet/starknet.go index 13868ae71..e61a7a67a 100644 --- a/pkg/parsers/starknet/starknet.go +++ b/pkg/parsers/starknet/starknet.go @@ -4,90 +4,15 @@ import ( "encoding/json" "fmt" "os" - "strconv" + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) -type Builtin uint8 - -const ( - Output Builtin = iota + 1 - RangeCheck - Pedersen - ECDSA - Keccak - Bitwise - ECOP - Poseidon - SegmentArena - RangeCheck96 -) - -func (b Builtin) MarshalJSON() ([]byte, error) { - switch b { - case Output: - return []byte("output"), nil - case RangeCheck: - return []byte("range_check"), nil - case RangeCheck96: - return []byte("range_check96"), nil - case Pedersen: - return []byte("pedersen"), nil - case ECDSA: - return []byte("ecdsa"), nil - case Keccak: - return []byte("keccak"), nil - case Bitwise: - return []byte("bitwise"), nil - case ECOP: - return []byte("ec_op"), nil - case Poseidon: - return []byte("poseidon"), nil - case SegmentArena: - return []byte("segment_arena"), nil - - } - return nil, fmt.Errorf("marshal unknown builtin: %d", uint8(b)) -} - -func (b *Builtin) UnmarshalJSON(data []byte) error { - builtinName, err := strconv.Unquote(string(data)) - if err != nil { - return fmt.Errorf("unmarshal builtin: %w", err) - } - - switch builtinName { - case "output": - *b = Output - case "range_check": - *b = RangeCheck - case "range_check96": - *b = RangeCheck96 - case "pedersen": - *b = Pedersen - case "ecdsa": - *b = ECDSA - case "keccak": - *b = Keccak - case "bitwise": - *b = Bitwise - case "ec_op": - *b = ECOP - case "poseidon": - *b = Poseidon - case "segment_arena": - *b = SegmentArena - default: - return fmt.Errorf("unmarshal unknown builtin: %s", builtinName) - } - return nil -} - type EntryPointInfo struct { - Selector fp.Element `json:"selector"` - Offset fp.Element `json:"offset"` - Builtins []Builtin `json:"builtins"` + Selector fp.Element `json:"selector"` + Offset fp.Element `json:"offset"` + Builtins []builtins.BuiltinType `json:"builtins"` } type EntryPointByType struct { diff --git a/pkg/parsers/starknet/starknet_test.go b/pkg/parsers/starknet/starknet_test.go index 46dc33e84..9e696e04d 100644 --- a/pkg/parsers/starknet/starknet_test.go +++ b/pkg/parsers/starknet/starknet_test.go @@ -6,6 +6,8 @@ import ( "github.com/go-playground/validator/v10" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" ) func TestCompilerVersionParsing(t *testing.T) { @@ -93,7 +95,7 @@ func TestEntryPointInfoParsing(t *testing.T) { assert.Len(t, entryPointInfo.Builtins, 9) for i := 0; i < 9; i++ { - assert.Equal(t, Builtin(i+1), entryPointInfo.Builtins[i]) + assert.Equal(t, builtins.BuiltinType(i+1), entryPointInfo.Builtins[i]) } } diff --git a/pkg/parsers/zero/zero.go b/pkg/parsers/zero/zero.go index 925350266..1e2305e88 100644 --- a/pkg/parsers/zero/zero.go +++ b/pkg/parsers/zero/zero.go @@ -4,7 +4,7 @@ import ( "encoding/json" "os" - starknetParser "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" ) type FlowTrackingData struct { @@ -68,16 +68,16 @@ type AttributeScope struct { } type ZeroProgram struct { - Prime string `json:"prime"` - Data []string `json:"data"` - Builtins []starknetParser.Builtin `json:"builtins"` - Hints map[string][]Hint `json:"hints"` - CompilerVersion string `json:"version"` - MainScope string `json:"main_scope"` - Identifiers map[string]*Identifier `json:"identifiers"` - ReferenceManager ReferenceManager `json:"reference_manager"` - Attributes []AttributeScope `json:"attributes"` - DebugInfo DebugInfo `json:"debug_info"` + Prime string `json:"prime"` + Data []string `json:"data"` + Builtins []builtins.BuiltinType `json:"builtins"` + Hints map[string][]Hint `json:"hints"` + CompilerVersion string `json:"version"` + MainScope string `json:"main_scope"` + Identifiers map[string]*Identifier `json:"identifiers"` + ReferenceManager ReferenceManager `json:"reference_manager"` + Attributes []AttributeScope `json:"attributes"` + DebugInfo DebugInfo `json:"debug_info"` } type Identifier struct { diff --git a/pkg/parsers/zero/zero_test.go b/pkg/parsers/zero/zero_test.go index 0db00ef56..527e4dbe3 100644 --- a/pkg/parsers/zero/zero_test.go +++ b/pkg/parsers/zero/zero_test.go @@ -3,7 +3,7 @@ package zero import ( "testing" - starknetParser "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" "github.com/stretchr/testify/require" ) @@ -69,10 +69,10 @@ func TestBuiltins(t *testing.T) { require.Equal(t, &ZeroProgram{ - Builtins: []starknetParser.Builtin{ - starknetParser.Output, - starknetParser.RangeCheck, - starknetParser.Bitwise, + Builtins: []builtins.BuiltinType{ + builtins.OutputType, + builtins.RangeCheckType, + builtins.BitwiseType, }, }, zeroProgram, diff --git a/pkg/runner/air_input.go b/pkg/runner/air_input.go index 17ae40a08..767452a0f 100644 --- a/pkg/runner/air_input.go +++ b/pkg/runner/air_input.go @@ -62,31 +62,31 @@ func (runner *ZeroRunner) GetAirPrivateInput(tracePath, memoryPath string) (AirP if ok { // some checks might be missing here switch builtinName { - case "range_check": + case builtins.RangeCheckName: { airPrivateInput.RangeCheck = bRunner.Runner.(*builtins.RangeCheck).GetAirPrivateInput(builtinSegment) } - case "bitwise": + case builtins.BitwiseName: { airPrivateInput.Bitwise = bRunner.Runner.(*builtins.Bitwise).GetAirPrivateInput(builtinSegment) } - case "poseidon": + case builtins.PoseidonName: { airPrivateInput.Poseidon = bRunner.Runner.(*builtins.Poseidon).GetAirPrivateInput(builtinSegment) } - case "pedersen": + case builtins.PedersenName: { airPrivateInput.Pedersen = bRunner.Runner.(*builtins.Pedersen).GetAirPrivateInput(builtinSegment) } - case "ec_op": + case builtins.EcOpName: { airPrivateInput.EcOp = bRunner.Runner.(*builtins.EcOp).GetAirPrivateInput(builtinSegment) } - case "keccak": + case builtins.KeccakName: { airPrivateInput.Keccak = bRunner.Runner.(*builtins.Keccak).GetAirPrivateInput(builtinSegment) } - case "ecdsa": + case builtins.ECDSAName: { ecdsaAirPrivateInput, err := bRunner.Runner.(*builtins.ECDSA).GetAirPrivateInput(builtinSegment) if err != nil { diff --git a/pkg/runner/program.go b/pkg/runner/program.go index c84a0bc12..c00835c40 100644 --- a/pkg/runner/program.go +++ b/pkg/runner/program.go @@ -3,8 +3,8 @@ package runner import ( "fmt" - sn "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "github.com/NethermindEth/cairo-vm-go/pkg/parsers/zero" + "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -16,7 +16,7 @@ type ZeroProgram struct { // it stores the start and end label pcs Labels map[string]uint64 // builtins - Builtins []sn.Builtin + Builtins []builtins.BuiltinType } type CairoProgram struct{} diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 391c39498..b18463079 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -6,7 +6,6 @@ import ( "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner" "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" - "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "github.com/NethermindEth/cairo-vm-go/pkg/utils" "github.com/NethermindEth/cairo-vm-go/pkg/vm" "github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins" @@ -186,7 +185,7 @@ func (runner *ZeroRunner) initializeEntrypoint( } func (runner *ZeroRunner) initializeBuiltins(memory *mem.Memory) ([]mem.MemoryValue, error) { - builtinsSet := make(map[starknet.Builtin]bool) + builtinsSet := make(map[builtins.BuiltinType]bool) for _, bRunner := range runner.layout.Builtins { builtinsSet[bRunner.Builtin] = true } @@ -312,7 +311,7 @@ func (runner *ZeroRunner) checkRangeCheckUsage() error { rcMin, rcMax := runner.getPermRangeCheckLimits() var rcUnitsUsedByBuiltins uint64 for _, builtin := range runner.program.Builtins { - if builtin == starknet.RangeCheck { + if builtin == builtins.RangeCheckType { for _, layoutBuiltin := range runner.layout.Builtins { if builtin == layoutBuiltin.Builtin { rangeCheckRunner, ok := layoutBuiltin.Runner.(*builtins.RangeCheck) @@ -341,7 +340,7 @@ func (runner *ZeroRunner) getPermRangeCheckLimits() (uint16, uint16) { rcMin, rcMax := runner.vm.RcLimitsMin, runner.vm.RcLimitsMax for _, builtin := range runner.program.Builtins { - if builtin == starknet.RangeCheck { + if builtin == builtins.RangeCheckType { bRunner := builtins.Runner(builtin) rangeCheckRunner, _ := bRunner.(*builtins.RangeCheck) rangeCheckSegment, ok := runner.vm.Memory.FindSegmentWithBuiltin(rangeCheckRunner.String()) @@ -406,7 +405,7 @@ func (runner *ZeroRunner) Output() []*fp.Element { } output := []*fp.Element{} - outputSegment, ok := runner.vm.Memory.FindSegmentWithBuiltin("output") + outputSegment, ok := runner.vm.Memory.FindSegmentWithBuiltin(builtins.OutputName) if !ok { return output } diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index f6953ea15..d7b3d99cb 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -7,8 +7,8 @@ import ( "github.com/NethermindEth/cairo-vm-go/pkg/assembler" "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" - sn "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "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" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" pedersenhash "github.com/consensys/gnark-crypto/ecc/stark-curve/pedersen-hash" @@ -186,12 +186,12 @@ func TestBitwiseBuiltin(t *testing.T) { [ap + 1] = 9; [ap + 2] = 15; ret; - `, "starknet_with_keccak", sn.Bitwise) + `, "starknet_with_keccak", builtins.BitwiseType) err := runner.Run() require.NoError(t, err) - bitwise, ok := runner.vm.Memory.FindSegmentWithBuiltin("bitwise") + bitwise, ok := runner.vm.Memory.FindSegmentWithBuiltin(builtins.BitwiseName) require.True(t, ok) requireEqualSegments(t, createSegment(14, 7, 6, 9, 15), bitwise) @@ -202,7 +202,7 @@ func TestBitwiseBuiltinError(t *testing.T) { runner := createRunner(` [ap] = [[fp - 3]]; ret; - `, "starknet_with_keccak", sn.Bitwise) + `, "starknet_with_keccak", builtins.BitwiseType) err := runner.Run() require.ErrorContains(t, err, "cannot infer value") @@ -211,7 +211,7 @@ func TestBitwiseBuiltinError(t *testing.T) { runner = createRunner(` [ap] = [[fp - 3] + 1]; ret; - `, "starknet_with_keccak", sn.Bitwise) + `, "starknet_with_keccak", builtins.BitwiseType) err = runner.Run() require.ErrorContains(t, err, "cannot infer value") @@ -219,7 +219,7 @@ func TestBitwiseBuiltinError(t *testing.T) { runner = createRunner(` [ap] = [[fp - 3] + 2]; ret; - `, "starknet_with_keccak", sn.Bitwise) + `, "starknet_with_keccak", builtins.BitwiseType) err = runner.Run() require.ErrorContains(t, err, "input value at offset 0 is unknown") @@ -233,7 +233,7 @@ func TestOutputBuiltin(t *testing.T) { [ap + 1] = 7; [ap + 1] = [[fp - 3] + 1]; ret; - `, "small", sn.Output) + `, "small", builtins.OutputType) err := runner.Run() require.NoError(t, err) @@ -263,11 +263,11 @@ func TestPedersenBuiltin(t *testing.T) { ret; `, val1.Text(10), val2.Text(10), val3.Text(10)) - runner := createRunner(code, "small", sn.Pedersen) + runner := createRunner(code, "small", builtins.PedersenType) err := runner.Run() require.NoError(t, err) - pedersen, ok := runner.vm.Memory.FindSegmentWithBuiltin("pedersen") + pedersen, ok := runner.vm.Memory.FindSegmentWithBuiltin(builtins.PedersenName) require.True(t, ok) requireEqualSegments(t, createSegment(&val1, &val2, &val3), pedersen) } @@ -276,14 +276,14 @@ func TestPedersenBuiltinError(t *testing.T) { runner := createRunner(` [ap] = [[fp - 3]]; ret; - `, "small", sn.Pedersen) + `, "small", builtins.PedersenType) err := runner.Run() require.ErrorContains(t, err, "cannot infer value") runner = createRunner(` [ap] = [[fp - 3] + 2]; ret; - `, "small", sn.Pedersen) + `, "small", builtins.PedersenType) err = runner.Run() require.ErrorContains(t, err, "input value at offset 0 is unknown") } @@ -298,7 +298,7 @@ func TestRangeCheckBuiltin(t *testing.T) { [ap + 1] = 0xffffffffffffffffffffffffffffffff; [ap + 1] = [[fp - 3] + 1]; ret; - `, "small", sn.RangeCheck) + `, "small", builtins.RangeCheckType) err := runner.Run() require.NoError(t, err) @@ -319,7 +319,7 @@ func TestRangeCheckBuiltinError(t *testing.T) { [ap] = 0x100000000000000000000000000000000; [ap] = [[fp - 3]]; ret; - `, "small", sn.RangeCheck) + `, "small", builtins.RangeCheckType) err := runner.Run() require.ErrorContains(t, err, "check write: 2**128 <") @@ -328,7 +328,7 @@ func TestRangeCheckBuiltinError(t *testing.T) { runner = createRunner(` [ap] = [[fp - 3]]; ret; - `, "small", sn.RangeCheck) + `, "small", builtins.RangeCheckType) err = runner.Run() require.ErrorContains(t, err, "cannot infer value") @@ -344,12 +344,12 @@ func TestRangeCheck96Builtin(t *testing.T) { [ap + 1] = 0xffffffffffffffffffffffff; [ap + 1] = [[fp - 3] + 1]; ret; - `, "all_cairo", sn.RangeCheck96) + `, "all_cairo", builtins.RangeCheck96Type) err := runner.Run() require.NoError(t, err) - rangeCheck96, ok := runner.vm.Memory.FindSegmentWithBuiltin("range_check96") + rangeCheck96, ok := runner.vm.Memory.FindSegmentWithBuiltin(builtins.RangeCheck96Name) require.True(t, ok) felt := &fp.Element{} @@ -365,7 +365,7 @@ func TestRangeCheck96BuiltinError(t *testing.T) { [ap] = 0x1000000000000000000000000; [ap] = [[fp - 3]]; ret; - `, "all_cairo", sn.RangeCheck96) + `, "all_cairo", builtins.RangeCheck96Type) err := runner.Run() require.ErrorContains(t, err, "check write: 2**96 <") @@ -374,7 +374,7 @@ func TestRangeCheck96BuiltinError(t *testing.T) { runner = createRunner(` [ap] = [[fp - 3]]; ret; - `, "all_cairo", sn.RangeCheck96) + `, "all_cairo", builtins.RangeCheck96Type) err = runner.Run() require.ErrorContains(t, err, "cannot infer value") @@ -404,13 +404,13 @@ func TestEcOpBuiltin(t *testing.T) { [ap + 5] = 108925483682366235368969256555281508851459278989259552980345066351008608800; [ap + 6] = 1592365885972480102953613056006596671718206128324372995731808913669237079419; ret; - `, "starknet_with_keccak", sn.ECOP) + `, "starknet_with_keccak", builtins.ECOPType) err := runner.Run() require.NoError(t, err) } -func createRunner(code string, layoutName string, builtins ...sn.Builtin) ZeroRunner { +func createRunner(code string, layoutName string, builtins ...builtins.BuiltinType) ZeroRunner { program := createProgramWithBuiltins(code, builtins...) hints := make(map[uint64][]hinter.Hinter) @@ -476,7 +476,7 @@ func createProgram(code string) *ZeroProgram { return &program } -func createProgramWithBuiltins(code string, builtins ...sn.Builtin) *ZeroProgram { +func createProgramWithBuiltins(code string, builtins ...builtins.BuiltinType) *ZeroProgram { program := createProgram(code) program.Builtins = builtins return program diff --git a/pkg/vm/builtins/bitwise.go b/pkg/vm/builtins/bitwise.go index b42567ae9..0906b0921 100644 --- a/pkg/vm/builtins/bitwise.go +++ b/pkg/vm/builtins/bitwise.go @@ -10,11 +10,12 @@ import ( "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) -const BitwiseName = "bitwise" - -const cellsPerBitwise = 5 -const inputCellsPerBitwise = 2 -const instancesPerComponentBitwise = 1 +const ( + BitwiseName = "bitwise" + cellsPerBitwise = 5 + inputCellsPerBitwise = 2 + instancesPerComponentBitwise = 1 +) type Bitwise struct { ratio uint64 diff --git a/pkg/vm/builtins/builtin_runner.go b/pkg/vm/builtins/builtin_runner.go index 77b74fa00..77aa50c9a 100644 --- a/pkg/vm/builtins/builtin_runner.go +++ b/pkg/vm/builtins/builtin_runner.go @@ -3,33 +3,48 @@ package builtins import ( "fmt" "math" + "strconv" - starknetParser "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "github.com/NethermindEth/cairo-vm-go/pkg/utils" "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" ) -func Runner(name starknetParser.Builtin) memory.BuiltinRunner { +type BuiltinType uint8 + +const ( + OutputType BuiltinType = iota + 1 + RangeCheckType + PedersenType + ECDSAType + KeccakType + BitwiseType + ECOPType + PoseidonType + SegmentArenaType + RangeCheck96Type +) + +func Runner(name BuiltinType) memory.BuiltinRunner { switch name { - case starknetParser.Output: + case OutputType: return &Output{} - case starknetParser.RangeCheck: + case RangeCheckType: return &RangeCheck{0, 8} - case starknetParser.RangeCheck96: + case RangeCheck96Type: return &RangeCheck{0, 6} - case starknetParser.Pedersen: + case PedersenType: return &Pedersen{} - case starknetParser.ECDSA: + case ECDSAType: return &ECDSA{} - case starknetParser.Keccak: + case KeccakType: return &Keccak{} - case starknetParser.Bitwise: + case BitwiseType: return &Bitwise{} - case starknetParser.ECOP: + case ECOPType: return &EcOp{} - case starknetParser.Poseidon: + case PoseidonType: return &Poseidon{} - case starknetParser.SegmentArena: + case SegmentArenaType: panic("Not implemented") default: panic("Unknown builtin") @@ -65,3 +80,63 @@ func getBuiltinAllocatedSize(segmentUsedSize uint64, vmCurrentStep uint64, ratio } return allocatedInstances * cellsPerInstance, nil } + +func (b BuiltinType) MarshalJSON() ([]byte, error) { + switch b { + case OutputType: + return []byte(OutputName), nil + case RangeCheckType: + return []byte(RangeCheckName), nil + case RangeCheck96Type: + return []byte(RangeCheck96Name), nil + case PedersenType: + return []byte(PedersenName), nil + case ECDSAType: + return []byte(ECDSAName), nil + case KeccakType: + return []byte(KeccakName), nil + case BitwiseType: + return []byte(BitwiseName), nil + case ECOPType: + return []byte(EcOpName), nil + case PoseidonType: + return []byte(PoseidonName), nil + case SegmentArenaType: + return []byte(SegmentArenaName), nil + + } + return nil, fmt.Errorf("marshal unknown builtin: %d", uint8(b)) +} + +func (b *BuiltinType) UnmarshalJSON(data []byte) error { + builtinName, err := strconv.Unquote(string(data)) + if err != nil { + return fmt.Errorf("unmarshal builtin: %w", err) + } + + switch builtinName { + case OutputName: + *b = OutputType + case RangeCheckName: + *b = RangeCheckType + case RangeCheck96Name: + *b = RangeCheck96Type + case PedersenName: + *b = PedersenType + case ECDSAName: + *b = ECDSAType + case KeccakName: + *b = KeccakType + case BitwiseName: + *b = BitwiseType + case EcOpName: + *b = ECOPType + case PoseidonName: + *b = PoseidonType + case SegmentArenaName: + *b = SegmentArenaType + default: + return fmt.Errorf("unmarshal unknown builtin: %s", builtinName) + } + return nil +} diff --git a/pkg/vm/builtins/ecdsa.go b/pkg/vm/builtins/ecdsa.go index 8c65be11f..7aae622bb 100644 --- a/pkg/vm/builtins/ecdsa.go +++ b/pkg/vm/builtins/ecdsa.go @@ -11,11 +11,12 @@ import ( "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) -const ECDSAName = "ecdsa" -const inputCellsPerECDSA = 2 -const cellsPerECDSA = 2 - -const instancesPerComponentECDSA = 1 +const ( + ECDSAName = "ecdsa" + inputCellsPerECDSA = 2 + cellsPerECDSA = 2 + instancesPerComponentECDSA = 1 +) type ECDSA struct { Signatures map[uint64]ecdsa.Signature diff --git a/pkg/vm/builtins/ecop.go b/pkg/vm/builtins/ecop.go index 592a6c4da..6d28d1108 100644 --- a/pkg/vm/builtins/ecop.go +++ b/pkg/vm/builtins/ecop.go @@ -12,10 +12,12 @@ import ( "github.com/holiman/uint256" ) -const EcOpName = "ec_op" -const cellsPerEcOp = 7 -const inputCellsPerEcOp = 5 -const instancesPerComponentEcOp = 1 +const ( + EcOpName = "ec_op" + cellsPerEcOp = 7 + inputCellsPerEcOp = 5 + instancesPerComponentEcOp = 1 +) var feltThree fp.Element = fp.Element( []uint64{ diff --git a/pkg/vm/builtins/keccak.go b/pkg/vm/builtins/keccak.go index c7109230f..e7dcb7767 100644 --- a/pkg/vm/builtins/keccak.go +++ b/pkg/vm/builtins/keccak.go @@ -19,10 +19,12 @@ import ( // It's useful to give users options to use Keccak just as Rust VM does it with it's keccak.cairo as library. // -const KeccakName = "keccak" -const cellsPerKeccak = 16 -const inputCellsPerKeccak = 8 -const instancesPerComponentKeccak = 16 +const ( + KeccakName = "keccak" + cellsPerKeccak = 16 + inputCellsPerKeccak = 8 + instancesPerComponentKeccak = 16 +) type Keccak struct { ratio uint64 diff --git a/pkg/vm/builtins/layouts.go b/pkg/vm/builtins/layouts.go index 661598b48..19ff71893 100644 --- a/pkg/vm/builtins/layouts.go +++ b/pkg/vm/builtins/layouts.go @@ -3,7 +3,6 @@ package builtins import ( "fmt" - "github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet" "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -12,7 +11,7 @@ type LayoutBuiltin struct { // Runner for the builtin Runner memory.BuiltinRunner // Builtin id from starknet parser - Builtin starknet.Builtin + Builtin BuiltinType } type Layout struct { @@ -30,84 +29,84 @@ func getPlainLayout() Layout { func getSmallLayout() Layout { return Layout{Name: "small", RcUnits: 16, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 8}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &ECDSA{ratio: 512}, Builtin: starknet.ECDSA}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 8}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &ECDSA{ratio: 512}, Builtin: ECDSAType}, }} } func getDexLayout() Layout { return Layout{Name: "dex", RcUnits: 4, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 8}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &ECDSA{ratio: 512}, Builtin: starknet.ECDSA}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 8}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &ECDSA{ratio: 512}, Builtin: ECDSAType}, }} } func getRecursiveLayout() Layout { return Layout{Name: "recursive", RcUnits: 4, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 128}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &Bitwise{ratio: 8}, Builtin: starknet.Bitwise}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 128}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &Bitwise{ratio: 8}, Builtin: BitwiseType}, }} } func getStarknetLayout() Layout { return Layout{Name: "starknet", RcUnits: 4, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 32}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &ECDSA{ratio: 2048}, Builtin: starknet.ECDSA}, - {Runner: &Bitwise{ratio: 64}, Builtin: starknet.Bitwise}, - {Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP}, - {Runner: &Poseidon{ratio: 32, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 32}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &ECDSA{ratio: 2048}, Builtin: ECDSAType}, + {Runner: &Bitwise{ratio: 64}, Builtin: BitwiseType}, + {Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: ECOPType}, + {Runner: &Poseidon{ratio: 32, cache: make(map[uint64]fp.Element)}, Builtin: PoseidonType}, }} } func getStarknetWithKeccakLayout() Layout { return Layout{Name: "starknet_with_keccak", RcUnits: 4, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 32}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &ECDSA{ratio: 2048}, Builtin: starknet.ECDSA}, - {Runner: &Bitwise{ratio: 64}, Builtin: starknet.Bitwise}, - {Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP}, - {Runner: &Keccak{ratio: 2048, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Keccak}, - {Runner: &Poseidon{ratio: 32, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 32}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &ECDSA{ratio: 2048}, Builtin: ECDSAType}, + {Runner: &Bitwise{ratio: 64}, Builtin: BitwiseType}, + {Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: ECOPType}, + {Runner: &Keccak{ratio: 2048, cache: make(map[uint64]fp.Element)}, Builtin: KeccakType}, + {Runner: &Poseidon{ratio: 32, cache: make(map[uint64]fp.Element)}, Builtin: PoseidonType}, }} } func getRecursiveLargeOutputLayout() Layout { return Layout{Name: "recursive_large_output", RcUnits: 4, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 128}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &Bitwise{ratio: 8}, Builtin: starknet.Bitwise}, - {Runner: &Poseidon{ratio: 8, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 128}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &Bitwise{ratio: 8}, Builtin: BitwiseType}, + {Runner: &Poseidon{ratio: 8, cache: make(map[uint64]fp.Element)}, Builtin: PoseidonType}, }} } func getRecursiveWithPoseidonLayout() Layout { return Layout{Name: "recursive_with_poseidon", RcUnits: 4, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 256}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &Bitwise{ratio: 16}, Builtin: starknet.Bitwise}, - {Runner: &Poseidon{ratio: 64, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 256}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &Bitwise{ratio: 16}, Builtin: BitwiseType}, + {Runner: &Poseidon{ratio: 64, cache: make(map[uint64]fp.Element)}, Builtin: PoseidonType}, }} } func getAllSolidityLayout() Layout { return Layout{Name: "recursive_with_poseidon", RcUnits: 8, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 8}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &ECDSA{ratio: 512}, Builtin: starknet.ECDSA}, - {Runner: &Bitwise{ratio: 256}, Builtin: starknet.Bitwise}, - {Runner: &EcOp{ratio: 256, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 8}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &ECDSA{ratio: 512}, Builtin: ECDSAType}, + {Runner: &Bitwise{ratio: 256}, Builtin: BitwiseType}, + {Runner: &EcOp{ratio: 256, cache: make(map[uint64]fp.Element)}, Builtin: ECOPType}, }} } @@ -115,15 +114,15 @@ func getAllSolidityLayout() Layout { // refer: https://github.com/lambdaclass/cairo-vm/blob/main/vm/src/types/instance_definitions/builtins_instance_def.rs#L168 func getAllCairoLayout() Layout { return Layout{Name: "all_cairo", RcUnits: 8, Builtins: []LayoutBuiltin{ - {Runner: &Output{}, Builtin: starknet.Output}, - {Runner: &Pedersen{ratio: 256}, Builtin: starknet.Pedersen}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck}, - {Runner: &ECDSA{ratio: 2048}, Builtin: starknet.ECDSA}, - {Runner: &Bitwise{ratio: 16}, Builtin: starknet.Bitwise}, - {Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP}, - {Runner: &Keccak{ratio: 2048, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Keccak}, - {Runner: &Poseidon{ratio: 256, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon}, - {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 6}, Builtin: starknet.RangeCheck96}, + {Runner: &Output{}, Builtin: OutputType}, + {Runner: &Pedersen{ratio: 256}, Builtin: PedersenType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: RangeCheckType}, + {Runner: &ECDSA{ratio: 2048}, Builtin: ECDSAType}, + {Runner: &Bitwise{ratio: 16}, Builtin: BitwiseType}, + {Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: ECOPType}, + {Runner: &Keccak{ratio: 2048, cache: make(map[uint64]fp.Element)}, Builtin: KeccakType}, + {Runner: &Poseidon{ratio: 256, cache: make(map[uint64]fp.Element)}, Builtin: PoseidonType}, + {Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 6}, Builtin: RangeCheck96Type}, }} } diff --git a/pkg/vm/builtins/pedersen.go b/pkg/vm/builtins/pedersen.go index df05b57f9..195202303 100644 --- a/pkg/vm/builtins/pedersen.go +++ b/pkg/vm/builtins/pedersen.go @@ -10,10 +10,12 @@ import ( pedersenhash "github.com/consensys/gnark-crypto/ecc/stark-curve/pedersen-hash" ) -const PedersenName = "pedersen" -const cellsPerPedersen = 3 -const inputCellsPerPedersen = 2 -const instancesPerComponentPedersen = 1 +const ( + PedersenName = "pedersen" + cellsPerPedersen = 3 + inputCellsPerPedersen = 2 + instancesPerComponentPedersen = 1 +) type Pedersen struct { ratio uint64 diff --git a/pkg/vm/builtins/range_check.go b/pkg/vm/builtins/range_check.go index 895968453..bf2bf5ab3 100644 --- a/pkg/vm/builtins/range_check.go +++ b/pkg/vm/builtins/range_check.go @@ -11,13 +11,17 @@ import ( "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) -const inputCellsPerRangeCheck = 1 -const cellsPerRangeCheck = 1 -const instancesPerComponentRangeCheck = 1 +const ( + RangeCheckName = "range_check" + RangeCheck96Name = "range_check96" + inputCellsPerRangeCheck = 1 + cellsPerRangeCheck = 1 + instancesPerComponentRangeCheck = 1 -// Each range check instance consists of RangeCheckNParts 16-bit parts. INNER_RC_BOUND_SHIFT and INNER_RC_BOUND_MASK are used to extract 16-bit parts from the field elements stored in the range check segment. -const INNER_RC_BOUND_SHIFT = 16 -const INNER_RC_BOUND_MASK = (1 << 16) - 1 + // Each range check instance consists of RangeCheckNParts 16-bit parts. INNER_RC_BOUND_SHIFT and INNER_RC_BOUND_MASK are used to extract 16-bit parts from the field elements stored in the range check segment. + INNER_RC_BOUND_SHIFT = 16 + INNER_RC_BOUND_MASK = (1 << 16) - 1 +) type RangeCheck struct { ratio uint64 @@ -57,9 +61,9 @@ func (r *RangeCheck) InferValue(segment *memory.Segment, offset uint64) error { func (r *RangeCheck) String() string { if r.RangeCheckNParts == 6 { - return "range_check96" + return RangeCheck96Name } else { - return "range_check" + return RangeCheckName } } diff --git a/pkg/vm/builtins/segment_arena.go b/pkg/vm/builtins/segment_arena.go new file mode 100644 index 000000000..ac4c27e91 --- /dev/null +++ b/pkg/vm/builtins/segment_arena.go @@ -0,0 +1,5 @@ +package builtins + +const ( + SegmentArenaName string = "segment_arena" +)