-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Blake2s hints (Part 4) (#315)
* Begin implemneting blake2s * Finish blake2s impl * Add unit test * Add more unit tests * Add integration test * Implement BLAKE2S_COMPUTE * Add unit tests * Add unit tests * Add newline * Add integration test * Implement BLAKE2S_ADD_UINT256_BIGEND hint * Add unit test * Add integration test * Implement finalize_blake2s hint * Fix removed line * Add unit test * Fix test values * Add + expand integration test * Implement hint + add quickfix to makefile * Add hint + integration test * Implement finalize v3 * Add integration test * Implement sha256 input hint * Add unit tests * Add exmaple blake compress hint * Add unit test * Clone from main branch * Update cairo version * fix conflict * fix conflict * Update cairo-vm version --------- Co-authored-by: Pedro Fontana <[email protected]>
- Loading branch information
Showing
10 changed files
with
1,011 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,68 @@ | ||
%builtins range_check bitwise | ||
|
||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.cairo_blake2s.blake2s import blake2s, _finalize_blake2s_inner, _get_sigma, INSTANCE_SIZE, INPUT_BLOCK_FELTS | ||
from starkware.cairo.common.cairo_blake2s.packed_blake2s import N_PACKED_INSTANCES, blake2s_compress | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
from starkware.cairo.common.registers import get_fp_and_pc | ||
from starkware.cairo.common.math import assert_nn_le, split_felt, unsigned_div_rem | ||
|
||
const BLAKE2S_INPUT_CHUNK_SIZE_FELTS = INPUT_BLOCK_FELTS; | ||
|
||
// Verifies that the results of blake2s() are valid. | ||
func finalize_blake2s{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}( | ||
blake2s_ptr_start: felt*, blake2s_ptr_end: felt* | ||
) { | ||
alloc_locals; | ||
|
||
let (__fp__, _) = get_fp_and_pc(); | ||
|
||
let (sigma) = _get_sigma(); | ||
|
||
tempvar n = (blake2s_ptr_end - blake2s_ptr_start) / INSTANCE_SIZE; | ||
if (n == 0) { | ||
return (); | ||
} | ||
|
||
%{ | ||
# Add dummy pairs of input and output. | ||
from starkware.cairo.common.cairo_blake2s.blake2s_utils import IV, blake2s_compress | ||
_n_packed_instances = int(ids.N_PACKED_INSTANCES) | ||
assert 0 <= _n_packed_instances < 20 | ||
_blake2s_input_chunk_size_felts = int(ids.BLAKE2S_INPUT_CHUNK_SIZE_FELTS) | ||
assert 0 <= _blake2s_input_chunk_size_felts < 100 | ||
message = [0] * _blake2s_input_chunk_size_felts | ||
modified_iv = [IV[0] ^ 0x01010020] + IV[1:] | ||
output = blake2s_compress( | ||
message=message, | ||
h=modified_iv, | ||
t0=0, | ||
t1=0, | ||
f0=0xffffffff, | ||
f1=0, | ||
) | ||
padding = (modified_iv + message + [0, 0xffffffff] + output) * (_n_packed_instances - 1) | ||
segments.write_arg(ids.blake2s_ptr_end, padding) | ||
%} | ||
|
||
// Compute the amount of chunks (rounded up). | ||
let (local n_chunks, _) = unsigned_div_rem(n + N_PACKED_INSTANCES - 1, N_PACKED_INSTANCES); | ||
let blake2s_ptr = blake2s_ptr_start; | ||
_finalize_blake2s_inner{blake2s_ptr=blake2s_ptr}(n=n_chunks, sigma=sigma); | ||
return (); | ||
} | ||
|
||
func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { | ||
alloc_locals; | ||
let inputs: felt* = alloc(); | ||
assert inputs[0] = 'Hell'; | ||
assert inputs[1] = 'o Wo'; | ||
assert inputs[2] = 'rld'; | ||
let (local blake2s_ptr_start) = alloc(); | ||
let blake2s_ptr = blake2s_ptr_start; | ||
let (output) = blake2s{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}(inputs, 9); | ||
finalize_blake2s(blake2s_ptr_start, blake2s_ptr); | ||
return (); | ||
} |
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,3 @@ | ||
package hint_codes | ||
|
||
const SHA256_INPUT = "ids.full_word = int(ids.n_bytes >= 4)" |
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,19 @@ | ||
package hints | ||
|
||
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/vm" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
||
func sha256Input(ids IdsManager, vm *VirtualMachine) error { | ||
nBytes, err := ids.GetFelt("n_bytes", vm) | ||
if err != nil { | ||
return err | ||
} | ||
if nBytes.Cmp(FeltFromUint(4)) != -1 { | ||
return ids.Insert("full_word", NewMaybeRelocatableFelt(FeltOne()), vm) | ||
} | ||
return ids.Insert("full_word", NewMaybeRelocatableFelt(FeltZero()), vm) | ||
} |
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,64 @@ | ||
package hints_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/lambdaclass/cairo-vm.go/pkg/hints" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" | ||
. "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/vm" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
||
func TestSha256InputFalse(t *testing.T) { | ||
vm := NewVirtualMachine() | ||
vm.Segments.AddSegment() | ||
idsManager := SetupIdsForTest( | ||
map[string][]*MaybeRelocatable{ | ||
"n_bytes": {NewMaybeRelocatableFelt(FeltFromUint64(2))}, | ||
"full_word": {nil}, | ||
}, | ||
vm, | ||
) | ||
hintProcessor := CairoVmHintProcessor{} | ||
hintData := any(HintData{ | ||
Ids: idsManager, | ||
Code: SHA256_INPUT, | ||
}) | ||
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) | ||
if err != nil { | ||
t.Errorf("SHA256_INPUT hint test failed with error %s", err) | ||
} | ||
// Check ids.full_word | ||
fullWord, err := idsManager.GetFelt("full_word", vm) | ||
if err != nil || fullWord.Cmp(FeltZero()) != 0 { | ||
t.Error("Wrong/No value inserted into ids.full_word") | ||
} | ||
} | ||
|
||
func TestSha256InputTrue(t *testing.T) { | ||
vm := NewVirtualMachine() | ||
vm.Segments.AddSegment() | ||
idsManager := SetupIdsForTest( | ||
map[string][]*MaybeRelocatable{ | ||
"n_bytes": {NewMaybeRelocatableFelt(FeltFromUint64(8))}, | ||
"full_word": {nil}, | ||
}, | ||
vm, | ||
) | ||
hintProcessor := CairoVmHintProcessor{} | ||
hintData := any(HintData{ | ||
Ids: idsManager, | ||
Code: SHA256_INPUT, | ||
}) | ||
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) | ||
if err != nil { | ||
t.Errorf("SHA256_INPUT hint test failed with error %s", err) | ||
} | ||
// Check ids.full_word | ||
fullWord, err := idsManager.GetFelt("full_word", vm) | ||
if err != nil || fullWord.Cmp(FeltOne()) != 0 { | ||
t.Error("Wrong/No value inserted into ids.full_word") | ||
} | ||
} |
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