Skip to content

Commit

Permalink
refactor: blst in BLS finite field element pow (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobkaufmann authored Feb 6, 2024
1 parent bde6589 commit fc70797
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,15 @@ impl Fr {
}

pub fn pow(&self, power: &Self) -> Self {
let power = Scalar::from(power).to_be_bytes();
let mut power = U256::from_be_bytes(power);
let one = U256::from(1u64);
let mut power = *power;

let mut out = *self;
let mut tmp = Self::ONE;
while power > one {
while power != Self::ONE && power != Self::ZERO {
// remaining power odd
if power.bit(0) {
if power.is_odd() {
tmp = out * tmp;
power -= one;
power = power - Self::ONE;
}

out = out * out;
Expand Down Expand Up @@ -226,6 +224,16 @@ impl Fr {
}
}
}

fn is_odd(&self) -> bool {
let mut scalar = blst_scalar::default();
let mut bendian = [0; Self::BYTES];
unsafe {
blst_scalar_from_fr(&mut scalar, &self.element);
blst_bendian_from_scalar(bendian.as_mut_ptr(), &scalar);
}
bendian[Self::BYTES - 1] & 0b00000001 == 1
}
}

impl AsRef<blst_fr> for Fr {
Expand Down

0 comments on commit fc70797

Please sign in to comment.