-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86044a8
commit bfa79e0
Showing
8 changed files
with
212 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
CAIRO_VM_CLI:=cairo-vm/target/release/cairo-vm-cli | ||
|
||
$(CAIRO_VM_CLI): | ||
git clone --depth 1 -b v0.9.2 https://github.com/lambdaclass/cairo-vm | ||
git clone --depth 1 https://github.com/lambdaclass/cairo-vm | ||
cd cairo-vm; cargo b --release --bin cairo-vm-cli | ||
|
||
build_cairo_vm_cli: | $(CAIRO_VM_CLI) | ||
|
@@ -22,6 +22,7 @@ deps: | |
# Creates a pyenv and installs cairo-lang | ||
deps-macos: | ||
brew install gmp pyenv | ||
brew install [email protected] | ||
pyenv install -s 3.9.15 | ||
PYENV_VERSION=3.9.15 /opt/homebrew/bin/python3.9 -m venv cairo-vm-env | ||
. cairo-vm-env/bin/activate ; \ | ||
|
@@ -39,16 +40,11 @@ test: | |
test-filter: | ||
@zig build test --summary all -Dtest-filter="$(FILTER)" | ||
|
||
build-integration-test: | ||
@zig build -Doptimize=ReleaseFast integration_test | ||
|
||
run-integration-test: | ||
@zig build -Doptimize=ReleaseFast integration_test | ||
./zig-out/bin/integration_test | ||
|
||
run-integration-test-filter: | ||
@zig build integration_test | ||
./zig-out/bin/integration_test $(FILTER) | ||
@zig build -Doptimize=ReleaseFast integration_test $(FILTER) | ||
|
||
build-compare-benchmarks: build_cairo_vm_cli build-optimize | ||
cd scripts; sh benchmarks.sh | ||
|
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,44 @@ | ||
%builtins output range_check bitwise | ||
|
||
from starkware.cairo.common.cairo_keccak.keccak import _keccak | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.serialize import serialize_word | ||
|
||
func fill_array(array: felt*, base: felt, array_length: felt, iterator: felt) { | ||
if (iterator == array_length) { | ||
return (); | ||
} | ||
|
||
assert array[iterator] = base; | ||
|
||
return fill_array(array, base, array_length, iterator + 1); | ||
} | ||
|
||
func main{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { | ||
alloc_locals; | ||
|
||
let (output: felt*) = alloc(); | ||
let keccak_output = output; | ||
|
||
let (inputs: felt*) = alloc(); | ||
let inputs_start = inputs; | ||
fill_array(inputs, 9, 3, 0); | ||
|
||
let (state: felt*) = alloc(); | ||
let state_start = state; | ||
fill_array(state, 5, 25, 0); | ||
|
||
let n_bytes = 24; | ||
|
||
let (res: felt*) = _keccak{keccak_ptr=keccak_output}( | ||
inputs=inputs_start, n_bytes=n_bytes, state=state_start | ||
); | ||
|
||
serialize_word(res[0]); | ||
serialize_word(res[1]); | ||
serialize_word(res[2]); | ||
serialize_word(res[4]); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
%builtins output range_check bitwise | ||
|
||
from starkware.cairo.common.cairo_keccak.keccak import ( | ||
_prepare_block, | ||
KECCAK_FULL_RATE_IN_BYTES, | ||
KECCAK_FULL_RATE_IN_WORDS, | ||
KECCAK_STATE_SIZE_FELTS, | ||
) | ||
from starkware.cairo.common.math import assert_nn_le | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.serialize import serialize_word | ||
|
||
func _keccak_0_10_3{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: felt*}( | ||
inputs: felt*, n_bytes: felt, state: felt* | ||
) -> (output: felt*) { | ||
alloc_locals; | ||
if (nondet %{ ids.n_bytes >= ids.KECCAK_FULL_RATE_IN_BYTES %} != 0) { | ||
_prepare_block(inputs=inputs, n_bytes=KECCAK_FULL_RATE_IN_BYTES, state=state); | ||
_block_permutation_0_10_3(); | ||
|
||
return _keccak_0_10_3( | ||
inputs=inputs + KECCAK_FULL_RATE_IN_WORDS, | ||
n_bytes=n_bytes - KECCAK_FULL_RATE_IN_BYTES, | ||
state=keccak_ptr - KECCAK_STATE_SIZE_FELTS, | ||
); | ||
} | ||
|
||
assert_nn_le(n_bytes, KECCAK_FULL_RATE_IN_BYTES - 1); | ||
|
||
_prepare_block(inputs=inputs, n_bytes=n_bytes, state=state); | ||
_block_permutation_0_10_3(); | ||
|
||
return (output=keccak_ptr - KECCAK_STATE_SIZE_FELTS); | ||
} | ||
|
||
func _block_permutation_0_10_3{keccak_ptr: felt*}() { | ||
%{ | ||
from starkware.cairo.common.cairo_keccak.keccak_utils import keccak_func | ||
_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS) | ||
assert 0 <= _keccak_state_size_felts < 100 | ||
output_values = keccak_func(memory.get_range( | ||
ids.keccak_ptr - _keccak_state_size_felts, _keccak_state_size_felts)) | ||
segments.write_arg(ids.keccak_ptr, output_values) | ||
%} | ||
let keccak_ptr = keccak_ptr + KECCAK_STATE_SIZE_FELTS; | ||
|
||
return (); | ||
} | ||
|
||
func run_0_10_3{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { | ||
alloc_locals; | ||
|
||
let (output: felt*) = alloc(); | ||
let keccak_output = output; | ||
|
||
let (inputs: felt*) = alloc(); | ||
let inputs_start = inputs; | ||
fill_array(inputs, 9, 3, 0); | ||
|
||
let (state: felt*) = alloc(); | ||
let state_start = state; | ||
fill_array(state, 5, 25, 0); | ||
|
||
let n_bytes = 24; | ||
|
||
let (res: felt*) = _keccak_0_10_3{keccak_ptr=keccak_output}( | ||
inputs=inputs_start, n_bytes=n_bytes, state=state_start | ||
); | ||
|
||
serialize_word(res[0]); | ||
serialize_word(res[1]); | ||
serialize_word(res[2]); | ||
serialize_word(res[4]); | ||
|
||
return (); | ||
} | ||
|
||
func _block_permutation_cairo_keccak{output_ptr: felt*, keccak_ptr: felt*}() { | ||
alloc_locals; | ||
let output = output_ptr; | ||
let keccak_ptr_start = keccak_ptr - KECCAK_STATE_SIZE_FELTS; | ||
%{ | ||
from starkware.cairo.common.cairo_keccak.keccak_utils import keccak_func | ||
_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS) | ||
assert 0 <= _keccak_state_size_felts < 100 | ||
output_values = keccak_func(memory.get_range( | ||
ids.keccak_ptr_start, _keccak_state_size_felts)) | ||
segments.write_arg(ids.output, output_values) | ||
%} | ||
let keccak_ptr = keccak_ptr + KECCAK_STATE_SIZE_FELTS; | ||
|
||
return (); | ||
} | ||
|
||
func run_cairo_keccak{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { | ||
alloc_locals; | ||
|
||
let (output: felt*) = alloc(); | ||
let keccak_output = output; | ||
|
||
let (inputs: felt*) = alloc(); | ||
let inputs_start = inputs; | ||
fill_array(inputs, 9, 3, 0); | ||
|
||
let (state: felt*) = alloc(); | ||
let state_start = state; | ||
fill_array(state, 5, 25, 0); | ||
|
||
let n_bytes = 24; | ||
|
||
_prepare_block{keccak_ptr=output_ptr}(inputs=inputs, n_bytes=n_bytes, state=state); | ||
_block_permutation_cairo_keccak{keccak_ptr=output_ptr}(); | ||
|
||
local full_word: felt; | ||
%{ ids.full_word = int(ids.n_bytes >= 8) %} | ||
assert full_word = 1; | ||
|
||
let n_bytes = 8; | ||
local full_word: felt; | ||
%{ ids.full_word = int(ids.n_bytes >= 8) %} | ||
assert full_word = 1; | ||
|
||
let n_bytes = 7; | ||
local full_word: felt; | ||
%{ ids.full_word = int(ids.n_bytes >= 8) %} | ||
assert full_word = 0; | ||
|
||
return (); | ||
} | ||
|
||
func fill_array(array: felt*, base: felt, array_length: felt, iterator: felt) { | ||
if (iterator == array_length) { | ||
return (); | ||
} | ||
|
||
assert array[iterator] = base; | ||
|
||
return fill_array(array, base, array_length, iterator + 1); | ||
} | ||
|
||
func main{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { | ||
run_0_10_3(); | ||
run_cairo_keccak(); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
%builtins output pedersen range_check ecdsa bitwise ec_op | ||
|
||
from starkware.cairo.common.cairo_builtins import HashBuiltin | ||
from starkware.cairo.common.hash import hash2 | ||
|
||
func main{output_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, ecdsa_ptr, bitwise_ptr, ec_op_ptr}() { | ||
let (seed) = hash2{hash_ptr=pedersen_ptr}(0, 0); | ||
assert [output_ptr] = seed; | ||
let output_ptr = output_ptr + 1; | ||
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