Skip to content

Commit 4375a29

Browse files
committed
chore(aptos): bunch of fixes to zkgm
Signed-off-by: aeryz <abdullaheryz@protonmail.com>
1 parent 2bb2fd7 commit 4375a29

File tree

5 files changed

+424
-239
lines changed

5 files changed

+424
-239
lines changed

aptos/ibc/sources/ibc_commitment.move

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ module ibc::commitment {
256256
keccak256(packet_commitment_path(channel_id, sequence))
257257
}
258258

259-
public(friend) fun batch_packets_commitment_key(
260-
batch_hash: vector<u8>
261-
): vector<u8> {
259+
public fun batch_packets_commitment_key(batch_hash: vector<u8>): vector<u8> {
262260
keccak256(batch_packets_commitment_path(batch_hash))
263261
}
264262

aptos/ucs03-zkgm/sources/forward.move

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,23 @@ module zkgm::forward {
6666
use std::vector;
6767

6868
struct Forward has copy, drop, store {
69-
channel_id: u32,
69+
path: u256,
7070
timeout_height: u64,
7171
timeout_timestamp: u64,
7272
instruction: Instruction
7373
}
7474

75-
public fun channel_id(forward: &Forward): u32 {
76-
forward.channel_id
75+
public fun new(
76+
path: u256,
77+
timeout_height: u64,
78+
timeout_timestamp: u64,
79+
instruction: Instruction
80+
): Forward {
81+
Forward { path, timeout_height, timeout_timestamp, instruction }
82+
}
83+
84+
public fun path(forward: &Forward): u256 {
85+
forward.path
7786
}
7887

7988
public fun timeout_height(forward: &Forward): u64 {
@@ -90,18 +99,17 @@ module zkgm::forward {
9099

91100
public fun encode(forward: &Forward): vector<u8> {
92101
let buf = vector::empty<u8>();
93-
zkgm_ethabi::encode_uint<u8>(&mut buf, 0x20);
94-
zkgm_ethabi::encode_uint<u32>(&mut buf, forward.channel_id);
102+
zkgm_ethabi::encode_uint<u256>(&mut buf, forward.path);
95103
zkgm_ethabi::encode_uint<u64>(&mut buf, forward.timeout_height);
96104
zkgm_ethabi::encode_uint<u64>(&mut buf, forward.timeout_timestamp);
97-
zkgm_ethabi::encode_uint<u8>(&mut buf, 0x80);
105+
zkgm_ethabi::encode_uint<u8>(&mut buf, 0x60);
98106
let ins_buf = instruction::encode(&forward.instruction);
99107
vector::append(&mut buf, ins_buf);
100108
buf
101109
}
102110

103111
public fun decode(buf: &vector<u8>, index: &mut u64): Forward {
104-
let channel_id = zkgm_ethabi::decode_uint(buf, index);
112+
let path = zkgm_ethabi::decode_uint(buf, index);
105113
let timeout_height = zkgm_ethabi::decode_uint(buf, index);
106114
let timeout_timestamp = zkgm_ethabi::decode_uint(buf, index);
107115
*index = *index + 0x20;
@@ -114,7 +122,7 @@ module zkgm::forward {
114122
let instruction = instruction::new(version, opcode, operand);
115123

116124
Forward {
117-
channel_id: (channel_id as u32),
125+
path,
118126
timeout_height: (timeout_height as u64),
119127
timeout_timestamp: (timeout_timestamp as u64),
120128
instruction: instruction

aptos/ucs03-zkgm/sources/lib.move

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}

aptos/ucs03-zkgm/sources/multiplex.move

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ module zkgm::multiplex {
138138
}
139139
}
140140

141-
public fun encode_multiplex_sender_and_calldata(
142-
sender: vector<u8>, contract_calldata: vector<u8>
141+
public fun encode_calldata(
142+
path: u256, sender: vector<u8>, contract_calldata: vector<u8>
143143
): vector<u8> {
144144
let buf = vector::empty<u8>();
145-
zkgm_ethabi::encode_uint<u8>(&mut buf, 0x20);
145+
zkgm_ethabi::encode_uint<u256>(&mut buf, path);
146146
zkgm_ethabi::encode_uint<u8>(&mut buf, 0x40);
147147
let length_of_first = vector::length(&sender);
148148
zkgm_ethabi::encode_uint<u64>(&mut buf, ((length_of_first / 32) * 0x20) + 0x80);
@@ -172,7 +172,7 @@ module zkgm::multiplex {
172172
x"00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000d23078354233384461366137303163353638353435644366634230334663423837356635366265646443343078354233384461366137303163353638353435644366634230334663423837356635366265646443343078354233384461366137303163353638353435644366634230334663423837356635366265646443343078354233384461366137303163353638353435644366634230334663423837356635366265646443343078354233384461366137303163353638353435644366634230334663423837356635366265646443340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010e4578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c446174614578616d706c6553797363616c6c44617461000000000000000000000000000000000000";
173173

174174
let ack_bytes =
175-
encode_multiplex_sender_and_calldata(
175+
encode_multiplex_calldata(
176176
b"0x5B38Da6a701c568545dCfcB03FcB875f56beddC40x5B38Da6a701c568545dCfcB03FcB875f56beddC40x5B38Da6a701c568545dCfcB03FcB875f56beddC40x5B38Da6a701c568545dCfcB03FcB875f56beddC40x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
177177
b"ExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallDataExampleSyscallData"
178178
);

0 commit comments

Comments
 (0)