Skip to content

Commit

Permalink
move one math utils function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 committed Sep 26, 2024
1 parent 8a63ad6 commit 96d5623
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
10 changes: 0 additions & 10 deletions pkg/hintrunner/utils/math_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ func Divmod(n, m, p *big.Int) (big.Int, error) {
return *res, nil
}

func SafeDiv(x, y *big.Int) (big.Int, error) {
if y.Cmp(big.NewInt(0)) == 0 {
return *big.NewInt(0), fmt.Errorf("division by zero")
}
if new(big.Int).Mod(x, y).Cmp(big.NewInt(0)) != 0 {
return *big.NewInt(0), fmt.Errorf("%v is not divisible by %v", x, y)
}
return *new(big.Int).Div(x, y), nil
}

func IsQuadResidue(x *fp.Element) bool {
// Implementation adapted from sympy implementation which can be found here :
// https://github.com/sympy/sympy/blob/d91b8ad6d36a59a879cc70e5f4b379da5fdd46ce/sympy/ntheory/residue_ntheory.py#L689
Expand Down
2 changes: 1 addition & 1 deletion pkg/hintrunner/zero/zerohint_ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func newDivModNSafeDivPlusOneHint() hinter.Hinter {
valueBig.Mul(resBig, bBig)
valueBig.Sub(valueBig, aBig)

newValueBig, err := secp_utils.SafeDiv(valueBig, nBig)
newValueBig, err := utils.SafeDiv(valueBig, nBig)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/hintrunner/zero/zerohint_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter"
secp_utils "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/utils"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
VM "github.com/NethermindEth/cairo-vm-go/pkg/vm"
"github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins"
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
Expand Down Expand Up @@ -372,7 +373,7 @@ func newDivModSafeDivHint() hinter.Hinter {
}

divisor := new(big.Int).Sub(new(big.Int).Mul(res, b), a)
value, err := secp_utils.SafeDiv(divisor, N)
value, err := utils.SafeDiv(divisor, N)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,13 @@ func sign(n *big.Int) (int, big.Int) {
}
return 1, *new(big.Int).Set(n)
}

func SafeDiv(x, y *big.Int) (big.Int, error) {
if y.Cmp(big.NewInt(0)) == 0 {
return *big.NewInt(0), fmt.Errorf("division by zero")
}
if new(big.Int).Mod(x, y).Cmp(big.NewInt(0)) != 0 {
return *big.NewInt(0), fmt.Errorf("%v is not divisible by %v", x, y)
}
return *new(big.Int).Div(x, y), nil
}
7 changes: 7 additions & 0 deletions pkg/vm/builtins/modulo.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ func (m *ModBuiltin) fillValue(mem *memory.Memory, inputs ModBuiltinInputs, inde
}
}
} else {
x, _, gcd := utils.Igcdex(b, &inputs.p)
if gcd.Cmp(big.NewInt(1)) != 0 {
value = *new(big.Int).Div(&inputs.p, &gcd)
} else {
value = *new(big.Int).Mul(c, &x)
value = *value.Mod(&value, &inputs.p)
}
value = *new(big.Int).Div(c, b)
}
if err := m.writeNWordsValue(mem, addresses[0], value); err != nil {
Expand Down

0 comments on commit 96d5623

Please sign in to comment.