Skip to content

Commit

Permalink
Merge branch 'main' into make-hints-code-public
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Fontana committed Apr 17, 2023
2 parents 39302d9 + 7fe94ef commit 490d727
Show file tree
Hide file tree
Showing 14 changed files with 805 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Description of the pull request changes and motivation.
- [ ] This change requires new documentation.
- [ ] Documentation has been added/updated.
- [ ] CHANGELOG has been updated.

175 changes: 168 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
## Cairo-VM Changelog

#### Upcoming Changes
* 0.11 Support
* Layouts update [#874](https://github.com/lambdaclass/cairo-rs/pull/874)
* Keccak builtin updated [#873](https://github.com/lambdaclass/cairo-rs/pull/873), [#883](https://github.com/lambdaclass/cairo-rs/pull/883)
* Changes to `ec_op` [#876](https://github.com/lambdaclass/cairo-rs/pull/876)
* Poseidon builtin [#875](https://github.com/lambdaclass/cairo-rs/pull/875)
* Renamed Felt to Felt252 [#899](https://github.com/lambdaclass/cairo-rs/pull/899)
* Added SegmentArenaBuiltinRunner [#913](https://github.com/lambdaclass/cairo-rs/pull/913)
* Added `program_segment_size` argument to `verify_secure_runner` & `run_from_entrypoint` [#928](https://github.com/lambdaclass/cairo-rs/pull/928)
* Added dynamic layout [#879](https://github.com/lambdaclass/cairo-rs/pull/879)
* `get_segment_size` was exposed [#934](https://github.com/lambdaclass/cairo-rs/pull/934)

#### [0.3.0-rc1] - 2023-04-13
* Derive Deserialize for ExecutionResources [#922](https://github.com/lambdaclass/cairo-rs/pull/922)
* Remove builtin names from VirtualMachine.builtin_runners [#921](https://github.com/lambdaclass/cairo-rs/pull/921)
* Implemented hints on common/ec.cairo [#888](https://github.com/lambdaclass/cairo-rs/pull/888)
* Changed `Memory.insert` argument types [#902](https://github.com/lambdaclass/cairo-rs/pull/902)
* feat: implemented `Deserialize` on Program by changing builtins field type to enum [#896](https://github.com/lambdaclass/cairo-rs/pull/896)
* Effective size computation from the VM exposed [#887](https://github.com/lambdaclass/cairo-rs/pull/887)
* Wasm32 Support! [#828](https://github.com/lambdaclass/cairo-rs/pull/828), [#893](https://github.com/lambdaclass/cairo-rs/pull/893)
* `MathError` added for math operation [#855](https://github.com/lambdaclass/cairo-rs/pull/855)
* Check for overflows in relocatable operations [#859](https://github.com/lambdaclass/cairo-rs/pull/859)
* Use `Relocatable` instead of `&MaybeRelocatable` in `load_data` and `get_range`[#860](https://github.com/lambdaclass/cairo-rs/pull/860) [#867](https://github.com/lambdaclass/cairo-rs/pull/867)
* Memory-related errors moved to `MemoryError` [#854](https://github.com/lambdaclass/cairo-rs/pull/854)
* Removed unused error variants
* Moved memory-related error variants to `MemoryError`
* Changed memory getters to return `MemoryError` instead of `VirtualMachineError`
* Changed all memory-related errors in hint from `HintError::Internal(VmError::...` to `HintError::Memory(MemoryError::...`
* feat: Builder pattern for `VirtualMachine` [#820](https://github.com/lambdaclass/cairo-rs/pull/820)
* Simplified `Memory::get` return type to `Option` [#852](https://github.com/lambdaclass/cairo-rs/pull/852)
* Improved idenitifier variable error handling [#851](https://github.com/lambdaclass/cairo-rs/pull/851)
* `CairoRunner::write_output` now prints missing and relocatable values [#853](https://github.com/lambdaclass/cairo-rs/pull/853)
* `VirtualMachineError::FailedToComputeOperands` error message expanded [#848](https://github.com/lambdaclass/cairo-rs/pull/848)
* Builtin names made public [#849](https://github.com/lambdaclass/cairo-rs/pull/849)
* `secure_run` flag moved to `CairoRunConfig` struct [#832](https://github.com/lambdaclass/cairo-rs/pull/832)
* `vm_core` error types revised and iimplemented `AddAssign` for `Relocatable` [#837](https://github.com/lambdaclass/cairo-rs/pull/837)
* `to_bigint` and `to_biguint` deprecated [#757](https://github.com/lambdaclass/cairo-rs/pull/757)
* `Memory` moved into `MemorySegmentManager` [#830](https://github.com/lambdaclass/cairo-rs/pull/830)
* To reduce the complexity of the VM's memory and enforce proper usage (as the memory and its segment manager are now a "unified" entity)
* Removed `memory` field from `VirtualMachine`
* Added `memory` field to `MemorySegmentManager`
* Removed `Memory` argument from methods where `MemorySegmentManager` is also an argument
* Added test macro `segments` (an extension of the `memory` macro)
* `Display` trait added to Memory struct [#812](https://github.com/lambdaclass/cairo-rs/pull/812)
* feat: Extensible VirtualMachineError and removed PartialEq trait [#783](https://github.com/lambdaclass/cairo-rs/pull/783)
* `VirtualMachineError::Other(anyhow::Error)` was added to allow to returning custom errors when using `cairo-rs`
* The `PartialEq` trait was removed from the `VirtualMachineError` enum
* VM hooks added as a conditional feature [#761](https://github.com/lambdaclass/cairo-rs/pull/761)
* Cairo-rs based testing tools such as cairo-foundry or those built by FuzzingLabs need access to the state of the VM at specific points during the execution.
* This PR adds the possibility for users of the cairo-rs lib to execute their custom additional code during the program execution.
* The Rust "feature" mechanism was used in order to guarantee that this ability is only available when the lib user needs it, and is not compiled when it's not required.
* Three hooks were created:
* before the first step
* before each step
* after each step
* ExecutionResource operations: add and substract [#774](https://github.com/lambdaclass/cairo-rs/pull/774), multiplication [#908](https://github.com/lambdaclass/cairo-rs/pull/908) , and `AddAssign` [#914](https://github.com/lambdaclass/cairo-rs/pull/914)


* Add missing hint on cairo_secp lib [#984]:

`BuiltinHintProcessor` now supports the following hint:
```python
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
from starkware.python.math_utils import div_mod

# Compute the slope.
x0 = pack(ids.pt0.x, PRIME)
y0 = pack(ids.pt0.y, PRIME)
x1 = pack(ids.pt1.x, PRIME)
y1 = pack(ids.pt1.y, PRIME)
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
```

* Implement hints on uint384 lib (Part 2) [#971](https://github.com/lambdaclass/cairo-rs/pull/971)

`BuiltinHintProcessor` now supports the following hint:

```python
memory[ap] = 1 if 0 <= (ids.a.d2 % PRIME) < 2 ** 127 else 0
```

* Add alternative hint code for hint on _block_permutation used by 0.10.3 whitelist [#958](https://github.com/lambdaclass/cairo-rs/pull/958)

`BuiltinHintProcessor` now supports the following hint:

```python
from starkware.cairo.common.keccak_utils.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)
```

* Make hints code `src/hint_processor/builtin_hint_processor/hint_code.rs` public [#988](https://github.com/lambdaclass/cairo-rs/pull/988)

Expand Down Expand Up @@ -106,6 +199,11 @@
ids.root.d1 = root_split[1]
ids.root.d2 = root_split[2]
```

* Re-export the `cairo-felt` crate as `cairo_vm::felt` [#981](https://github.com/lambdaclass/cairo-rs/pull/981)
* Removes the need of explicitly importing `cairo-felt` in downstream projects
and helps ensure there is no version mismatch caused by that

* Implement hint on `uint256_mul_div_mod`[#957](https://github.com/lambdaclass/cairo-rs/pull/957)

`BuiltinHintProcessor` now supports the following hint:
Expand All @@ -126,6 +224,69 @@

Used by the common library function `uint256_mul_div_mod`

* Add missing hint on cairo_secp lib [#986]:

`BuiltinHintProcessor` now supports the following hint:
```python
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
from starkware.python.math_utils import div_mod

# Compute the slope.
x = pack(ids.pt.x, PRIME)
y = pack(ids.pt.y, PRIME)
value = slope = div_mod(3 * x ** 2, 2 * y, SECP_P)
```

* Add missing hint on cairo_secp lib [#984]:
`BuiltinHintProcessor` now supports the following hint:
```python
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
from starkware.python.math_utils import div_mod

# Compute the slope.
x0 = pack(ids.pt0.x, PRIME)
y0 = pack(ids.pt0.y, PRIME)
x1 = pack(ids.pt1.x, PRIME)
y1 = pack(ids.pt1.y, PRIME)
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
```

* Add missing hint on cairo_secp lib [#989]:

`BuiltinHintProcessor` now supports the following hint:
```python
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P
q, r = divmod(pack(ids.val, PRIME), SECP_P)
assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}."
ids.q = q % PRIME
```

* Add missing hint on cairo_secp lib [#986]:
`BuiltinHintProcessor` now supports the following hint:
```python
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
from starkware.python.math_utils import div_mod

# Compute the slope.
x = pack(ids.pt.x, PRIME)
y = pack(ids.pt.y, PRIME)
value = slope = div_mod(3 * x ** 2, 2 * y, SECP_P)
```

* Add missing hint on cairo_secp lib [#984]:
`BuiltinHintProcessor` now supports the following hint:
```python
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
from starkware.python.math_utils import div_mod

# Compute the slope.
x0 = pack(ids.pt0.x, PRIME)
y0 = pack(ids.pt0.y, PRIME)
x1 = pack(ids.pt1.x, PRIME)
y1 = pack(ids.pt1.y, PRIME)
value = slope = div_mod(y0 - y1, x0 - x1, SECP_P)
```

* Move `Memory` into `MemorySegmentManager` [#830](https://github.com/lambdaclass/cairo-rs/pull/830)
* Structural changes:
* Remove `memory: Memory` field from `VirtualMachine`
Expand All @@ -148,7 +309,7 @@
* `Memory::relocate_memory` now moves data in the temporary memory relocated by a relocation rule to the real memory
* Aditional Notes:
* When relocating temporary memory produces clashes with pre-existing values in the real memory, an InconsistentMemory error is returned instead of keeping the last inserted value. This differs from the original implementation.

* Restrict addresses to Relocatable + fix some error variants used in signature.rs [#792](https://github.com/lambdaclass/cairo-rs/pull/792)
* Public Api Changes:
* Change `ValidationRule` inner type to `Box<dyn Fn(&Memory, &Relocatable) -> Result<Vec<Relocatable>, MemoryError>>`.
Expand Down Expand Up @@ -190,13 +351,13 @@

* Use CairoArg enum instead of Any in CairoRunner::run_from_entrypoint [#686](https://github.com/lambdaclass/cairo-rs/pull/686)
* Public Api changes:
* Remove `Result` from `MaybeRelocatable::mod_floor`, it now returns a `MaybeRelocatable`
* Remove `Result` from `MaybeRelocatable::mod_floor`, it now returns a `MaybeRelocatable`
* Add struct `CairoArg`
* Change `arg` argument of `CairoRunner::run_from_entrypoint` from `Vec<&dyn Any>` to `&[&CairoArg]`
* Remove argument `typed_args` from `CairoRunner::run_from_entrypoint`
* Remove no longer used method `gen_typed_arg` from `VirtualMachine` & `MemorySegmentManager`
* Add methods `MemorySegmentManager::gen_cairo_arg` & `MemorySegmentManager::write_simple_args` as typed counterparts to `MemorySegmentManager::gen_arg` & `MemorySegmentManager::write_arg`

#### [0.1.1] - 2023-01-11

* Add input file contents to traceback [#666](https://github.com/lambdaclass/cairo-rs/pull/666/files)
Expand All @@ -210,8 +371,8 @@
* `VirtualMachineError`s produced by `HintProcessor::execute_hint()` will be wrapped in a `VirtualMachineError::Hint` error containing their hint_index
* `get_location()` now receives an an optional usize value `hint_index`, used to obtain hint locations
* Default implementation of compile_hint [#680](https://github.com/lambdaclass/cairo-rs/pull/680)
* Internal changes:
* Make the `compile_hint` implementation which was in the `BuiltinHintProcessor` the default implementation in the trait.
* Internal changes:
* Make the `compile_hint` implementation which was in the `BuiltinHintProcessor` the default implementation in the trait.
* Add new error type `HintError` [#676](https://github.com/lambdaclass/cairo-rs/pull/676)
* Public Api changes:
* `HintProcessor::execute_hint()` now returns a `HintError` instead of a `VirtualMachineError`
Expand All @@ -221,7 +382,7 @@
* `DictManager`, its dictionaries, and all dict module hints implemented in rust now use `MaybeRelocatable` for keys and values instead of `BigInt`
* Add helper functions that allow extracting ids variables as `MaybeRelocatable`: `get_maybe_relocatable_from_var_name` & `get_maybe_relocatable_from_reference`
* Change inner value type of dict-related `HintError` variants to `MaybeRelocatable`

* Implement `substitute_error_message_attribute_references` [#689] (https://github.com/lambdaclass/cairo-rs/pull/689)
* Public Api changes:
* Remove `error_message_attributes` field from `VirtualMachine`, and `VirtualMachine::new`
Expand All @@ -232,7 +393,7 @@

#### [0.1.0] - 2022-12-30
* Add traceback to VmException [#657](https://github.com/lambdaclass/cairo-rs/pull/657)
* Public API changes:
* Public API changes:
* `traceback` field added to `VmException` struct
* `pub fn from_vm_error(runner: &CairoRunner, error: VirtualMachineError, pc: usize) -> Self` is now `pub fn from_vm_error(runner: &CairoRunner, vm: &VirtualMachine, error: VirtualMachineError) -> Self`
* `pub fn get_location(pc: &usize, runner: &CairoRunner) -> Option<Location>` is now `pub fn get_location(pc: usize, runner: &CairoRunner) -> Option<Location>`
Expand Down
37 changes: 37 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Release Process
- [ ] Pull latest from `main` branch.
`git checkout main && git pull origin main`
- [ ] Determine release version string dependending on whether changes included
in changelog are API breaking, it's a release candidate, etc.
The release string should have the format "vX.Y.Z", with a possible
trailing "-rcN" and follow [semantic versioning](https://semver.org/).
- [ ] Checkout branch named `release-N` where N is the version string.
`git checkout -b release-N`
- [ ] Update the version field in the package entry of `Cargo.toml` files.
- The versions must be the same.
- There are 4 relevant `Cargo.toml` files in the repo:
- `Cargo.toml`: update the version string.
- `cairo-vm-cli/Cargo.toml`: update the version string and also the `cairo-vm` dependency version to match the above.
- `felt/Cargo.toml`: update the version string.
- `deps/parse-hyperlinks/Cargo.toml`: this vendored dependency needs its version bumped, but does not need to match the other crate versions.
- [Here](https://github.com/lambdaclass/cairo-rs/pull/948/files) is an example pull request with these changes.
- [ ] Run `cargo update` and `git add Cargo.lock`
- [ ] Update `CHANGELOG.md`:
- Verify that the changelog is up to date.
- Add a title with the release version string just below the `Upcoming Changes` section.
- [ ] Commit your changes, push your branch, and create a pull request.
- [ ] Merge after CI and review passes.
- [ ] Pull latest from `main` again.
- [ ] Tag commit with version string and push tag.
`git tag -a <version string> -m "Release..."`
- [ ] Watch the `publish` workflow run in Github Actions.
- [ ] Verify all the crates are available on crates.io with the correct versions.
- [cairo-vm](https://crates.io/crates/cairo-vm)
- [cairo-felt](https://crates.io/crates/cairo-felt)
- [cairo-take-until-unbalanced](https://crates.io/crates/cairo-take_until_unbalanced)
- [ ] Create a release in Github.
- Select the recently created tag.
- Set the title to the version string.
- If it is a release candidate, mark it as a draft release.
- [ ] Announce release through corresponding channels.

88 changes: 88 additions & 0 deletions cairo_programs/_keccak_alternative_hint.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
%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{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();

return _keccak(
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();

return (output=keccak_ptr - KECCAK_STATE_SIZE_FELTS);
}

func _block_permutation{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 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 ();
}

0 comments on commit 490d727

Please sign in to comment.