Skip to content

Commit

Permalink
chore: use LambdaWorks' implementation of bit operations (lambdaclass…
Browse files Browse the repository at this point in the history
…#1291)

* Use lambdaworks' implementation of bit operations

* Update changelog
  • Loading branch information
MegaRedHand authored and kariy committed Jul 4, 2023
1 parent 0614d45 commit ced530a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
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

0 comments on commit ced530a

Please sign in to comment.