Skip to content

Commit

Permalink
Added structure for integrating builtin with the vm
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 committed Sep 20, 2024
1 parent 23129af commit e1e550f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pkg/parsers/starknet/starknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
Poseidon
SegmentArena
RangeCheck96
AddMod
MulMod
)

func (b Builtin) MarshalJSON() ([]byte, error) {
Expand All @@ -46,7 +48,10 @@ func (b Builtin) MarshalJSON() ([]byte, error) {
return []byte("poseidon"), nil
case SegmentArena:
return []byte("segment_arena"), nil

case AddMod:
return []byte("add_mod"), nil
case MulMod:
return []byte("mul_mod"), nil
}
return nil, fmt.Errorf("marshal unknown builtin: %d", uint8(b))
}
Expand Down Expand Up @@ -78,6 +83,10 @@ func (b *Builtin) UnmarshalJSON(data []byte) error {
*b = Poseidon
case "segment_arena":
*b = SegmentArena
case "add_mod":
*b = AddMod
case "mul_mod":
*b = MulMod
default:
return fmt.Errorf("unmarshal unknown builtin: %s", builtinName)
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,28 @@ func TestEcOpBuiltin(t *testing.T) {
require.NoError(t, err)
}

func TestModuloBuiltin(t *testing.T) {
// modulo is located at fp - 3
// we first write 2048 and 5 to modulo
// then we read the modulo result from add and mul
// runner := createRunner(`
// [ap] = 2048;
// [ap] = [[fp - 3]];

// [ap + 1] = 5;
// [ap + 1] = [[fp - 3] + 1];
// ret;
// `, "small", sn.AddMod, sn.MulMod)

// err := runner.Run()
// require.NoError(t, err)

// modulo, ok := runner.vm.Memory.FindSegmentWithBuiltin("add_mod")
// require.True(t, ok)

// requireEqualSegments(t, createSegment(2048, 5), modulo)
}

func createRunner(code string, layoutName string, builtins ...sn.Builtin) ZeroRunner {
program := createProgramWithBuiltins(code, builtins...)

Expand Down
4 changes: 4 additions & 0 deletions pkg/vm/builtins/builtin_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func Runner(name starknetParser.Builtin) memory.BuiltinRunner {
return &EcOp{}
case starknetParser.Poseidon:
return &Poseidon{}
case starknetParser.AddMod:
return &ModBuiltin{modBuiltinType: Add}
case starknetParser.MulMod:
return &ModBuiltin{modBuiltinType: Mul}
case starknetParser.SegmentArena:
panic("Not implemented")
default:
Expand Down
4 changes: 2 additions & 2 deletions pkg/vm/builtins/layouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ func getAllSolidityLayout() Layout {
}}
}

// TODO: Add mul_mod and add_mod builtins
// 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},
Expand All @@ -124,6 +122,8 @@ func getAllCairoLayout() Layout {
{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: &ModBuiltin{ratio: 128, wordBitLen: 1, batchSize: 96, modBuiltinType: Add}, Builtin: starknet.AddMod},
{Runner: &ModBuiltin{ratio: 256, wordBitLen: 1, batchSize: 96, modBuiltinType: Mul}, Builtin: starknet.MulMod},
}}
}

Expand Down

0 comments on commit e1e550f

Please sign in to comment.