Skip to content

Commit a1b3f44

Browse files
authored
Merge pull request #32 from fjl/withdrawals-storage-interleave
withdrawals: interleave storage reads with output formatting
2 parents 3375a2a + 4637ffa commit a1b3f44

File tree

2 files changed

+130
-127
lines changed

2 files changed

+130
-127
lines changed

src/consolidations/main.eas

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -234,77 +234,89 @@ accum_loop:
234234
jumpi @update_head ;; [i, count, head_idx, tail_idx]
235235

236236
;; Determine the storage slot of the address for this iteration. This value is
237-
;; also the base for the other storage slots containing the source and the target
238-
;; public keys. The base slot will be (queue_offset + (queue_head + i)*SLOTS_PER_ITEM).
237+
;; also the base for the other storage slots containing the source and target
238+
;; public keys. The base slot will be (queue_offset + (queue_head + i)*4).
239239
dup3 ;; [head_idx, i, ..]
240240
dup2 ;; [i, head_idx, i, ..]
241241
add ;; [i+head_idx, i, ..]
242-
push SLOTS_PER_ITEM ;; [SLOTS_PER_ITEM, i+head_idx, i, ..]
243-
mul ;; [SLOTS_PER_ITEM*(i+head_idx), i, ..]
244-
push QUEUE_OFFSET ;; [offset, SLOTS_PER_ITEM*(i+head_idx), i, ..]
245-
add ;; [addr_offset, i, ..]
246-
247-
;; Read address from slot 0.
248-
dup1 ;; [addr_offset, addr_offset, i, ..]
249-
sload ;; [addr, addr_offset, i, ..]
250-
251-
;; Read source[0:32] from slot 1.
252-
swap1 ;; [addr_offset, addr, i, ..]
253-
push 1 ;; [1, addr_offset, addr, i, ..]
254-
add ;; [slot1_offset, addr, i, ..]
255-
dup1 ;; [slot1_offset, slot1_offset, addr, i, ..]
256-
sload ;; [source[0:32], slot1_offset, addr, i, ..]
257-
258-
;; Read source[32:48] and target[0:16] from slot 2.
259-
swap1 ;; [slot1_offset, source[0:32], addr, i, ..]
260-
push 1 ;; [1, slot1_offset, source[0:32], addr, i, ..]
261-
add ;; [slot2_offset, source[0:32], addr, i, ..]
262-
dup1 ;; [slot2_offset, slot2_offset, source[0:32], addr, i, ..]
263-
sload ;; [src[32:48] ++ tgt[0:16], slot2_offset, source[0:32], addr, i, ..]
242+
push SLOTS_PER_ITEM ;; [4, i+head_idx, i, ..]
243+
mul ;; [4*(i+head_idx), i, ..]
244+
push QUEUE_OFFSET ;; [offset, 4*(i+head_idx), i, ..]
245+
add ;; [slotbase, i, ..]
264246

265-
;; Read target[16:48] from slot 3.
266-
swap1 ;; [slot2_offset, src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
267-
push 1 ;; [1, slot2_offset, src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
268-
add ;; [slot3_offset, src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
269-
sload ;; [target[16:32], src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
270-
271-
;; Write values to memory flat and contiguously. This require combining the
272-
;; four storage elements so there is no padding:
273-
;; (addr, source[0:32], source[32:48] ++ target[0:16], target[16:48])
247+
;; Write values to memory flat and contiguously. This requires combining the
248+
;; four storage elements (addr, spk1, spk2_tpk1, tpk2) so there is no padding.
249+
;;
250+
;; The slots have the following layout:
251+
;;
252+
;; 0: addr
253+
;; 0x00 | 00 00 00 00 00 00 00 00 00 00 00 00 aa aa aa aa
254+
;; 0x10 | aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
255+
;;
256+
;; 1: source[0:32] -> spk1
257+
;; 0x00 | bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
258+
;; 0x10 | bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
259+
;;
260+
;; 2: source[32:48] ++ target[0:16] -> spk2_tpk1
261+
;; 0x00 | bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
262+
;; 0x10 | cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
263+
;;
264+
;; 3: target[16:48] -> tpk2
265+
;; 0x20 | cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
266+
;; 0x30 | cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
274267

275-
;; Compute offset = i*RECORD_SIZE.
268+
;; Compute the output offset = i*RECORD_SIZE.
269+
dup2 ;; [i, slotbase, i, ..]
270+
push RECORD_SIZE ;; [size, i, slotbase, i, ..]
271+
mul ;; [offset=size*i, slotbase, i, ..]
276272

277-
dup5 ;; [i, target[16:32], src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
278-
push RECORD_SIZE ;; [size, i, target[16:32], src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
279-
mul ;; [offset, target[16:32], src[32:48] ++ tgt[0:16], source[0:32], addr, i, ..]
273+
;; Read slot 'addr' from storage.
274+
dup2 ;; [slotbase, offset, slotbase, ..]
275+
sload ;; [addr, offset, slotbase, ..]
280276

281277
;; Shift addr bytes.
282-
swap4 ;; [addr, src[32:48] ++ tgt[0:16], source[0:32], target[16:32], offset, i, ..]
283-
push 12*8 ;; [96, addr, src[32:48] ++ tgt[0:16], source[0:32], target[16:32], offset, i, ..]
284-
shl ;; [addr<<96, src[32:48] ++ tgt[0:16], source[0:32], target[16:32], offset, i, ..]
278+
push 12*8 ;; [96, addr, offset, slotbase, ..]
279+
shl ;; [addr<<96, offset, slotbase, ..]
285280

286281
;; Store addr at offset = i*RECORD_SIZE.
287-
dup5 ;; [offset, addr<<96, offset, src[32:48] ++ tgt[0:16], source[0:32], target[16:32], i, ..]
288-
mstore ;; [offset, src[32:48] ++ tgt[0:16], source[0:32], target[16:32], i, ..]
289-
290-
;; Store source[0:32] at offset = i*RECORD_SIZE + 20.
291-
swap2 ;; [source[0:32], src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
292-
dup4 ;; [offset, source[0:32], src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
293-
push 20 ;; [20, offset, source[0:32], src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
294-
add ;; [offset+20, source[0:32], src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
295-
mstore ;; [src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
296-
297-
;; Store src[32:48] ++ tgt[0:16] at offset = i*RECORD_SIZE + 52.
298-
dup3 ;; [offset, src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
299-
push 52 ;; [52, offset, src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
300-
add ;; [offset+52, src[32:48] ++ tgt[0:16], target[16:32], offset, i, ..]
301-
mstore ;; [target[16:32], offset, i, ..]
302-
303-
;; Store target[16:48] at offset = i*RECORD_SIZE + 84.
304-
swap1 ;; [offset, target[16:32], i, ..]
305-
push 84 ;; [84, offset, target[16:32], i, ..]
306-
add ;; [offset+84, target[16:32], i, ..]
307-
mstore ;; [i, ..]
282+
dup2 ;; [offset, addr<<96, offset, slotbase, ..]
283+
mstore ;; [offset, slotbase, ..]
284+
push 20 ;; [20, offset, slotbase, ..]
285+
add ;; [offset=offset+20, slotbase, ..]
286+
287+
;; Read slot 'spk1' from storage.
288+
dup2 ;; [slotbase, offset, slotbase, ..]
289+
push 1 ;; [1, slotbase, offset, slotbase, ..]
290+
add ;; [slot, offset, slotbase, ..]
291+
sload ;; [spk1, offset, slotbase, ..]
292+
293+
;; Store spk1 at output offset = i*RECORD_SIZE+20.
294+
dup2 ;; [offset, spk1, offset, slotbase, ..]
295+
mstore ;; [offset, slotbase, ..]
296+
push 32 ;; [32, offset, slotbase, ..]
297+
add ;; [offset=offset+32, slotbase, ..]
298+
299+
;; Read slot 'spk2_tpk1' from stoarge.
300+
dup2 ;; [slotbase, offset, slotbase, ..]
301+
push 2 ;; [1, slotbase, offset, slotbase, ..]
302+
add ;; [slot, offset, slotbase, ..]
303+
sload ;; [spk2_tpk1, offset, slotbase, ..]
304+
305+
;; Store spk2_tpk1 at output offset = i*RECORD_SIZE+52.
306+
dup2 ;; [offset, src[32:48] ++ tgt[0:16], offset, slotbase, ..]
307+
mstore ;; [offset, slotbase, ..]
308+
push 32 ;; [32, offset, slotbase, ..]
309+
add ;; [offset=offset+32, slotbase, ..]
310+
311+
;; Read target[16:48] from slot 3.
312+
swap1 ;; [slotbase, offset, ..]
313+
push 3 ;; [3, slotbase, offset, ..]
314+
add ;; [slot, offset, ..]
315+
sload ;; [tpk2, offset, ..]
316+
317+
;; Store tpk2 at output offset = i*RECORD_SIZE+84.
318+
swap1 ;; [offset, tpk2, ..]
319+
mstore ;; [..]
308320

309321
;; Increment i.
310322
push 1 ;; [1, i, ..]

src/withdrawals/main.eas

Lines changed: 57 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -236,92 +236,83 @@ accum_loop:
236236
add ;; [i+head_idx, i, ..]
237237
push 3 ;; [3, i+head_idx, i, ..]
238238
mul ;; [3*(i+head_idx), i, ..]
239-
push QUEUE_OFFSET ;; [offset, 3*(i+head_idx), i, ..]
240-
add ;; [addr_offset, i, ..]
241-
242-
;; Read address.
243-
dup1 ;; [addr_offset, addr_offset, i, ..]
244-
sload ;; [addr, addr_offset, i, ..]
245-
246-
;; Compute pk1 offset and read it.
247-
swap1 ;; [addr_offset, addr, i, ..]
248-
push 1 ;; [1, addr_offset, addr, i, ..]
249-
add ;; [pk1_offset, addr, i, ..]
250-
dup1 ;; [pk1_offset, pk1_offset, addr, i, ..]
251-
sload ;; [pk1, pk1_offset, addr, i, ..]
252-
253-
;; Compute pk2_am offset and read it.
254-
swap1 ;; [pk1_offset, pk1, addr, i, ..]
255-
push 1 ;; [1, pk1_offset, pk1, addr, i, ..]
256-
add ;; [pk2_am_offset, pk1, addr, i, ..]
257-
sload ;; [pk2_am, pk1, addr, i, ..]
258-
259-
;; Reorder values.
260-
swap2 ;; [addr, pk1, pk2_am, i, ..]
261-
262-
;; Write values to memory flat and contiguously. This require combining the
239+
push QUEUE_OFFSET ;; [queue_offset, 3*(i+head_idx), i, ..]
240+
add ;; [slotbase, i, ..]
241+
242+
;; Write values to memory flat and contiguously. This requires combining the
263243
;; three storage elements (addr, pk1, pk2_am) so there is no padding.
264244
;;
265-
;; Each stack element has the following layout:
245+
;; The slots have the following layout:
266246
;;
267-
;; A: addr
247+
;; 0: addr
268248
;; 0x00 | 00 00 00 00 00 00 00 00 00 00 00 00 aa aa aa aa
269249
;; 0x10 | aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
270250
;;
271-
;; B: pk[0:32] -> pk1
251+
;; 1: pk[0:32] -> pk1
272252
;; 0x00 | bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
273253
;; 0x10 | bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
274254
;;
275-
;; C: pk[32:48] ++ am[0:8] -> pk2_am
276-
;; 0x00 | cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
277-
;; 0x10 | dd dd dd dd dd dd dd dd 00 00 00 00 00 00 00 00
278-
;;
279-
;; To get these three stack elements into the correct contiguous format, it is
280-
;; neccessary to combine them in the follow form:
281-
;;
282-
;; (A[12:32] ++ B[0:12], B[12:32] ++ C[0:12], C[12:24])
255+
;; 2: pk[32:48] ++ am[0:8] -> pk2_am
256+
;; 0x00 | bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
257+
;; 0x10 | cc cc cc cc cc cc cc cc 00 00 00 00 00 00 00 00
258+
259+
;; Compute the output offset = i*RECORD_SIZE.
260+
dup2 ;; [i, slotbase, i, ..]
261+
push RECORD_SIZE ;; [size, i, slotbase, i, ..]
262+
mul ;; [offset=size*i, slotbase, i, ..]
283263

284-
;; Compute offset = i*RECORD_SIZE.
285-
dup4 ;; [i, addr, pk1, pk2_am, i, ..]
286-
push RECORD_SIZE ;; [size, i, addr, pk1, pk2_am, i, ..]
287-
mul ;; [offset, addr, pk1, pk2_am, i, ..]
264+
;; Read slot 'addr' from storage.
265+
dup2 ;; [slotbase, offset, slotbase, ..]
266+
sload ;; [addr, offset, slotbase, ..]
288267

289268
;; Shift addr bytes.
290-
swap1 ;; [addr, offset, pk1, pk2_am, i, ..]
291-
push 12*8 ;; [96, addr, offset, pk1, pk2_am, i, ..]
292-
shl ;; [addr<<96, offset, pk1, pk2_am, i, ..]
293-
294-
;; Store addr at current offset.
295-
dup2 ;; [offset, addr<<96, offset, pk1, pk2_am, i, ..]
296-
mstore ;; [offset, pk1, pk2_am, i, ..]
297-
push 20 ;; [20, offset, pk1, pk2_am, i, ..]
298-
add ;; [offset, pk1, pk2_am, i, ..]
299-
300-
;; Store pk1 at offset = i*RECORD_SIZE + 20.
301-
swap1 ;; [pk1, offset, pk2_am, i, ..]
302-
dup2 ;; [offset, pk1, offset, pk2_am, i, ..]
303-
mstore ;; [offset, pk2_am, i, ..]
304-
push 32 ;; [32, offset, pk2_am, i, ..]
305-
add ;; [offset, pk2_am, i, ..]
269+
push 12*8 ;; [96, addr, offset, slotbase, ..]
270+
shl ;; [addr<<96, offset, slotbase, ..]
271+
272+
;; Store addr at output offset = i*RECORD_SIZE.
273+
dup2 ;; [offset, addr<<96, offset, slotbase, ..]
274+
mstore ;; [offset, slotbase, ..]
275+
push 20 ;; [20, offset, slotbase, ..]
276+
add ;; [offset=offset+20, slotbase, ..]
277+
278+
;; Read slot 'pk1' from storage.
279+
dup2 ;; [slotbase, offset, slotbase, ..]
280+
push 1 ;; [1, slotbase, offset, slotbase, ..]
281+
add ;; [slot, offset, slotbase, ..]
282+
sload ;; [pk1, offset, slotbase, ..]
283+
284+
;; Store pk1 at output offset = i*RECORD_SIZE + 20.
285+
dup2 ;; [offset, pk1, offset, slotbase, ..]
286+
mstore ;; [offset, slotbase, ..]
287+
push 32 ;; [32, offset, slotbase, ..]
288+
add ;; [offset=offset+32, slotbase, ..]
289+
290+
;; Read slot 'pk2_am' from storage.
291+
swap1 ;; [slotbase, offset, ..]
292+
push 2 ;; [2, slotbase, offset, ..]
293+
add ;; [slot, offset, ..]
294+
sload ;; [pk2_am, offset, ..]
306295

307296
;; Extract pk2 from pk2_am.
308-
dup2 ;; [pk2_am, offset, pk2_am, i, ..]
309-
push pk2_mask ;; [mask, pk2_am, offset, pk2_am, i, ..]
310-
and ;; [pk2, offset, pk2_am, i, ..]
297+
dup1 ;; [pk2_am, pk2_am, offset, ..]
298+
push pk2_mask ;; [mask, pk2_am, offset, ..]
299+
and ;; [pk2, pk2_am, offset, ..]
311300

312301
;; Store pk2 at offset = i*RECORD_SIZE + 52.
313-
dup2 ;; [offset, pk2, offset, pk2_am, i, ..]
314-
mstore ;; [offset, pk2_am, i, ..]
315-
push 16 ;; [16, offset, pk2_am, i, ..]
316-
add ;; [offset, pk2_am, i, ..]
302+
dup3 ;; [offset, pk2, pk2_am, offset, ..]
303+
mstore ;; [pk2_am, offset, ..]
304+
swap1 ;; [offset, pk2_am, ..]
305+
push 16 ;; [16, offset, pk2_am, ..]
306+
add ;; [offset=offset+16, pk2_am, ..]
317307

318308
;; Extract am from pk2_am.
319-
swap1 ;; [pk2_am, offset, i, ..]
320-
push 8*8 ;; [shft, pk2_am, offset, i, ..]
321-
shr ;; [am, offset, i, ..]
309+
swap1 ;; [pk2_am, offset, slotbase, ..]
310+
push 8*8 ;; [shft, pk2_am, offset, ..]
311+
shr ;; [am, offset, ..]
322312

323313
;; Store am at offset = i*RECORD_SIZE + 68.
324-
swap1 ;; [offset, am, i, ..]
314+
;; Note we convert to little-endian.
315+
swap1 ;; [offset, am, ..]
325316
%mstore_uint64_le() ;; [i, ..]
326317

327318
;; Increment i.

0 commit comments

Comments
 (0)