Skip to content

Commit d02b329

Browse files
fmolettapefontanaMegaRedHandOppen
authored andcommitted
feat: Add feature lambdaworks-felt to felt & cairo-vm crates (lambdaclass#1281)
* wip * Manually implement some common derives Also comment `from` impls for primitive numbers and (De)Serialize derives * Implement FromPrimitive * Implement ToPrimitive * Add BitAnd/Xor/Or implementations * Implement bit shift operators * Remove Signed implementation * Remove Integer impl * Impl Bounded * Impl from_str_radix * Add iter_u64_digits impl * Add Add impls * Add Sub impls * Patch arbitrary * Fix some warnings * Implement parse_bytes * Implement utility methods for tests * Fix test compilation errors * Add From impl for signed primitive nums * Impl From<BigInt> * Re-add bits fn * Impl Signed * Impl (De)Serialize * Fix compile errors and clippy suggestions * Pin cairo 1 compiler version * Reorder impls * Fix compile error * Fix various errors (tests pass!) * Remove to_bytes_be * Fix panicky from_bytes_be * Fix is_positive * Fix str conversions * Add documentation * Fix from_i64 accepting negative numbers * Use BigUint in from_bytes_be * Remove unneded field macro * Pin lambdaworks commit * Fix keccak * Fix from_i64 condition was reversed * Change Debug::fmt to return number in decimal * Appease clippy in felt crate * Silence clippy warning (for now) * Fix nostd error * Update lambdaworks to latest revision * Update rust version in CI * Change sqrt for lambdaworks' * Manually build BigDigits on to_biguint * Use a bigger number of iterations for square bench * Update lambdaworks-math revision * Fix sqrt tests and Shl/Shr impl (+add tests) * Update Cargo.lock * Appease clippy and fix Shr<usize> * Square input instead of using prop_assume * Revert the change to Shr * Appease clippy * Use bits instead of shl in range check * Remove `FeltBigInt` * Add lamdaworks-benchmarks.sh * Add lamdaworks-benchmarks.md * Update lamdaworks-benchmarks.md * Update commits * Update .md commits * Looooong benchmark * Remove bigbox clippy allow * Update lambdaworks to latest, and change AddAssign * Remove `-P` option in `xargs` In the measurements we got through `perf`, there were 3 to 4 times more page faults compared to base. This can be explained by the runner using swap memory because of the increase in memory usage. We can fix it by reducing the amount of processes ran in parallel by xargs (2 -> 1) * Change `to_(b|l)e_bytes` to not use lw primitives * Change comments in `Add<&Felt252> for u64` * Fix: wasn't indexing properly :P * Override default `div_mod_floor` impl for Felt252 * Disable swap memory before benchmarking * Reduce number of warmups and runs * Optimize `assert_le_felt` (used in math_integration_benchmark) * Use constant for zero() function * Extract division by constant * Use BigUint in assert_le_felt This part uses comparisons and integer division (that use `to_biguint`), so it's better to use `BigUints` directly. * Avoid calling `BigInt::abs` * Update changelog * Add tests for felt * Add TODO * Add other texts * Update lambdaworks to latest * Revert hyperfine arguments to main * Remove unneeded clones and into_owneds * Remove unneeded references and clones * Add BREAKING note to changelog * Make Felt252::one just copy a constant * Impl From<bool> for Felt252 * Change some uses of get_ref with get_mut_ref Using `get_ref` and later updating the variable with an `exec_scopes.insert_value(...)` causes two lookups in a hashmap, along with two creation of `String` from a slice. This change reduces it to just a single lookup and `String` creation. * Unify mem*_continue_* functions * Run benchmarks sequentially to avoid mem issues * Use div_mod_floor instead of div and mod * Use BigUint for non-modular calculations * Add TODO * Include both lib.rs * Fix lib_bigint_felt * Add test-lambdaworks-felt workflow * Fix failling example * Move extern crate import to lib.rs * Update changelog * Fix changelog * Fix example * Remove benchmark docs * Remove clone * Move crate-level attribute to lib.rs * Fix changelog * Remove blank line in toml * Use one line cfg directives * Remove reference * Restore clone * Fix doc test * Add `lambdaworks-felt` feature to vm crate * Add instructions to (de)activate the new feature * Use different matrix group for lambdaworks felt in CI * Move the sections a bit * Update lambdaworks-math version to 0.1.1 * Invert the part talking about features --------- Co-authored-by: Pedro Fontana <[email protected]> Co-authored-by: Tomá <[email protected]> Co-authored-by: Mario Rugiero <[email protected]>
1 parent 2f48247 commit d02b329

30 files changed

+3647
-1819
lines changed

.github/workflows/bench.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ jobs:
1414
build:
1515
runs-on: ubuntu-20.04
1616
steps:
17-
- name: Install Rust 1.66.1
18-
uses: actions-rs/toolchain@v1
17+
- name: Install Rust
18+
uses: dtolnay/rust-toolchain@1.69.0
1919
with:
20-
toolchain: 1.66.1
21-
override: true
2220
components: rustfmt, clippy
2321
- uses: actions/checkout@v3
2422
- name: Python3 Build

.github/workflows/hint_accountant.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-22.04
1313
steps:
1414
- name: Install Rust toolchain
15-
uses: dtolnay/rust-toolchain@1.66.1
15+
uses: dtolnay/rust-toolchain@1.69.0
1616
- name: Set up Cargo cache
1717
uses: Swatinem/rust-cache@v2
1818
- name: Checkout

.github/workflows/hyperfine.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ jobs:
7474

7575
- name: Install Rust
7676
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
77-
uses: dtolnay/rust-toolchain@stable
78-
with:
79-
toolchain: 1.66.1
77+
uses: dtolnay/[email protected]
8078

8179
- name: Checkout
8280
if: ${{ steps.cache.outputs.cache-hit != 'true' }}

.github/workflows/iai_main.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ jobs:
99
runs-on: ubuntu-22.04
1010
steps:
1111
- name: Install Rust
12-
uses: actions-rs/toolchain@v1
13-
with:
14-
toolchain: 1.66.1
15-
override: true
16-
profile: minimal
12+
uses: dtolnay/[email protected]
1713
- name: Python3 Build
1814
uses: actions/setup-python@v4
1915
with:

.github/workflows/iai_pr.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ jobs:
99
runs-on: ubuntu-22.04
1010
steps:
1111
- name: Install Rust
12-
uses: actions-rs/toolchain@v1
13-
with:
14-
toolchain: 1.66.1
15-
override: true
16-
profile: minimal
12+
uses: dtolnay/[email protected]
1713
- name: Python3 Build
1814
uses: actions/setup-python@v4
1915
with:
@@ -46,11 +42,7 @@ jobs:
4642
runs-on: ubuntu-22.04
4743
steps:
4844
- name: Install Rust
49-
uses: actions-rs/toolchain@v1
50-
with:
51-
toolchain: 1.66.1
52-
override: true
53-
profile: minimal
45+
uses: dtolnay/[email protected]
5446
- name: Python3 Build
5547
uses: actions/setup-python@v4
5648
with:

.github/workflows/publish.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ jobs:
1313
- name: Checkout sources
1414
uses: actions/checkout@v2
1515
- name: Install stable toolchain
16-
uses: actions-rs/toolchain@v1
17-
with:
18-
profile: minimal
19-
toolchain: stable
20-
override: true
16+
uses: dtolnay/[email protected]
2117
- name: Publish crate cairo-take_until_unbalanced
2218
env:
2319
CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
@@ -34,4 +30,3 @@ jobs:
3430
env:
3531
CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
3632
run: cargo publish --token ${CRATES_TOKEN} --all-features -p cairo-vm
37-

.github/workflows/rust.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ jobs:
170170
strategy:
171171
fail-fast: false
172172
matrix:
173+
special-features: ["", "lambdaworks-felt"]
173174
target: [ test, test-no_std, test-wasm ]
174175
# TODO: features
175176
name: Run tests
@@ -221,14 +222,14 @@ jobs:
221222
# FIXME: we need to update the Makefile to do this correctly
222223
case ${{ matrix.target }} in
223224
'test')
224-
cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features "cairo-1-hints, test_utils"
225+
cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features "cairo-1-hints, test_utils, ${{ matrix.special_features }}"
225226
;;
226227
'test-no_std')
227-
cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --no-default-features
228+
cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --no-default-features --features "${{ matrix.special_features }}"
228229
;;
229230
'test-wasm')
230231
# NOTE: release mode is needed to avoid "too many locals" error
231-
wasm-pack test --release --node vm --no-default-features
232+
wasm-pack test --release --node vm --no-default-features --features "${{ matrix.special_features }}"
232233
;;
233234
esac
234235

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#### Upcoming Changes
44

