From f68e9c51a104c0c32e346af5d6de77ea31189883 Mon Sep 17 00:00:00 2001 From: Lua Date: Tue, 20 Feb 2024 16:22:54 +0100 Subject: [PATCH] update pre-modexp --- main/precompiled/pre-modexp.zkasm | 78 ++++++++++++++----------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/main/precompiled/pre-modexp.zkasm b/main/precompiled/pre-modexp.zkasm index 4022c9ea..3e0b4baa 100644 --- a/main/precompiled/pre-modexp.zkasm +++ b/main/precompiled/pre-modexp.zkasm @@ -38,9 +38,9 @@ VAR GLOBAL modexp_Mend VAR GLOBAL modexp_offset VAR GLOBAL modexp_returnIndex VAR GLOBAL modexp_returnFirstIndex -VAR GLOBAL modexp_returnIndexRem VAR GLOBAL expLenBits VAR GLOBAL retCopyLen +VAR GLOBAL offsetCopyReturn funcModexp: @@ -273,35 +273,48 @@ finalMODEXP: B => A finalMODEXPreturn: - ; write data into memory + ; save lower between retCallLength and modexd_Msize in retCopyLen A :MSTORE(retCopyLen) + + ; write data into memory 0 => B + ; memory length === Msize $ => C :MLOAD(modexp_Msize) C :MSTORE(arithA) 32 :MSTORE(arithB), CALL(divARITH) + ; number of chunks (32 bytes) to copy from modexp_out to memory $ => E :MLOAD(arithRes1) + ; first index of modexp_out is the biggest --> 0x | modexp_out+N | .... | modexp_out+0 E :MSTORE(modexp_returnFirstIndex) $ => A :MLOAD(arithRes2) - A :MSTORE(modexp_returnIndexRem),JMPZ(memoryLoop) + A :JMPZ(memoryLoop) ; if Msize % 32 > 0, copy last bytes, else --> memoryLoop A => C + ; A = 0x0000000000000000000000000000000000000000XXXXXXXXXXXXXXXXXXXXXXXX + ; C = lenght X's $ => A :MLOAD(modexp_out+E) 32 - C => D :CALL(SHLarith) + ; A = 0xXXXXXXXXXXXXXXXXXXXXXXXX0000000000000000000000000000000000000000 A :MSTORE(bytesToStore) A :MSTORE(modexp_out+E) B => E + ; MSTORE X with C = lenght X's :CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset] + ; E == new offset (0 + length X's) E => B $ => A :MLOAD(modexp_Msize) A - C => C :JMPZ(modexpReturn) + ; C new_size = total_size - total_size%32 $ => E :MLOAD(modexp_returnFirstIndex) memoryLoop: + ; loop chunks of 32 bytes to memory %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) + ; E = update index modexp_out E - 1 => E :MSTORE(modexp_returnIndex) $ => A :MLOAD(modexp_out+E) A :MSTORE(bytesToStore) @@ -318,52 +331,33 @@ modexpReturn: 0 :MSTORE(retDataOffset) $ => C :MLOAD(modexp_Msize) C :MSTORE(retDataLength) - $ => B :MLOAD(retCallOffset) + $ => D :MLOAD(retCallOffset) + D :MSTORE(offsetCopyReturn) $ => A :MLOAD(originCTX), JMPZ(handleGas) ; set retDataCTX - $ => E :MLOAD(currentCTX) + $ => B :MLOAD(currentCTX) A => CTX - E :MSTORE(retDataCTX) - - $ => E :MLOAD(modexp_returnFirstIndex) - + B :MSTORE(retDataCTX) $ => C :MLOAD(retCopyLen) - C :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH) - $ => A :MLOAD(arithRes2), JMPZ(returnLoop) - A => C - $ => A :MLOAD(modexp_returnIndexRem),JMPNZ(returnInit) - E - 1 => E + 0 => E -returnInit: - $ => A :MLOAD(modexp_out+E) +modexpReturnLoop: + C - 32 :JMPN(modexpReturnFinal) + B => CTX :CALL(MLOAD32) + E => D + $ => CTX :MLOAD(originCTX) A :MSTORE(bytesToStore) - B => E - :CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset] - E => B - $ => A :MLOAD(retCopyLen) - A - C => C :JMPZ(endMODEXP) - $ => E :MLOAD(modexp_returnFirstIndex) - -returnLoop: - %MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary) - %MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep) - - E - 1 => E :MSTORE(modexp_returnIndex) - C - 32 :JMPN(returnLoopFinal) - $ => A :MLOAD(modexp_out+E) - A :MSTORE(bytesToStore) - B => E - :CALL(MSTORE32) ; in: [bytesToStore, E: offset] out: [E: new offset] - E => B - $ => E :MLOAD(modexp_returnIndex) - C - 32 => C :JMPZ(endMODEXP, returnLoop) - -returnLoopFinal: - $ => A :MLOAD(modexp_out+E) + $ => E :MLOAD(offsetCopyReturn) + :CALL(MSTORE32) + E :MSTORE(offsetCopyReturn) + D => E + C - 32 => C :JMPZ(endMODEXP, modexpReturnLoop) + +modexpReturnFinal: + B => CTX :CALL(MLOADX) + $ => CTX :MLOAD(originCTX) A :MSTORE(bytesToStore) - B => E - :CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset] + $ => E :MLOAD(offsetCopyReturn),CALL(MSTOREX) :JMP(endMODEXP) preEndMODEXP: