1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Script numeric

use std::ops;
use bytes::Bytes;
use Error;

/// Script numeric
///
/// Numeric opcodes (`OP_1ADD`, etc) are restricted to operating on 4-byte integers.
/// The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
/// but results may overflow (and are valid as long as they are not used in a subsequent
/// numeric operation). `CScriptNum` enforces those semantics by storing results as
/// an int64 and allowing out-of-range values to be returned as a vector of bytes but
/// throwing an exception if arithmetic is done or the result is interpreted as an integer.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Num {
	value: i64,
}

impl From<bool> for Num {
	fn from(i: bool) -> Self {
		let v = if i { 1 } else { 0 };
		Num {
			value: v
		}
	}
}

impl From<u8> for Num {
	fn from(i: u8) -> Self {
		Num {
			value: i as i64
		}
	}
}

impl From<u32> for Num {
	fn from(i: u32) -> Self {
		Num {
			value: i as i64
		}
	}
}

impl From<usize> for Num {
	fn from(i: usize) -> Self {
		Num {
			value: i as i64
		}
	}
}

impl From<i32> for Num {
	fn from(i: i32) -> Self {
		Num {
			value: i as i64
		}
	}
}

impl From<i64> for Num {
	fn from(i: i64) -> Self {
		Num {
			value: i
		}
	}
}

impl From<Num> for i64 {
	fn from(n: Num) -> Self {
		n.value
	}
}

impl From<Num> for u32 {
	fn from(n: Num) -> Self {
		n.value as u32
	}
}

impl From<Num> for usize {
	fn from(n: Num) -> Self {
		n.value as usize
	}
}

impl Num {
	/// Reduce the data size to its minimal, and then try to convert it to a num.
	pub fn minimally_encode(data: &[u8], max_size: usize) -> Result<Self, Error> {
		match data.last() {
			None => Num::from_slice(data, true, max_size),
			Some(last) => {
				if *last != 0x00 && *last != 0x80 {
					return Num::from_slice(data, true, max_size);
				}

				if data.len() == 1 {
					return Num::from_slice(&[], true, max_size);
				}

				if data[data.len() - 2] & 0x80 == 0x80 {
					return Num::from_slice(data, true, max_size);
				}

				// We are not minimally encoded. Create a vector so that we can trim the result. The last byte is not included,
				// as we first trim all zeros. And then a conditional to decide what to do with the last byte.
				let mut data: Vec<u8> = data[0..(data.len()-1)].iter().cloned().rev().skip_while(|x| *x == 0x00).collect();
				data.reverse();

				if data.len() == 0 {
					// At this point, last is either equal to 0x00 or 0x80. The result is empty.
					return Num::from_slice(&[], true, max_size);
				}

				let second_last = *data.last().expect("vec emptiness is checked above; qed");
				if second_last & 0x80 == 0x80 {
					data.push(*last);
				} else {
					*data.last_mut().expect("vec emptiness is checked above; qed") |= *last
				}

				Num::from_slice(&data, true, max_size)
			}
		}
	}

	pub fn from_slice(data: &[u8], require_minimal: bool, max_size: usize) -> Result<Self, Error> {
		if data.len() > max_size {
			return Err(Error::NumberOverflow);
		}

		if data.is_empty() {
			return Ok(0u8.into());
		}

		// Check that the number is encoded with the minimum possible
		// number of bytes.
		//
		// If the most-significant-byte - excluding the sign bit - is zero
		// then we're not minimal. Note how this test also rejects the
		// negative-zero encoding, 0x80.
		if require_minimal &&
			(data.last().unwrap() & 0x7f) == 0 &&
			(data.len() <= 1 || (data[data.len() - 2] & 0x80) == 0) {
			return Err(Error::NumberNotMinimallyEncoded)
		}

		let mut result = 0i64;
		for (i, item) in data.iter().enumerate() {
			result |= (*item as i64) << (8 * i);
		}

		// If the input vector's most significant byte is 0x80, remove it from
		// the result's msb and return a negative.
		if data.last().unwrap() & 0x80 != 0 {
			Ok((-(result & !(0x80i64 << (8 * (data.len() - 1))))).into())
		} else {
			Ok(result.into())
		}
	}

	pub fn to_bytes(&self) -> Bytes {
		if self.value == 0 {
			return Bytes::default();
		}

		let mut result = vec![];
		let negative = self.value < 0;
		let mut absvalue = if negative {
			(-self.value) as u64
		} else {
			self.value as u64
		};

		while absvalue > 0 {
			result.push(absvalue as u8 & 0xff);
			absvalue >>= 8;
		}

		//    - If the most significant byte is >= 0x80 and the value is positive, push a
		//    new zero-byte to make the significant byte < 0x80 again.

		//    - If the most significant byte is >= 0x80 and the value is negative, push a
		//    new 0x80 byte that will be popped off when converting to an integral.

		//    - If the most significant byte is < 0x80 and the value is negative, add
		//    0x80 to it, since it will be subtracted and interpreted as a negative when
		//    converting to an integral.

		if result[result.len() - 1] & 0x80 != 0 {
			if negative {
				result.push(0x80);
			} else {
				result.push(0);
			}
		} else if negative {
			let rlen = result.len();
			result[rlen - 1] |= 0x80;
		}

		result.into()
	}

	pub fn is_negative(&self) -> bool {
		self.value < 0
	}

	pub fn is_zero(&self) -> bool {
		self.value == 0
	}

	pub fn abs(&self) -> Num {
		if self.value < 0 {
			(-self.value).into()
		} else {
			self.value.into()
		}
	}
}

impl ops::BitAnd for Num {
	type Output = Self;

	fn bitand(self, rhs: Self) -> Self::Output {
		(self.value & rhs.value).into()
	}
}

impl ops::Add for Num {
	type Output = Self;

	fn add(self, rhs: Self) -> Self::Output {
		(self.value + rhs.value).into()
	}
}

impl ops::Sub for Num {
	type Output = Self;

	fn sub(self, rhs: Self) -> Self::Output {
		(self.value - rhs.value).into()
	}
}

impl ops::Neg for Num {
	type Output = Self;

	fn neg(self) -> Self::Output {
		(-self.value).into()
	}
}

impl ops::Div for Num {
	type Output = Self;

	fn div(self, rhs: Self) -> Self::Output {
		(self.value / rhs.value).into()
	}
}

impl ops::Rem for Num {
	type Output = Self;

	fn rem(self, rhs: Self) -> Self::Output {
		(self.value % rhs.value).into()
	}
}