Skip to content

Commit

Permalink
feat: double method for FieldElement (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Feb 17, 2024
1 parent 3b89b73 commit 6ebaa9b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 1 addition & 5 deletions starknet-accounts/tests/single_owner_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,7 @@ async fn can_execute_tst_mint_inner<P: Provider + Send + Sync>(provider: P, addr
Call {
to: tst_token_address,
selector: get_selector_from_name("mint").unwrap(),
calldata: vec![
address,
random_amount * FieldElement::TWO,
FieldElement::ZERO,
],
calldata: vec![address, random_amount.double(), FieldElement::ZERO],
},
])
.send()
Expand Down
4 changes: 2 additions & 2 deletions starknet-crypto-codegen/src/poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub fn compress_roundkeys_partial(rcs: &[[FieldElement; 3]]) -> Vec<FieldElement

// MixLayer
let t = state[0] + state[1] + state[2];
state[0] = t + FieldElement::TWO * state[0];
state[1] = t - FieldElement::TWO * state[1];
state[0] = t + state[0].double();
state[1] = t - state[1].double();
state[2] = t - FieldElement::THREE * state[2];

idx += 1;
Expand Down
4 changes: 2 additions & 2 deletions starknet-crypto/src/poseidon_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ pub fn poseidon_permute_comp(state: &mut [FieldElement; 3]) {
#[inline(always)]
fn mix(state: &mut [FieldElement; 3]) {
let t = state[0] + state[1] + state[2];
state[0] = t + FieldElement::TWO * state[0];
state[1] = t - FieldElement::TWO * state[1];
state[0] = t + state[0].double();
state[1] = t - state[1].double();
state[2] = t - FieldElement::THREE * state[2];
}

Expand Down
10 changes: 5 additions & 5 deletions starknet-curve/src/ec_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl AffinePoint {
// l = (3x^2+a)/2y with a=1 from stark curve
let lambda = {
let dividend = FieldElement::THREE * (self.x * self.x) + FieldElement::ONE;
let divisor_inv = (FieldElement::TWO * self.y).invert().unwrap();
let divisor_inv = self.y.double().invert().unwrap();
dividend * divisor_inv
};

Expand Down Expand Up @@ -167,14 +167,14 @@ impl ProjectivePoint {

// t=3x^2+az^2 with a=1 from stark curve
let t = FieldElement::THREE * self.x * self.x + self.z * self.z;
let u = FieldElement::TWO * self.y * self.z;
let v = FieldElement::TWO * u * self.x * self.y;
let w = t * t - FieldElement::TWO * v;
let u = self.y.double() * self.z;
let v = u.double() * self.x * self.y;
let w = t * t - v.double();

let uy = u * self.y;

let x = u * w;
let y = t * (v - w) - FieldElement::TWO * uy * uy;
let y = t * (v - w) - (uy * uy).double();
let z = u * u * u;

self.x = x;
Expand Down
4 changes: 4 additions & 0 deletions starknet-ff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ impl FieldElement {
self.inner.sqrt().map(|inner| Self { inner })
}

pub fn double(&self) -> FieldElement {
*self + *self
}

/// Performs a floor division. It's not implemented as the `Div` trait on purpose to
/// distinguish from the "felt division".
pub fn floor_div(&self, rhs: FieldElement) -> FieldElement {
Expand Down

0 comments on commit 6ebaa9b

Please sign in to comment.