Skip to content

Commit

Permalink
Add IS_ZERO_PACK
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Oct 2, 2023
1 parent 9493264 commit f418046
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pkg/hints/hint_codes/secp_hint_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ ids.q = q % PRIME`
const BIGINT_TO_UINT256 = "ids.low = (ids.x.d0 + ids.x.d1 * ids.BASE) & ((1 << 128) - 1)"

const IS_ZERO_NONDET = "memory[ap] = to_felt_or_relocatable(x == 0)"

const IS_ZERO_INT = "memory[ap] = int(x == 0)"

const IS_ZERO_PACK_V1 = `from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
x = pack(ids.x, PRIME) % SECP_P`

const IS_ZERO_PACK_V2 = `from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
x = pack(ids.x, PRIME) % SECP_P`
4 changes: 3 additions & 1 deletion pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return verifyZero(data.Ids, vm, execScopes, hint_utils.SECP_P_V2())
case BIGINT_TO_UINT256:
return bigintToUint256(data.Ids, vm, constants)
case IS_ZERO_NONDET:
case IS_ZERO_NONDET, IS_ZERO_INT:
return isZeroNondet(data.Ids, vm)
case IS_ZERO_PACK_V1, IS_ZERO_PACK_V2:
return isZeroPack(data.Ids, vm, execScopes)
default:
return errors.Errorf("Unknown Hint: %s", data.Code)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/hints/secp_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,16 @@ func isZeroNondet(ids IdsManager, vm *VirtualMachine) error {
}
return vm.Segments.Memory.Insert(vm.RunContext.Ap, memory.NewMaybeRelocatableFelt(lambdaworks.FeltZero()))
}

func isZeroPack(ids IdsManager, vm *VirtualMachine, scopes *ExecutionScopes) error {
xUnpacked, err := Uint384FromVarName("x", ids, vm)
if err != nil {
return err
}
x := xUnpacked.Pack86()
secpP := SECP_P()
scopes.AssignOrUpdateVariable("SECP_P", secpP)
xModP := x.Mod(&x, &secpP)
scopes.AssignOrUpdateVariable("x", *xModP)
return nil
}
32 changes: 32 additions & 0 deletions pkg/hints/secp_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,35 @@ func TestIsZeroFalse(t *testing.T) {
t.Error("Wrong/No value inserted into ap")
}
}

func TestIsZeroPack(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"x": {
NewMaybeRelocatableFelt(FeltFromUint(6)),
NewMaybeRelocatableFelt(FeltFromUint(6)),
NewMaybeRelocatableFelt(FeltFromUint(6)),
},
},
vm,
)

hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: IS_ZERO_PACK_V1,
})
scopes := NewExecutionScopes()

err := hintProcessor.ExecuteHint(vm, &hintData, nil, scopes)
if err != nil {
t.Errorf("IS_ZERO_PACK_V1 hint failed with error %s", err)
}
// Check scope vars
CheckScopeVar[big.Int]("SECP_P", SECP_P(), scopes, t)

xUnpacked := Uint384{Limbs: []Felt{FeltFromUint(6), FeltFromUint(6), FeltFromUint(6)}}
CheckScopeVar[big.Int]("x", xUnpacked.Pack86(), scopes, t)
}

0 comments on commit f418046

Please sign in to comment.