Use GroupEncoding instead of Curve's from_slice/to_bytes

Increases usage of standardization while expanding dalek_ff_group.

Closes https://github.com/serai-dex/serai/issues/26 by moving 
dfg::EdwardsPoint to only be for the prime subgroup.
This commit is contained in:
Luke Parker
2022-06-28 01:25:26 -04:00
parent ac17645fc8
commit 3de7a76051
14 changed files with 141 additions and 178 deletions

View File

@@ -27,7 +27,7 @@ use dalek::{
}
};
use group::{ff::{Field, PrimeField}, Group};
use group::{ff::{Field, PrimeField}, Group, GroupEncoding, prime::PrimeGroup};
macro_rules! deref_borrow {
($Source: ident, $Target: ident) => {
@@ -192,6 +192,7 @@ macro_rules! dalek_group {
(
$Point: ident,
$DPoint: ident,
$torsion_free: expr,
$Table: ident,
$DTable: ident,
@@ -225,6 +226,29 @@ macro_rules! dalek_group {
fn double(&self) -> Self { *self + self }
}
impl GroupEncoding for $Point {
type Repr = [u8; 32];
fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
if let Some(point) = $DCompressed(*bytes).decompress() {
if $torsion_free(point) {
return CtOption::new($Point(point), Choice::from(1));
}
}
CtOption::new($Point::identity(), Choice::from(0))
}
fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
$Point::from_bytes(bytes)
}
fn to_bytes(&self) -> Self::Repr {
self.0.compress().to_bytes()
}
}
impl PrimeGroup for $Point {}
pub struct $Compressed(pub $DCompressed);
deref_borrow!($Compressed, $DCompressed);
impl $Compressed {
@@ -261,6 +285,7 @@ macro_rules! dalek_group {
dalek_group!(
EdwardsPoint,
DEdwardsPoint,
|point: DEdwardsPoint| point.is_torsion_free(),
EdwardsBasepointTable,
DEdwardsBasepointTable,
@@ -272,15 +297,10 @@ dalek_group!(
ED25519_BASEPOINT_TABLE
);
impl EdwardsPoint {
pub fn is_torsion_free(&self) -> bool {
self.0.is_torsion_free()
}
}
dalek_group!(
RistrettoPoint,
DRistrettoPoint,
|_| true,
RistrettoBasepointTable,
DRistrettoBasepointTable,