use curve25519_dalek::{
edwards::{CompressedEdwardsY, EdwardsPoint},
ristretto::{CompressedRistretto, RistrettoPoint},
scalar::Scalar,
};
use crate::{
codecs::{Decoding, Encoding},
error::VerificationError,
io::NargDeserialize,
VerificationResult,
};
impl crate::Unit for Scalar {
const ZERO: Self = Self::ZERO;
}
impl Decoding<[u8]> for Scalar {
type Repr = super::Array64;
fn decode(buf: Self::Repr) -> Self {
let mut le_bytes = buf.0;
le_bytes.reverse();
Self::from_bytes_mod_order_wide(&le_bytes)
}
}
impl Decoding<[u8]> for RistrettoPoint {
type Repr = super::Array64;
fn decode(buf: Self::Repr) -> Self {
Self::from_uniform_bytes(&buf.0)
}
}
impl NargDeserialize for Scalar {
fn deserialize_from_narg(buf: &mut &[u8]) -> VerificationResult<Self> {
const N: usize = 32;
if buf.len() < N {
return Err(VerificationError);
}
let be_bytes = &buf[..N];
let mut le_bytes = [0u8; N];
le_bytes.copy_from_slice(be_bytes);
le_bytes.reverse();
Self::from_canonical_bytes(le_bytes)
.into_option()
.inspect(|_| *buf = &buf[N..])
.ok_or(VerificationError)
}
}
impl NargDeserialize for EdwardsPoint {
fn deserialize_from_narg(buf: &mut &[u8]) -> VerificationResult<Self> {
if buf.len() < 32 {
return Err(VerificationError);
}
let (head, tail) = buf.split_at(32);
*buf = tail;
CompressedEdwardsY(head.try_into().unwrap())
.decompress()
.ok_or(VerificationError)
}
}
impl NargDeserialize for RistrettoPoint {
fn deserialize_from_narg(buf: &mut &[u8]) -> VerificationResult<Self> {
if buf.len() < 32 {
return Err(VerificationError);
}
let (head, tail) = buf.split_at(32);
*buf = tail;
CompressedRistretto(head.try_into().unwrap())
.decompress()
.ok_or(VerificationError)
}
}
impl Encoding<[u8]> for Scalar {
fn encode(&self) -> impl AsRef<[u8]> {
let mut le_bytes = self.to_bytes();
le_bytes.reverse();
le_bytes
}
}
impl Encoding<[u8]> for EdwardsPoint {
fn encode(&self) -> impl AsRef<[u8]> {
self.compress().to_bytes()
}
}
impl Encoding<[u8]> for RistrettoPoint {
fn encode(&self) -> impl AsRef<[u8]> {
self.compress().to_bytes()
}
}