Skip to content

Commit

Permalink
update cairo programs and fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed Jun 14, 2024
1 parent 86044a8 commit bfa79e0
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 13 deletions.
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ; \
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cairo_programs/field_arithmetic.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ from starkware.cairo.common.math_cmp import is_le
from starkware.cairo.common.pow import pow
from starkware.cairo.common.registers import get_ap, get_fp_and_pc
from starkware.cairo.common.uint256 import Uint256
from uint384 import u384, Uint384, Uint384_expand, SHIFT, HALF_SHIFT
from uint384_extension import u384_ext, Uint768
from cairo_programs.uint384 import u384, Uint384, Uint384_expand, SHIFT, HALF_SHIFT
from cairo_programs.uint384_extension import u384_ext, Uint768

// Functions for operating elements in a finite field F_p (i.e. modulo a prime p), with p of at most 384 bits
namespace field_arithmetic {
Expand Down
44 changes: 44 additions & 0 deletions cairo_programs/keccak.cairo
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 ();
}
148 changes: 148 additions & 0 deletions cairo_programs/keccak_alternative_hint.cairo
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 ();
}
11 changes: 11 additions & 0 deletions cairo_programs/pedersen_extra_builtins.cairo
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 ();
}
4 changes: 2 additions & 2 deletions cairo_programs/secp_integration_tests.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
%builtins range_check

from starkware.cairo.common.cairo_secp.bigint import (
BigInt3,
bigint_mul,
nondet_bigint3,
bigint_to_uint256,
uint256_to_bigint,
)
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3, SumBigInt3
from starkware.cairo.common.cairo_secp.signature import (
get_generator_point,
validate_signature_entry,
Expand Down Expand Up @@ -86,7 +86,7 @@ func test_operations{range_check_ptr}(point: EcPoint) {
let (zero_uint, _) = uint256_add(slope_uint, neg_slope);
let (zero) = uint256_to_bigint(zero_uint);

let (is_z) = is_zero(zero);
let (is_z) = is_zero(SumBigInt3(d0=zero.d0, d1=zero.d1, d2=zero.d2));
assert is_z = 1;

let (pow2, scaled) = ec_mul_inner(point, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion cairo_programs/uint384_extension.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from starkware.cairo.common.math_cmp import is_le
from starkware.cairo.common.pow import pow
from starkware.cairo.common.registers import get_ap, get_fp_and_pc
// Import uint384 files
from uint384 import u384, Uint384, Uint384_expand, ALL_ONES
from cairo_programs.uint384 import u384, Uint384, Uint384_expand, ALL_ONES
// Functions for operating 384-bit integers with 768-bit integers

// Represents an integer in the range [0, 2^768).
Expand Down
2 changes: 1 addition & 1 deletion cairo_programs/uint384_extension_test.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%builtins range_check

from uint384_extension import u384_ext, Uint768, Uint384
from cairo_programs.uint384_extension import u384_ext, Uint768, Uint384

func test_uint384_extension_operations{range_check_ptr}() {
// Test unsigned_div_rem_uint768_by_uint384
Expand Down

0 comments on commit bfa79e0

Please sign in to comment.