Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement most signature hints #291

Merged
merged 101 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
977ba29
Add ec hints
mmsc2 Sep 14, 2023
70a9db2
Implement hints
mmsc2 Sep 15, 2023
864b7b8
Add the hints to the processor
mmsc2 Sep 15, 2023
7df4192
Test pack86 function
mmsc2 Sep 15, 2023
1a66309
Test hint
mmsc2 Sep 15, 2023
91ffa57
Delete debug info, Test ec negative op
mmsc2 Sep 18, 2023
7bf1909
Second hint test
mmsc2 Sep 18, 2023
5d2d256
Merge main
mmsc2 Sep 18, 2023
a08c642
Test embedded hint
mmsc2 Sep 18, 2023
029fe50
Merge branch 'main' into EcHint
mmsc2 Sep 18, 2023
762a566
Merge branch 'main' into EcHint
mmsc2 Sep 18, 2023
6c6eed9
Change to Camel case
mmsc2 Sep 19, 2023
ec9345c
Implement slope hints
mmsc2 Sep 19, 2023
9d14678
Fix merge conflict
mmsc2 Sep 19, 2023
ff7fb50
Fix format
mmsc2 Sep 19, 2023
4e9afc5
Delete github conflict string
mmsc2 Sep 19, 2023
cc53b65
Tests hints
mmsc2 Sep 19, 2023
a0d269b
Tests hints slopes
mmsc2 Sep 19, 2023
c25e25c
Fix merge conflict
mmsc2 Sep 19, 2023
a27b814
Rename misleading name function
mmsc2 Sep 19, 2023
51fedf0
Merge branch 'main' into EcSlopeHint
mmsc2 Sep 19, 2023
9e39502
Fix function name
mmsc2 Sep 19, 2023
327299b
Fix format
mmsc2 Sep 19, 2023
0fdad83
Fix error in function call
mmsc2 Sep 19, 2023
ba81ca8
Merge branch 'main' into EcSlopeHint
entropidelic Sep 19, 2023
78dd5d0
Delete debug info
mmsc2 Sep 19, 2023
c023bea
Merge branch 'EcSlopeHint' of github.com:lambdaclass/cairo-vm.go into…
mmsc2 Sep 19, 2023
facfb8a
Delete unused import
mmsc2 Sep 19, 2023
f195395
Merge main
mmsc2 Sep 20, 2023
76c8b8b
Secp hints
mmsc2 Sep 20, 2023
2e1b1f9
Merge main
mmsc2 Sep 20, 2023
41c5e00
Secpr21
mmsc2 Sep 20, 2023
ed75168
Add it to the hint processor
mmsc2 Sep 20, 2023
e868fa0
Hints secp
mmsc2 Sep 20, 2023
b88e485
Merge main
mmsc2 Sep 20, 2023
efad459
bigint3 nondet
mmsc2 Sep 20, 2023
113bb9b
bigint bug fixed
mmsc2 Sep 20, 2023
c09d921
Zero verify
mmsc2 Sep 20, 2023
50994ad
Merge main
mmsc2 Sep 21, 2023
2216d94
Merge branch 'main' into SecpPHints
mmsc2 Sep 21, 2023
2d44427
Add hint to hint processor
mmsc2 Sep 21, 2023
d50207a
Mege main
mmsc2 Sep 21, 2023
c9f1709
Merge branch 'main' into SecpPHints
mmsc2 Sep 21, 2023
46e8bd6
Merge branch 'SecpPHints' of github.com:lambdaclass/cairo-vm.go into …
mmsc2 Sep 21, 2023
7720f42
Debug info
mmsc2 Sep 22, 2023
5775336
Merge main
mmsc2 Sep 22, 2023
5ce7e74
Prints
mmsc2 Sep 22, 2023
3cd665a
Merge branch 'SecpPHints' of github.com:lambdaclass/cairo-vm.go into …
mmsc2 Sep 22, 2023
c7faf9a
Merge branch 'SecpPHints' of github.com:lambdaclass/cairo-vm.go into …
mmsc2 Sep 22, 2023
3969b1b
Test verify with unit test
mmsc2 Sep 25, 2023
942c204
Debug unit test
mmsc2 Sep 25, 2023
ee521d0
Test verify zero with debug
mmsc2 Sep 25, 2023
621a616
Non det big 3 test
mmsc2 Sep 25, 2023
6d2cc54
Modify test to use ids manager
mmsc2 Sep 25, 2023
cef26c0
Add hint codes
fmoletta Sep 25, 2023
ca18c74
Implement base hint
fmoletta Sep 25, 2023
ea13432
Add hints
fmoletta Sep 25, 2023
dd910fa
Add hints to ExecuteHint
fmoletta Sep 25, 2023
552715d
debug info
mmsc2 Sep 26, 2023
af1afcb
Fix broken test
mmsc2 Sep 26, 2023
69d015e
Move integration test to cairo_run_test.go
mmsc2 Sep 26, 2023
b5f44e2
Move file from hints_utils and rename
mmsc2 Sep 26, 2023
6cfabdf
Delete debug
mmsc2 Sep 26, 2023
6527b72
Return error of IdsData.Insert
mmsc2 Sep 26, 2023
2bcf015
Change to camel case
mmsc2 Sep 26, 2023
b542a09
Add unit test
fmoletta Sep 26, 2023
8cd64b6
Add unit test
fmoletta Sep 26, 2023
f3d9dbe
Add hint codes
fmoletta Sep 26, 2023
91f8188
Implement hint
fmoletta Sep 26, 2023
29efa2d
Add SafeDivBig
fmoletta Sep 26, 2023
d0cc45e
Add generic way to fetch scope variables
fmoletta Sep 26, 2023
0655cfe
Add generic fetch
fmoletta Sep 26, 2023
ca654b4
Add generic way to fetch scope variables
fmoletta Sep 26, 2023
08ebcd6
Use more specific error
fmoletta Sep 26, 2023
bbd8b8a
Merge branch 'scopes-fetch-generic' into signature-hints-3
fmoletta Sep 26, 2023
a6e4d4a
Add hints to ExecuteHint
fmoletta Sep 26, 2023
c85e650
Add extra hint
fmoletta Sep 26, 2023
2e948d8
Fix logic, add unit test
fmoletta Sep 26, 2023
97c9cc7
Add unit test
fmoletta Sep 26, 2023
66c38a6
use boolean flag instead or arg
fmoletta Sep 26, 2023
68b4387
Merge remote-tracking branch 'origin/SecpPHints' into signature-hints-3
fmoletta Sep 26, 2023
f6c402d
Fix scope var name
fmoletta Sep 26, 2023
97a67f8
Fix scope var name in tests
fmoletta Sep 26, 2023
69993be
Make FetchScopeVar work despite references
fmoletta Sep 26, 2023
2739694
Revert "Make FetchScopeVar work despite references"
fmoletta Sep 26, 2023
bcd34f6
Merge branch 'signature-hints-2' into signature-hints-3
fmoletta Sep 26, 2023
9116e1b
Handle scope variables as big.Int instead of *big.Int
fmoletta Sep 26, 2023
62b9ed2
Fix merge cnflicts
fmoletta Sep 26, 2023
ccd5805
Fix tests
fmoletta Sep 26, 2023
e0573c1
Implement Igcdex + add tests
fmoletta Sep 26, 2023
04df104
Implement DivMod
fmoletta Sep 26, 2023
7bd7edc
Use DivMod instead of Div + Mod
fmoletta Sep 26, 2023
b1c81af
Dont modify the original value in bigint3_split function
fmoletta Sep 26, 2023
e6882ff
Merge branch 'SecpPHints' into signature-hints-3
fmoletta Sep 26, 2023
2fbc48d
Push test file
fmoletta Sep 26, 2023
468e176
Remove redundant check
fmoletta Sep 26, 2023
18cfa39
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into signat…
fmoletta Sep 27, 2023
8f8bda9
Merge branch 'main' into signature-hints-3
fmoletta Sep 29, 2023
90862cd
Merge branch 'main' into signature-hints-3
pefontana Sep 29, 2023
0b46bc5
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into signat…
fmoletta Oct 2, 2023
53fcdb5
Merge math_utils/utils & utils/math_utils
fmoletta Oct 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions cairo_programs/div_mod_n.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
%builtins range_check

