Skip to content

Commit

Permalink
patch: G1Projective ops, fp6 compile to default target
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Jan 3, 2025
1 parent 10da64a commit ccda38e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,22 @@ impl_binops_multiplicative_mixed!(Scalar, G1Affine, G1Projective);
impl_binops_multiplicative_mixed!(Scalar, G1Projective, G1Projective);

#[inline(always)]
#[cfg(not(target_os = "zkvm"))]
fn mul_by_3b(a: Fp) -> Fp {
let a = a + a; // 2
let a = a + a; // 4
a + a + a // 12
}

#[inline(always)]
#[cfg(target_os = "zkvm")]
fn mul_by_3b(a: Fp) -> Fp {
let a = a.add_inp(a); // 2
let a = a.add_inp(a);

a.add_inp(a).add_inp(a) // 12
}

impl G1Projective {
/// Returns the identity of the group: the point at infinity.
pub fn identity() -> G1Projective {
Expand Down Expand Up @@ -837,6 +847,7 @@ impl G1Projective {
}

/// Multiply `self` by `crate::BLS_X`, using double and add.
#[cfg(not(target_os = "zkvm"))]
fn mul_by_x(&self) -> G1Projective {
let mut xself = G1Projective::identity();
// NOTE: in BLS12-381 we can just skip the first bit.
Expand All @@ -857,6 +868,27 @@ impl G1Projective {
xself
}

#[cfg(target_os = "zkvm")]
fn mul_by_x(&self) -> G1Projective {
let mut xself = G1Affine::identity();

let mut x = crate::BLS_X >> 1;
let mut tmp = G1Affine::from(*self);
while x != 0 {
tmp = tmp.double();

if x % 2 == 1 {
xself += tmp;
}
x >>= 1;
}
// finally, flip the sign
if crate::BLS_X_IS_NEGATIVE {
xself = -xself;
}
xself
}

/// Multiplies by $(1 - z)$, where $z$ is the parameter of BLS12-381, which
/// [suffices to clear](https://ia.cr/2019/403) the cofactor and map
/// elliptic curve points to elements of $\mathbb{G}\_1$.
Expand Down

0 comments on commit ccda38e

Please sign in to comment.