forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
182 lines (165 loc) · 4.98 KB
/
Copy pathutils.rs
File metadata and controls
182 lines (165 loc) · 4.98 KB
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
use crate::treepp::*;
/// Calculates ceil(log_base(n))
pub(super) const fn log_base_ceil(n: u32, base: u32) -> u32 {
let mut res: u32 = 0;
let mut cur: u64 = 1;
while cur < (n as u64) {
cur *= base as u64;
res += 1;
}
res
}
/// Converts the given `checksum` into a vector of digits.
///
/// ## Output format
///
/// - sequence of `n_digits` many digits
/// - each digit a `u32` value in range `0..base`
/// - checksum converted into BE bytes, in turn converted into digits
pub(super) fn checksum_to_digits(mut checksum: u32, base: u32, n_digits: u32) -> Vec<u32> {
debug_assert!((16..=256).contains(&base));
debug_assert!(
base.checked_pow(n_digits)
.map(|upper_limit| checksum < upper_limit)
.unwrap_or(true),
"Checksum is too large to fit into the given number of digits"
);
let mut digits = vec![0; n_digits as usize]; // cast safety: 32-bit machine or higher
for digit in digits.iter_mut().rev() {
*digit = checksum % base;
checksum = (checksum - *digit) / base;
}
digits
}
/// Converts the given `message` into a vector of digits.
///
/// ## Output format
///
/// - sequence of `n_digits` many digits
/// - each digit a `u32` value in range `0..2.pow(log2_base)`
/// - message bytes are reversed (but not their nibbles!)
pub(crate) fn message_to_digits(n_digits: u32, log2_base: u32, message: &[u8]) -> Vec<u32> {
debug_assert!((4..=8).contains(&log2_base));
debug_assert!(
message.len() as u32 * 8 <= n_digits * log2_base,
"Message is too long to fit into the given number of digits"
);
let mut digits = vec![0u32; n_digits as usize]; // cast safety: 32-bit machine or higher
let mut digit_idx: u32 = 0;
let mut bit_idx: u32 = 0;
for mut byte in message.iter().copied() {
for _ in 0..8 {
if bit_idx == log2_base {
bit_idx = 0;
digit_idx += 1;
}
digits[digit_idx as usize] |= ((byte & 1) as u32) << bit_idx; // cast safety: 32-bit machine or higher
byte >>= 1;
bit_idx += 1;
}
}
digits.reverse();
digits
}
/// Returns a Bitcoin script that converts a message into a number.
///
/// ## Precondition
///
/// - message is at stack top
/// - message is split into `N_DIGITS` digits
/// - each digit is in range `0..2.pow(LOG2_BASE)`
///
/// ## Postcondition
///
/// - converted number is at stack top
pub fn digits_to_number<const N_DIGITS: usize, const LOG2_BASE: usize>() -> Script {
script! {
for _ in 0..N_DIGITS - 1 {
OP_TOALTSTACK
}
for _ in 0..N_DIGITS - 1 {
for _ in 0..LOG2_BASE {
OP_DUP OP_ADD // simulating OP_MUL
}
OP_FROMALTSTACK
OP_ADD
}
}
}
pub fn bitcoin_representation(x: i32) -> Vec<u8> {
let mut buf = [0u8; 8];
let len = bitcoin::script::write_scriptint(&mut buf, x as i64);
return buf[0..len].to_vec();
}
#[cfg(test)]
mod test {
use super::*;
use crate::run;
#[test]
fn test_bitcoin_representation() {
for i in 0..256 {
run(script! {
{ i }
{ bitcoin_representation(i) }
OP_EQUAL
})
}
}
#[test]
fn checksum_to_digits_endianness() {
// Integer is encoded as BE digit sequence
assert_eq!(
checksum_to_digits(0x12345678, 16, 8),
vec![1, 2, 3, 4, 5, 6, 7, 8],
);
}
#[test]
fn message_to_digits_endianness() {
let message: u32 = 0x12345678;
// Bytes are reversed from BE to LE
// Digits stay BE
// The result is a weird mix of endianness
assert_eq!(
message_to_digits(8, 4, &message.to_be_bytes()),
vec![7, 8, 5, 6, 3, 4, 1, 2],
);
// Bytes are reversed from LE to BE
// Digits stay BE
// The result is a BE sequence
assert_eq!(
message_to_digits(8, 4, &message.to_le_bytes()),
vec![1, 2, 3, 4, 5, 6, 7, 8],
);
}
#[test]
fn digits_to_number_endianness() {
// LE input is glued together as LE
let script = script! {
{ vec![0x07, 0x08, 0x05, 0x06, 0x03, 0x04, 0x01, 0x02] }
{ digits_to_number::<8, 4>() }
{ 0x78563412 }
OP_EQUAL
};
run(script);
// BE input is glued together as BE
let script = script! {
{ vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] }
{ digits_to_number::<8, 4>() }
{ 0x12345678 }
OP_EQUAL
};
run(script);
}
#[test]
fn message_digits_roundtrip() {
let message: u32 = 0x12345678;
let digits = message_to_digits(8, 4, &message.to_le_bytes());
let script = script! {
{ digits }
{ digits_to_number::<8, 4>() }
{ message }
OP_EQUAL
};
run(script);
}
}