use super::{EcOperation, HashTranscript, Sha512};
use cryptoxide::hashing::sha2;
use eccoxide::curve::curve25519::Scalar;
use eccoxide::curve::curve25519::ristretto255::RistrettoPoint;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Ristretto255;
impl EcOperation for Ristretto255 {
type Scalar = Scalar;
type Point = RistrettoPoint;
type Transcript = HashTranscript<Self, Sha512>;
const SCALAR_BYTES: usize = Scalar::SIZE_BYTES;
fn scalar_from_bytes(bytes: &[u8]) -> Option<Self::Scalar> {
let arr = <[u8; Scalar::SIZE_BYTES]>::try_from(bytes).ok()?;
Scalar::from_bytes(&arr)
}
fn scalar_from_wide_bytes(bytes: &[u8]) -> Self::Scalar {
Scalar::init_from_wide_bytes(
bytes
.try_into()
.expect("wide scalar buffer must be 2 * SCALAR_BYTES"),
)
}
fn scalar_to_bytes(s: &Self::Scalar) -> Vec<u8> {
s.to_bytes().to_vec()
}
fn point_to_bytes(p: &Self::Point) -> Vec<u8> {
p.compress().to_vec()
}
fn point_from_bytes(slice: &[u8]) -> Option<Self::Point> {
let bytes = <&[u8; 32]>::try_from(slice).ok()?;
RistrettoPoint::decompress(bytes).into_option()
}
fn point_try_hash_to_curve(slice: &[u8]) -> Option<Self::Point> {
Some(Self::point_hash_to_curve(slice))
}
fn point_hash_to_curve(slice: &[u8]) -> Self::Point {
let digest = sha2::Sha512::new().update(slice).finalize();
RistrettoPoint::from_uniform_bytes(&digest)
}
}