from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, BASE, bigint_mul
from starkware.cairo.common.cairo_secp.constants import BETA, N0, N1, N2

// Source: https://github.com/myBraavos/efficient-secp256r1/blob/73cca4d53730cb8b2dcf34e36c7b8f34b96b3230/src/secp256r1/signature.cairo

// Computes a * b^(-1) modulo the size of the elliptic curve (N).
//
// Prover assumptions:
// * All the limbs of a are in the range (-2 ** 210.99, 2 ** 210.99).
// * All the limbs of b are in the range (-2 ** 124.99, 2 ** 124.99).
// * b is in the range [0, 2 ** 256).
//
// Soundness assumptions:
// * The limbs of a are in the range (-2 ** 249, 2 ** 249).
// * The limbs of b are in the range (-2 ** 159.83, 2 ** 159.83).
func div_mod_n{range_check_ptr}(a: BigInt3, b: BigInt3) -> (res: BigInt3) {
%{
from starkware.cairo.common.cairo_secp.secp_utils import N, pack
from starkware.python.math_utils import div_mod, safe_div
a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = div_mod(a, b, N)
%}
let (res) = nondet_bigint3();

%{ value = k_plus_one = safe_div(res * b - a, N) + 1 %}
let (k_plus_one) = nondet_bigint3();
let k = BigInt3(d0=k_plus_one.d0 - 1, d1=k_plus_one.d1, d2=k_plus_one.d2);

let (res_b) = bigint_mul(res, b);
let n = BigInt3(N0, N1, N2);
let (k_n) = bigint_mul(k, n);

// We should now have res_b = k_n + a. Since the numbers are in unreduced form,
// we should handle the carry.

tempvar carry1 = (res_b.d0 - k_n.d0 - a.d0) / BASE;
assert [range_check_ptr + 0] = carry1 + 2 ** 127;

tempvar carry2 = (res_b.d1 - k_n.d1 - a.d1 + carry1) / BASE;
assert [range_check_ptr + 1] = carry2 + 2 ** 127;

tempvar carry3 = (res_b.d2 - k_n.d2 - a.d2 + carry2) / BASE;
assert [range_check_ptr + 2] = carry3 + 2 ** 127;

tempvar carry4 = (res_b.d3 - k_n.d3 + carry3) / BASE;
assert [range_check_ptr + 3] = carry4 + 2 ** 127;

assert res_b.d4 - k_n.d4 + carry4 = 0;

let range_check_ptr = range_check_ptr + 4;

return (res=res);
}

