Skip to main content

Encoding

Trait Encoding 

Source
pub trait Encoding<const N: usize>
where Self: Sized,
{ // Required methods fn from_bytes(bytes: [u8; N]) -> Option<Self>; fn to_bytes(&self) -> [u8; N]; // Provided methods fn from_slice(slice: &[u8]) -> Option<Self> { ... } fn copy_to_slice(&self, slice: &mut [u8]) -> Option<()> { ... } fn from_hex(hex: &str) -> Option<Self> { ... } fn to_hex(&self) -> String { ... } unsafe fn from_ptr(ptr: *const u8) -> Option<Self> { ... } unsafe fn copy_to_ptr(self, ptr: *mut u8) { ... } }
Expand description

Adds encoding and decoding methods to PrivateKey, PublicKey, Triple, Scalar and RistrettoPoint which can all be represented as [u8; N]s for some N.

Not all arrays of the form [u8; N] may be a valid representation of the type of object in question, though.

Required Methods§

Source

fn from_bytes(bytes: [u8; N]) -> Option<Self>

Decodes Some(object) from bytes if bytes encodes some object of type Self; otherwise returns None.

Source

fn to_bytes(&self) -> [u8; N]

Encodes self as [u8; N].

Provided Methods§

Source

fn from_slice(slice: &[u8]) -> Option<Self>

Like Self::from_bytes, but reads [u8; N] from slice. Returns None if slice.len()!=N or when the slice is not a valid encoding.

Source

fn copy_to_slice(&self, slice: &mut [u8]) -> Option<()>

Copies the encoding of self into slice. Returns None when slice.len()!=N.

Source

fn from_hex(hex: &str) -> Option<Self>

Like Self::from_bytes, but reads the [u8; N] from the 2*N-digit hex string hex. The case of the hex digits is ignored.

Source

fn to_hex(&self) -> String

Returns the 2*N-digit lower-case hex representation of self.

Source

unsafe fn from_ptr(ptr: *const u8) -> Option<Self>

Loads object from the N-byte buffer pointed to by ptr.

§Safety

The caller must make sure that ptr is properly alligned, the N-byte buffer is readable, and isn’t modified for the duration of the call.

See the ‘Safety’ section of core::slice::from_raw_parts for more details.

Source

unsafe fn copy_to_ptr(self, ptr: *mut u8)

Writes the N-byte representation of this object to the memory location ptr.

§Safety

The caller must make sure that ptr is properly alligned, the N-byte buffer is writable, and isn’t modified for the duration of the call.

See the ‘Safety’ section of core::slice::from_raw_parts_mut for more details.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Encoding<32> for CompressedRistretto

Source§

impl Encoding<32> for RistrettoPoint

Source§

impl Encoding<32> for Scalar

Source§

fn from_bytes(bytes: [u8; 32]) -> Option<Scalar>

Source§

fn to_bytes(&self) -> [u8; 32]

Implementors§