Skip to content

Commit

Permalink
Div safe test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsc2 committed Sep 27, 2023
1 parent 1d5ef6e commit 0aaf96e
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 4 deletions.
83 changes: 80 additions & 3 deletions pkg/hints/bigint_hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package hints

import (
"errors"
"fmt"
"math/big"

. "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils"
"github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks"
. "github.com/lambdaclass/cairo-vm.go/pkg/types"
. "github.com/lambdaclass/cairo-vm.go/pkg/utils"
. "github.com/lambdaclass/cairo-vm.go/pkg/vm"
"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory"
)
Expand All @@ -20,8 +22,8 @@ Implements hint:
%}
*/

func NondetBigInt3(virtual_machine VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error {
resRelloc, err := idsData.GetAddr("res", &virtual_machine)
func NondetBigInt3(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error {
resRelloc, err := idsData.GetAddr("res", &vm)
if err != nil {
return err
}
Expand All @@ -47,10 +49,85 @@ func NondetBigInt3(virtual_machine VirtualMachine, execScopes ExecutionScopes, i
arg = append(arg, *m)
}

_, loadErr := virtual_machine.Segments.LoadData(resRelloc, &arg)
_, loadErr := vm.Segments.LoadData(resRelloc, &arg)
if loadErr != nil {
return loadErr
}

return nil
}

/// Implements hint:
/// ```python
/// k = safe_div(res * y - x, p)
/// value = k if k > 0 else 0 - k
/// ids.flag = 1 if k > 0 else 0
/// ```

func SafeDivBigint(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error {
resUncast, err := execScopes.Get("res")
if err != nil {
return err
}
res, ok := resUncast.(big.Int)
if !ok {
return errors.New("Could not cast res value in SafeDivBigint")
}

yUncast, err := execScopes.Get("y")
if err != nil {
return err
}
y, ok := yUncast.(big.Int)
if !ok {
return errors.New("Could not cast y value in SafeDivBigint")
}

xUncast, err := execScopes.Get("x")
if err != nil {
return err
}
x, ok := xUncast.(big.Int)
if !ok {
return errors.New("Could not cast x value in SafeDivBigint")
}

pUncast, err := execScopes.Get("p")
if err != nil {
return err
}
p, ok := pUncast.(big.Int)
if !ok {
return errors.New("Could not cast p value in SafeDivBigint")
}

param_x := new(big.Int).Mul(&res, &y)
param_x.Sub(param_x, &x)

fmt.Println("param x: ",param_x)
k, err := SafeDivBig(param_x, &p)
fmt.Println("k", k)
if err != nil {
return err
}

var value big.Int
var flag lambdaworks.Felt

// check if k is positive
if k.Cmp(big.NewInt(0)) == 1 {
value = *k
flag = lambdaworks.FeltFromUint(1)
} else {
value = *new(big.Int).Neg(k)
flag = lambdaworks.FeltFromUint(0)
}

execScopes.AssignOrUpdateVariable("k", k)
execScopes.AssignOrUpdateVariable("value", value)

val := memory.NewMaybeRelocatableFelt(flag)
idsData.Insert("flag", val, &vm)

return nil
}
64 changes: 64 additions & 0 deletions pkg/hints/bigint_hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,67 @@ func TestNonDetBigInt3Ok(t *testing.T) {
}
}
}

func TestSafeDivBigInt(t *testing.T) {
vm := NewVirtualMachine()

vm.Segments.AddSegment()
vm.Segments.AddSegment()
vm.Segments.AddSegment()

execScopes := NewExecutionScopes()

res, _ := new(big.Int).SetString("109567829260688255124154626727441144629993228404337546799996747905569082729709", 10)
x, _ := new(big.Int).SetString("91414600319290532004473480113251693728834511388719905794310982800988866814583", 10)
y, _ := new(big.Int).SetString("38047400353360331012910998489219098987968251547384484838080352663220422975266", 10)
p, _ := new(big.Int).SetString("115792089237316195423570985008687907852837564279074904382605163141518161494337", 10)

execScopes.AssignOrUpdateVariable("res", *res)
execScopes.AssignOrUpdateVariable("x", *x)
execScopes.AssignOrUpdateVariable("y", *y)
execScopes.AssignOrUpdateVariable("p", *p)

vm.RunContext.Fp = NewRelocatable(1, 0)
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"flag": {nil},
},
vm,
)

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

err := hintProcessor.ExecuteHint(vm, &hintData, nil, execScopes)

if err != nil {
t.Errorf("Safe Big int div hint test failed with error: %s", err)
} else {
expectedK, _ := new(big.Int).SetString("36002209591245282109880156842267569109802494162594623391338581162816748840003", 10)
expectedVal, _ := new(big.Int).SetString("36002209591245282109880156842267569109802494162594623391338581162816748840003", 10)

kUncast, err := execScopes.Get("k")
if err != nil {
t.Errorf("%s", err)
}
k, _ := kUncast.(big.Int)

valUncast, err := execScopes.Get("value")
if err != nil {
t.Errorf("%s", err)
}

value, _ := valUncast.(big.Int)

if expectedK.Cmp(&k) != 0 {
t.Errorf("incorrect K value expected: %s, got: %s", expectedK.Text(10), k.Text(10))
}

if expectedVal.Cmp(&value) != 0 {
t.Errorf("incorrect value expected: %s, got: %s", expectedVal.Text(10), value.Text(10))
}
}
}
2 changes: 1 addition & 1 deletion pkg/hints/hint_codes/ec_op_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P"`

const FAST_EC_ADD_ASSIGN_NEW_Y = "value = new_y = (slope * (x0 - new_x) - y0) % SECP_P"

const BIGINT_SAFE_DIV = "k = safe_div(res * y - x, p)\nvalue = k if k > 0 else 0 - k\nids.flag = 1 if k > 0 else 0"
const BIGINT_SAFE_DIV = "k = safe_div(res * y - x, p)\nvalue = k if k > 0 else 0 - k\nids.flag = 1 if k > 0 else 0"
2 changes: 2 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return fastEcAddAssignNewX(data.Ids, vm, execScopes, "pt0", "pt1", SECP_P())
case FAST_EC_ADD_ASSIGN_NEW_Y:
return fastEcAddAssignNewY(execScopes)
case BIGINT_SAFE_DIV:
return SafeDivBigint(*vm, *execScopes, data.Ids)
default:
return errors.Errorf("Unknown Hint: %s", data.Code)
}
Expand Down

0 comments on commit 0aaf96e

Please sign in to comment.