Skip to content

Commit

Permalink
perf: optimize Record::is_owner
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Zwolinski <[email protected]>
  • Loading branch information
zvolin committed Oct 26, 2022
1 parent e1150d1 commit a05f753
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
8 changes: 5 additions & 3 deletions console/program/src/data/record/is_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ impl<N: Network> Record<N, Ciphertext<N>> {
// Compute the 0th randomizer.
let randomizer = N::hash_many_psd8(&[N::encryption_domain(), record_view_key], 1);
// Decrypt the owner.
match Address::from_field(&(ciphertext[0] - randomizer[0])) {
Ok(owner) => &owner == address,
Err(_) => false,
let x_coordinate = ciphertext[0] - randomizer[0];
if let Some((point1, point2)) = N::Affine::from_x_coordinate_variants(*x_coordinate) {
point1 == ***address || point2 == ***address
} else {
false
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions curves/src/templates/short_weierstrass_jacobian/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ impl<P: Parameters> AffineCurve for Affine<P> {
})
}

/// Attempts to construct an affine point given an x-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
/// Returns variants with and without the lexicographically largest
/// y-coordinate selected.
fn from_x_coordinate_variants(x: Self::BaseField) -> Option<(Self, Self)> {
// Compute x^3 + ax + b
let x3b = P::add_b(&((x.square() * x) + P::mul_by_a(&x)));

x3b.sqrt().map(|y| {
let negy = -y;

let y1 = if (y < negy) ^ false { y } else { negy };
let y2 = if (y < negy) ^ true { y } else { negy };
(Self::new(x, y1, false), Self::new(x, y2, false))
})
}

/// Attempts to construct an affine point given a y-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
///
Expand Down
20 changes: 20 additions & 0 deletions curves/src/templates/twisted_edwards_extended/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ impl<P: Parameters> AffineCurve for Affine<P> {
})
}

/// Attempts to construct an affine point given an x-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
/// Returns variants with and without the lexicographically largest
/// y-coordinate selected.
#[inline]
fn from_x_coordinate_variants(x: Self::BaseField) -> Option<(Self, Self)> {
// y = sqrt( (a * x^2 - 1) / (d * x^2 - 1) )
let x2 = x.square();
let one = Self::BaseField::one();
let numerator = P::mul_by_a(&x2) - one;
let denominator = P::EDWARDS_D * x2 - one;
let y2 = denominator.inverse().map(|denom| denom * numerator);
y2.and_then(|y2| y2.sqrt()).map(|y| {
let negy = -y;
let y1 = if (y < negy) ^ false { y } else { negy };
let y2 = if (y < negy) ^ true { y } else { negy };
(Self::new(x, y1, x * y1), Self::new(x, y2, x * y2))
})
}

/// Attempts to construct an affine point given a y-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
///
Expand Down
6 changes: 6 additions & 0 deletions curves/src/traits/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ pub trait AffineCurve:
/// largest y-coordinate be selected.
fn from_x_coordinate(x: Self::BaseField, greatest: bool) -> Option<Self>;

/// Attempts to construct an affine point given an x-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
/// Returns variants with and without the lexicographically largest
/// y-coordinate selected.
fn from_x_coordinate_variants(x: Self::BaseField) -> Option<(Self, Self)>;

/// Attempts to construct an affine point given a y-coordinate. The
/// point is not guaranteed to be in the prime order subgroup.
///
Expand Down

0 comments on commit a05f753

Please sign in to comment.