lambdaworks_math/
traits.rs1use crate::{
2 errors::DeserializationError,
3 field::{element::FieldElement, traits::IsField},
4};
5
6use crate::errors::ByteConversionError;
7pub trait ByteConversion {
11 #[cfg(feature = "alloc")]
13 fn to_bytes_be(&self) -> alloc::vec::Vec<u8>;
14
15 #[cfg(feature = "alloc")]
17 fn to_bytes_le(&self) -> alloc::vec::Vec<u8>;
18
19 fn from_bytes_be(bytes: &[u8]) -> Result<Self, ByteConversionError>
21 where
22 Self: Sized;
23
24 fn from_bytes_le(bytes: &[u8]) -> Result<Self, ByteConversionError>
26 where
27 Self: Sized;
28}
29
30#[cfg(feature = "alloc")]
33pub trait AsBytes {
34 fn as_bytes(&self) -> alloc::vec::Vec<u8>;
36}
37
38#[cfg(feature = "alloc")]
39impl AsBytes for u32 {
40 fn as_bytes(&self) -> alloc::vec::Vec<u8> {
41 self.to_le_bytes().to_vec()
42 }
43}
44
45#[cfg(feature = "alloc")]
46impl AsBytes for u64 {
47 fn as_bytes(&self) -> alloc::vec::Vec<u8> {
48 self.to_le_bytes().to_vec()
49 }
50}
51
52pub trait Deserializable {
54 fn deserialize(bytes: &[u8]) -> Result<Self, DeserializationError>
55 where
56 Self: Sized;
57}
58
59pub trait IsRandomFieldElementGenerator<F: IsField> {
60 fn generate(&self) -> FieldElement<F>;
61}