ethers_primitive_types_rs/
lib.rs

1// Copyright 2020 Parity 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 Substrate and Parity Ethereum.
10//!
11//! Those are uint types `U128`, `U256` and `U512`, and fixed hash types `H160`,
12//! `H256` and `H512`, with optional serde serialization, parity-scale-codec and
13//! rlp encoding.
14
15#![cfg_attr(not(feature = "std"), no_std)]
16
17#[cfg(feature = "fp-conversion")]
18mod fp_conversion;
19
20use core::convert::TryFrom;
21use fixed_hash::{construct_fixed_hash, impl_fixed_hash_conversions};
22#[cfg(feature = "scale-info")]
23use scale_info_crate::TypeInfo;
24use uint::{construct_uint, uint_full_mul_reg};
25
26/// Error type for conversion.
27#[derive(Debug, PartialEq, Eq)]
28pub enum Error {
29	/// Overflow encountered.
30	Overflow,
31}
32
33construct_uint! {
34	/// 128-bit unsigned integer.
35	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
36	pub struct U128(2);
37}
38construct_uint! {
39	/// 256-bit unsigned integer.
40	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
41	pub struct U256(4);
42}
43construct_uint! {
44	/// 512-bits unsigned integer.
45	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
46	pub struct U512(8);
47}
48
49construct_fixed_hash! {
50	/// Fixed-size uninterpreted hash type with 16 bytes (128 bits) size.
51	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
52	pub struct H128(16);
53}
54
55construct_fixed_hash! {
56	/// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size.
57	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
58	pub struct H160(20);
59}
60construct_fixed_hash! {
61	/// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size.
62	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
63	pub struct H256(32);
64}
65construct_fixed_hash! {
66	/// Fixed-size uninterpreted hash type with 48 bytes (384 bits) size.
67	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
68	pub struct H384(48);
69}
70construct_fixed_hash! {
71	/// Fixed-size uninterpreted hash type with 64 bytes (512 bits) size.
72	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
73	pub struct H512(64);
74}
75construct_fixed_hash! {
76	/// Fixed-size uninterpreted hash type with 96 bytes (768 bits) size.
77	#[cfg_attr(feature = "scale-info", derive(TypeInfo))]
78	pub struct H768(96);
79}
80
81#[cfg(feature = "num-traits")]
82mod num_traits {
83	use super::*;
84	use impl_num_traits::impl_uint_num_traits;
85
86	impl_uint_num_traits!(U128, 2);
87	impl_uint_num_traits!(U256, 4);
88	impl_uint_num_traits!(U512, 8);
89}
90
91#[cfg(feature = "impl-serde")]
92mod serde {
93	use super::*;
94	use impl_serde::{impl_fixed_hash_serde, impl_uint_serde};
95
96	impl_uint_serde!(U128, 2);
97	impl_uint_serde!(U256, 4);
98	impl_uint_serde!(U512, 8);
99
100	impl_fixed_hash_serde!(H128, 16);
101	impl_fixed_hash_serde!(H160, 20);
102	impl_fixed_hash_serde!(H256, 32);
103	impl_fixed_hash_serde!(H384, 48);
104	impl_fixed_hash_serde!(H512, 64);
105	impl_fixed_hash_serde!(H768, 96);
106}
107
108#[cfg(feature = "impl-codec")]
109mod codec {
110	use super::*;
111	use impl_codec::{impl_fixed_hash_codec, impl_uint_codec};
112
113	impl_uint_codec!(U128, 2);
114	impl_uint_codec!(U256, 4);
115	impl_uint_codec!(U512, 8);
116
117	impl_fixed_hash_codec!(H128, 16);
118	impl_fixed_hash_codec!(H160, 20);
119	impl_fixed_hash_codec!(H256, 32);
120	impl_fixed_hash_codec!(H384, 48);
121	impl_fixed_hash_codec!(H512, 64);
122	impl_fixed_hash_codec!(H768, 96);
123}
124
125#[cfg(feature = "impl-rlp")]
126mod rlp {
127	use super::*;
128	use impl_rlp::{impl_fixed_hash_rlp, impl_uint_rlp};
129
130	impl_uint_rlp!(U128, 2);
131	impl_uint_rlp!(U256, 4);
132	impl_uint_rlp!(U512, 8);
133
134	impl_fixed_hash_rlp!(H128, 16);
135	impl_fixed_hash_rlp!(H160, 20);
136	impl_fixed_hash_rlp!(H256, 32);
137	impl_fixed_hash_rlp!(H384, 48);
138	impl_fixed_hash_rlp!(H512, 64);
139	impl_fixed_hash_rlp!(H768, 96);
140}
141
142impl_fixed_hash_conversions!(H256, H160);
143
144impl U128 {
145	/// Multiplies two 128-bit integers to produce full 256-bit integer.
146	/// Overflow is not possible.
147	#[inline(always)]
148	pub fn full_mul(self, other: U128) -> U256 {
149		U256(uint_full_mul_reg!(U128, 2, self, other))
150	}
151}
152
153impl U256 {
154	/// Multiplies two 256-bit integers to produce full 512-bit integer.
155	/// Overflow is not possible.
156	#[inline(always)]
157	pub fn full_mul(self, other: U256) -> U512 {
158		U512(uint_full_mul_reg!(U256, 4, self, other))
159	}
160}
161
162impl From<U256> for U512 {
163	fn from(value: U256) -> U512 {
164		let U256(ref arr) = value;
165		let mut ret = [0; 8];
166		ret[0] = arr[0];
167		ret[1] = arr[1];
168		ret[2] = arr[2];
169		ret[3] = arr[3];
170		U512(ret)
171	}
172}
173
174impl TryFrom<U256> for U128 {
175	type Error = Error;
176
177	fn try_from(value: U256) -> Result<U128, Error> {
178		let U256(ref arr) = value;
179		if arr[2] | arr[3] != 0 {
180			return Err(Error::Overflow)
181		}
182		let mut ret = [0; 2];
183		ret[0] = arr[0];
184		ret[1] = arr[1];
185		Ok(U128(ret))
186	}
187}
188
189impl TryFrom<U512> for U256 {
190	type Error = Error;
191
192	fn try_from(value: U512) -> Result<U256, Error> {
193		let U512(ref arr) = value;
194		if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
195			return Err(Error::Overflow)
196		}
197		let mut ret = [0; 4];
198		ret[0] = arr[0];
199		ret[1] = arr[1];
200		ret[2] = arr[2];
201		ret[3] = arr[3];
202		Ok(U256(ret))
203	}
204}
205
206impl TryFrom<U512> for U128 {
207	type Error = Error;
208
209	fn try_from(value: U512) -> Result<U128, Error> {
210		let U512(ref arr) = value;
211		if arr[2] | arr[3] | arr[4] | arr[5] | arr[6] | arr[7] != 0 {
212			return Err(Error::Overflow)
213		}
214		let mut ret = [0; 2];
215		ret[0] = arr[0];
216		ret[1] = arr[1];
217		Ok(U128(ret))
218	}
219}
220
221impl From<U128> for U512 {
222	fn from(value: U128) -> U512 {
223		let U128(ref arr) = value;
224		let mut ret = [0; 8];
225		ret[0] = arr[0];
226		ret[1] = arr[1];
227		U512(ret)
228	}
229}
230
231impl From<U128> for U256 {
232	fn from(value: U128) -> U256 {
233		let U128(ref arr) = value;
234		let mut ret = [0; 4];
235		ret[0] = arr[0];
236		ret[1] = arr[1];
237		U256(ret)
238	}
239}
240
241impl<'a> From<&'a U256> for U512 {
242	fn from(value: &'a U256) -> U512 {
243		let U256(ref arr) = *value;
244		let mut ret = [0; 8];
245		ret[0] = arr[0];
246		ret[1] = arr[1];
247		ret[2] = arr[2];
248		ret[3] = arr[3];
249		U512(ret)
250	}
251}
252
253impl<'a> TryFrom<&'a U512> for U256 {
254	type Error = Error;
255
256	fn try_from(value: &'a U512) -> Result<U256, Error> {
257		let U512(ref arr) = *value;
258		if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
259			return Err(Error::Overflow)
260		}
261		let mut ret = [0; 4];
262		ret[0] = arr[0];
263		ret[1] = arr[1];
264		ret[2] = arr[2];
265		ret[3] = arr[3];
266		Ok(U256(ret))
267	}
268}