mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-10 21:00:35 +00:00
Add 'repr-c' feature to facilitate FFI.
This commit is contained in:
parent
de99732e20
commit
872065c8a1
4 changed files with 28 additions and 7 deletions
|
|
@ -58,3 +58,4 @@ default = ["bits", "sqrt-table"]
|
||||||
alloc = ["group/alloc", "blake2b_simd"]
|
alloc = ["group/alloc", "blake2b_simd"]
|
||||||
bits = ["ff/bits"]
|
bits = ["ff/bits"]
|
||||||
sqrt-table = ["alloc", "lazy_static"]
|
sqrt-table = ["alloc", "lazy_static"]
|
||||||
|
repr-c = []
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ macro_rules! new_curve_impl {
|
||||||
$curve_id:literal, $a_raw:expr, $b_raw:expr, $curve_type:ident) => {
|
$curve_id:literal, $a_raw:expr, $b_raw:expr, $curve_type:ident) => {
|
||||||
/// Represents a point in the projective coordinate space.
|
/// Represents a point in the projective coordinate space.
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
#[cfg_attr(feature = "repr-c", repr(C))]
|
||||||
$($privacy)* struct $name {
|
$($privacy)* struct $name {
|
||||||
x: $base,
|
x: $base,
|
||||||
y: $base,
|
y: $base,
|
||||||
|
|
@ -48,15 +49,17 @@ macro_rules! new_curve_impl {
|
||||||
/// Represents a point in the affine coordinate space (or the point at
|
/// Represents a point in the affine coordinate space (or the point at
|
||||||
/// infinity).
|
/// infinity).
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
#[cfg_attr(feature = "repr-c", repr(C))]
|
||||||
$($privacy)* struct $name_affine {
|
$($privacy)* struct $name_affine {
|
||||||
x: $base,
|
x: $base,
|
||||||
y: $base,
|
y: $base,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice,
|
infinity: Choice,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for $name_affine {
|
impl fmt::Debug for $name_affine {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
if self.infinity.into() {
|
if self.is_identity().into() {
|
||||||
write!(f, "Infinity")
|
write!(f, "Infinity")
|
||||||
} else {
|
} else {
|
||||||
write!(f, "({:?}, {:?})", self.x, self.y)
|
write!(f, "({:?}, {:?})", self.x, self.y)
|
||||||
|
|
@ -81,6 +84,7 @@ macro_rules! new_curve_impl {
|
||||||
let p = $name_affine {
|
let p = $name_affine {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice::from(0u8),
|
infinity: Choice::from(0u8),
|
||||||
};
|
};
|
||||||
break p.to_curve();
|
break p.to_curve();
|
||||||
|
|
@ -200,7 +204,8 @@ macro_rules! new_curve_impl {
|
||||||
|
|
||||||
q.x = p.x * tmp2;
|
q.x = p.x * tmp2;
|
||||||
q.y = p.y * tmp3;
|
q.y = p.y * tmp3;
|
||||||
q.infinity = Choice::from(0u8);
|
#[cfg(not(feature = "repr-c"))]
|
||||||
|
{ q.infinity = Choice::from(0u8); }
|
||||||
|
|
||||||
*q = $name_affine::conditional_select(&q, &$name_affine::identity(), skip);
|
*q = $name_affine::conditional_select(&q, &$name_affine::identity(), skip);
|
||||||
}
|
}
|
||||||
|
|
@ -216,6 +221,7 @@ macro_rules! new_curve_impl {
|
||||||
let tmp = $name_affine {
|
let tmp = $name_affine {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice::from(0u8),
|
infinity: Choice::from(0u8),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -502,6 +508,7 @@ macro_rules! new_curve_impl {
|
||||||
$name_affine {
|
$name_affine {
|
||||||
x: self.x,
|
x: self.x,
|
||||||
y: -self.y,
|
y: -self.y,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: self.infinity,
|
infinity: self.infinity,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -621,19 +628,25 @@ macro_rules! new_curve_impl {
|
||||||
Self {
|
Self {
|
||||||
x: $base::zero(),
|
x: $base::zero(),
|
||||||
y: $base::zero(),
|
y: $base::zero(),
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice::from(1u8),
|
infinity: Choice::from(1u8),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
fn is_identity(&self) -> Choice {
|
fn is_identity(&self) -> Choice {
|
||||||
self.infinity
|
self.infinity
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "repr-c")]
|
||||||
|
fn is_identity(&self) -> Choice {
|
||||||
|
self.x.is_zero() & self.y.is_zero()
|
||||||
|
}
|
||||||
|
|
||||||
fn to_curve(&self) -> Self::Curve {
|
fn to_curve(&self) -> Self::Curve {
|
||||||
$name {
|
$name {
|
||||||
x: self.x,
|
x: self.x,
|
||||||
y: self.y,
|
y: self.y,
|
||||||
z: $base::conditional_select(&$base::one(), &$base::zero(), self.infinity),
|
z: $base::conditional_select(&$base::one(), &$base::zero(), self.is_identity()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -679,6 +692,7 @@ macro_rules! new_curve_impl {
|
||||||
$name_affine {
|
$name_affine {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice::from(0u8),
|
infinity: Choice::from(0u8),
|
||||||
},
|
},
|
||||||
Choice::from(1u8),
|
Choice::from(1u8),
|
||||||
|
|
@ -717,7 +731,7 @@ macro_rules! new_curve_impl {
|
||||||
fn is_on_curve(&self) -> Choice {
|
fn is_on_curve(&self) -> Choice {
|
||||||
// y^2 - x^3 - ax ?= b
|
// y^2 - x^3 - ax ?= b
|
||||||
(self.y.square() - (self.x.square() + &$name::curve_constant_a()) * self.x).ct_eq(&$name::curve_constant_b())
|
(self.y.square() - (self.x.square() + &$name::curve_constant_a()) * self.x).ct_eq(&$name::curve_constant_b())
|
||||||
| self.infinity
|
| self.is_identity()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn coordinates(&self) -> CtOption<Coordinates<Self>> {
|
fn coordinates(&self) -> CtOption<Coordinates<Self>> {
|
||||||
|
|
@ -726,7 +740,9 @@ macro_rules! new_curve_impl {
|
||||||
|
|
||||||
fn from_xy(x: Self::Base, y: Self::Base) -> CtOption<Self> {
|
fn from_xy(x: Self::Base, y: Self::Base) -> CtOption<Self> {
|
||||||
let p = $name_affine {
|
let p = $name_affine {
|
||||||
x, y, infinity: 0u8.into()
|
x, y,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
|
infinity: 0u8.into()
|
||||||
};
|
};
|
||||||
CtOption::new(p, p.is_on_curve())
|
CtOption::new(p, p.is_on_curve())
|
||||||
}
|
}
|
||||||
|
|
@ -760,8 +776,8 @@ macro_rules! new_curve_impl {
|
||||||
|
|
||||||
impl ConstantTimeEq for $name_affine {
|
impl ConstantTimeEq for $name_affine {
|
||||||
fn ct_eq(&self, other: &Self) -> Choice {
|
fn ct_eq(&self, other: &Self) -> Choice {
|
||||||
let z1 = self.infinity;
|
let z1 = self.is_identity();
|
||||||
let z2 = other.infinity;
|
let z2 = other.is_identity();
|
||||||
|
|
||||||
(z1 & z2) | ((!z1) & (!z2) & (self.x.ct_eq(&other.x)) & (self.y.ct_eq(&other.y)))
|
(z1 & z2) | ((!z1) & (!z2) & (self.x.ct_eq(&other.x)) & (self.y.ct_eq(&other.y)))
|
||||||
}
|
}
|
||||||
|
|
@ -780,6 +796,7 @@ macro_rules! new_curve_impl {
|
||||||
$name_affine {
|
$name_affine {
|
||||||
x: $base::conditional_select(&a.x, &b.x, choice),
|
x: $base::conditional_select(&a.x, &b.x, choice),
|
||||||
y: $base::conditional_select(&a.y, &b.y, choice),
|
y: $base::conditional_select(&a.y, &b.y, choice),
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice::conditional_select(&a.infinity, &b.infinity, choice),
|
infinity: Choice::conditional_select(&a.infinity, &b.infinity, choice),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -951,6 +968,7 @@ macro_rules! impl_affine_curve_specific {
|
||||||
Self {
|
Self {
|
||||||
x: NEGATIVE_ONE,
|
x: NEGATIVE_ONE,
|
||||||
y: TWO,
|
y: TWO,
|
||||||
|
#[cfg(not(feature = "repr-c"))]
|
||||||
infinity: Choice::from(0u8),
|
infinity: Choice::from(0u8),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ use crate::arithmetic::SqrtTables;
|
||||||
// integers in little-endian order. `Fp` values are always in
|
// integers in little-endian order. `Fp` values are always in
|
||||||
// Montgomery form; i.e., Fp(a) = aR mod p, with R = 2^256.
|
// Montgomery form; i.e., Fp(a) = aR mod p, with R = 2^256.
|
||||||
#[derive(Clone, Copy, Eq)]
|
#[derive(Clone, Copy, Eq)]
|
||||||
|
#[repr(transparent)]
|
||||||
pub struct Fp(pub(crate) [u64; 4]);
|
pub struct Fp(pub(crate) [u64; 4]);
|
||||||
|
|
||||||
impl fmt::Debug for Fp {
|
impl fmt::Debug for Fp {
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ use crate::arithmetic::SqrtTables;
|
||||||
// integers in little-endian order. `Fq` values are always in
|
// integers in little-endian order. `Fq` values are always in
|
||||||
// Montgomery form; i.e., Fq(a) = aR mod q, with R = 2^256.
|
// Montgomery form; i.e., Fq(a) = aR mod q, with R = 2^256.
|
||||||
#[derive(Clone, Copy, Eq)]
|
#[derive(Clone, Copy, Eq)]
|
||||||
|
#[repr(transparent)]
|
||||||
pub struct Fq(pub(crate) [u64; 4]);
|
pub struct Fq(pub(crate) [u64; 4]);
|
||||||
|
|
||||||
impl fmt::Debug for Fq {
|
impl fmt::Debug for Fq {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue