Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add feature lambdaworks-felt to felt & cairo-vm crates #1281

Merged
merged 135 commits into from Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
061064e
wip
pefontana May 29, 2023
cb0ee30
Manually implement some common derives
MegaRedHand May 29, 2023
2d1659e
Implement FromPrimitive
MegaRedHand May 29, 2023
fbe3676
Implement ToPrimitive
MegaRedHand May 29, 2023
9cf6988
Add BitAnd/Xor/Or implementations
MegaRedHand May 29, 2023
e0f18f4
Implement bit shift operators
MegaRedHand May 29, 2023
6747209
Remove Signed implementation
MegaRedHand May 29, 2023
d653271
Remove Integer impl
MegaRedHand May 29, 2023
fc0ca5e
Impl Bounded
MegaRedHand May 29, 2023
e164ef5
Impl from_str_radix
MegaRedHand May 29, 2023
1a45f45
Add iter_u64_digits impl
MegaRedHand May 29, 2023
a74de07
Add Add impls
MegaRedHand May 29, 2023
3a32072
Add Sub impls
MegaRedHand May 29, 2023
2088c18
Patch arbitrary
MegaRedHand May 29, 2023
0093e6f
Fix some warnings
MegaRedHand May 29, 2023
c2a2c2c
Implement parse_bytes
MegaRedHand May 29, 2023
bbb1758
Implement utility methods for tests
MegaRedHand May 29, 2023
84dec45
Fix test compilation errors
MegaRedHand May 29, 2023
4ad7493
Add From impl for signed primitive nums
MegaRedHand May 29, 2023
c854aa5
Impl From<BigInt>
MegaRedHand May 29, 2023
a8a8aa5
Re-add bits fn
MegaRedHand May 29, 2023
8228211
Impl Signed
MegaRedHand May 30, 2023
7a44c1a
Impl (De)Serialize
MegaRedHand May 30, 2023
0f23d8e
Fix compile errors and clippy suggestions
MegaRedHand May 30, 2023
c8e51c8
Pin cairo 1 compiler version
MegaRedHand May 30, 2023
ff3f93e
Reorder impls
MegaRedHand May 30, 2023
4c38bac
Fix compile error
MegaRedHand May 30, 2023
5185ea8
Fix various errors (tests pass!)
MegaRedHand May 30, 2023
b6ad868
Remove to_bytes_be
MegaRedHand May 30, 2023
a1e6b74
Fix panicky from_bytes_be
MegaRedHand May 30, 2023
ae927b7
Fix is_positive
MegaRedHand May 30, 2023
37ef285
Fix str conversions
MegaRedHand May 30, 2023
f49f2f7
Add documentation
MegaRedHand May 30, 2023
92f57a8
Fix from_i64 accepting negative numbers
MegaRedHand May 30, 2023
5881d8c
Use BigUint in from_bytes_be
MegaRedHand May 30, 2023
a150ac7
Remove unneded field macro
MegaRedHand May 30, 2023
a5c25ff
Pin lambdaworks commit
MegaRedHand May 30, 2023
3978a4a
Fix keccak
MegaRedHand May 30, 2023
70f28b8
Fix from_i64 condition was reversed
MegaRedHand May 30, 2023
37e9a45
Change Debug::fmt to return number in decimal
MegaRedHand May 30, 2023
a2b003e
Appease clippy in felt crate
MegaRedHand May 30, 2023
168ba45
Merge branch 'main' into lambdaworks-felt
MegaRedHand May 30, 2023
647345b
Silence clippy warning (for now)
MegaRedHand May 30, 2023
ed67ccc
Fix nostd error
MegaRedHand May 30, 2023
7b6ecfd
Update lambdaworks to latest revision
MegaRedHand May 31, 2023
06d206f
Update rust version in CI
MegaRedHand May 31, 2023
0d0c0b2
Merge branch 'main' into lambdaworks-felt
MegaRedHand May 31, 2023
c2ca57b
Change sqrt for lambdaworks'
MegaRedHand May 31, 2023
647ff67
Manually build BigDigits on to_biguint
MegaRedHand Jun 1, 2023
aa15dc5
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 1, 2023
5b2d505
Use a bigger number of iterations for square bench
MegaRedHand Jun 1, 2023
c2ee0cf
Update lambdaworks-math revision
MegaRedHand Jun 1, 2023
4a17dc1
Fix sqrt tests and Shl/Shr impl (+add tests)
MegaRedHand Jun 1, 2023
939ee06
Update Cargo.lock
MegaRedHand Jun 1, 2023
175289a
Appease clippy and fix Shr<usize>
MegaRedHand Jun 1, 2023
4667b3e
Square input instead of using prop_assume
MegaRedHand Jun 1, 2023
63d1360
Revert the change to Shr
MegaRedHand Jun 1, 2023
5db1ebe
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 1, 2023
413b813
Appease clippy
MegaRedHand Jun 1, 2023
808c803
Use bits instead of shl in range check
MegaRedHand Jun 1, 2023
6a8ca26
Remove `FeltBigInt`
MegaRedHand Jun 2, 2023
f3a7a0d
Add lamdaworks-benchmarks.sh
pefontana Jun 2, 2023
987a114
Add lamdaworks-benchmarks.md
pefontana Jun 2, 2023
7dd7d58
Update lamdaworks-benchmarks.md
pefontana Jun 2, 2023
60b3e37
Update commits
pefontana Jun 2, 2023
906adfd
Update .md commits
pefontana Jun 2, 2023
c2b7f18
Looooong benchmark
Oppen Jun 2, 2023
3ec78e4
Remove bigbox clippy allow
MegaRedHand Jun 5, 2023
96f12a4
Update lambdaworks to latest, and change AddAssign
MegaRedHand Jun 5, 2023
b64b0ca
Remove `-P` option in `xargs`
MegaRedHand Jun 5, 2023
400effe
Change `to_(b|l)e_bytes` to not use lw primitives
MegaRedHand Jun 5, 2023
9ba90c3
Change comments in `Add<&Felt252> for u64`
MegaRedHand Jun 5, 2023
03cb2dd
Fix: wasn't indexing properly :P
MegaRedHand Jun 5, 2023
171520c
Override default `div_mod_floor` impl for Felt252
MegaRedHand Jun 5, 2023
0be0200
Merge branch 'main' into lambdaworks-felt
pefontana Jun 5, 2023
11bc48a
Disable swap memory before benchmarking
MegaRedHand Jun 6, 2023
0b1a15e
Reduce number of warmups and runs
MegaRedHand Jun 6, 2023
9bec2cf
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 6, 2023
82e5882
Optimize `assert_le_felt`
MegaRedHand Jun 6, 2023
5a366e9
Avoid calling `BigInt::abs`
MegaRedHand Jun 6, 2023
8602139
Update changelog
MegaRedHand Jun 6, 2023
6009eba
Add tests for felt
MegaRedHand Jun 7, 2023
9411f03
Add TODO
MegaRedHand Jun 7, 2023
b310376
Add other texts
MegaRedHand Jun 7, 2023
58920da
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 7, 2023
bba3a59
Update lambdaworks to latest
MegaRedHand Jun 7, 2023
c276737
Revert hyperfine arguments to main
MegaRedHand Jun 8, 2023
4bce996
Remove unneeded clones and into_owneds
MegaRedHand Jun 8, 2023
fbfa88c
Remove unneeded references and clones
MegaRedHand Jun 8, 2023
3e67fbb
Add BREAKING note to changelog
MegaRedHand Jun 8, 2023
026ebce
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 8, 2023
308f580
Make Felt252::one just copy a constant
MegaRedHand Jun 8, 2023
6508d21
Impl From<bool> for Felt252
MegaRedHand Jun 8, 2023
06b0c82
Change some uses of get_ref with get_mut_ref
MegaRedHand Jun 8, 2023
37ba2a4
Unify mem*_continue_* functions
MegaRedHand Jun 8, 2023
ed2cd48
Run benchmarks sequentially to avoid mem issues
MegaRedHand Jun 8, 2023
6c00c45
Use div_mod_floor instead of div and mod
MegaRedHand Jun 9, 2023
073f44c
Use BigUint for non-modular calculations
MegaRedHand Jun 9, 2023
376c723
Add TODO
MegaRedHand Jun 9, 2023
b441d2f
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 10, 2023
5926939
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 12, 2023
4793edb
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 13, 2023
6f73dba
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 13, 2023
766a67c
Merge branch 'main' into lambdaworks-felt
MegaRedHand Jun 15, 2023
eeecc8b
Merge branch 'main' into lambdaworks-felt
pefontana Jun 21, 2023
90c48de
Merge branch 'main' into lambdaworks-felt
pefontana Jun 23, 2023
785f5c9
Merge branch 'main' into lambdaworks-felt
pefontana Jun 26, 2023
041da94
Include both lib.rs
fmoletta Jun 26, 2023
2dd2be4
Fix lib_bigint_felt
fmoletta Jun 26, 2023
dff778f
Merge branch 'main' of github.com:lambdaclass/cairo-rs into lambdawor…
fmoletta Jun 26, 2023
dc6ecae
Add test-lambdaworks-felt workflow
fmoletta Jun 26, 2023
2ccb08e
Fix failling example
fmoletta Jun 26, 2023
10814cc
Move extern crate import to lib.rs
fmoletta Jun 26, 2023
ddff68b
Update changelog
fmoletta Jun 26, 2023
b85a6c3
Merge branch 'main' of github.com:lambdaclass/cairo-rs into lambdawor…
fmoletta Jun 26, 2023
cfb15d9
Fix changelog
fmoletta Jun 26, 2023
bdb5452
Fix example
fmoletta Jun 26, 2023
59218ab
Remove benchmark docs
fmoletta Jun 26, 2023
057d94e
Remove clone
fmoletta Jun 26, 2023
4add087
Move crate-level attribute to lib.rs
fmoletta Jun 26, 2023
f528c5e
Fix changelog
fmoletta Jun 26, 2023
2182ea8
Remove blank line in toml
fmoletta Jun 26, 2023
4a47e1a
Use one line cfg directives
fmoletta Jun 26, 2023
bb5c2a2
Merge branch 'main' of github.com:lambdaclass/cairo-rs into lambdawor…
fmoletta Jun 26, 2023
ac8cb98
Remove reference
fmoletta Jun 26, 2023
dd7b797
Restore clone
fmoletta Jun 26, 2023
ef6f1a5
Fix doc test
fmoletta Jun 26, 2023
ed2ed3c
Add `lambdaworks-felt` feature to vm crate
fmoletta Jun 26, 2023
779da51
Add instructions to (de)activate the new feature
MegaRedHand Jun 26, 2023
6639d81
Use different matrix group for lambdaworks felt in CI
fmoletta Jun 26, 2023
c264536
Merge branch 'lambdaworks-felt-feature' of github.com:lambdaclass/cai…
fmoletta Jun 26, 2023
b01bc2b
Move the sections a bit
MegaRedHand Jun 26, 2023
3818bbe
Update lambdaworks-math version to 0.1.1
fmoletta Jun 26, 2023
55b73da
Merge branch 'lambdaworks-felt-feature' of github.com:lambdaclass/cai…
fmoletta Jun 26, 2023
acc1a36
Invert the part talking about features
MegaRedHand Jun 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/bench.yml
Expand Up @@ -14,11 +14,9 @@ jobs:
build:
runs-on: ubuntu-20.04
steps:
- name: Install Rust 1.66.1
uses: actions-rs/toolchain@v1
- name: Install Rust
uses: dtolnay/rust-toolchain@1.69.0
with:
toolchain: 1.66.1
override: true
components: rustfmt, clippy
- uses: actions/checkout@v3
- name: Python3 Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/hint_accountant.yml
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@1.66.1
uses: dtolnay/rust-toolchain@1.69.0
- name: Set up Cargo cache
uses: Swatinem/rust-cache@v2
- name: Checkout
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/hyperfine.yml
Expand Up @@ -74,9 +74,7 @@ jobs:

- name: Install Rust
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.66.1
uses: dtolnay/rust-toolchain@1.69.0

- name: Checkout
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/iai_main.yml
Expand Up @@ -9,11 +9,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.66.1
override: true
profile: minimal
uses: dtolnay/rust-toolchain@1.69.0
- name: Python3 Build
uses: actions/setup-python@v4
with:
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/iai_pr.yml
Expand Up @@ -9,11 +9,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.66.1
override: true
profile: minimal
uses: dtolnay/rust-toolchain@1.69.0
- name: Python3 Build
uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -46,11 +42,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.66.1
override: true
profile: minimal
uses: dtolnay/rust-toolchain@1.69.0
- name: Python3 Build
uses: actions/setup-python@v4
with:
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/publish.yml
Expand Up @@ -13,11 +13,7 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
uses: dtolnay/rust-toolchain@1.69.0
- name: Publish crate cairo-take_until_unbalanced
env:
CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
Expand All @@ -34,4 +30,3 @@ jobs:
env:
CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --token ${CRATES_TOKEN} --all-features -p cairo-vm

5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Expand Up @@ -170,7 +170,7 @@ jobs:
strategy:
fail-fast: false
matrix:
target: [ test, test-no_std, test-wasm ]
target: [ test, test-no_std, test-wasm, test-lambdaworks-felt ]
Oppen marked this conversation as resolved.
Show resolved Hide resolved
# TODO: features
name: Run tests
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -230,6 +230,9 @@ jobs:
# NOTE: release mode is needed to avoid "too many locals" error
wasm-pack test --release --node vm --no-default-features
;;
'test-lambdaworks-felt')
cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features "cairo-1-hints, test_utils, lambdaworks-felt"
;;
esac

- name: Save coverage
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,12 @@

#### Upcoming Changes

* feat: Add feature `lambdaworks-felt` to `felt` & `cairo-vm` crates [#1218](https://github.com/lambdaclass/cairo-rs/pull/1281)

Changes under this feature:
* `Felt252` now uses _lambdaworks_' `FieldElement` internally
* BREAKING: some methods of `Felt252` were removed, namely: `modpow` and `to_bytes_be`

#### [0.7.0] - 2023-6-26

* BREAKING: Integrate `RunResources` logic into `HintProcessor` trait [#1274](https://github.com/lambdaclass/cairo-rs/pull/1274)
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deps/parse-hyperlinks/src/lib.rs
Expand Up @@ -15,7 +15,7 @@ use nom::IResult;
/// ```
/// use nom::bytes::complete::tag;
/// use nom::sequence::delimited;
/// use parse_hyperlinks::take_until_unbalanced;
/// use cairo_take_until_unbalanced::take_until_unbalanced;
///
/// let mut parser = delimited(tag("<"), take_until_unbalanced('<', '>'), tag(">"));
/// assert_eq!(parser("<<inside>inside>abc"), Ok(("abc", "<inside>inside")));
Expand Down
2 changes: 2 additions & 0 deletions felt/Cargo.toml
Expand Up @@ -9,6 +9,7 @@ description = "Field elements representation for the Cairo VM"
default = ["std"]
std = []
alloc = []
lambdaworks-felt = ["dep:lambdaworks-math"]

[dependencies]
num-integer = { version = "0.1.45", default-features = false }
Expand All @@ -18,6 +19,7 @@ lazy_static = { version = "1.4.0", default-features = false, features = [
"spin_no_std",
] }
serde = { version = "1.0", features = ["derive"], default-features = false }
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks", rev = "5d61e79", default-features = false, optional=true }

[dev-dependencies]
proptest = "1.1.0"
Expand Down
File renamed without changes.
47 changes: 47 additions & 0 deletions felt/src/arbitrary_lambdaworks.rs
@@ -0,0 +1,47 @@
use lambdaworks_math::{field::element::FieldElement, unsigned_integer::element::UnsignedInteger};
use num_traits::Zero;
use proptest::prelude::*;

use crate::{Felt252, FIELD_HIGH, FIELD_LOW};

/// Returns a [`Strategy`] that generates any valid Felt252
fn any_felt252() -> impl Strategy<Value = Felt252> {
(0..=FIELD_HIGH)
// turn range into `impl Strategy`
.prop_map(|x| x)
// choose second 128-bit limb capped by first one
.prop_flat_map(|high| {
let low = if high == FIELD_HIGH {
(0..FIELD_LOW).prop_map(|x| x).sboxed()
} else {
any::<u128>().sboxed()
};
(Just(high), low)
})
// turn (u128, u128) into limbs array and then into Felt252
.prop_map(|(high, low)| {
let limbs = [
(high >> 64) as u64,
(high & ((1 << 64) - 1)) as u64,
(low >> 64) as u64,
(low & ((1 << 64) - 1)) as u64,
];
FieldElement::new(UnsignedInteger::from_limbs(limbs))
})
.prop_map(|value| Felt252 { value })
}

/// Returns a [`Strategy`] that generates any nonzero Felt252
pub fn nonzero_felt252() -> impl Strategy<Value = Felt252> {
any_felt252().prop_filter("is zero", |x| !x.is_zero())
}

impl Arbitrary for Felt252 {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
any_felt252().sboxed()
}

type Strategy = SBoxedStrategy<Self>;
}
8 changes: 1 addition & 7 deletions felt/src/bigint_felt.rs
Expand Up @@ -12,7 +12,7 @@ use core::{
},
};

use crate::{FeltOps, ParseFeltError};
use crate::{lib_bigint_felt::FeltOps, ParseFeltError};

pub const FIELD_HIGH: u128 = (1 << 123) + (17 << 64); // this is equal to 10633823966279327296825105735305134080
pub const FIELD_LOW: u128 = 1;
Expand Down Expand Up @@ -840,12 +840,6 @@ impl<const PH: u128, const PL: u128> fmt::Debug for FeltBigInt<PH, PL> {
}
}

impl fmt::Display for ParseFeltError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{ParseFeltError:?}")
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down