|
| 1 | +module zkgm::lib { |
| 2 | + use std::aptos_hash; |
| 3 | + use std::bcs; |
| 4 | + use std::from_bcs; |
| 5 | + |
| 6 | + friend zkgm::ibc_app; |
| 7 | + |
| 8 | + const E_INVALID_HOPS: u64 = 2; |
| 9 | + |
| 10 | + const OP_FORWARD: u8 = 0x00; |
| 11 | + const OP_MULTIPLEX: u8 = 0x01; |
| 12 | + const OP_BATCH: u8 = 0x02; |
| 13 | + const OP_FUNGIBLE_ASSET_ORDER: u8 = 0x03; |
| 14 | + |
| 15 | + const FORWARD_SALT_MAGIC: u256 = 0xC0DE00000000000000000000000000000000000000000000000000000000BABE; |
| 16 | + |
| 17 | + // TODO(aeryz): test this well |
| 18 | + public(friend) fun is_forwarded_packet( |
| 19 | + salt: vector<u8> |
| 20 | + ): bool { |
| 21 | + let salt = from_bcs::to_u256(salt); |
| 22 | + |
| 23 | + (salt & FORWARD_SALT_MAGIC) == FORWARD_SALT_MAGIC |
| 24 | + } |
| 25 | + |
| 26 | + /// Find last set (most significant bit). |
| 27 | + /// Returns the index of the most significant bit of `x`. |
| 28 | + /// If `x` is zero, returns 256. |
| 29 | + public(friend) fun fls(x: u256): u256 { |
| 30 | + if (x == 0) { |
| 31 | + return 256 |
| 32 | + }; |
| 33 | + |
| 34 | + let r: u256 = 0; |
| 35 | + |
| 36 | + // Check higher 128 bits |
| 37 | + if (x > 0xffffffffffffffffffffffffffffffff) { |
| 38 | + r = 128; |
| 39 | + x = x >> 128; |
| 40 | + }; |
| 41 | + |
| 42 | + // Check higher 64 bits |
| 43 | + if (x > 0xffffffffffffffff) { |
| 44 | + r = r + 64; |
| 45 | + x = x >> 64; |
| 46 | + }; |
| 47 | + |
| 48 | + // Check higher 32 bits |
| 49 | + if (x > 0xffffffff) { |
| 50 | + r = r + 32; |
| 51 | + x = x >> 32; |
| 52 | + }; |
| 53 | + |
| 54 | + // Check higher 16 bits |
| 55 | + if (x > 0xffff) { |
| 56 | + r = r + 16; |
| 57 | + x = x >> 16; |
| 58 | + }; |
| 59 | + |
| 60 | + // Check higher 8 bits |
| 61 | + if (x > 0xff) { |
| 62 | + r = r + 8; |
| 63 | + x = x >> 8; |
| 64 | + }; |
| 65 | + |
| 66 | + // Check higher 4 bits |
| 67 | + if (x > 0xf) { |
| 68 | + r = r + 4; |
| 69 | + x = x >> 4; |
| 70 | + }; |
| 71 | + |
| 72 | + // Check higher 2 bits |
| 73 | + if (x > 0x3) { |
| 74 | + r = r + 2; |
| 75 | + x = x >> 2; |
| 76 | + }; |
| 77 | + |
| 78 | + // Check higher 1 bit |
| 79 | + if (x > 0x1) { |
| 80 | + r = r + 1; |
| 81 | + }; |
| 82 | + |
| 83 | + r |
| 84 | + } |
| 85 | + |
| 86 | + public(friend) fun dequeue_channel_from_path( |
| 87 | + path: u256 |
| 88 | + ): (u256, u32) { |
| 89 | + (path >> 32, (path as u32)) |
| 90 | + } |
| 91 | + |
| 92 | + public(friend) fun reverse_channel_path( |
| 93 | + path: u256 |
| 94 | + ): u256 { |
| 95 | + (((path >> 0) as u32) as u256) << 224 |
| 96 | + | (((path >> 32) as u32) as u256) << 192 |
| 97 | + | (((path >> 64) as u32) as u256) << 160 |
| 98 | + | (((path >> 96) as u32) as u256) << 128 |
| 99 | + | (((path >> 128) as u32) as u256) << 96 |
| 100 | + | (((path >> 160) as u32) as u256) << 64 |
| 101 | + | (((path >> 192) as u32) as u256) << 32 |
| 102 | + | (((path >> 224) as u32) as u256) << 0 |
| 103 | + } |
| 104 | + |
| 105 | + public(friend) fun pop_channel_from_path(path: u256): (u256, u32) { |
| 106 | + if (path == 0) { |
| 107 | + return (0, 0) |
| 108 | + }; |
| 109 | + let current_hop_index = fls(path) / 32; |
| 110 | + let clear_shift = (((8 - current_hop_index) * 32) as u8); |
| 111 | + ( |
| 112 | + (path << clear_shift) >> clear_shift, |
| 113 | + ((path >> ((current_hop_index * 32) as u8)) as u32) |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + public(friend) fun update_channel_path(path: u256, next_channel_id: u32): u256 { |
| 118 | + if (path == 0) { |
| 119 | + return (next_channel_id as u256) |
| 120 | + }; |
| 121 | + let next_hop_index = ((fls(path) / 32) as u8) + 1; |
| 122 | + if (next_hop_index > 7) { |
| 123 | + abort E_INVALID_HOPS |
| 124 | + }; |
| 125 | + |
| 126 | + let next_channel = (((next_channel_id as u256) << (next_hop_index * 32)) as u256) |
| 127 | + | path; |
| 128 | + (next_channel as u256) |
| 129 | + } |
| 130 | + |
| 131 | + public(friend) fun is_allowed_batch_instruction(opcode: u8): bool { |
| 132 | + opcode == OP_MULTIPLEX || opcode == OP_FUNGIBLE_ASSET_ORDER |
| 133 | + } |
| 134 | + |
| 135 | + public(friend) fun is_allowed_forward_instruction(opcode: u8): bool { |
| 136 | + opcode == OP_MULTIPLEX || opcode == OP_FUNGIBLE_ASSET_ORDER || opcode == OP_BATCH |
| 137 | + } |
| 138 | + |
| 139 | + public(friend) fun tint_forward_salt(salt: vector<u8>): vector<u8> { |
| 140 | + let salt = from_bcs::to_u256(salt); |
| 141 | + let salt = FORWARD_SALT_MAGIC | (salt & (FORWARD_SALT_MAGIC ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); |
| 142 | + bcs::to_bytes(&salt) |
| 143 | + } |
| 144 | + |
| 145 | + public(friend) fun derive_forward_salt(salt: vector<u8>): vector<u8> { |
| 146 | + tint_forward_salt(aptos_hash::keccak256(salt)) |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + #[test] |
| 151 | + public fun test_update_channel_path() { |
| 152 | + assert!(update_channel_path(0, 0) == 0, 1); |
| 153 | + assert!(update_channel_path(0, 34) == 34, 1); |
| 154 | + assert!(update_channel_path(12414123, 111) == 476753783979, 1); |
| 155 | + assert!(update_channel_path(44, 22) == 94489280556, 1); |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + #[test] |
| 160 | + fun test_fls() { |
| 161 | + assert!(fls(0) == 256, 1); |
| 162 | + assert!(fls(22) == 4, 23); |
| 163 | + assert!(fls(32) == 5, 33); |
| 164 | + assert!(fls(444) == 8, 33); |
| 165 | + assert!(fls(6671) == 12, 33); |
| 166 | + assert!(fls(33334411) == 24, 33); |
| 167 | + } |
| 168 | +} |
0 commit comments