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

update equals on wcpoperation due to Bytes32 equal method inconsistency. #1573

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,28 @@ public static EWord ofQuantity(final Quantity quantity) {
public int byteLength() {
return (this.bitLength() + 7) / 8;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Bytes)) {
return false;
}

Bytes other = (Bytes) obj;
if (this.size() != other.size()) {
return false;
}

// start from the end for perf reasons as ewords often start with 0s.
for (int i = this.size() - 1; i >= 0; i--) {
if (this.get(i) != other.get(i)) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright ConsenSys 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.container.stacked;

import net.consensys.linea.zktracer.module.wcp.WcpOperation;
import net.consensys.linea.zktracer.types.EWord;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class WcpOperationTests {

@Test
void equals() {
Bytes32 a = Bytes32.ZERO.copy().mutableCopy();
EWord ew = EWord.ofHexString(a.toHexString());
// Even though both are Bytes32, the equal method fails on them:
// Assertions.assertTrue(ew.equals(a));

WcpOperation wo1 = new WcpOperation(WcpOperation.LEQbv, a, a);
WcpOperation wo2 = new WcpOperation(WcpOperation.LEQbv, ew, ew);
Assertions.assertTrue(wo1.equals(wo2));
Assertions.assertTrue(wo2.equals(wo1));
}

@Test
void different() {
Bytes32 a = Bytes32.ZERO.copy().mutableCopy();
EWord ew = EWord.ofHexString(Bytes32.repeat((byte) 1).toHexString());

WcpOperation wo1 = new WcpOperation(WcpOperation.LEQbv, a, a);
WcpOperation wo2 = new WcpOperation(WcpOperation.LEQbv, ew, ew);
Assertions.assertFalse(wo1.equals(wo2));
Assertions.assertFalse(wo2.equals(wo1));
}
}
Loading