Fixed nat_mod<->byte seq in lib
@@ -770,6 +770,26 @@ macro_rules! nat_mod {
(type_name: $name:ident, type_of_canvas: $base:ident, bit_size_of_field: $bits:literal, modulo_value: $n:literal) => {
abstract_nat_mod!($name, $base, $bits, $n);
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
+ pub fn from_byte_seq_be<A: SeqTrait<U8>>(s: A) -> $name {
+ $name::from_be_bytes(
+ s.iter()
+ .map(|x| U8::declassify(*x))
+ .collect::<Vec<_>>()
+ .as_slice(),
+ )
+ }
+
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
+ pub fn to_byte_seq_be(self) -> Seq<U8> {
+ Seq::from_vec(
+ self.to_be_bytes()
+ .iter()
+ .map(|x| U8::classify(*x))
+ .collect::<Vec<U8>>(),
+ )
+ }
+
impl NumericCopy for $name {}
impl Integer for $name {
const NUM_BITS: usize = $bits;
@@ -1019,11 +1039,10 @@ macro_rules! public_nat_mod {
unsigned_public_integer!($base, $bits);
abstract_public_modular_integer!($name, $base, $base::from_hex($n));
- // FIXME: check if we really need this and maybe move this somewhere.
impl $name {
- #[cfg_attr(feature = "use_attributes", in_hacspec)]
- pub fn from_byte_seq_le<A: SeqTrait<U8>>(s: A) -> $name {
- $name::from_le_bytes(
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
+ pub fn from_byte_seq_be<A: SeqTrait<U8>>(s: A) -> $name {
+ $name::from_be_bytes(
s.iter()
.map(|x| U8::declassify(*x))
.collect::<Vec<_>>()
@@ -1031,22 +1050,27 @@ macro_rules! public_nat_mod {
)
}
- #[cfg_attr(feature = "use_attributes", in_hacspec)]
- pub fn to_public_byte_seq_le(self) -> Seq<u8> {
- Seq::from_vec(self.to_le_bytes())
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
+ pub fn from_public_byte_seq_be<A: SeqTrait<u8>>(s: A) -> $name {
+ $name::from_be_bytes(s.iter().map(|x| *x).collect::<Vec<_>>().as_slice())
}
- #[cfg_attr(feature = "use_attributes", in_hacspec)]
- pub fn to_byte_seq_le(self) -> Seq<U8> {
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
+ pub fn to_byte_seq_be(self) -> Seq<U8> {
Seq::from_vec(
- self.to_le_bytes()
+ self.to_be_bytes()
.iter()
.map(|x| U8::classify(*x))
.collect::<Vec<U8>>(),
)
}
- #[cfg_attr(feature = "use_attributes", in_hacspec)]
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
+ pub fn to_public_byte_seq_be(self) -> Seq<u8> {
+ Seq::from_vec(self.to_be_bytes())
+ }
+
+ #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
pub fn from_secret_literal(x: U128) -> $name {
$name::from_literal(U128::declassify(x))
}
