-
Notifications
You must be signed in to change notification settings - Fork 34
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: make the trace deterministic #1346
Changes from 3 commits
dd5c4f3
a5d6a8f
12bd5de
fc956e4
1385093
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.module.add; | ||
|
||
import java.util.Comparator; | ||
|
||
public class AddOperationComparator implements Comparator<AddOperation> { | ||
public int compare(AddOperation op1, AddOperation op2) { | ||
// First sort by OpCode | ||
final int opCodeComp = op1.opCode().compareTo(op2.opCode()); | ||
if (opCodeComp != 0) { | ||
return opCodeComp; | ||
} | ||
// Second sort by Arg1 | ||
final int arg1Comp = op1.arg1().compareTo(op2.arg1()); | ||
if (arg1Comp != 0) { | ||
return arg1Comp; | ||
} | ||
// Third, sort by Arg2 | ||
return op1.arg2().compareTo(op2.arg2()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.module.bin; | ||
|
||
import java.util.Comparator; | ||
|
||
public class BinOperationComparator implements Comparator<BinOperation> { | ||
public int compare(BinOperation op1, BinOperation op2) { | ||
// First sort by OpCode | ||
final int opCodeComp = op1.opCode().compareTo(op2.opCode()); | ||
if (opCodeComp != 0) { | ||
return opCodeComp; | ||
} | ||
// Second sort by Arg1 | ||
final int arg1Comp = op1.arg1().getBytes32().compareTo(op2.arg1().getBytes32()); | ||
if (arg1Comp != 0) { | ||
return arg1Comp; | ||
} | ||
// Third, sort by Arg2 | ||
return op1.arg2().getBytes32().compareTo(op2.arg2().getBytes32()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.module.euc; | ||
|
||
import java.util.Comparator; | ||
|
||
public class EucOperationComparator implements Comparator<EucOperation> { | ||
public int compare(EucOperation op1, EucOperation op2) { | ||
// Second sort by Dividend | ||
final int arg1Comp = op1.dividend().compareTo(op2.dividend()); | ||
if (arg1Comp != 0) { | ||
return arg1Comp; | ||
} else { | ||
// Second, sort by Divisor | ||
return op1.divisor().compareTo(op2.divisor()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.module.exp; | ||
|
||
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.EXP_INST_EXPLOG; | ||
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.EXP_INST_MODEXPLOG; | ||
|
||
import java.util.Comparator; | ||
|
||
import net.consensys.linea.zktracer.module.hub.fragment.imc.exp.ExplogExpCall; | ||
import net.consensys.linea.zktracer.module.hub.fragment.imc.exp.ModexpLogExpCall; | ||
|
||
public class ExpOperationComparator implements Comparator<ExpOperation> { | ||
@Override | ||
public int compare(ExpOperation op1, ExpOperation op2) { | ||
final int instructionComp = | ||
Integer.compare(op1.expCall().expInstruction(), op2.expCall().expInstruction()); | ||
if (instructionComp != 0) { | ||
return instructionComp; | ||
} | ||
|
||
if (op1.expCall.expInstruction() == EXP_INST_EXPLOG) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem ideal. I would have thought it makes more sense to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do agree. |
||
final ExplogExpCall o1 = (ExplogExpCall) op1.expCall(); | ||
final ExplogExpCall o2 = (ExplogExpCall) op2.expCall(); | ||
|
||
final int dynCostComp = Long.compare(o1.dynCost(), o2.dynCost()); | ||
if (dynCostComp != 0) { | ||
return dynCostComp; | ||
} | ||
return o1.exponent().compareTo(o2.exponent()); | ||
} | ||
|
||
if (op1.expCall.expInstruction() == EXP_INST_MODEXPLOG) { | ||
final ModexpLogExpCall o1 = (ModexpLogExpCall) op1.expCall(); | ||
final ModexpLogExpCall o2 = (ModexpLogExpCall) op2.expCall(); | ||
|
||
final int cdsCutoffComp = Integer.compare(o1.getCdsCutoff(), o2.getCdsCutoff()); | ||
if (cdsCutoffComp != 0) { | ||
return cdsCutoffComp; | ||
} | ||
final int ebsCutoffComp = Integer.compare(o1.getEbsCutoff(), o2.getEbsCutoff()); | ||
if (ebsCutoffComp != 0) { | ||
return ebsCutoffComp; | ||
} | ||
final int leadLogComp = o1.getLeadLog().compareTo(o2.getLeadLog()); | ||
if (leadLogComp != 0) { | ||
return leadLogComp; | ||
} | ||
|
||
return o1.getRawLeadingWord().compareTo(o2.getRawLeadingWord()); | ||
} | ||
|
||
throw new IllegalStateException("Unknown instruction type"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems odd that you've implemented all the
Comparators
separately from the class itself. You can have e.g. doneclass AddOperation extends ModuleOperation implements Comparable<AddOperation>
then added acompareTo()
method as required for the interface. Just saves lines I guess --- not a big deal though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the reason for doing it this way is to avoid implemented a
Comparator
for allOperation
classes? I think you still could do it though by modifying the signature ofOperationSetModule.sortOperations()
. I'd have to check it worked though --- so not worth bothering!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, one reason for having
AddOperation
implementComparable<AddOperation>
is that you can then modifyModuleOperationStackedSet
to require that its elements must be sortable like so:That just helps reduce the chance of a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made more consistent. For BlockHash we have to sort before commit time, as we have to trigger WCP.
added