From db93ed52398133007769a410a7fa31e510ae41d2 Mon Sep 17 00:00:00 2001 From: krlosMata Date: Wed, 29 Nov 2023 12:51:24 +0100 Subject: [PATCH 1/3] optimize arrays --- main/modexp/array_lib/array_div_mod_long.zkasm | 14 +++++++------- main/modexp/array_lib/array_div_mod_short.zkasm | 16 +++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/main/modexp/array_lib/array_div_mod_long.zkasm b/main/modexp/array_lib/array_div_mod_long.zkasm index f37dc514..ecf38f06 100644 --- a/main/modexp/array_lib/array_div_mod_long.zkasm +++ b/main/modexp/array_lib/array_div_mod_long.zkasm @@ -156,14 +156,14 @@ array_div_mod_long_inALTinB_before_end: ; End of edge cases array_div_mod_long_prepare_mul_quo_inB: - ${receiveLenQuotient()} => C + ${receiveLenQuotient()} => C :JMPN(failAssert) - ; 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 + ; ensure C0 > 0 + C - 1 :JMPN(failAssert) + ; if C > %ARRAY_MAX_LEN --> does not jump to `continue_xx` label + C - %ARRAY_MAX_LEN_PLUS_ONE :JMPN(continue_array_div_mod_long_prepare_mul_quo_inB, failAssert) + +continue_array_div_mod_long_prepare_mul_quo_inB: ; From here, 1 <= C <= %ARRAY_MAX_LEN ; To avoid non-determinism, we must ensure that the quotient is trimmed diff --git a/main/modexp/array_lib/array_div_mod_short.zkasm b/main/modexp/array_lib/array_div_mod_short.zkasm index a804bd32..f6ed18db 100644 --- a/main/modexp/array_lib/array_div_mod_short.zkasm +++ b/main/modexp/array_lib/array_div_mod_short.zkasm @@ -134,14 +134,16 @@ array_div_mod_short_inALTinB: ; End of edge cases array_div_mod_short_prepare_mul_quo_inB: - ${receiveLenQuotient_short()} => C + ; C = [c7, c6, ..., c0] + ; JMPN instruction assures c0 is within the range [0, 2**32 - 1] + ${receiveLenQuotient_short()} => C :JMPN(failAssert) - ; 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 + ; ensure C0 > 0 + C - 1 :JMPN(failAssert) + ; if C > %ARRAY_MAX_LEN --> does not jump to `continue_xx` label + C - %ARRAY_MAX_LEN_PLUS_ONE :JMPN(continue_array_div_mod_short_prepare_mul_quo_inB, failAssert) + +continue_array_div_mod_short_prepare_mul_quo_inB: ; From here, 1 <= C <= %ARRAY_MAX_LEN ; To avoid non-determinism, we must ensure that the quotient is trimmed From 3d8d880dcddc0bbae99a11e6c8bbc16f25b2daa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Masip?= Date: Wed, 29 Nov 2023 17:42:25 +0100 Subject: [PATCH 2/3] Fixed the optimization. Moved an unused folder --- .../modexp/array_lib/array_div_mod_long.zkasm | 24 +++++++------------ .../array_lib/array_div_mod_short.zkasm | 15 ++++-------- package.json | 4 ++-- test/testArrayArith.zkasm | 5 ++++ test/testModExp.zkasm | 5 ++++ {test => tools}/modexp-utils/README.md | 0 .../modexp-utils/modexp-test-gen.js | 0 .../modexp-utils/modexp-test-int.sage | 0 8 files changed, 24 insertions(+), 29 deletions(-) rename {test => tools}/modexp-utils/README.md (100%) rename {test => tools}/modexp-utils/modexp-test-gen.js (100%) rename {test => tools}/modexp-utils/modexp-test-int.sage (100%) diff --git a/main/modexp/array_lib/array_div_mod_long.zkasm b/main/modexp/array_lib/array_div_mod_long.zkasm index ecf38f06..ac0e98f6 100644 --- a/main/modexp/array_lib/array_div_mod_long.zkasm +++ b/main/modexp/array_lib/array_div_mod_long.zkasm @@ -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 :JMPN(failAssert) + $0{receiveLenQuotient()} => C - ; ensure C0 > 0 - C - 1 :JMPN(failAssert) - ; if C > %ARRAY_MAX_LEN --> does not jump to `continue_xx` label - C - %ARRAY_MAX_LEN_PLUS_ONE :JMPN(continue_array_div_mod_long_prepare_mul_quo_inB, failAssert) - -continue_array_div_mod_long_prepare_mul_quo_inB: + ; The received length must be between 1 and %ARRAY_MAX_LEN + 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 @@ -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 diff --git a/main/modexp/array_lib/array_div_mod_short.zkasm b/main/modexp/array_lib/array_div_mod_short.zkasm index f6ed18db..f2f37e43 100644 --- a/main/modexp/array_lib/array_div_mod_short.zkasm +++ b/main/modexp/array_lib/array_div_mod_short.zkasm @@ -134,28 +134,21 @@ array_div_mod_short_inALTinB: ; End of edge cases array_div_mod_short_prepare_mul_quo_inB: - ; C = [c7, c6, ..., c0] - ; JMPN instruction assures c0 is within the range [0, 2**32 - 1] - ${receiveLenQuotient_short()} => C :JMPN(failAssert) + $0{receiveLenQuotient_short()} => C - ; ensure C0 > 0 - C - 1 :JMPN(failAssert) - ; if C > %ARRAY_MAX_LEN --> does not jump to `continue_xx` label - C - %ARRAY_MAX_LEN_PLUS_ONE :JMPN(continue_array_div_mod_short_prepare_mul_quo_inB, failAssert) - -continue_array_div_mod_short_prepare_mul_quo_inB: + ; The received length must be between 1 and %ARRAY_MAX_LEN + 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) diff --git a/package.json b/package.json index 98cd8c60..8efa574f 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,12 @@ "url": "https://github.com/0xPolygonHermez/zkevm-rom.git" }, "dependencies": { - "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#feature/fork-etrog", + "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#feature/infree0", "yargs": "^17.5.1" }, "devDependencies": { "@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#feature/fork-etrog", - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs-internal#feature/fork-etrog", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs-internal#feature/infree0", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors-internal#feature/fork-etrog", "chai": "^4.3.6", "chalk": "^3.0.0", diff --git a/test/testArrayArith.zkasm b/test/testArrayArith.zkasm index 6e1a95d9..6b8b821a 100644 --- a/test/testArrayArith.zkasm +++ b/test/testArrayArith.zkasm @@ -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) diff --git a/test/testModExp.zkasm b/test/testModExp.zkasm index a12774e3..2ef55605 100644 --- a/test/testModExp.zkasm +++ b/test/testModExp.zkasm @@ -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) diff --git a/test/modexp-utils/README.md b/tools/modexp-utils/README.md similarity index 100% rename from test/modexp-utils/README.md rename to tools/modexp-utils/README.md diff --git a/test/modexp-utils/modexp-test-gen.js b/tools/modexp-utils/modexp-test-gen.js similarity index 100% rename from test/modexp-utils/modexp-test-gen.js rename to tools/modexp-utils/modexp-test-gen.js diff --git a/test/modexp-utils/modexp-test-int.sage b/tools/modexp-utils/modexp-test-int.sage similarity index 100% rename from test/modexp-utils/modexp-test-int.sage rename to tools/modexp-utils/modexp-test-int.sage From fbed9b35e4df16de1f036f93a7d707ab7e1d5e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Masip=20Ardevol?= Date: Wed, 29 Nov 2023 17:55:09 +0100 Subject: [PATCH 3/3] Update package.json --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8efa574f..a4bea130 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,12 @@ "url": "https://github.com/0xPolygonHermez/zkevm-rom.git" }, "dependencies": { - "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#feature/infree0", + "@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#feature/fork-etrog", "yargs": "^17.5.1" }, "devDependencies": { "@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#feature/fork-etrog", - "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs-internal#feature/infree0", + "@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs-internal#feature/fork-etrog", "@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors-internal#feature/fork-etrog", "chai": "^4.3.6", "chalk": "^3.0.0", @@ -51,4 +51,4 @@ "eslint-plugin-mocha": "^10.1.0", "mocha": "^10.2.0" } -} \ No newline at end of file +}