Skip to content

Commit

Permalink
Merge pull request #102 from 0xPolygonHermez/fix-sp-negative-offset
Browse files Browse the repository at this point in the history
fix problem with negative offset
  • Loading branch information
zkronos73 authored Dec 14, 2022
2 parents 270960e + 5980581 commit e72191a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sm/sm_main/sm_main_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ module.exports = async function execute(pols, input, rom, config = {}, metadata
addrRel += fe2n(Fr, ctx.RR, ctx);
}
if (l.offset) addrRel += l.offset;
if (l.isStack == 1) addrRel += Number(ctx.SP);
if (!skipAddrRelControl) {
if (addrRel >= 0x20000 || (!l.isMem && addrRel >= 0x10000)) throw new Error(`Address too big ${sourceRef}`);
if (addrRel <0 ) throw new Error(`Address can not be negative ${sourceRef}`);
Expand All @@ -481,7 +482,6 @@ module.exports = async function execute(pols, input, rom, config = {}, metadata
}
if (l.isStack==1) {
addr += 0x10000;
addr += Number(ctx.SP);
pols.isStack[i] = 1n;
} else {
pols.isStack[i] = 0n;
Expand Down
19 changes: 19 additions & 0 deletions test/fix/sp_negative_offset/sp_negative_offset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const chai = require("chai");
const assert = chai.assert;
const fs = require("fs");
const path = require("path");

const {verifyZkasm} = require("../../verify_zkasm");

describe("Test Fix Negative Offset SP", async function () {
this.timeout(10000000000);

it("Test Fix Negative Offset SP", async () => {
await verifyZkasm(__dirname + "/sp_negative_offset.zkasm", true,
{ defines: {N: 2 ** 16},
namespaces: ['Global', 'Main', 'Rom'],
verbose: true,
color: true,
disableUnusedError: true});
});
});
18 changes: 18 additions & 0 deletions test/fix/sp_negative_offset/sp_negative_offset.zkasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
start:

STEP => A
0 :ASSERT


128514851843584385348488432423523454n => A :MSTORE(SP++)
$ => B :MLOAD(SP-1)
B :ASSERT

end:
0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, RCX, HASHPOS

finalWait:
${beforeLast()} : JMPN(finalWait)

: JMP(start)
opINVALID:

0 comments on commit e72191a

Please sign in to comment.