-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* packedSha256 * add hintcode'' * fmt * test template * add sha256 helpers * initialize slice * add unit test * improve unit test * integration test * add check for length of input * add unit test for Sha256AndBlake2sInput * fmt * use AddOfsset method
- Loading branch information
Showing
12 changed files
with
767 additions
and
9 deletions.
There are no files selected for viewing
417 changes: 417 additions & 0 deletions
417
integration_tests/cairo_zero_hint_tests/packed_sha256.starknet_with_keccak.cairo
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...ration_tests/cairo_zero_hint_tests_in_progress/keccak_uint256s.starknet_with_keccak.cairo
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package zero | ||
|
||
import ( | ||
"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" | ||
hintrunnerUtils "github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/utils" | ||
"github.com/NethermindEth/cairo-vm-go/pkg/utils" | ||
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" | ||
|
||
VM "github.com/NethermindEth/cairo-vm-go/pkg/vm" | ||
) | ||
|
||
func newPackedSha256Hint(sha256Start, output hinter.ResOperander) hinter.Hinter { | ||
return &GenericZeroHinter{ | ||
Name: "PackedSha256", | ||
Op: func(vm *VM.VirtualMachine, ctx *hinter.HintRunnerContext) error { | ||
//> from starkware.cairo.common.cairo_sha256.sha256_utils import ( | ||
//> IV, compute_message_schedule, sha2_compress_function) | ||
//> | ||
//> _sha256_input_chunk_size_felts = int(ids.SHA256_INPUT_CHUNK_SIZE_FELTS) | ||
//> assert 0 <= _sha256_input_chunk_size_felts < 100 | ||
//> | ||
//> w = compute_message_schedule(memory.get_range( | ||
//> ids.sha256_start, _sha256_input_chunk_size_felts)) | ||
//> new_state = sha2_compress_function(IV, w) | ||
//> segments.write_arg(ids.output, new_state) | ||
|
||
Sha256InputChunkSize := uint64(16) | ||
|
||
sha256Start, err := hinter.ResolveAsAddress(vm, sha256Start) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w, err := vm.Memory.GetConsecutiveMemoryValues(*sha256Start, Sha256InputChunkSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
wUint32 := make([]uint32, len(w)) | ||
for i := 0; i < len(w); i++ { | ||
value, err := hintrunnerUtils.ToSafeUint32(&w[i]) | ||
if err != nil { | ||
return err | ||
} | ||
wUint32[i] = value | ||
} | ||
|
||
messageSchedule, err := utils.ComputeMessageSchedule(wUint32) | ||
if err != nil { | ||
return err | ||
} | ||
newState := utils.Sha256Compress(utils.IV(), messageSchedule) | ||
|
||
output, err := hinter.ResolveAsAddress(vm, output) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := 0; i < len(newState); i++ { | ||
newStateValue := mem.MemoryValueFromInt(newState[i]) | ||
outputOffset, err := output.AddOffset(int16(i)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = vm.Memory.WriteToAddress(&outputOffset, &newStateValue) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func createPackedSha256Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { | ||
sha256Start, err := resolver.GetResOperander("sha256_start") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
output, err := resolver.GetResOperander("output") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newPackedSha256Hint(sha256Start, output), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package zero | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" | ||
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp" | ||
) | ||
|
||
func TestZeroHintSha256(t *testing.T) { | ||
runHinterTests(t, map[string][]hintTestCase{ | ||
"PackedSha256": { | ||
{ | ||
operanders: []*hintOperander{ | ||
{Name: "sha256_start", Kind: apRelative, Value: addr(6)}, | ||
{Name: "output", Kind: apRelative, Value: addr(22)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
{Name: "buffer", Kind: apRelative, Value: feltUint64(0)}, | ||
}, | ||
makeHinter: func(ctx *hintTestContext) hinter.Hinter { | ||
return newPackedSha256Hint(ctx.operanders["sha256_start"], ctx.operanders["output"]) | ||
}, | ||
check: consecutiveVarAddrResolvedValueEquals( | ||
"output", | ||
[]*fp.Element{ | ||
feltString("3663108286"), | ||
feltString("398046313"), | ||
feltString("1647531929"), | ||
feltString("2006957770"), | ||
feltString("2363872401"), | ||
feltString("3235013187"), | ||
feltString("3137272298"), | ||
feltString("406301144"), | ||
}), | ||
}, | ||
}, | ||
}) | ||
} |
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.