-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cairo0 div mod n safe div hint (#344)
* Implement VerifyECDSASignature hint * Fixed retrieving memory with signature via pointer * Scooped out the deref from double deref in test cases * Implement GetPointFromXHint * Modify zerohint.go * Add testcases to the hint * Add final testcases * Implement DivModNSafeDivHint * Add checking errors * Add error checks * Changes from main merge * Remove faulty integration tests * Implements comments from discussion * Fixes from merge * Changes adressed by PR discussion * Fixes after merge
- Loading branch information
1 parent
d50bca6
commit 7d292fa
Showing
8 changed files
with
159 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp" | ||
) | ||
|
||
func AsInt(valueFelt *fp.Element) *big.Int { | ||
// https://github.com/starkware-libs/cairo-lang/blob/efa9648f57568aad8f8a13fbf027d2de7c63c2c0/src/starkware/cairo/common/math_utils.py#L8 | ||
|
||
func AsInt(valueFelt *fp.Element) big.Int { | ||
var valueBig big.Int | ||
valueFelt.BigInt(&valueBig) | ||
return AsIntBig(&valueBig) | ||
} | ||
|
||
func AsIntBig(value *big.Int) *big.Int { | ||
func AsIntBig(value *big.Int) big.Int { | ||
boundBig := new(big.Int).Div(fp.Modulus(), big.NewInt(2)) | ||
|
||
// val if val < prime // 2 else val - prime | ||
if value.Cmp(boundBig) == -1 { | ||
return value | ||
return *value | ||
} | ||
return *new(big.Int).Sub(value, fp.Modulus()) | ||
} | ||
|
||
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).Sub(value, fp.Modulus()) | ||
return *new(big.Int).Div(x, y), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters