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

chore: use LambdaWorks' implementation of bit operations #1291

Merged
merged 3 commits into from Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,9 +2,11 @@

#### Upcoming Changes

* chore: use LambdaWorks' implementation of bit operations for `Felt252` [#1291](https://github.com/lambdaclass/cairo-rs/pull/1291)

#### [0.8.0] - 2023-6-26

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

Changes under this feature:
* `Felt252` now uses _lambdaworks_' `FieldElement` internally
Expand Down
2 changes: 1 addition & 1 deletion felt/Cargo.toml
Expand Up @@ -19,7 +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 = { version = "0.1.1", default-features = false, optional=true }
lambdaworks-math = { version = "0.1.1", default-features = false, optional = true }

[dev-dependencies]
proptest = "1.1.0"
Expand Down
22 changes: 5 additions & 17 deletions felt/src/lib_lambdaworks.rs
Expand Up @@ -853,15 +853,13 @@ impl ShrAssign<usize> for Felt252 {
}
}

// TODO: move to upstream
impl<'a> BitAnd for &'a Felt252 {
type Output = Felt252;
fn bitand(self, rhs: Self) -> Self::Output {
self.clone() & rhs
}
}

// TODO: move to upstream
impl<'a> BitAnd<&'a Felt252> for Felt252 {
type Output = Self;
fn bitand(self, rhs: &Self) -> Self {
Expand All @@ -872,9 +870,9 @@ impl<'a> BitAnd<&'a Felt252> for Felt252 {
impl<'a> BitAnd<Felt252> for &'a Felt252 {
type Output = Felt252;
fn bitand(self, rhs: Self::Output) -> Self::Output {
// TODO: move to upstream
let a = self.value.representative();
let b = rhs.value.representative();

let value = FieldElement::new(a & b);
Self::Output { value }
}
Expand All @@ -883,31 +881,21 @@ impl<'a> BitAnd<Felt252> for &'a Felt252 {
impl<'a> BitOr for &'a Felt252 {
type Output = Felt252;
fn bitor(self, rhs: Self) -> Self::Output {
// TODO: move to upstream
let mut a = self.value.representative();
let a = self.value.representative();
let b = rhs.value.representative();

for i in 0..a.limbs.len() {
a.limbs[i] |= b.limbs[i];
}
let value = FieldElement::new(a);
// let value = FieldElement::new(a | b);
let value = FieldElement::new(a | b);
Self::Output { value }
}
}

impl<'a> BitXor for &'a Felt252 {
type Output = Felt252;
fn bitxor(self, rhs: Self) -> Self::Output {
// TODO: move to upstream
let mut a = self.value.representative();
let a = self.value.representative();
let b = rhs.value.representative();

for i in 0..a.limbs.len() {
a.limbs[i] ^= b.limbs[i];
}
let value = FieldElement::new(a);
// let value = FieldElement::new(a ^ b);
let value = FieldElement::new(a ^ b);
Self::Output { value }
}
}
Expand Down