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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use primitive_types::{H256, U256};
use sha2::{Digest, Sha256};
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
use crate::types::H256Le;
const P2PKH_IN_WEIGHT: u32 = 148 * 4;
const P2PKH_OUT_SIZE: u32 = 34;
const P2SH_OUT_SIZE: u32 = 32;
const P2WPKH_OUT_SIZE: u32 = 31;
const PUBKEY_SIZE: u32 = 33;
const SIGNATURE_SIZE: u32 = 72;
const OP_RETURN_OUT_SIZE: u32 = 34;
const P2WPKH_IN_WEIGHT: u32 = 271; pub fn sha256d(bytes: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::default();
hasher.input(bytes);
let digest = hasher.result();
let mut second_hasher = Sha256::default();
second_hasher.input(digest);
let mut ret = [0; 32];
ret.copy_from_slice(&second_hasher.result()[..]);
ret
}
pub fn hash256_merkle_step(a: &[u8], b: &[u8]) -> H256Le {
let mut res: Vec<u8> = vec![];
res.extend(a);
res.extend(b);
H256Le::from_bytes_le(&sha256d(&res))
}
pub fn reverse_endianness(bytes: &[u8]) -> Vec<u8> {
let mut vec = Vec::from(bytes);
vec.reverse();
vec
}
pub fn log2(value: u64) -> u32 {
let mut current = value - 1;
let mut result: u32 = 0;
while current > 0 {
current >>= 1;
result += 1;
}
result
}
pub fn log256(value: &U256) -> u8 {
let mut current = value - 1;
let mut result: u8 = 0;
while current > 0.into() {
current >>= 8;
result += 1;
}
result
}
pub fn sha256d_be(bytes: &[u8]) -> H256 {
H256::from_slice(&sha256d(bytes)[..])
}
pub fn sha256d_le(bytes: &[u8]) -> H256Le {
H256Le::from_bytes_le(&sha256d(bytes))
}
pub enum InputType {
P2PKH,
P2SH { num_signatures: u32, num_pubkeys: u32 },
P2WPKHv0,
}
pub struct TransactionInputMetadata {
pub script_type: InputType,
pub count: u32,
}
pub struct TransactionOutputMetadata {
pub num_p2pkh: u32,
pub num_p2sh: u32,
pub num_p2wpkh: u32,
pub num_op_return: u32,
}
const fn script_length_size(length: u32) -> u32 {
if length < 75 {
1
} else if length <= 255 {
2
} else if length <= 65535 {
3
} else {
5
}
}
const fn var_int_size(length: u32) -> u32 {
if length < 253 {
1
} else if length < 65535 {
3
} else if length < 4294967295 {
5
} else {
9
}
}
const fn transaction_header_weight(input_type: InputType, input_count: u32, output_count: u32) -> u32 {
let extra_witness_weight = match input_type {
InputType::P2PKH | InputType::P2SH { .. } => 0,
InputType::P2WPKHv0 => var_int_size(input_count) + 2, };
let header_bytes = 4 + var_int_size(input_count) + var_int_size(output_count) + 4; header_bytes * 4 + extra_witness_weight
}
const fn transaction_weight(input: TransactionInputMetadata, output: TransactionOutputMetadata) -> u32 {
let input_weight = input.count
* match input.script_type {
InputType::P2PKH => P2PKH_IN_WEIGHT,
InputType::P2WPKHv0 => P2WPKH_IN_WEIGHT,
InputType::P2SH {
num_signatures,
num_pubkeys,
} => {
let redeem_script_size = 1 + num_pubkeys * (1 + PUBKEY_SIZE) + 1 + 1; let script_sig_size = 1 + num_signatures * (1 + SIGNATURE_SIZE) + script_length_size(redeem_script_size)
+ redeem_script_size;
let input_size = 32 + 4 + var_int_size(script_sig_size) + script_sig_size + 4;
input_size * 4
}
};
let output_weight = 4
* (output.num_p2pkh * P2PKH_OUT_SIZE
+ output.num_p2sh * P2SH_OUT_SIZE
+ output.num_p2wpkh * P2WPKH_OUT_SIZE
+ output.num_op_return * OP_RETURN_OUT_SIZE);
let output_count = output.num_op_return + output.num_p2pkh + output.num_p2sh + output.num_p2wpkh;
let header_weight = transaction_header_weight(input.script_type, input.count, output_count);
input_weight + output_weight + header_weight
}
pub const fn virtual_transaction_size(input: TransactionInputMetadata, output: TransactionOutputMetadata) -> u32 {
let weight = transaction_weight(input, output);
(weight + 3) / 4
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_log256() {
let value = U256::from_dec_str("680733321990486529407107157001552378184394215934016880640").unwrap();
let result = log256(&value);
assert_eq!(result, 24);
}
#[test]
fn test_sha256d() {
assert_eq!(
[
97, 244, 23, 55, 79, 68, 0, 180, 125, 202, 225, 168, 244, 2, 212, 244, 218, 207, 69, 90, 4, 66, 160,
106, 164, 85, 164, 71, 176, 212, 225, 112
],
sha256d(b"Hello World!")
);
}
#[test]
fn test_transaction_weight() {
assert_eq!(
transaction_weight(
TransactionInputMetadata {
count: 2,
script_type: InputType::P2PKH
},
TransactionOutputMetadata {
num_op_return: 1,
num_p2pkh: 3,
num_p2sh: 4,
num_p2wpkh: 5
}
),
2764 + 4 * OP_RETURN_OUT_SIZE
);
assert_eq!(
transaction_weight(
TransactionInputMetadata {
count: 1,
script_type: InputType::P2PKH
},
TransactionOutputMetadata {
num_op_return: 0,
num_p2pkh: 1,
num_p2sh: 0,
num_p2wpkh: 0
}
),
768
);
assert_eq!(
transaction_weight(
TransactionInputMetadata {
count: 3,
script_type: InputType::P2SH {
num_pubkeys: 9,
num_signatures: 7
}
},
TransactionOutputMetadata {
num_op_return: 1,
num_p2pkh: 5,
num_p2sh: 3,
num_p2wpkh: 4
}
),
12004 + OP_RETURN_OUT_SIZE * 4
);
assert_eq!(
transaction_weight(
TransactionInputMetadata {
count: 3,
script_type: InputType::P2WPKHv0
},
TransactionOutputMetadata {
num_op_return: 1,
num_p2pkh: 5,
num_p2sh: 3,
num_p2wpkh: 4
}
),
2416 + OP_RETURN_OUT_SIZE * 4
);
}
#[test]
fn test_virtual_transaction_size() {
assert_eq!(
virtual_transaction_size(
TransactionInputMetadata {
count: 2,
script_type: InputType::P2PKH
},
TransactionOutputMetadata {
num_op_return: 1,
num_p2pkh: 2,
num_p2sh: 0,
num_p2wpkh: 0,
}
),
374 + OP_RETURN_OUT_SIZE
);
}
}