Skip to content

Commit

Permalink
Merge pull request #63 from 0xPolygonHermez/feature/optimize-arrays
Browse files Browse the repository at this point in the history
optimize arrays
  • Loading branch information
hecmas authored Nov 30, 2023
2 parents 93984c7 + fbed9b3 commit 623f2cb
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
22 changes: 7 additions & 15 deletions main/modexp/array_lib/array_div_mod_long.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,15 @@ array_div_mod_long_inALTinB_before_end:
; End of edge cases

array_div_mod_long_prepare_mul_quo_inB:
${receiveLenQuotient()} => C
$0{receiveLenQuotient()} => C

; The received length must be between 1 and %ARRAY_MAX_LEN
C => A
0 => B
0 :EQ
%ARRAY_MAX_LEN_PLUS_ONE => B
1 :LT
C - 1 => RR :JMPN(failAssert) ; If C = 0, then fail
%ARRAY_MAX_LEN - C :JMPN(failAssert) ; If C > %ARRAY_MAX_LEN, then fail
; From here, 1 <= C <= %ARRAY_MAX_LEN

; To avoid non-determinism, we must ensure that the quotient is trimmed
; i.e., that its last chunk is not 0
C - 1 => RR
${receiveQuotientChunk(RR)} => A
0 => B
0 :EQ
Expand Down Expand Up @@ -201,19 +197,15 @@ array_div_mod_long_mul_quo_inB:
%MAX_CNT_STEPS - STEP - 14 :JMPN(outOfCountersStep)

; Check the remainder
${receiveLenRemainder()} => D
$0{receiveLenRemainder()} => D

; 1] The received length must be between 1 and %ARRAY_MAX_LEN
D => A
0 => B
0 :EQ
%ARRAY_MAX_LEN_PLUS_ONE => B
1 :LT
; From here, 1 <= C <= %ARRAY_MAX_LEN
D - 1 => E :JMPN(failAssert) ; If D = 0, then fail
%ARRAY_MAX_LEN - D :JMPN(failAssert) ; If D > %ARRAY_MAX_LEN, then fail
; From here, 1 <= D <= %ARRAY_MAX_LEN

; 2] To avoid non-determinism, we must ensure that the remainder is trimmed
; i.e., that its last chunk is not 0
D - 1 => E
${receiveRemainderChunk(E)} => A
0 => B
0 :EQ
Expand Down
11 changes: 3 additions & 8 deletions main/modexp/array_lib/array_div_mod_short.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,21 @@ array_div_mod_short_inALTinB:
; End of edge cases

array_div_mod_short_prepare_mul_quo_inB:
${receiveLenQuotient_short()} => C
$0{receiveLenQuotient_short()} => C

; The received length must be between 1 and %ARRAY_MAX_LEN
C => A
0 => B
0 :EQ
%ARRAY_MAX_LEN_PLUS_ONE => B
1 :LT
C - 1 => RR :JMPN(failAssert) ; If C = 0, then fail
%ARRAY_MAX_LEN - C :JMPN(failAssert) ; If C > %ARRAY_MAX_LEN, then fail
; From here, 1 <= C <= %ARRAY_MAX_LEN

; To avoid non-determinism, we must ensure that the quotient is trimmed
; i.e., that its last chunk is not 0
C - 1 => RR
${receiveQuotientChunk_short(RR)} => A
0 => B
0 :EQ
; From here, the quotient is trimmed

C :MSTORE(array_div_mod_short_len_quo)

C => RR

%MAX_CNT_STEPS - STEP - 5*%ARRAY_MAX_LEN - 4 :JMPN(outOfCountersStep)
Expand Down
5 changes: 5 additions & 0 deletions test/testArrayArith.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,11 @@ outOfCountersStep:
outOfCountersArith:
${dump(CNT_ARITH)} :JMP(end)

;@info function to force a failed assert
failAssert:
1 => A
2 :ASSERT

end:

$ => A :MLOAD(initial_A)
Expand Down
5 changes: 5 additions & 0 deletions test/testModExp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ outOfCountersStep:
outOfCountersArith:
${dump(CNT_ARITH)} :JMP(end)

;@info function to force a failed assert
failAssert:
1 => A
2 :ASSERT

end:

$ => A :MLOAD(initial_A)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 623f2cb

Please sign in to comment.