@@ -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, ..]
0 commit comments