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

Extracting the base correctly #1299

Merged
merged 1 commit into from
Sep 24, 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 @@ -135,17 +135,19 @@ public boolean extractExponent() {

public Bytes base() {
Bytes unpadded = Bytes.EMPTY;
if (callData.size() >= BASE_MIN_OFFSET) {
final int sizeToExtract = Math.min(bbsInt(), callData.size() - MBS_MIN_OFFSET);
unpadded = callData.slice(MBS_MIN_OFFSET, sizeToExtract);
final int firstOffset = BASE_MIN_OFFSET;
if (callData.size() > firstOffset) {
final int sizeToExtract = Math.min(bbsInt(), callData.size() - firstOffset);
unpadded = callData.slice(BASE_MIN_OFFSET, sizeToExtract);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were using MBS_MIN_OFFSET as opposed to BASE_MIN_OFFSET. Also the comparisons have to be strict.

}
return rightPadTo(unpadded, bbsInt());
}

public Bytes exp() {
Bytes unpadded = Bytes.EMPTY;
if (callData.size() >= BASE_MIN_OFFSET + bbsInt()) {
final int sizeToExtract = Math.min(ebsInt(), callData.size() - BASE_MIN_OFFSET - bbsInt());
final int firstOffset = BASE_MIN_OFFSET + bbsInt();
if (callData.size() > firstOffset) {
final int sizeToExtract = Math.min(ebsInt(), callData.size() - firstOffset);
unpadded = callData.slice(BASE_MIN_OFFSET + bbsInt(), sizeToExtract);
}
return rightPadTo(unpadded, ebsInt());
Expand All @@ -154,7 +156,7 @@ public Bytes exp() {
public Bytes mod() {
Bytes unpadded = Bytes.EMPTY;
final int firstOffset = BASE_MIN_OFFSET + bbsInt() + ebsInt();
if (callData.size() >= firstOffset) {
if (callData.size() > firstOffset) {
final int sizeToExtract = Math.min(mbsInt(), callData.size() - firstOffset);
unpadded = callData.slice(firstOffset, sizeToExtract);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@

public class ModexpSubsection extends PrecompileSubsection {

private final ModexpMetadata modExpMetadata;
private final ModexpMetadata modexpMetaData;
private ModexpPricingOobCall sixthOobCall;
private ImcFragment seventhImcFragment;

public ModexpSubsection(final Hub hub, final CallSection callSection) {
super(hub, callSection);

modExpMetadata = new ModexpMetadata(hub, callData);
if (modExpMetadata
modexpMetaData = new ModexpMetadata(hub, callData);
if (modexpMetaData
.bbs()
.toUnsignedBigInteger()
.compareTo(BigInteger.valueOf(PROVER_MAX_INPUT_BYTE_SIZE))
>= 0
|| modExpMetadata
|| modexpMetaData
.mbs()
.toUnsignedBigInteger()
.compareTo(BigInteger.valueOf(PROVER_MAX_INPUT_BYTE_SIZE))
>= 0
|| modExpMetadata
|| modexpMetaData
.ebs()
.toUnsignedBigInteger()
.compareTo(BigInteger.valueOf(PROVER_MAX_INPUT_BYTE_SIZE))
Expand All @@ -81,26 +81,26 @@ public ModexpSubsection(final Hub hub, final CallSection callSection) {

final ImcFragment secondImcFragment = ImcFragment.empty(hub);
fragments().add(secondImcFragment);
if (modExpMetadata.extractBbs()) {
final MmuCall mmuCall = forModexpExtractBbs(hub, this, modExpMetadata);
if (modexpMetaData.extractBbs()) {
final MmuCall mmuCall = forModexpExtractBbs(hub, this, modexpMetaData);
secondImcFragment.callMmu(mmuCall);
}
final ModexpXbsOobCall secondOobCall = new ModexpXbsOobCall(OOB_INST_MODEXP_BBS);
secondImcFragment.callOob(secondOobCall);

final ImcFragment thirdImcFragment = ImcFragment.empty(hub);
fragments().add(thirdImcFragment);
if (modExpMetadata.extractEbs()) {
final MmuCall mmuCall = forModexpExtractEbs(hub, this, modExpMetadata);
if (modexpMetaData.extractEbs()) {
final MmuCall mmuCall = forModexpExtractEbs(hub, this, modexpMetaData);
thirdImcFragment.callMmu(mmuCall);
}
final ModexpXbsOobCall thirdOobCall = new ModexpXbsOobCall(OOB_INST_MODEXP_EBS);
thirdImcFragment.callOob(thirdOobCall);

final ImcFragment fourthImcFragment = ImcFragment.empty(hub);
fragments().add(fourthImcFragment);
if (modExpMetadata.extractMbs()) {
final MmuCall mmuCall = forModexpExtractMbs(hub, this, modExpMetadata);
if (modexpMetaData.extractMbs()) {
final MmuCall mmuCall = forModexpExtractMbs(hub, this, modexpMetaData);
fourthImcFragment.callMmu(mmuCall);
}
final ModexpXbsOobCall fourthOobCall = new ModexpXbsOobCall(OOB_INST_MODEXP_MBS);
Expand All @@ -110,10 +110,10 @@ public ModexpSubsection(final Hub hub, final CallSection callSection) {
fragments().add(fifthImcFragment);
final ModexpLeadOobCall fifthOobCall = new ModexpLeadOobCall();
fifthImcFragment.callOob(fifthOobCall);
if (modExpMetadata.loadRawLeadingWord()) {
final MmuCall mmuCall = forModexpLoadLead(hub, this, modExpMetadata);
if (modexpMetaData.loadRawLeadingWord()) {
final MmuCall mmuCall = forModexpLoadLead(hub, this, modexpMetaData);
fifthImcFragment.callMmu(mmuCall);
final ExpCall modexpLogCallToExp = new ModexpLogExpCall(modExpMetadata);
final ExpCall modexpLogCallToExp = new ModexpLogExpCall(modexpMetaData);
fifthImcFragment.callExp(modexpLogCallToExp);
}

Expand Down Expand Up @@ -143,40 +143,40 @@ public void resolveAtContextReEntry(Hub hub, CallFrame callFrame) {
return;
}

modExpMetadata.rawResult(returnData);
hub.blakeModexpData().callModexp(modExpMetadata, this.exoModuleOperationId());
modexpMetaData.rawResult(returnData);
hub.blakeModexpData().callModexp(modexpMetaData, this.exoModuleOperationId());

fragments().add(seventhImcFragment);
if (modExpMetadata.extractModulus()) {
final MmuCall mmuCall = forModexpExtractBase(hub, this, modExpMetadata);
if (modexpMetaData.extractModulus()) {
final MmuCall mmuCall = forModexpExtractBase(hub, this, modexpMetaData);
seventhImcFragment.callMmu(mmuCall);
}

final ImcFragment eighthImcFragment = ImcFragment.empty(hub);
fragments().add(eighthImcFragment);
if (modExpMetadata.extractModulus()) {
final MmuCall mmuCall = forModexpExtractExponent(hub, this, modExpMetadata);
if (modexpMetaData.extractModulus()) {
final MmuCall mmuCall = forModexpExtractExponent(hub, this, modexpMetaData);
eighthImcFragment.callMmu(mmuCall);
}

final ImcFragment ninthImcFragment = ImcFragment.empty(hub);
fragments().add(ninthImcFragment);
if (modExpMetadata.extractModulus()) {
final MmuCall mmuCall = forModexpExtractModulus(hub, this, modExpMetadata);
if (modexpMetaData.extractModulus()) {
final MmuCall mmuCall = forModexpExtractModulus(hub, this, modexpMetaData);
ninthImcFragment.callMmu(mmuCall);
}

final ImcFragment tenthImcFragment = ImcFragment.empty(hub);
fragments().add(tenthImcFragment);
if (modExpMetadata.extractModulus()) {
final MmuCall mmuCall = forModexpFullResultCopy(hub, this, modExpMetadata);
if (modexpMetaData.extractModulus()) {
final MmuCall mmuCall = forModexpFullResultCopy(hub, this, modexpMetaData);
tenthImcFragment.callMmu(mmuCall);
}

final ImcFragment eleventhImcFragment = ImcFragment.empty(hub);
fragments().add(eleventhImcFragment);
if (modExpMetadata.mbsNonZero() && !parentReturnDataTarget.isEmpty()) {
final MmuCall mmuCall = forModexpPartialResultCopy(hub, this, modExpMetadata);
if (modexpMetaData.mbsNonZero() && !parentReturnDataTarget.isEmpty()) {
final MmuCall mmuCall = forModexpPartialResultCopy(hub, this, modexpMetaData);
eleventhImcFragment.callMmu(mmuCall);
}
}
Expand Down
Loading