Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing dict functionalities #680

Merged
merged 19 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/cairo_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func TestCairoFiles(t *testing.T) {
{"./cairo_zero_file_tests/", true},
{"./builtin_tests/", true},
// {"./cairo_1_programs/", false},
// {"./cairo_1_programs/dict_non_squashed", false},
}

// filter is for debugging purposes
Expand Down
24 changes: 17 additions & 7 deletions pkg/hintrunner/core/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,8 @@ func (hint *Felt252DictEntryInit) Execute(vm *VM.VirtualMachine, ctx *hinter.Hin

prevValue, err := ctx.DictionaryManager.At(dictPtr, key)
if err != nil {
return fmt.Errorf("get dictionary entry: %w", err)
mv := mem.MemoryValueFromFieldElement(&utils.FeltZero)
prevValue = &mv
MaksymMalicki marked this conversation as resolved.
Show resolved Hide resolved
}
if prevValue == nil {
mv := mem.EmptyMemoryValueAsFelt()
Expand Down Expand Up @@ -1237,7 +1238,8 @@ func (hint *InitSquashData) Execute(vm *VM.VirtualMachine, ctx *hinter.HintRunne
// todo(rodro): Don't know if it could be called multiple times, or
err := hinter.InitializeSquashedDictionaryManager(ctx)
if err != nil {
return err
ctx.SquashedDictionaryManager = hinter.SquashedDictionaryManager{}
_ = hinter.InitializeSquashedDictionaryManager(ctx)
MaksymMalicki marked this conversation as resolved.
Show resolved Hide resolved
}

dictAccessPtr, err := hinter.ResolveAsAddress(vm, hint.DictAccesses)
Expand Down Expand Up @@ -1272,7 +1274,7 @@ func (hint *InitSquashData) Execute(vm *VM.VirtualMachine, ctx *hinter.HintRunne

// sort the keys in descending order
sort.Slice(ctx.SquashedDictionaryManager.Keys, func(i, j int) bool {
return ctx.SquashedDictionaryManager.Keys[i].Cmp(&ctx.SquashedDictionaryManager.Keys[j]) < 0
return ctx.SquashedDictionaryManager.Keys[i].Cmp(&ctx.SquashedDictionaryManager.Keys[j]) > 0
})

// if the first key is bigger than 2^128, signal it
Expand Down Expand Up @@ -1401,11 +1403,15 @@ func (hint *ShouldContinueSquashLoop) Execute(vm *VM.VirtualMachine, ctx *hinter
}

var shouldContinueLoop f.Element
if lastIndices, err := ctx.SquashedDictionaryManager.LastIndices(); err == nil && len(lastIndices) <= 1 {
shouldContinueLoop.SetOne()
} else if err != nil {
lastIndices, err := ctx.SquashedDictionaryManager.LastIndices()
if err != nil {
return fmt.Errorf("get last indices: %w", err)
}
if len(lastIndices) > 1 {
shouldContinueLoop.SetOne()
} else {
shouldContinueLoop.SetZero()
}

mv := mem.MemoryValueFromFieldElement(&shouldContinueLoop)
return vm.Memory.WriteToAddress(&shouldContinuePtr, &mv)
Expand All @@ -1425,11 +1431,15 @@ func (hint *GetNextDictKey) Execute(vm *VM.VirtualMachine, ctx *hinter.HintRunne
return fmt.Errorf("get next key address: %w", err)
}

nextKey, err := ctx.SquashedDictionaryManager.PopKey()
_, err = ctx.SquashedDictionaryManager.PopKey()
if err != nil {
return fmt.Errorf("pop key: %w", err)
}

nextKey, err := ctx.SquashedDictionaryManager.LastKey()
if err != nil {
return fmt.Errorf("get last key: %w", err)
}
mv := mem.MemoryValueFromFieldElement(&nextKey)
return vm.Memory.WriteToAddress(&nextKeyAddr, &mv)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func gasInitialization(memory *mem.Memory) error {
return err
}

preCostTokenTypes := []TokenGasCost{PedersenToken, BitwiseToken, EcOpToken, PoseidonToken, AddModToken, MulModToken}
preCostTokenTypes := []TokenGasCost{PedersenToken, PoseidonToken, BitwiseToken, EcOpToken, AddModToken, MulModToken}
MaksymMalicki marked this conversation as resolved.
Show resolved Hide resolved

for _, token := range preCostTokenTypes {
cost, err := getTokenGasCost(token)
Expand Down
42 changes: 37 additions & 5 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,47 @@ func GetEntryCodeInstructions(function starknet.EntryPointByFunction, finalizeFo
}

ctx := &InlineCasmContext{}

gotSegmentArena := false
for _, builtin := range function.Builtins {
if builtin == builtins.SegmentArenaType {
gotSegmentArena = true
}
}

hints := make(map[uint64][]hinter.Hinter)

if gotSegmentArena {
hints[uint64(ctx.currentCodeOffset)] = []hinter.Hinter{
&core.AllocSegment{
Dst: hinter.ApCellRef(0),
},
&core.AllocSegment{
Dst: hinter.ApCellRef(1),
},
}
ctx.AddInlineCASM(
"[ap+2] = 0, ap++;",
)
ctx.AddInlineCASM(
"[ap] = [[ap-1]], ap++;",
)
ctx.AddInlineCASM(
`
[ap] = [[ap-2]+1], ap++;
[ap-1] = [[ap-3]+2];
`,
)
apOffset += 3
}

paramsSize := 0
for _, param := range paramTypes {
paramsSize += param.Size
}
apOffset += paramsSize
usedArgs := 0
var hints map[uint64][]hinter.Hinter

for _, builtin := range function.Builtins {
if offset, isBuiltin := builtinsOffsetsMap[builtin]; isBuiltin {
ctx.AddInlineCASM(
Expand All @@ -561,10 +595,8 @@ func GetEntryCodeInstructions(function starknet.EntryPointByFunction, finalizeFo
)
apOffset += 1
} else if builtin == builtins.GasBuiltinType {
hints = map[uint64][]hinter.Hinter{
uint64(ctx.currentCodeOffset): {
&core.ExternalWriteArgsToMemory{},
},
hints[uint64(ctx.currentCodeOffset)] = []hinter.Hinter{
&core.ExternalWriteArgsToMemory{},
}
ctx.AddInlineCASM("ap += 1;")
apOffset += 1
Expand Down
1 change: 1 addition & 0 deletions pkg/vm/builtins/builtin_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
AddModeType
MulModType
GasBuiltinType
SystemType
MaksymMalicki marked this conversation as resolved.
Show resolved Hide resolved
)

func Runner(name BuiltinType) memory.BuiltinRunner {
Expand Down
Loading