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 a05f753 commit 6509598
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 49 deletions.
9 changes: 3 additions & 6 deletions console/program/src/data/record/is_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ 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.
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
}
let owner_x = ciphertext[0] - randomizer[0];
// Compare the x coordinates of computed and supplied affines.
owner_x == address.to_x_coordinate()
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions curves/src/templates/short_weierstrass_jacobian/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,6 @@ 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: 0 additions & 20 deletions curves/src/templates/twisted_edwards_extended/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,6 @@ 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: 0 additions & 6 deletions curves/src/traits/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@ 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 6509598

Please sign in to comment.