From 65095988c7f8cc67bb12a2650e2e32a43f23bff0 Mon Sep 17 00:00:00 2001 From: Maciej Zwolinski Date: Wed, 26 Oct 2022 14:44:40 +0200 Subject: [PATCH] perf: optimize Record::is_owner Signed-off-by: Maciej Zwolinski --- console/program/src/data/record/is_owner.rs | 9 +++------ .../short_weierstrass_jacobian/affine.rs | 17 ---------------- .../twisted_edwards_extended/affine.rs | 20 ------------------- curves/src/traits/group.rs | 6 ------ 4 files changed, 3 insertions(+), 49 deletions(-) diff --git a/console/program/src/data/record/is_owner.rs b/console/program/src/data/record/is_owner.rs index fc3136e774..862235a6eb 100644 --- a/console/program/src/data/record/is_owner.rs +++ b/console/program/src/data/record/is_owner.rs @@ -29,12 +29,9 @@ impl Record> { // 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() } } } diff --git a/curves/src/templates/short_weierstrass_jacobian/affine.rs b/curves/src/templates/short_weierstrass_jacobian/affine.rs index 99586f47a9..6bab36023f 100644 --- a/curves/src/templates/short_weierstrass_jacobian/affine.rs +++ b/curves/src/templates/short_weierstrass_jacobian/affine.rs @@ -142,23 +142,6 @@ impl AffineCurve for Affine

{ }) } - /// 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. /// diff --git a/curves/src/templates/twisted_edwards_extended/affine.rs b/curves/src/templates/twisted_edwards_extended/affine.rs index 8376fdd9a5..db4b9e2c79 100644 --- a/curves/src/templates/twisted_edwards_extended/affine.rs +++ b/curves/src/templates/twisted_edwards_extended/affine.rs @@ -142,26 +142,6 @@ impl AffineCurve for Affine

{ }) } - /// 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. /// diff --git a/curves/src/traits/group.rs b/curves/src/traits/group.rs index c4863578ef..1a936271e5 100644 --- a/curves/src/traits/group.rs +++ b/curves/src/traits/group.rs @@ -163,12 +163,6 @@ pub trait AffineCurve: /// largest y-coordinate be selected. fn from_x_coordinate(x: Self::BaseField, greatest: bool) -> Option; - /// 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. ///