Skip to content

Commit

Permalink
Implement is zero nondet hint + test
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Oct 2, 2023
1 parent c51e50e commit 9493264
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/hints/hint_codes/secp_hint_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}
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)"
2 changes: 2 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ 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:
return isZeroNondet(data.Ids, vm)
default:
return errors.Errorf("Unknown Hint: %s", data.Code)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/hints/secp_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ func bigintToUint256(ids IdsManager, vm *VirtualMachine, constants *map[string]l
low := xD0.Add(xD1.Mul(base)).And((lambdaworks.FeltOne().Shl(128)).Sub(lambdaworks.FeltOne()))
return ids.Insert("low", memory.NewMaybeRelocatableFelt(low), vm)
}

func isZeroNondet(ids IdsManager, vm *VirtualMachine) error {
x, err := ids.GetFelt("x", vm)
if err != nil {
return err
}
if x.IsZero() {
return vm.Segments.Memory.Insert(vm.RunContext.Ap, memory.NewMaybeRelocatableFelt(lambdaworks.FeltOne()))
}
return vm.Segments.Memory.Insert(vm.RunContext.Ap, memory.NewMaybeRelocatableFelt(lambdaworks.FeltZero()))
}
62 changes: 62 additions & 0 deletions pkg/hints/secp_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,65 @@ func TestBigIntTo256(t *testing.T) {
t.Error("Wrong/No ids.low")
}
}

func TestIsZeroTrue(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"x": {
NewMaybeRelocatableFelt(FeltZero()),
},
},
vm,
)
// Advance ap to avoid clashes
vm.RunContext.Ap.Offset += 1

hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: IS_ZERO_NONDET,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err != nil {
t.Errorf("IS_ZERO_NONDET hint failed with error %s", err)
}
// Check memory[ap]
val, err := vm.Segments.Memory.GetFelt(vm.RunContext.Ap)
if err != nil || val != FeltOne() {
t.Error("Wrong/No value inserted into ap")
}
}

func TestIsZeroFalse(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"x": {
NewMaybeRelocatableFelt(FeltOne()),
},
},
vm,
)
// Advance ap to avoid clashes
vm.RunContext.Ap.Offset += 1

hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: IS_ZERO_NONDET,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err != nil {
t.Errorf("IS_ZERO_NONDET hint failed with error %s", err)
}
// Check memory[ap]
val, err := vm.Segments.Memory.GetFelt(vm.RunContext.Ap)
if err != nil || val != FeltZero() {
t.Error("Wrong/No value inserted into ap")
}
}

0 comments on commit 9493264

Please sign in to comment.