Skip to content

Commit

Permalink
IsZeroPack Hint Implementation (#469)
Browse files Browse the repository at this point in the history
* Implemented IsZeroPack Hint

* Added Documentation

* Update zerohint_signature.go

* Update zerohint_signature.go

* nits

* nit

* nit
  • Loading branch information
Sh0g0-1758 authored Jun 19, 2024
1 parent fc520ad commit 1153823
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/hintrunner/zero/hintcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const (
importSecp256R1PCode string = "from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P"
verifyZeroCode string = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nq, r = divmod(pack(ids.val, PRIME), SECP_P)\nassert r == 0, f\"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}.\"\nids.q = q % PRIME"
divModNPackedDivmodV1Code string = "from starkware.cairo.common.cairo_secp.secp_utils import N, pack\nfrom starkware.python.math_utils import div_mod, safe_div\n\na = pack(ids.a, PRIME)\nb = pack(ids.b, PRIME)\nvalue = res = div_mod(a, b, N)"
isZeroPackCode string = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nx = pack(ids.x, PRIME) % SECP_P"

// ------ Blake Hash hints related code ------
blake2sAddUint256BigendCode string = "B = 32\nMASK = 2 ** 32 - 1\nsegments.write_arg(ids.data, [(ids.high >> (B * (3 - i))) & MASK for i in range(4)])\nsegments.write_arg(ids.data + 4, [(ids.low >> (B * (3 - i))) & MASK for i in range(4)])"
Expand Down
2 changes: 2 additions & 0 deletions pkg/hintrunner/zero/zerohint.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64
return createVerifyZeroHinter(resolver)
case divModNPackedDivmodV1Code:
return createDivModNPackedDivmodV1Hinter(resolver)
case isZeroPackCode:
return createIsZeroPackHinter(resolver)
// EC hints
case ecNegateCode:
return createEcNegateHinter(resolver)
Expand Down
54 changes: 54 additions & 0 deletions pkg/hintrunner/zero/zerohint_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,57 @@ func createDivModNPackedDivmodV1Hinter(resolver hintReferenceResolver) (hinter.H

return newDivModNPackedDivmodV1Hint(a, b), nil
}

// IsZeroPack hint computes packed value modulo SECP_P prime
//
// `newIsZeroPackHint` takes 1 operander as argument
// - `x` is the value that will be packed and taken modulo SECP_P prime
//
// `newIsZeroPackHint` assigns the result as `x` in the current scope
func newIsZeroPackHint(x hinter.ResOperander) hinter.Hinter {
return &GenericZeroHinter{
Name: "IsZeroPack",
Op: func(vm *VM.VirtualMachine, ctx *hinter.HintRunnerContext) error {
//> from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
//> x = pack(ids.x, PRIME) % SECP_P

xAddr, err := x.GetAddress(vm)
if err != nil {
return err
}

xValues, err := vm.Memory.ResolveAsBigInt3(xAddr)
if err != nil {
return err
}

secPBig, ok := secp_utils.GetSecPBig()
if !ok {
return fmt.Errorf("GetSecPBig failed")
}

xPackedBig, err := secp_utils.SecPPacked(xValues)
if err != nil {
return err
}

value := new(big.Int)
value.Mod(&xPackedBig, &secPBig)

if err := ctx.ScopeManager.AssignVariable("x", value); err != nil {
return err
}

return nil
},
}
}

func createIsZeroPackHinter(resolver hintReferenceResolver) (hinter.Hinter, error) {
x, err := resolver.GetResOperander("x")
if err != nil {
return nil, err
}

return newIsZeroPackHint(x), nil
}
46 changes: 46 additions & 0 deletions pkg/hintrunner/zero/zerohint_signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,51 @@ func TestVerifyZeroHint(t *testing.T) {
}),
},
},
"IsZeroPack": {
{
operanders: []*hintOperander{
{Name: "x.d0", Kind: apRelative, Value: feltString("42")},
{Name: "x.d1", Kind: apRelative, Value: feltString("0")},
{Name: "x.d2", Kind: apRelative, Value: feltString("0")},
},
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newIsZeroPackHint(ctx.operanders["x.d0"])
},
check: varValueInScopeEquals("x", big.NewInt(42)),
},
{
operanders: []*hintOperander{
{Name: "x.d0", Kind: apRelative, Value: feltString("100")},
{Name: "x.d1", Kind: apRelative, Value: feltString("99")},
{Name: "x.d2", Kind: apRelative, Value: feltString("88")},
},
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newIsZeroPackHint(ctx.operanders["x.d0"])
},
check: varValueInScopeEquals("x", bigIntString("526795342172649295060681798242672774947232024188944484", 10)),
},
{
operanders: []*hintOperander{
{Name: "x.d0", Kind: apRelative, Value: feltString("77371252455336262886226991")},
{Name: "x.d1", Kind: apRelative, Value: feltString("77371252455336267181195263")},
{Name: "x.d2", Kind: apRelative, Value: feltString("19342813113834066795298815")},
},
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newIsZeroPackHint(ctx.operanders["x.d0"])
},
check: varValueInScopeEquals("x", big.NewInt(0)),
},
{
operanders: []*hintOperander{
{Name: "x.d0", Kind: apRelative, Value: feltString("3618502788666131213697322783095070105623107215331596699973092056135872020481")},
{Name: "x.d1", Kind: apRelative, Value: feltString("0")},
{Name: "x.d2", Kind: apRelative, Value: feltString("0")},
},
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newIsZeroPackHint(ctx.operanders["x.d0"])
},
check: varValueInScopeEquals("x", big.NewInt(0)),
},
},
})
}

0 comments on commit 1153823

Please sign in to comment.