Skip to content

Commit

Permalink
test(rom) (#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzogentile404 authored Oct 21, 2024
1 parent 7258116 commit 4205f90
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,29 @@ public static String odd(int nLeadingZeros) {
return new BigInteger("0".repeat(nLeadingZeros) + "1".repeat(256 - nLeadingZeros), 2)
.toString(16);
}

/**
* Generates a hexadecimal string with `k` bytes, where the first `k-l` bytes are "00" and the
* last `l` bytes are "ff".
*
* @param k the total number of bytes in the resulting string
* @param l the number of trailing "ff" bytes
* @return the resulting hexadecimal string
*/
public static String trailingFF(int k, int l) {
return "00".repeat(k - l) + "ff".repeat(l);
}

/**
* Generates a hexadecimal string with `a` leading "00" bytes, followed by `b` "ff" bytes, and `c`
* trailing "00" bytes.
*
* @param a the number of leading "00" bytes
* @param b the number of "ff" bytes in the middle
* @param c the number of trailing "00" bytes
* @return the resulting hexadecimal string
*/
public static String middleFF(int a, int b, int c) {
return "00".repeat(a) + "ff".repeat(b) + "00".repeat(c);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.zktracer.module.rom;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import kotlin.Pair;
import net.consensys.linea.testing.BytecodeCompiler;
import net.consensys.linea.testing.BytecodeRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class RomTest {

@ParameterizedTest
@MethodSource("incompletePushRomTestSource")
void incompletePushRomTest(int j, int k) {
BytecodeCompiler program = BytecodeCompiler.newProgram();
program.incompletePush(k, "ff".repeat(j));
BytecodeRunner.of(program.compile()).run();
}

private static Stream<Arguments> incompletePushRomTestSource() {
List<Arguments> trailingFFRomTestSourceList = new ArrayList<>();
for (int k = 1; k <= 32; k++) {
for (int j = 0; j <= k; j++) {
trailingFFRomTestSourceList.add(Arguments.of(j, k));
}
}
return trailingFFRomTestSourceList.stream();
}

@Test
void randomConcatenationOfIncompletePushesRomTest() {
List<Pair<Integer, Integer>> permutationOfKAndJPairs = new ArrayList<>();
for (int k = 1; k <= 32; k++) {
for (int j = 0; j <= k; j++) {
permutationOfKAndJPairs.add(new Pair<>(k, j));
}
}
Collections.shuffle(permutationOfKAndJPairs);

BytecodeCompiler program = BytecodeCompiler.newProgram();
for (Pair<Integer, Integer> kAndJPair : permutationOfKAndJPairs) {
int k = kAndJPair.getFirst();
int j = kAndJPair.getSecond();
program.incompletePush(k, "5b".repeat(j));
}

BytecodeRunner.of(program.compile()).run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,33 @@ public BytecodeCompiler push(final int w, final UInt256 x) {
public Bytes compile() {
return Bytes.concatenate(byteCode);
}

/**
* Adds an incomplete PUSH operation of the given width and its argument to the bytecode sequence.
*
* @param w the width to push (in the range [1, 32])
* @param bs byte array to be added
* @return current instance
*/
public BytecodeCompiler incompletePush(final int w, final byte[] bs) {
Preconditions.condition(w > 0 && w <= 32, "Invalid PUSH width");
Preconditions.condition(bs.length <= w, "PUSH argument must be smaller than the width");

this.op(OpCode.of(0x5f + w));
this.byteCode.add(Bytes.of(bs));

return this;
}

/**
* Adds an incomplete PUSH operation of the given width and its argument to the bytecode sequence.
*
* @param w the width to push (in the range [1, 32])
* @param x string representing a hex number to be added
* @return current instance
*/
public BytecodeCompiler incompletePush(final int w, String x) {
return this.incompletePush(
w, bigIntegerToBytes(new BigInteger(x.isEmpty() ? "0" : x, 16)).toArray());
}
}

0 comments on commit 4205f90

Please sign in to comment.