func div_mod_n_alt{range_check_ptr}(a: BigInt3, b: BigInt3) -> (res: BigInt3) {
// just used to import N
%{
from starkware.cairo.common.cairo_secp.secp_utils import N, pack
from starkware.python.math_utils import div_mod, safe_div
a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = div_mod(a, b, N)
%}

%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div
a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = div_mod(a, b, N)
%}
let (res) = nondet_bigint3();

%{ value = k_plus_one = safe_div(res * b - a, N) + 1 %}
let (k_plus_one) = nondet_bigint3();
let k = BigInt3(d0=k_plus_one.d0 - 1, d1=k_plus_one.d1, d2=k_plus_one.d2);

let (res_b) = bigint_mul(res, b);
let n = BigInt3(N0, N1, N2);
let (k_n) = bigint_mul(k, n);

tempvar carry1 = (res_b.d0 - k_n.d0 - a.d0) / BASE;
assert [range_check_ptr + 0] = carry1 + 2 ** 127;

tempvar carry2 = (res_b.d1 - k_n.d1 - a.d1 + carry1) / BASE;
assert [range_check_ptr + 1] = carry2 + 2 ** 127;

tempvar carry3 = (res_b.d2 - k_n.d2 - a.d2 + carry2) / BASE;
assert [range_check_ptr + 2] = carry3 + 2 ** 127;

tempvar carry4 = (res_b.d3 - k_n.d3 + carry3) / BASE;
assert [range_check_ptr + 3] = carry4 + 2 ** 127;

