From d06727bcc8f7d7b2fe54c71c4cf7c29fdbe5ced2 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 14:58:14 +0100 Subject: [PATCH 01/10] Improve updateTemporaryTargetRam.updateTemporaryTargetRam performance Signed-off-by: Ameziane H --- .../zktracer/module/mmio/MmioPatterns.java | 25 +++++++++---------- linea-constraints | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index d7798d4944..8ba03eac39 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -181,18 +181,17 @@ public static Bytes16 excision( } public static void updateTemporaryTargetRam( - MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { - final Bytes bytesPreLimb = - Bytes.repeat( - (byte) 0, - (int) - (LLARGE - * targetLimbOffsetToUpdate)); // We won't access the preLimb again, so we don't - // care - // of its value - final Bytes bytesPostLimb = - mmuData.targetRamBytes().slice((int) ((targetLimbOffsetToUpdate + 1) * LLARGE)); - - mmuData.targetRamBytes(Bytes.concatenate(bytesPreLimb, newLimb, bytesPostLimb)); + MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { + byte[] originalRam = mmuData.targetRamBytes().toArray(); + + int limbStart = (int) (LLARGE * targetLimbOffsetToUpdate); + int limbEnd = limbStart + (int) LLARGE; + + byte[] updatedRam = new byte[originalRam.length]; + System.arraycopy(originalRam, 0, updatedRam, 0, limbStart); + System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); + System.arraycopy(originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), + originalRam.length - limbEnd); + mmuData.targetRamBytes(Bytes.wrap(updatedRam)); } } diff --git a/linea-constraints b/linea-constraints index f0ce0da1e1..1c658b55bc 160000 --- a/linea-constraints +++ b/linea-constraints @@ -1 +1 @@ -Subproject commit f0ce0da1e102ecff0ed1d83e6ece0e2874cc07ad +Subproject commit 1c658b55bcde39395b636f1ff70cb93aaee2d06b From 521fc7b576f5e039c545cd9c4238bf6f86d55ac1 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 15:11:42 +0100 Subject: [PATCH 02/10] Remove unnecessary cast Signed-off-by: Ameziane H --- .../net/consensys/linea/zktracer/module/mmio/MmioPatterns.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index 8ba03eac39..824c64ad39 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -185,7 +185,7 @@ public static void updateTemporaryTargetRam( byte[] originalRam = mmuData.targetRamBytes().toArray(); int limbStart = (int) (LLARGE * targetLimbOffsetToUpdate); - int limbEnd = limbStart + (int) LLARGE; + int limbEnd = limbStart + LLARGE; byte[] updatedRam = new byte[originalRam.length]; System.arraycopy(originalRam, 0, updatedRam, 0, limbStart); From 596d750b4ce2aaee5c565703b6cad8bbde0496ce Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 15:13:50 +0100 Subject: [PATCH 03/10] Spotless Signed-off-by: Ameziane H --- .../consensys/linea/zktracer/module/mmio/MmioPatterns.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index 824c64ad39..c4474149e9 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -181,7 +181,7 @@ public static Bytes16 excision( } public static void updateTemporaryTargetRam( - MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { + MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { byte[] originalRam = mmuData.targetRamBytes().toArray(); int limbStart = (int) (LLARGE * targetLimbOffsetToUpdate); @@ -190,8 +190,8 @@ public static void updateTemporaryTargetRam( byte[] updatedRam = new byte[originalRam.length]; System.arraycopy(originalRam, 0, updatedRam, 0, limbStart); System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); - System.arraycopy(originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), - originalRam.length - limbEnd); + System.arraycopy( + originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), originalRam.length - limbEnd); mmuData.targetRamBytes(Bytes.wrap(updatedRam)); } } From 7beaec407c48ead5df5c1d291b4dcaaac32d1f91 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 16:17:05 +0100 Subject: [PATCH 04/10] Fix index issue Signed-off-by: Ameziane H --- .../linea/zktracer/module/mmio/MmioPatterns.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index c4474149e9..ca4e8248db 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -182,16 +182,18 @@ public static Bytes16 excision( public static void updateTemporaryTargetRam( MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { - byte[] originalRam = mmuData.targetRamBytes().toArray(); int limbStart = (int) (LLARGE * targetLimbOffsetToUpdate); int limbEnd = limbStart + LLARGE; + byte[] originalRam = mmuData.targetRamBytes().toArray(); - byte[] updatedRam = new byte[originalRam.length]; - System.arraycopy(originalRam, 0, updatedRam, 0, limbStart); + // The original formula below is limbStart + newLimb.size() + mmuData.targetRamBytes().size() - limbStart + int size = newLimb.size() + mmuData.targetRamBytes().size(); + byte[] updatedRam = new byte[size]; System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); System.arraycopy( originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), originalRam.length - limbEnd); + mmuData.targetRamBytes(Bytes.wrap(updatedRam)); } } From 905f5b3410465b75d32c10a7a79ee3674bda5e5c Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 16:34:54 +0100 Subject: [PATCH 05/10] Fix index issue Signed-off-by: Ameziane H --- .../zktracer/module/mmio/MmioPatterns.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index ca4e8248db..8ce1256b5b 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -187,8 +187,7 @@ public static void updateTemporaryTargetRam( int limbEnd = limbStart + LLARGE; byte[] originalRam = mmuData.targetRamBytes().toArray(); - // The original formula below is limbStart + newLimb.size() + mmuData.targetRamBytes().size() - limbStart - int size = newLimb.size() + mmuData.targetRamBytes().size(); + int size = limbStart + newLimb.size() + mmuData.targetRamBytes().size() - limbEnd; byte[] updatedRam = new byte[size]; System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); System.arraycopy( @@ -196,4 +195,20 @@ public static void updateTemporaryTargetRam( mmuData.targetRamBytes(Bytes.wrap(updatedRam)); } + + public static void updateTemporaryTargetRam0( + MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { + final Bytes bytesPreLimb = + Bytes.repeat( + (byte) 0, + (int) + (LLARGE + * targetLimbOffsetToUpdate)); // We won't access the preLimb again, so we don't + // care + // of its value + final Bytes bytesPostLimb = + mmuData.targetRamBytes().slice((int) ((targetLimbOffsetToUpdate + 1) * LLARGE)); + + mmuData.targetRamBytes(Bytes.concatenate(bytesPreLimb, newLimb, bytesPostLimb)); + } } From 8292ad65dbde8bcb3ad12833d235ea6ec0b8ca02 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 16:49:24 +0100 Subject: [PATCH 06/10] Fix index issue Signed-off-by: Ameziane H --- .../net/consensys/linea/zktracer/module/mmio/MmioPatterns.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index 8ce1256b5b..ff09cd8486 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -187,7 +187,7 @@ public static void updateTemporaryTargetRam( int limbEnd = limbStart + LLARGE; byte[] originalRam = mmuData.targetRamBytes().toArray(); - int size = limbStart + newLimb.size() + mmuData.targetRamBytes().size() - limbEnd; + int size = limbStart + newLimb.size() + originalRam.length - limbEnd; byte[] updatedRam = new byte[size]; System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); System.arraycopy( From 9dc21be048d63a2d66608a21f1c1e9b92fe0d1c3 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Mon, 2 Dec 2024 17:11:54 +0100 Subject: [PATCH 07/10] Fix index issue Signed-off-by: Ameziane H --- .../consensys/linea/zktracer/module/mmio/MmioPatterns.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index ff09cd8486..1d889c8873 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -186,12 +186,13 @@ public static void updateTemporaryTargetRam( int limbStart = (int) (LLARGE * targetLimbOffsetToUpdate); int limbEnd = limbStart + LLARGE; byte[] originalRam = mmuData.targetRamBytes().toArray(); + int sliceSize = Math.max(0, originalRam.length - limbEnd); - int size = limbStart + newLimb.size() + originalRam.length - limbEnd; + int size = limbStart + newLimb.size() + sliceSize; byte[] updatedRam = new byte[size]; System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); - System.arraycopy( - originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), originalRam.length - limbEnd); + if (sliceSize > 0) + System.arraycopy(originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), sliceSize); mmuData.targetRamBytes(Bytes.wrap(updatedRam)); } From e2eae6e1bc0c0a10d4c8461480b728c024458da3 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Tue, 3 Dec 2024 11:04:19 +0100 Subject: [PATCH 08/10] remove dead code Signed-off-by: Ameziane H --- .../linea/zktracer/module/mmio/MmioPatterns.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index 1d889c8873..af9a9c79c6 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -196,20 +196,4 @@ public static void updateTemporaryTargetRam( mmuData.targetRamBytes(Bytes.wrap(updatedRam)); } - - public static void updateTemporaryTargetRam0( - MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { - final Bytes bytesPreLimb = - Bytes.repeat( - (byte) 0, - (int) - (LLARGE - * targetLimbOffsetToUpdate)); // We won't access the preLimb again, so we don't - // care - // of its value - final Bytes bytesPostLimb = - mmuData.targetRamBytes().slice((int) ((targetLimbOffsetToUpdate + 1) * LLARGE)); - - mmuData.targetRamBytes(Bytes.concatenate(bytesPreLimb, newLimb, bytesPostLimb)); - } } From ee0fa203fc9acfa4267864f654ad75bc10e6ceb7 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Wed, 4 Dec 2024 11:51:46 +0100 Subject: [PATCH 09/10] Test a different approach Signed-off-by: Ameziane H --- .../linea/zktracer/module/mmio/MmioPatterns.java | 14 +------------- .../linea/zktracer/module/mmu/MmuData.java | 7 ++++--- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java index af9a9c79c6..4939d588cb 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmio/MmioPatterns.java @@ -182,18 +182,6 @@ public static Bytes16 excision( public static void updateTemporaryTargetRam( MmuData mmuData, final long targetLimbOffsetToUpdate, final Bytes16 newLimb) { - - int limbStart = (int) (LLARGE * targetLimbOffsetToUpdate); - int limbEnd = limbStart + LLARGE; - byte[] originalRam = mmuData.targetRamBytes().toArray(); - int sliceSize = Math.max(0, originalRam.length - limbEnd); - - int size = limbStart + newLimb.size() + sliceSize; - byte[] updatedRam = new byte[size]; - System.arraycopy(newLimb.toArray(), 0, updatedRam, limbStart, newLimb.size()); - if (sliceSize > 0) - System.arraycopy(originalRam, limbEnd, updatedRam, limbStart + newLimb.size(), sliceSize); - - mmuData.targetRamBytes(Bytes.wrap(updatedRam)); + mmuData.targetRamBytes().set((int) targetLimbOffsetToUpdate, newLimb); } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/MmuData.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/MmuData.java index 87e10554b2..0a0261fdda 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/MmuData.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/MmuData.java @@ -37,6 +37,7 @@ import net.consensys.linea.zktracer.module.mmu.values.MmuToMmioInstruction; import net.consensys.linea.zktracer.module.mmu.values.MmuWcpCallRecord; import org.apache.tuweni.bytes.Bytes; +import org.apache.tuweni.bytes.MutableBytes; @AllArgsConstructor @Getter @@ -56,7 +57,7 @@ public class MmuData { private boolean mmuInstAnyToRamWithPaddingIsPurePadding; private Bytes exoBytes; private Bytes sourceRamBytes; - private Bytes targetRamBytes; + private MutableBytes targetRamBytes; private final boolean exoLimbIsSource; private final boolean exoLimbIsTarget; private static final List MMU_INST_EXO_IS_SOURCE = @@ -83,7 +84,7 @@ public MmuData(final MmuCall mmuCall) { false, Bytes.EMPTY, Bytes.EMPTY, - Bytes.EMPTY, + MutableBytes.EMPTY, MMU_INST_EXO_IS_SOURCE.contains(mmuCall.instruction()), MMU_INST_EXO_IS_TARGET.contains(mmuCall.instruction())); @@ -112,7 +113,7 @@ public void setSourceRamBytes() { public void setTargetRamBytes() { if (mmuCall.targetRamBytes().isPresent()) { - targetRamBytes(mmuCall.targetRamBytes().get()); + targetRamBytes(mmuCall.targetRamBytes().get().mutableCopy()); } } From 59734b94c23040ccd5ce031dcce9f28cf8024522 Mon Sep 17 00:00:00 2001 From: "Ameziane H." Date: Wed, 4 Dec 2024 11:58:07 +0100 Subject: [PATCH 10/10] Test a different approach Signed-off-by: Ameziane H --- .../zktracer/module/mmu/instructions/ExoToRamTransplants.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/instructions/ExoToRamTransplants.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/instructions/ExoToRamTransplants.java index 53ee520567..afa133717f 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/instructions/ExoToRamTransplants.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/mmu/instructions/ExoToRamTransplants.java @@ -32,6 +32,7 @@ import net.consensys.linea.zktracer.module.mmu.values.MmuToMmioInstruction; import net.consensys.linea.zktracer.module.mmu.values.MmuWcpCallRecord; import org.apache.tuweni.bytes.Bytes; +import org.apache.tuweni.bytes.MutableBytes; public class ExoToRamTransplants implements MmuInstruction { private final Euc euc; @@ -89,7 +90,7 @@ public MmuData setMicroInstructions(MmuData mmuData) { // Setting the target ram bytes // The target CN is ALWAYS a new, virgin, fictitious context, where is either write the call // data, or the result of a precompile - mmuData.targetRamBytes(Bytes.EMPTY); + mmuData.targetRamBytes(MutableBytes.EMPTY); // setting the MMIO instructions for (int i = 0; i < mmuData.totalNonTrivialInitials(); i++) {