#![no_std]
use core::mem::transmute;
use core::fmt;
#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct U256 (pub[u64;4]);
#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct U384 (pub[u64;6]);
#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct U512 (pub[u64;8]);
#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct U1024 (pub[u64;16]);
#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct U2048 (pub[u64;32]);
impl U256 {
pub fn new () -> U256 {
return U256([0u64;4]);
}
#[deprecated]
pub fn from_bytes (bytes: [u8;32]) -> [u64;4] {
return U256::from(bytes).0;
}
#[deprecated]
pub fn to_bytes (largebytes: [u64;4]) -> [u8;32] {
let output: [u8;32] = U256(largebytes).into();
return output;
}
}
impl From<[u8;32]> for U256 {
fn from(byte_array: [u8;32]) -> Self {
unsafe {
return transmute::<[u8;32], U256>(byte_array);
}
}
}
impl Into<[u8;32]> for U256 {
fn into(self) -> [u8;32] {
let U256(array) = self;
let mut output: [u8;32] = [0;32];
let mut counter: usize = 0;
for &x in array.iter() {
let bytes = x.to_be_bytes();
for &byte in bytes.iter() {
output[counter] = byte;
counter += 1;
}
}
return output;
}
}
impl fmt::Display for U256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let x = self.0;
let mut i: usize = 0;
let max = x.len() - 1;
while i < max {
i += 1;
write!(f, "0x{:X} ", x[i]);
}
write!(f, "0x{:X}", x[i])
}
}
impl U384 {
pub fn new () -> U384 {
return U384([0u64;6])
}
#[deprecated]
pub fn from_bytes (bytes: [u8;48]) -> [u64;6] {
return U384::from(bytes).0;
}
#[deprecated]
pub fn to_bytes (largebytes: [u64;6]) -> [u8;48] {
return U384(largebytes).into()
}
}
impl From<[u8;48]> for U384 {
fn from(byte_array: [u8;48]) -> Self {
unsafe {
return transmute::<[u8;48], U384>(byte_array);
}
}
}
impl Into<[u8;48]> for U384 {
fn into(self) -> [u8;48] {
let U384(array) = self;
let mut output: [u8;48] = [0;48];
let mut counter: usize = 0;
for &x in array.iter() {
let bytes = x.to_be_bytes();
for &byte in bytes.iter() {
output[counter] = byte;
counter += 1;
}
}
return output;
}
}
impl fmt::Display for U384 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let x = self.0;
let mut i: usize = 0;
let max = x.len() - 1;
while i < max {
i += 1;
write!(f, "0x{:X} ", x[i]);
}
write!(f, "0x{:X}", x[i])
}
}
impl U512 {
pub fn new () -> U512 {
return U512([0u64;8])
}
#[deprecated]
pub fn from_bytes (bytes: [u8;64]) -> [u64;8] {
return U512::from(bytes).0;
}
#[deprecated]
pub fn to_bytes (largebytes: [u64;8]) -> [u8;64] {
return U512(largebytes).into()
}
}
impl From<[u8;64]> for U512 {
fn from(byte_array: [u8;64]) -> Self {
unsafe {
return transmute::<[u8;64], U512>(byte_array);
}
}
}
impl Into<[u8;64]> for U512 {
fn into(self) -> [u8;64] {
let U512(array) = self;
let mut output: [u8;64] = [0;64];
let mut counter: usize = 0;
for &x in array.iter() {
let bytes = x.to_be_bytes();
for &byte in bytes.iter() {
output[counter] = byte;
counter += 1;
}
}
return output;
}
}
impl fmt::Display for U512 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let x = self.0;
let mut i: usize = 0;
let max = x.len() - 1;
while i < max {
i += 1;
write!(f, "0x{:X} ", x[i]);
}
write!(f, "0x{:X}", x[i])
}
}
impl U1024 {
pub fn new () -> U1024 {
return U1024([0u64;16])
}
#[deprecated]
pub fn from_bytes (bytes: [u8;128]) -> [u64;16] {
return U1024::from(bytes).0;
}
#[deprecated]
pub fn to_bytes (largebytes: [u64;16]) -> [u8;128] {
return U1024(largebytes).into()
}
}
impl From<[u8;128]> for U1024 {
fn from(byte_array: [u8;128]) -> Self {
unsafe {
return transmute::<[u8;128], U1024>(byte_array);
}
}
}
impl Into<[u8;128]> for U1024 {
fn into(self) -> [u8;128] {
let U1024(array) = self;
let mut output: [u8;128] = [0;128];
let mut counter: usize = 0;
for &x in array.iter() {
let bytes = x.to_be_bytes();
for &byte in bytes.iter() {
output[counter] = byte;
counter += 1;
}
}
return output;
}
}
impl fmt::Display for U1024 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let x = self.0;
let mut i: usize = 0;
let max = x.len() - 1;
while i < max {
i += 1;
write!(f, "0x{:X} ", x[i]);
}
write!(f, "0x{:X}", x[i])
}
}
impl U2048 {
pub fn new () -> U2048 {
return U2048([0u64;32])
}
#[deprecated]
pub fn from_bytes (bytes: [u8;256]) -> [u64;32] {
return U2048::from(bytes).0;
}
#[deprecated]
pub fn to_bytes (largebytes: [u64;32]) -> [u8;256] {
return U2048(largebytes).into()
}
}
impl From<[u8;256]> for U2048 {
fn from(byte_array: [u8;256]) -> Self {
unsafe {
return transmute::<[u8;256], U2048>(byte_array);
}
}
}
impl Into<[u8;256]> for U2048 {
fn into(self) -> [u8;256] {
let U2048(array) = self;
let mut output: [u8;256] = [0;256];
let mut counter: usize = 0;
for &x in array.iter() {
let bytes = x.to_be_bytes();
for &byte in bytes.iter() {
output[counter] = byte;
counter += 1;
}
}
return output;
}
}
impl fmt::Display for U2048 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let x = self.0;
let mut i: usize = 0;
let max = x.len() - 1;
while i < max {
i += 1;
write!(f, "0x{:X} ", x[i]);
}
write!(f, "0x{:X}", x[i])
}
}