types_primitive/
lib.rs

1// Copyright 2015-2018 Susy Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Primitive types shared by Higgsfield and Susy Sophon.
10//!
11//! Those are uint types `U128`, `U256` and `U512`, and fixed hash types `H160`,
12//! `H256` and `H512`, with optional serde serialization, susy-codec and
13//! rlp encoding.
14
15#![cfg_attr(not(feature = "std"), no_std)]
16
17#[macro_use]
18extern crate uint;
19
20#[macro_use]
21extern crate fixed_hash;
22
23#[cfg(feature = "impl-serde")]
24#[macro_use]
25extern crate impl_serde;
26
27#[cfg(feature = "codec-impl")]
28#[macro_use]
29extern crate codec_impl;
30
31#[cfg(feature = "impl-rlp")]
32#[macro_use]
33extern crate impl_rlp;
34
35construct_uint! {
36	/// 128-bit unsigned integer.
37	pub struct U128(2);
38}
39construct_uint! {
40	/// 256-bit unsigned integer.
41	pub struct U256(4);
42}
43construct_uint! {
44	/// 512-bits unsigned integer.
45	pub struct U512(8);
46}
47
48construct_fixed_hash! {
49	/// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size.
50	pub struct H160(20);
51}
52construct_fixed_hash! {
53	/// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size.
54	pub struct H256(32);
55}
56construct_fixed_hash! {
57	/// Fixed-size uninterpreted hash type with 64 bytes (512 bits) size.
58	pub struct H512(64);
59}
60
61#[cfg(feature = "impl-serde")]
62mod serde {
63	use super::*;
64
65	impl_uint_serde!(U128, 2);
66	impl_uint_serde!(U256, 4);
67	impl_uint_serde!(U512, 8);
68
69	impl_fixed_hash_serde!(H160, 20);
70	impl_fixed_hash_serde!(H256, 32);
71	impl_fixed_hash_serde!(H512, 64);
72}
73
74#[cfg(feature = "codec-impl")]
75mod codec {
76	use super::*;
77
78	impl_uint_codec!(U128, 2);
79	impl_uint_codec!(U256, 4);
80	impl_uint_codec!(U512, 8);
81
82	impl_fixed_hash_codec!(H160, 20);
83	impl_fixed_hash_codec!(H256, 32);
84	impl_fixed_hash_codec!(H512, 64);
85}
86
87#[cfg(feature = "impl-rlp")]
88mod rlp {
89	use super::*;
90
91	impl_uint_rlp!(U128, 2);
92	impl_uint_rlp!(U256, 4);
93	impl_uint_rlp!(U512, 8);
94
95	impl_fixed_hash_rlp!(H160, 20);
96	impl_fixed_hash_rlp!(H256, 32);
97	impl_fixed_hash_rlp!(H512, 64);
98}
99
100
101impl_fixed_hash_conversions!(H256, H160);
102
103impl U256 {
104	/// Multiplies two 256-bit integers to produce full 512-bit integer
105	/// No overflow possible
106	#[inline(always)]
107	pub fn full_mul(self, other: U256) -> U512 {
108		U512(uint_full_mul_reg!(U256, 4, self, other))
109	}
110}
111
112impl From<U256> for U512 {
113	fn from(value: U256) -> U512 {
114		let U256(ref arr) = value;
115		let mut ret = [0; 8];
116		ret[0] = arr[0];
117		ret[1] = arr[1];
118		ret[2] = arr[2];
119		ret[3] = arr[3];
120		U512(ret)
121	}
122}
123
124impl From<U512> for U256 {
125	fn from(value: U512) -> U256 {
126		let U512(ref arr) = value;
127		if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
128			panic!("From<U512> for U256: encountered overflow")
129		}
130		let mut ret = [0; 4];
131		ret[0] = arr[0];
132		ret[1] = arr[1];
133		ret[2] = arr[2];
134		ret[3] = arr[3];
135		U256(ret)
136	}
137}
138
139impl<'a> From<&'a U256> for U512 {
140	fn from(value: &'a U256) -> U512 {
141		let U256(ref arr) = *value;
142		let mut ret = [0; 8];
143		ret[0] = arr[0];
144		ret[1] = arr[1];
145		ret[2] = arr[2];
146		ret[3] = arr[3];
147		U512(ret)
148	}
149}
150
151impl<'a> From<&'a U512> for U256 {
152	fn from(value: &'a U512) -> U256 {
153		let U512(ref arr) = *value;
154		if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
155			panic!("From<&U512> for U256: encountered overflow")
156		}
157		let mut ret = [0; 4];
158		ret[0] = arr[0];
159		ret[1] = arr[1];
160		ret[2] = arr[2];
161		ret[3] = arr[3];
162		U256(ret)
163	}
164}