mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-02-07 06:44:11 +00:00
commit
dfb836170b
@ -85,6 +85,34 @@ macro_rules! curve_impl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl $affine {
|
impl $affine {
|
||||||
|
/// Attempts to construct an affine point given an x-coordinate. The
|
||||||
|
/// point is not guaranteed to be in the prime order subgroup.
|
||||||
|
///
|
||||||
|
/// If and only if `greatest` is set will the lexicographically
|
||||||
|
/// largest y-coordinate be selected.
|
||||||
|
fn get_point_from_x(x: $basefield, greatest: bool) -> Option<$affine> {
|
||||||
|
// Compute x^3 + b
|
||||||
|
let mut x3b = x;
|
||||||
|
x3b.square();
|
||||||
|
x3b.mul_assign(&x);
|
||||||
|
x3b.add_assign(&$affine::get_coeff_b());
|
||||||
|
|
||||||
|
x3b.sqrt().map(|y| {
|
||||||
|
let mut negy = y;
|
||||||
|
negy.negate();
|
||||||
|
|
||||||
|
$affine {
|
||||||
|
x: x,
|
||||||
|
y: if (y < negy) ^ greatest {
|
||||||
|
y
|
||||||
|
} else {
|
||||||
|
negy
|
||||||
|
},
|
||||||
|
infinity: false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn is_on_curve(&self) -> bool {
|
fn is_on_curve(&self) -> bool {
|
||||||
if self.is_zero() {
|
if self.is_zero() {
|
||||||
true
|
true
|
||||||
@ -781,32 +809,7 @@ pub mod g1 {
|
|||||||
// Interpret as Fq element.
|
// Interpret as Fq element.
|
||||||
let x = Fq::from_repr(x).map_err(|e| GroupDecodingError::CoordinateDecodingError("x coordinate", e))?;
|
let x = Fq::from_repr(x).map_err(|e| GroupDecodingError::CoordinateDecodingError("x coordinate", e))?;
|
||||||
|
|
||||||
// Compute x^3 + b
|
G1Affine::get_point_from_x(x, greatest).ok_or(GroupDecodingError::NotOnCurve)
|
||||||
let mut x3b = x;
|
|
||||||
x3b.square();
|
|
||||||
x3b.mul_assign(&x);
|
|
||||||
x3b.add_assign(&G1Affine::get_coeff_b());
|
|
||||||
|
|
||||||
// Attempt to compute y
|
|
||||||
match x3b.sqrt() {
|
|
||||||
Some(y) => {
|
|
||||||
let mut negy = y;
|
|
||||||
negy.negate();
|
|
||||||
|
|
||||||
// Get the parity of the sqrt we found.
|
|
||||||
let parity = y > negy;
|
|
||||||
|
|
||||||
Ok(G1Affine {
|
|
||||||
x: x,
|
|
||||||
y: if parity == greatest { y } else { negy },
|
|
||||||
infinity: false
|
|
||||||
})
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
// Point must not be on the curve.
|
|
||||||
Err(GroupDecodingError::NotOnCurve)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn from_affine(affine: G1Affine) -> Self {
|
fn from_affine(affine: G1Affine) -> Self {
|
||||||
@ -1307,32 +1310,7 @@ pub mod g2 {
|
|||||||
c1: Fq::from_repr(x_c1).map_err(|e| GroupDecodingError::CoordinateDecodingError("x coordinate (c1)", e))?
|
c1: Fq::from_repr(x_c1).map_err(|e| GroupDecodingError::CoordinateDecodingError("x coordinate (c1)", e))?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compute x^3 + b
|
G2Affine::get_point_from_x(x, greatest).ok_or(GroupDecodingError::NotOnCurve)
|
||||||
let mut x3b = x;
|
|
||||||
x3b.square();
|
|
||||||
x3b.mul_assign(&x);
|
|
||||||
x3b.add_assign(&G2Affine::get_coeff_b());
|
|
||||||
|
|
||||||
// Attempt to compute y
|
|
||||||
match x3b.sqrt() {
|
|
||||||
Some(y) => {
|
|
||||||
let mut negy = y;
|
|
||||||
negy.negate();
|
|
||||||
|
|
||||||
// Get the parity of the sqrt we found.
|
|
||||||
let parity = y > negy;
|
|
||||||
|
|
||||||
Ok(G2Affine {
|
|
||||||
x: x,
|
|
||||||
y: if parity == greatest { y } else { negy },
|
|
||||||
infinity: false
|
|
||||||
})
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
// Point must not be on the curve.
|
|
||||||
Err(GroupDecodingError::NotOnCurve)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn from_affine(affine: G2Affine) -> Self {
|
fn from_affine(affine: G2Affine) -> Self {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
// This library relies on the Rust nightly compiler's `i128_type` feature.
|
// If the "u128-support" feature is enabled, this library can use
|
||||||
// If that's not okay for you, disable the u128-support feature. (Pass
|
// more efficient arithmetic. Only available in the nightly compiler.
|
||||||
// --no-default-features for example.)
|
|
||||||
#![cfg_attr(feature = "u128-support", feature(i128_type))]
|
#![cfg_attr(feature = "u128-support", feature(i128_type))]
|
||||||
|
|
||||||
// `clippy` is a code linting tool for improving code quality by catching
|
// `clippy` is a code linting tool for improving code quality by catching
|
||||||
@ -618,7 +617,6 @@ use self::arith::*;
|
|||||||
|
|
||||||
#[cfg(feature = "u128-support")]
|
#[cfg(feature = "u128-support")]
|
||||||
mod arith {
|
mod arith {
|
||||||
|
|
||||||
/// Calculate a - b - borrow, returning the result and modifying
|
/// Calculate a - b - borrow, returning the result and modifying
|
||||||
/// the borrow value.
|
/// the borrow value.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
Loading…
Reference in New Issue
Block a user