-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Ecdoubleslopev1 hint (#341)
* Implement EcNegate hint * Implement NondetBigint3V1 hint * Fix context usage + add some functions to return constants * Add scope check test utils + usage * More comments and tests * More tests * Clean up some code * Implement FastEcAddAssignNewY hint * Implement FastEcAddAssignNewX hint * Make hint code less verbose * Add more tests * Fix comment formatting * Clean up EcNegate using new util functions * Implement EcDoubleSlopeV1 hint * Use FeltZero/FeltOne in tests * Use FeltZero in tests * Add secp util functions to clean up hint * Make function private * Moving comments around * Fix import * Change zero compare * Make return simpler * Cleaner return * Add more tests * Comment test values * Comment test values * Comment test values * Comment test values * Update comment * Simpler context init in tests * Add GetVariableValueAsBigInt method to scope manager * Simpler tests * Remove duplicate test check * Clean up scope usage * Use existing constant * Simpler loop * Add AssignVariables method to scope manager + use it * Use AssignVariables in FastEcAddAssignNewY * Use util methods to make code simpler * Use feltUint64 when applicable * Change argument to accept slice * Add tests for div_mod and igcdex * Fetch SECP_P from scope in FastEcAddAssignNewY * Use InitializeScopeManager * Return big.Int instead of *big.Int * More return big.Int instead of *big.Int * Rename to divmod * Change signature of EcDoubleSlope * More return big.Int instead of *big.Int * Remove redundant tests * Fix some nits * Return a new big.Int for sign function --------- Co-authored-by: Carmen Cabrera <kr1000a@gmail.com>
- Loading branch information
Showing
11 changed files
with
748 additions
and
399 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
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package utils | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func TestDivMod(t *testing.T) { | ||
// https://github.com/starkware-libs/cairo-lang/blob/efa9648f57568aad8f8a13fbf027d2de7c63c2c0/src/starkware/python/math_utils_test.py#L108 | ||
tests := []struct { | ||
name string | ||
n, m, p *big.Int | ||
expected *big.Int | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
name: "Basic case", | ||
n: big.NewInt(2), | ||
m: big.NewInt(3), | ||
p: big.NewInt(5), | ||
expected: big.NewInt(4), | ||
}, | ||
{ | ||
name: "Error case", | ||
n: big.NewInt(8), | ||
m: big.NewInt(10), | ||
p: big.NewInt(5), | ||
expectedErrMsg: "no solution exists (gcd(m, p) != 1)", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual, err := divmod(tt.n, tt.m, tt.p) | ||
|
||
if err != nil { | ||
if err.Error() != tt.expectedErrMsg { | ||
t.Errorf("got error: %v, want: %v", err, tt.expectedErrMsg) | ||
} | ||
return | ||
} | ||
|
||
if actual.Cmp(tt.expected) != 0 { | ||
t.Errorf("got quotient: %v, want: %v", actual, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIgcdex(t *testing.T) { | ||
// https://github.com/sympy/sympy/blob/e7fb2714f17b30b83e424448aad0da9e94a4b577/sympy/core/tests/test_numbers.py#L278 | ||
tests := []struct { | ||
name string | ||
a, b *big.Int | ||
expectedX, expectedY, expectedG *big.Int | ||
}{ | ||
{ | ||
name: "Case 1", | ||
a: big.NewInt(2), | ||
b: big.NewInt(3), | ||
expectedX: big.NewInt(-1), | ||
expectedY: big.NewInt(1), | ||
expectedG: big.NewInt(1), | ||
}, | ||
{ | ||
name: "Case 2", | ||
a: big.NewInt(10), | ||
b: big.NewInt(12), | ||
expectedX: big.NewInt(-1), | ||
expectedY: big.NewInt(1), | ||
expectedG: big.NewInt(2), | ||
}, | ||
{ | ||
name: "Case 3", | ||
a: big.NewInt(100), | ||
b: big.NewInt(2004), | ||
expectedX: big.NewInt(-20), | ||
expectedY: big.NewInt(1), | ||
expectedG: big.NewInt(4), | ||
}, | ||
{ | ||
name: "Case 4", | ||
a: big.NewInt(0), | ||
b: big.NewInt(0), | ||
expectedX: big.NewInt(0), | ||
expectedY: big.NewInt(1), | ||
expectedG: big.NewInt(0), | ||
}, | ||
{ | ||
name: "Case 5", | ||
a: big.NewInt(1), | ||
b: big.NewInt(0), | ||
expectedX: big.NewInt(1), | ||
expectedY: big.NewInt(0), | ||
expectedG: big.NewInt(1), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actualX, actualY, actualG := igcdex(tt.a, tt.b) | ||
|
||
if actualX.Cmp(tt.expectedX) != 0 { | ||
t.Errorf("got x: %v, want: %v", actualX, tt.expectedX) | ||
} | ||
if actualY.Cmp(tt.expectedY) != 0 { | ||
t.Errorf("got x: %v, want: %v", actualY, tt.expectedY) | ||
} | ||
if actualG.Cmp(tt.expectedG) != 0 { | ||
t.Errorf("got x: %v, want: %v", actualG, tt.expectedG) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.