5+
* feat: Add feature `lambdaworks-felt` to `felt` & `cairo-vm` crates [#1218](https://github.com/lambdaclass/cairo-rs/pull/1281)
6+
7+
Changes under this feature:
8+
* `Felt252` now uses _lambdaworks_' `FieldElement` internally
9+
* BREAKING: some methods of `Felt252` were removed, namely: `modpow` and `to_bytes_be`
10+
511
#### [0.7.0] - 2023-6-26
612

713
* BREAKING: Integrate `RunResources` logic into `HintProcessor` trait [#1274](https://github.com/lambdaclass/cairo-rs/pull/1274)

Cargo.lock

Lines changed: 76 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,26 @@ These dependencies are only necessary in order to run the original VM, compile C
8181

8282
## 🚀 Usage
8383

84-
### Running cairo-rs
84+
### Adding cairo-rs as a dependency
85+
86+
You can add the following to your rust project's `Cargo.toml`:
87+
88+
```toml
89+
cairo-vm = { version = '0.7.0', features = ["lambdaworks-felt"] }
90+
```
91+
92+
The `features = ["lambdaworks-felt"]` part adds usage of [`lambdaworks-math`](https://github.com/lambdaclass/lambdaworks) as the backend for `Felt252`. This improves performance by more than 20%, and will be the default in the future.
93+
94+
### Running cairo-rs from CLI
8595

8696
To run programs from the command line, first compile the repository from the cairo-vm-cli folder:
8797

8898
```bash
89-
cd cairo-vm-cli; cargo build --release; cd ..
99+
cd cairo-vm-cli; cargo build --release -F lambdaworks-felt; cd ..
90100
```
91101

102+
The `-F lambdaworks-felt` part adds usage of [`lambdaworks-math`](https://github.com/lambdaclass/lambdaworks) as the backend for `Felt252`. This improves performance by more than 20%, and will be the default in the future.
103+
92104
Once the binary is built, it can be found in `target/release/` under the name `cairo-rvm-cli`.
93105

94106
To compile a program, use `cairo-compile [path_to_the_.cairo_file] --output [desired_path_of_the_compiled_.json_file]`. For example:

0 commit comments

Comments
 (0)