Skip to content

Commit

Permalink
Finished unit test for divrem hints
Browse files Browse the repository at this point in the history
  • Loading branch information
entropidelic committed Sep 19, 2023
1 parent 8dcf49c commit e568e66
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/hints/math_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func unsignedDivRem(ids IdsManager, vm *VirtualMachine) error {
divGreater := div.ToBigInt().Cmp(primeBoundDivision) == 1

if div.IsZero() || divGreater {
return errors.Errorf("Div out of range: 0 < %d <= %d", div, rcBound)
return errors.Errorf("Div out of range: 0 < %d <= %d", div.ToBigInt(), rcBound.ToBigInt())
}

q, r := value.DivRem(div)
Expand Down
196 changes: 191 additions & 5 deletions pkg/hints/math_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ func TestUnsignedDivRemHintSuccess(t *testing.T) {
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"div": {NewMaybeRelocatableFelt(FeltFromDecString("3"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("10"))},
"r": nil,
"q": nil,
"div": {NewMaybeRelocatableFelt(FeltFromDecString("7"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("15"))},
"r": {nil},
"q": {nil},
},
vm,
)
Expand Down Expand Up @@ -355,11 +355,197 @@ func TestUnsignedDivRemHintSuccess(t *testing.T) {
t.Errorf("failed to get `r`: %s", err)
}

if q != FeltFromUint64(3) {
if q != FeltFromUint64(2) {
t.Errorf("Expected q=3, got: %v", q)
}

if r != FeltFromUint64(1) {
t.Errorf("Expected r=1, got: %v", r)
}
}

func TestUnsignedDivRemHintDivZeroError(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
// This is the condition that should make the hint execution error.
"div": {NewMaybeRelocatableFelt(FeltFromDecString("0"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("15"))},
"r": {nil},
"q": {nil},
},
vm,
)
rcBuiltin := builtins.DefaultRangeCheckBuiltinRunner()
vm.BuiltinRunners = []builtins.BuiltinRunner{rcBuiltin}

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err == nil {
t.Error("UNSIGNED_DIV_REM should have failed")
}
}

func TestUnsignedDivRemHintOutOfBoundsError(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
// This is the condition that should make the hint execution error.
"div": {NewMaybeRelocatableFelt(FeltFromDecString("10633823966279327296825105735305134081"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("15"))},
"r": {nil},
"q": {nil},
},
vm,
)
rcBuiltin := builtins.DefaultRangeCheckBuiltinRunner()
vm.BuiltinRunners = []builtins.BuiltinRunner{rcBuiltin}

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err == nil {
t.Errorf("UNSIGNED_DIV_REM should have failed")
}
}

func TestSignedDivRemHintSuccess(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"div": {NewMaybeRelocatableFelt(FeltFromDecString("3"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("10"))},
"bound": {NewMaybeRelocatableFelt(FeltFromDecString("29"))},
"r": {nil},
"biased_q": {nil},
},
vm,
)
rcBuiltin := builtins.DefaultRangeCheckBuiltinRunner()
vm.BuiltinRunners = []builtins.BuiltinRunner{rcBuiltin}

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err != nil {
t.Errorf("UNSIGNED_DIV_REM hint failed with error: %s", err)
}

biasedQ, err := idsManager.GetFelt("biased_q", vm)
if err != nil {
t.Errorf("failed to get `biased_q`: %s", err)
}

r, err := idsManager.GetFelt("r", vm)
if err != nil {
t.Errorf("failed to get `r`: %s", err)
}

if biasedQ != FeltFromUint64(32) {
t.Errorf("Expected biased_q=32, got: %v", biasedQ)
}

if r != FeltFromUint64(1) {
t.Errorf("Expected r=1, got: %v", r)
}
}

func TestSignedDivRemHintDivZeroError(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"div": {NewMaybeRelocatableFelt(FeltFromDecString("0"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("10"))},
"bound": {NewMaybeRelocatableFelt(FeltFromDecString("29"))},
"r": {nil},
"biased_q": {nil},
},
vm,
)
rcBuiltin := builtins.DefaultRangeCheckBuiltinRunner()
vm.BuiltinRunners = []builtins.BuiltinRunner{rcBuiltin}

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err == nil {
t.Errorf("UNSIGNED_DIV_REM hint should have failed")
}
}

func TestSignedDivRemHintOutOfRcBoundsError(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"div": {NewMaybeRelocatableFelt(FeltFromDecString("10633823966279327296825105735305134081"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("10"))},
"bound": {NewMaybeRelocatableFelt(FeltFromDecString("29"))},
"r": {nil},
"biased_q": {nil},
},
vm,
)
rcBuiltin := builtins.DefaultRangeCheckBuiltinRunner()
vm.BuiltinRunners = []builtins.BuiltinRunner{rcBuiltin}

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err == nil {
t.Errorf("UNSIGNED_DIV_REM hint should have failed")
}
}

func TestSignedDivRemHintOutOfBoundsError(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"div": {NewMaybeRelocatableFelt(FeltFromDecString("4"))},
"value": {NewMaybeRelocatableFelt(FeltFromDecString("16"))},
"bound": {NewMaybeRelocatableFelt(FeltFromDecString("2"))},
"r": {nil},
"biased_q": {nil},
},
vm,
)
rcBuiltin := builtins.DefaultRangeCheckBuiltinRunner()
vm.BuiltinRunners = []builtins.BuiltinRunner{rcBuiltin}

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err == nil {
t.Errorf("UNSIGNED_DIV_REM hint should have failed")
}
}

0 comments on commit e568e66

Please sign in to comment.