|
| 1 | +/* |
| 2 | + * Copyright ConsenSys Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | + |
| 16 | +package net.consensys.linea.zktracer.module.shakira; |
| 17 | + |
| 18 | +import static net.consensys.linea.zktracer.module.constants.GlobalConstants.WORD_SIZE; |
| 19 | +import static net.consensys.linea.zktracer.opcode.OpCode.*; |
| 20 | +import static net.consensys.linea.zktracer.types.Utils.rightPadTo; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Random; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import net.consensys.linea.UnitTestWatcher; |
| 28 | +import net.consensys.linea.testing.BytecodeCompiler; |
| 29 | +import net.consensys.linea.testing.BytecodeRunner; |
| 30 | +import net.consensys.linea.zktracer.opcode.OpCode; |
| 31 | +import org.apache.tuweni.bytes.Bytes; |
| 32 | +import org.apache.tuweni.bytes.Bytes32; |
| 33 | +import org.hyperledger.besu.datatypes.Address; |
| 34 | +import org.junit.jupiter.api.Tag; |
| 35 | +import org.junit.jupiter.api.TestInstance; |
| 36 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.Arguments; |
| 39 | +import org.junit.jupiter.params.provider.MethodSource; |
| 40 | + |
| 41 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 42 | +@ExtendWith(UnitTestWatcher.class) |
| 43 | +public class ShakiraInputsExtensiveTests { |
| 44 | + |
| 45 | + private final Random SEED = new Random(666); |
| 46 | + private final List<Integer> SIZE = |
| 47 | + List.of(0, 1, 2, 8, 15, 16, 17, 31, 32, 33, 254, 255, 256, 257, 258, 259); |
| 48 | + private final List<Integer> OFFSET = |
| 49 | + List.of(0, 1, 2, 15, 16, 17, 23, 31, 32, 33, 255, 256, 257, 65535, 65535, 65537); |
| 50 | + private final List<OpCode> INSTRUCTION = |
| 51 | + List.of(CALL, CALLCODE, STATICCALL, DELEGATECALL, SHA3, CREATE2, RETURN); |
| 52 | + |
| 53 | + private static final short CREATE_OPCODE_LENGTH = 21; |
| 54 | + |
| 55 | + @Tag("Weekly") |
| 56 | + @ParameterizedTest |
| 57 | + @MethodSource("inputs") |
| 58 | + void shakiraInputTesting(final int size, final int offset, final OpCode instruction) { |
| 59 | + BytecodeRunner.of( |
| 60 | + BytecodeCompiler.newProgram() |
| 61 | + .op(CALLDATASIZE) |
| 62 | + .push(0) |
| 63 | + .push(0) |
| 64 | + .op(CALLDATACOPY) |
| 65 | + .immediate(instructionSpecificBytecode(size, offset, instruction)) |
| 66 | + .compile()) |
| 67 | + .run(); |
| 68 | + } |
| 69 | + |
| 70 | + private Stream<Arguments> inputs() { |
| 71 | + final List<Arguments> inputs = new ArrayList<>(); |
| 72 | + for (int size : SIZE) { |
| 73 | + for (int offset : OFFSET) { |
| 74 | + inputs.add( |
| 75 | + Arguments.of(size, offset, INSTRUCTION.get(SEED.nextInt(0, INSTRUCTION.size())))); |
| 76 | + } |
| 77 | + } |
| 78 | + return inputs.stream(); |
| 79 | + } |
| 80 | + |
| 81 | + private Bytes instructionSpecificBytecode(int size, int offset, OpCode instruction) { |
| 82 | + switch (instruction) { |
| 83 | + case CALL, CALLCODE: |
| 84 | + { |
| 85 | + return BytecodeCompiler.newProgram() |
| 86 | + .push(SEED.nextInt(WORD_SIZE)) |
| 87 | + .push(0) |
| 88 | + .push(size) |
| 89 | + .push(offset) |
| 90 | + .push(0) |
| 91 | + .push(Address.SHA256) |
| 92 | + .push(1000000) |
| 93 | + .op(instruction) |
| 94 | + .compile(); |
| 95 | + } |
| 96 | + case STATICCALL, DELEGATECALL: |
| 97 | + { |
| 98 | + return BytecodeCompiler.newProgram() |
| 99 | + .push(SEED.nextInt(WORD_SIZE)) |
| 100 | + .push(0) |
| 101 | + .push(size) |
| 102 | + .push(offset) |
| 103 | + .push(Address.SHA256) |
| 104 | + .push(1000000) |
| 105 | + .op(instruction) |
| 106 | + .compile(); |
| 107 | + } |
| 108 | + case SHA3: |
| 109 | + { |
| 110 | + return BytecodeCompiler.newProgram().push(size).push(offset).op(SHA3).compile(); |
| 111 | + } |
| 112 | + case RETURN: |
| 113 | + { |
| 114 | + return BytecodeCompiler.newProgram() |
| 115 | + .push( |
| 116 | + rightPadTo( |
| 117 | + Bytes.concatenate( |
| 118 | + // size |
| 119 | + Bytes.of(CALLDATASIZE.byteValue()), |
| 120 | + Bytes.of(PUSH2.byteValue()), |
| 121 | + Bytes.ofUnsignedShort(CREATE_OPCODE_LENGTH), |
| 122 | + Bytes.of(SUB.byteValue()), |
| 123 | + // offset |
| 124 | + Bytes.of(PUSH2.byteValue()), |
| 125 | + Bytes.ofUnsignedShort(CREATE_OPCODE_LENGTH), |
| 126 | + // destOffset |
| 127 | + Bytes.of(PUSH2.byteValue()), |
| 128 | + Bytes.ofUnsignedShort(0), |
| 129 | + Bytes.of(CALLDATACOPY.byteValue()), |
| 130 | + Bytes.of(PUSH2.byteValue()), |
| 131 | + Bytes.ofUnsignedShort(size), |
| 132 | + Bytes.of(PUSH4.byteValue()), |
| 133 | + Bytes.ofUnsignedInt(offset), |
| 134 | + Bytes.of(RETURN.byteValue())), |
| 135 | + WORD_SIZE)) |
| 136 | + .push(0) |
| 137 | + .op(MSTORE) |
| 138 | + .op(CALLDATASIZE) |
| 139 | + .push(0) |
| 140 | + .push(CREATE_OPCODE_LENGTH) |
| 141 | + .op(CALLDATACOPY) |
| 142 | + .op(MSIZE) // size |
| 143 | + .push(0) // offset |
| 144 | + .push(0) // value |
| 145 | + .op(CREATE) |
| 146 | + .compile(); |
| 147 | + } |
| 148 | + case CREATE2: |
| 149 | + { |
| 150 | + return BytecodeCompiler.newProgram() |
| 151 | + .push(Bytes32.random(SEED)) |
| 152 | + .push(size) |
| 153 | + .push(offset) |
| 154 | + .push(12) // value |
| 155 | + .op(CREATE2) |
| 156 | + .compile(); |
| 157 | + } |
| 158 | + default: |
| 159 | + throw new IllegalArgumentException("Unsupported instruction: " + instruction); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments