| 57 | } |
| 58 | |
| 59 | pub fn double_padding(num_bytes: u32) -> (Vec<Script>, u32) { |
| 60 | //55 bytes fits in one block |
| 61 | //56 to 64 requires two block padding |
| 62 | |
| 63 | let mut chunks = num_bytes / 64; |
| 64 | chunks += 1; |
| 65 | |
| 66 | if num_bytes % 64 > 55 { |
| 67 | let mut bytes_left_first_padding = 64 - (num_bytes % 64); |
| 68 | bytes_left_first_padding -= 1; // remove the 0x80 that will be added always |
| 69 | let script1 = script! { |
| 70 | 8 |
| 71 | 0 |
| 72 | for _ in 0..bytes_left_first_padding { //can optimize with a couple of dups |
| 73 | 0 |
| 74 | 0 |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | let script2 = script! { |
| 79 | 0 |
| 80 | OP_DUP |
| 81 | for _ in 0..59 { |
| 82 | OP_2DUP |
| 83 | } |
| 84 | { u4_number_to_nibble( num_bytes * 8 ) } |
| 85 | }; |
| 86 | |
| 87 | chunks += 1; |
| 88 | |
| 89 | let mut results = Vec::new(); |
| 90 | for _ in 0..(chunks - 2) { |
| 91 | results.push(script! {}); |
| 92 | } |
| 93 | results.push(script1); |
| 94 | results.push(script2); |
| 95 | |
| 96 | (results, chunks) |
| 97 | } else { |
| 98 | let (script1, _) = padding(num_bytes); |
| 99 | let mut results = Vec::new(); |
| 100 | for _ in 0..(chunks - 1) { |
| 101 | results.push(script! {}); |
| 102 | } |
| 103 | results.push(script1); |
| 104 | (results, chunks) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub fn padding(num_bytes: u32) -> (Script, u32) { |
| 109 | let l = (num_bytes * 8) as i32; |