assert res_b.d4 - k_n.d4 + carry4 = 0;

let range_check_ptr = range_check_ptr + 4;

return (res=res);
}

func test_div_mod_n{range_check_ptr: felt}() {
let a: BigInt3 = BigInt3(100, 99, 98);
let b: BigInt3 = BigInt3(10, 9, 8);

let (res) = div_mod_n(a, b);

assert res = BigInt3(
3413472211745629263979533, 17305268010345238170172332, 11991751872105858217578135
);

// test alternative hint
let (res_alt) = div_mod_n_alt(a, b);

assert res_alt = res;

return ();
}

func main{range_check_ptr: felt}() {
test_div_mod_n();

return ();
}
5 changes: 2 additions & 3 deletions pkg/builtins/ec_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/big"

"github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks"
"github.com/lambdaclass/cairo-vm.go/pkg/math_utils"
"github.com/lambdaclass/cairo-vm.go/pkg/utils"
"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory"
"github.com/pkg/errors"
Expand Down Expand Up @@ -263,7 +262,7 @@ func LineSlope(point_a PartialSumB, point_b DoublePointB, prime big.Int) (big.In
n := new(big.Int).Sub(&point_a.Y, &point_b.Y)
m := new(big.Int).Sub(&point_a.X, &point_b.X)

z, err := math_utils.DivMod(n, m, &prime)
z, err := utils.DivMod(n, m, &prime)
if err != nil {
return big.Int{}, err
}
Expand Down Expand Up @@ -299,7 +298,7 @@ func EcDoubleSlope(point DoublePointB, alpha big.Int, prime big.Int) (big.Int, e
n.Add(n, &alpha)

m := new(big.Int).Mul(&point.Y, big.NewInt(2))
z, err := math_utils.DivMod(n, m, &prime)
z, err := utils.DivMod(n, m, &prime)

if err != nil {
return big.Int{}, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/hints/hint_codes/signature_hint_codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hint_codes

const DIV_MOD_N_PACKED_DIVMOD_V1 = `from starkware.cairo.common.cairo_secp.secp_utils import N, pack
from starkware.python.math_utils import div_mod, safe_div

a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = div_mod(a, b, N)`

const DIV_MOD_N_PACKED_DIVMOD_EXTERNAL_N = `from starkware.cairo.common.cairo_secp.secp_utils import pack
from starkware.python.math_utils import div_mod, safe_div

a = pack(ids.a, PRIME)
b = pack(ids.b, PRIME)
value = res = div_mod(a, b, N)`

const DIV_MOD_N_SAFE_DIV = "value = k = safe_div(res * b - a, N)"

const DIV_MOD_N_SAFE_DIV_PLUS_ONE = "value = k_plus_one = safe_div(res * b - a, N) + 1"

const XS_SAFE_DIV = "value = k = safe_div(res * s - x, N)"
10 changes: 10 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return splitInt(data.Ids, vm)
case SPLIT_INT_ASSERT_RANGE:
return splitIntAssertRange(data.Ids, vm)
case DIV_MOD_N_PACKED_DIVMOD_V1:
return divModNPackedDivMod(data.Ids, vm, execScopes)
case DIV_MOD_N_PACKED_DIVMOD_EXTERNAL_N:
return divModNPackedDivModExternalN(data.Ids, vm, execScopes)
case XS_SAFE_DIV:
return divModNSafeDiv(data.Ids, execScopes, "x", "s", false)
case DIV_MOD_N_SAFE_DIV:
return divModNSafeDiv(data.Ids, execScopes, "a", "b", false)
case DIV_MOD_N_SAFE_DIV_PLUS_ONE:
return divModNSafeDiv(data.Ids, execScopes, "a", "b", true)
case VERIFY_ZERO_EXTERNAL_SECP:
return verifyZeroWithExternalConst(*vm, *execScopes, data.Ids)
case FAST_EC_ADD_ASSIGN_NEW_X:
Expand Down
17 changes: 8 additions & 9 deletions pkg/hints/hint_utils/bigint_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ func BigInt3FromBaseAddr(addr Relocatable, name string, vm *VirtualMachine) (Big
}

func BigInt3FromVarName(name string, ids IdsManager, vm *VirtualMachine) (BigInt3, error) {
bigIntAddr, err := ids.GetAddr(name, vm)
if err != nil {
return BigInt3{}, err
}
limbs, err := limbsFromVarName(3, name, ids, vm)
return BigInt3{Limbs: limbs}, err
}

bigInt, err := BigInt3FromBaseAddr(bigIntAddr, name, vm)
if err != nil {
return BigInt3{}, err
}
// Uint384

type Uint384 = BigInt3

return bigInt, err
func Uint384FromVarName(name string, ids IdsManager, vm *VirtualMachine) (Uint384, error) {
return BigInt3FromVarName(name, ids, vm)
}
2 changes: 1 addition & 1 deletion pkg/hints/hint_utils/secp_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Bigint3Split(integer big.Int) ([]big.Int, error) {

for i := 0; i < 3; i++ {
canonicalRepr[i] = *new(big.Int).And(&num, BASE_MINUS_ONE())
num.Rsh(&num, 86)
num = *new(big.Int).Rsh(&num, 86)
}
if num.Cmp(big.NewInt(0)) != 0 {
return nil, errors.New("HintError SecpSplitOutOfRange")
Expand Down
2 changes: 1 addition & 1 deletion pkg/hints/math_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
. "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/lambdaworks"
. "github.com/lambdaclass/cairo-vm.go/pkg/math_utils"
. "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"
"github.com/pkg/errors"
Expand Down
84 changes: 84 additions & 0 deletions pkg/hints/signature_hints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package hints

import (
"math/big"

. "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils"
. "github.com/lambdaclass/cairo-vm.go/pkg/types"
"github.com/lambdaclass/cairo-vm.go/pkg/utils"
. "github.com/lambdaclass/cairo-vm.go/pkg/vm"
)

func divModNPacked(ids IdsManager, vm *VirtualMachine, scopes *ExecutionScopes, n *big.Int) error {
a, err := Uint384FromVarName("a", ids, vm)
if err != nil {
return err
}
b, err := Uint384FromVarName("b", ids, vm)
if err != nil {
return err
}
packedA := a.Pack86()
packedB := b.Pack86()

val, err := utils.DivMod(&packedA, &packedB, n)
if err != nil {
return err
}

scopes.AssignOrUpdateVariable("a", packedA)
scopes.AssignOrUpdateVariable("b", packedB)
scopes.AssignOrUpdateVariable("value", *val)
scopes.AssignOrUpdateVariable("res", *val)

return nil
}

func divModNPackedDivMod(ids IdsManager, vm *VirtualMachine, scopes *ExecutionScopes) error {
n, _ := new(big.Int).SetString("115792089237316195423570985008687907852837564279074904382605163141518161494337", 10)
scopes.AssignOrUpdateVariable("N", *n)
return divModNPacked(ids, vm, scopes, n)
}

func divModNPackedDivModExternalN(ids IdsManager, vm *VirtualMachine, scopes *ExecutionScopes) error {
n, err := FetchScopeVar[big.Int]("N", scopes)
if err != nil {
return err
}
return divModNPacked(ids, vm, scopes, &n)
}

func divModNSafeDiv(ids IdsManager, scopes *ExecutionScopes, aAlias string, bAlias string, addOne bool) error {
// Fetch scope variables
a, err := FetchScopeVar[big.Int](aAlias, scopes)
if err != nil {
return err
}

b, err := FetchScopeVar[big.Int](bAlias, scopes)
if err != nil {
return err
}

res, err := FetchScopeVar[big.Int]("res", scopes)
if err != nil {
return err
}

n, err := FetchScopeVar[big.Int]("N", scopes)
if err != nil {
return err
}

// Hint logic
value, err := utils.SafeDivBig(new(big.Int).Sub(new(big.Int).Mul(&res, &b), &a), &n)
if err != nil {
return err
}
if addOne {
value = new(big.Int).Add(value, big.NewInt(1))
}
// Update scope
scopes.AssignOrUpdateVariable("value", *value)
return nil
}
Loading
Loading