Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement BIN counting #471

Merged
merged 12 commits into from
Dec 7, 2023
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nohup.out
.settings
.springBeans
.vertx
bin/
./bin
local.properties
target/
tmp/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Objects;

import net.consensys.linea.zktracer.types.Bytes16;
import net.consensys.linea.zktracer.types.UnsignedByte;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.MutableBytes32;

Expand Down Expand Up @@ -126,4 +127,43 @@ public Bytes32 getBytes32() {
public boolean isZero() {
return bytes32.isZero();
}

/**
* Add operation on an instance of {@link UnsignedByte}.
*
* @param other right hand side of and operation.
* @return the result of add operation as an instance of {@link UnsignedByte}.
*/
public BaseBytes and(final BaseBytes other) {
return new BaseBytes(bytes32.and(other.bytes32));
}

/**
* Or operation on an instance of {@link UnsignedByte}.
*
* @param other right hand side of or operation.
* @return the result of or operation as an instance of {@link UnsignedByte}.
*/
public BaseBytes or(final BaseBytes other) {
return new BaseBytes(bytes32.or(other.bytes32));
}

/**
* Xor operation on an instance of {@link UnsignedByte}.
*
* @param other right hand side of xor operation.
* @return the result of xor operation as an instance of {@link UnsignedByte}.
*/
public BaseBytes xor(final BaseBytes other) {
return new BaseBytes(bytes32.xor(other.bytes32));
}

/**
* Not operation on an instance of {@link UnsignedByte}.
*
* @return the result of not operation as an instance of {@link UnsignedByte}.
*/
public BaseBytes not() {
return new BaseBytes(bytes32.not());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.bin;

import java.nio.MappedByteBuffer;
import java.util.List;

import net.consensys.linea.zktracer.ColumnHeader;
import net.consensys.linea.zktracer.bytestheta.BaseBytes;
import net.consensys.linea.zktracer.container.stacked.set.StackedSet;
import net.consensys.linea.zktracer.module.Module;
import net.consensys.linea.zktracer.module.hub.Hub;
import net.consensys.linea.zktracer.opcode.OpCode;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.evm.frame.MessageFrame;

/** Implementation of a {@link Module} for addition/subtraction. */
public class Bin implements Module {
private final Hub hub;

/** A set of the operations to trace */
private final StackedSet<BinOperation> chunks = new StackedSet<>();

public Bin(final Hub hub) {
this.hub = hub;
}

@Override
public String moduleKey() {
return "BIN";
}

@Override
public void enterTransaction() {
this.chunks.enter();
}

@Override
public void popTransaction() {
this.chunks.pop();
}

@Override
public void tracePreOpcode(MessageFrame frame) {
final OpCode opCode = this.hub.opCode();
final Bytes32 arg1 = Bytes32.leftPad(frame.getStackItem(0));
final Bytes32 arg2 =
opCode == OpCode.NOT ? Bytes32.ZERO : Bytes32.leftPad(frame.getStackItem(1));

this.chunks.add(
new BinOperation(opCode, BaseBytes.fromBytes32(arg1), BaseBytes.fromBytes32(arg2)));
}

@Override
public void commit(List<MappedByteBuffer> buffers) {}

@Override
public List<ColumnHeader> columnsHeaders() {
return Trace.headers(this.lineCount());
}

@Override
public int lineCount() {
int sum = 0;
for (BinOperation op : this.chunks) {
sum += op.maxCt();
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.bin;

import com.google.common.base.Objects;
import lombok.Getter;
import lombok.experimental.Accessors;
import net.consensys.linea.zktracer.bytestheta.BaseBytes;
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.types.Bytes16;

@Getter
@Accessors(fluent = true)
public class BinOperation {
private static final int LIMB_SIZE = 16;

private final OpCode opCode;
private final BaseBytes arg1;
private final BaseBytes arg2;

final Bytes16 arg1Hi;

public BinOperation(OpCode opCode, BaseBytes arg1, BaseBytes arg2) {
this.opCode = opCode;
this.arg1 = arg1;
this.arg2 = arg2;

arg1Hi = arg1.getHigh();
}

@Override
public int hashCode() {
return Objects.hashCode(this.opCode, this.arg1, this.arg2);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final BinOperation that = (BinOperation) o;
return java.util.Objects.equals(opCode, that.opCode)
&& java.util.Objects.equals(arg1, that.arg1)
&& java.util.Objects.equals(arg2, that.arg2);
}

public boolean isOneLineInstruction() {
return (opCode == OpCode.BYTE || opCode == OpCode.SIGNEXTEND) && !arg1Hi.isZero();
}

public int maxCt() {
return isOneLineInstruction() ? 1 : LIMB_SIZE;
}
}
Loading
Loading