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

Repeat instruction #92

Merged
merged 3 commits into from
Nov 30, 2022
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
130 changes: 70 additions & 60 deletions pil/main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ namespace Main(%N);
pol commit zkPC; // Program Counter of current micro instruction
pol commit RR; // Micro code return address (subroutines)
pol commit HASHPOS;
pol commit RCX;

/////// Instruction related polynomials
pol commit CONST7, CONST6, CONST5, CONST4, CONST3, CONST2, CONST1, CONST0;
pol commit FREE7, FREE6, FREE5, FREE4, FREE3, FREE2, FREE1, FREE0;
pol commit inA, inB, inC, inROTL_C, inD, inE, inSR, inFREE, inCTX, inSP, inPC, inGAS, inMAXMEM, inSTEP, inRR, inHASHPOS;
pol commit setA, setB, setC, setD, setE, setSR, setCTX, setSP, setPC, setGAS, setMAXMEM, JMP, JMPN, JMPC, setRR, setHASHPOS;
pol commit inA, inB, inC, inROTL_C, inD, inE, inSR, inFREE, inCTX, inSP, inPC, inGAS, inMAXMEM, inSTEP, inRR, inHASHPOS, inRCX;
pol commit setA, setB, setC, setD, setE, setSR, setCTX, setSP, setPC, setGAS, setMAXMEM, JMP, JMPN, JMPC, setRR, setHASHPOS, setRCX;
pol commit offset;
pol commit incStack;
pol commit isStack;
Expand All @@ -68,6 +69,7 @@ namespace Main(%N);
pol commit bin;
pol commit binOpcode;
pol commit assert;
pol commit repeat;

/////// Intermediary commit polynomials
pol commit isNeg;
Expand Down Expand Up @@ -109,6 +111,7 @@ namespace Main(%N);
cntKeccakF * inCntKeccakF +
cntPoseidonG * inCntPoseidonG +
cntPaddingPG * inCntPaddingPG +
RCX * inRCX +
CONST0;

pol op1 =
Expand Down Expand Up @@ -228,6 +231,11 @@ namespace Main(%N);
2**27*hJmpnCondValueBit[4] + 2**26*hJmpnCondValueBit[3] + 2**25*hJmpnCondValueBit[2] + 2**24*hJmpnCondValueBit[1] +
2**23*hJmpnCondValueBit[0] + lJmpnCondValue;

/// RCX check zero
pol commit RCXInv;
pol RCXIsZero = 1 - RCX*RCXInv;
RCXIsZero*RCX = 0;

/////// MAXMEM intermediary vals New State

pol diffMem = isMaxMem* ( (maxMemRel - MAXMEM) -
Expand Down Expand Up @@ -301,17 +309,17 @@ namespace Main(%N);
RR' = setRR * (op0 - RR) + RR;
GAS' = setGAS * (op0 - GAS) + GAS;

pol decRCX = repeat * (1-RCXIsZero);
RCX' = setRCX * (op0 - (RCX-decRCX)) + (RCX-decRCX);

MAXMEM' = setMAXMEM * (op0 - maxMemCalculated) + maxMemCalculated;

HASHPOS' = setHASHPOS * (op0 - HASHPOS) + HASHPOS + (hashK + hashP)*D0;

pol doJMP = JMPN*isNeg + JMP + JMPC*carry;

zkPC' = doJMP * (addr - (zkPC+1)) +
(zkPC+1) ;


zkPC' = doJMP * (addr - (zkPC+1-decRCX)) +
(zkPC+1-decRCX);

(A0-op0)*assert = 0;
(A1-op1)*assert = 0;
Expand Down Expand Up @@ -432,72 +440,74 @@ namespace Main(%N);

/*
code generated with:
node tools/pil_pol_table/bits_compose.js "arithEq0,arithEq1,arithEq2,assert,bin,hashK,hashKDigest,hashKLen,hashP,hashPDigest,hashPLen,ind,indRR,isMem,isStack,JMP,JMPC,JMPN,memAlignRD,memAlignWR,memAlignWR8,mOp,mWR,setA,setB,setC,setCTX,setD,setE,setGAS,setHASHPOS,setMAXMEM,setPC,setRR,setSP,setSR,sRD,sWR,useCTX" -b
node tools/pil_pol_table/bits_compose.js "arithEq0,arithEq1,arithEq2,assert,bin,hashK,hashKDigest,hashKLen,hashP,hashPDigest,hashPLen,ind,indRR,isMem,isStack,JMP,JMPC,JMPN,memAlignRD,memAlignWR,memAlignWR8,mOp,mWR,repeat,setA,setB,setC,setCTX,setD,setE,setGAS,setHASHPOS,setMAXMEM,setPC,setRCX,setRR,setSP,setSR,sRD,sWR,useCTX" -b
*/

pol operations =
2**0 * arithEq0 + 2**1 * arithEq1 + 2**2 * arithEq2 + 2**3 * assert
+ 2**4 * bin + 2**5 * hashK + 2**6 * hashKDigest + 2**7 * hashKLen
+ 2**8 * hashP + 2**9 * hashPDigest + 2**10 * hashPLen + 2**11 * ind
+ 2**12 * indRR + 2**13 * isMem + 2**14 * isStack + 2**15 * JMP
+ 2**16 * JMPC + 2**17 * JMPN + 2**18 * memAlignRD + 2**19 * memAlignWR
+ 2**20 * memAlignWR8 + 2**21 * mOp + 2**22 * mWR + 2**23 * setA
+ 2**24 * setB + 2**25 * setC + 2**26 * setCTX + 2**27 * setD
+ 2**28 * setE + 2**29 * setGAS + 2**30 * setHASHPOS + 2**31 * setMAXMEM
+ 2**32 * setPC + 2**33 * setRR + 2**34 * setSP + 2**35 * setSR
+ 2**36 * sRD + 2**37 * sWR + 2**38 * useCTX;

(1 - arithEq0) * arithEq0 = 0;
(1 - arithEq1) * arithEq1 = 0;
(1 - arithEq2) * arithEq2 = 0;
(1 - assert) * assert = 0;
(1 - bin) * bin = 0;
(1 - hashK) * hashK = 0;
(1 - hashKDigest) * hashKDigest = 0;
(1 - hashKLen) * hashKLen = 0;
(1 - hashP) * hashP = 0;
(1 - hashPDigest) * hashPDigest = 0;
(1 - hashPLen) * hashPLen = 0;
(1 - ind) * ind = 0;
(1 - indRR) * indRR = 0;
(1 - isMem) * isMem = 0;
(1 - isStack) * isStack = 0;
(1 - JMP) * JMP = 0;
(1 - JMPC) * JMPC = 0;
(1 - JMPN) * JMPN = 0;
(1 - memAlignRD) * memAlignRD = 0;
(1 - memAlignWR) * memAlignWR = 0;
(1 - memAlignWR8) * memAlignWR8 = 0;
(1 - mOp) * mOp = 0;
(1 - mWR) * mWR = 0;
(1 - setA) * setA = 0;
(1 - setB) * setB = 0;
(1 - setC) * setC = 0;
(1 - setCTX) * setCTX = 0;
(1 - setD) * setD = 0;
(1 - setE) * setE = 0;
(1 - setGAS) * setGAS = 0;
(1 - setHASHPOS) * setHASHPOS = 0;
(1 - setMAXMEM) * setMAXMEM = 0;
(1 - setPC) * setPC = 0;
(1 - setRR) * setRR = 0;
(1 - setSP) * setSP = 0;
(1 - setSR) * setSR = 0;
(1 - sRD) * sRD = 0;
(1 - sWR) * sWR = 0;
(1 - useCTX) * useCTX = 0;

2**0 * arithEq0 + 2**1 * arithEq1 + 2**2 * arithEq2 + 2**3 * assert
+ 2**4 * bin + 2**5 * hashK + 2**6 * hashKDigest + 2**7 * hashKLen
+ 2**8 * hashP + 2**9 * hashPDigest + 2**10 * hashPLen + 2**11 * ind
+ 2**12 * indRR + 2**13 * isMem + 2**14 * isStack + 2**15 * JMP
+ 2**16 * JMPC + 2**17 * JMPN + 2**18 * memAlignRD + 2**19 * memAlignWR
+ 2**20 * memAlignWR8 + 2**21 * mOp + 2**22 * mWR + 2**23 * repeat
+ 2**24 * setA + 2**25 * setB + 2**26 * setC + 2**27 * setCTX
+ 2**28 * setD + 2**29 * setE + 2**30 * setGAS + 2**31 * setHASHPOS
+ 2**32 * setMAXMEM + 2**33 * setPC + 2**34 * setRCX + 2**35 * setRR
+ 2**36 * setSP + 2**37 * setSR + 2**38 * sRD + 2**39 * sWR
+ 2**40 * useCTX;

(1 - arithEq0) * arithEq0 = 0;
(1 - arithEq1) * arithEq1 = 0;
(1 - arithEq2) * arithEq2 = 0;
(1 - assert) * assert = 0;
(1 - bin) * bin = 0;
(1 - hashK) * hashK = 0;
(1 - hashKDigest) * hashKDigest = 0;
(1 - hashKLen) * hashKLen = 0;
(1 - hashP) * hashP = 0;
(1 - hashPDigest) * hashPDigest = 0;
(1 - hashPLen) * hashPLen = 0;
(1 - ind) * ind = 0;
(1 - indRR) * indRR = 0;
(1 - isMem) * isMem = 0;
(1 - isStack) * isStack = 0;
(1 - JMP) * JMP = 0;
(1 - JMPC) * JMPC = 0;
(1 - JMPN) * JMPN = 0;
(1 - memAlignRD) * memAlignRD = 0;
(1 - memAlignWR) * memAlignWR = 0;
(1 - memAlignWR8) * memAlignWR8 = 0;
(1 - mOp) * mOp = 0;
(1 - mWR) * mWR = 0;
(1 - repeat) * repeat = 0;
(1 - setA) * setA = 0;
(1 - setB) * setB = 0;
(1 - setC) * setC = 0;
(1 - setCTX) * setCTX = 0;
(1 - setD) * setD = 0;
(1 - setE) * setE = 0;
(1 - setGAS) * setGAS = 0;
(1 - setHASHPOS) * setHASHPOS = 0;
(1 - setMAXMEM) * setMAXMEM = 0;
(1 - setPC) * setPC = 0;
(1 - setRCX) * setRCX = 0;
(1 - setRR) * setRR = 0;
(1 - setSP) * setSP = 0;
(1 - setSR) * setSR = 0;
(1 - sRD) * sRD = 0;
(1 - sWR) * sWR = 0;
(1 - useCTX) * useCTX = 0;

{
CONST0, CONST1, CONST2, CONST3, CONST4, CONST5, CONST6, CONST7,
inA, inB, inC, inROTL_C, inD, inE, inSR, inFREE,
inCTX, inSP, inPC, inGAS, inMAXMEM, inHASHPOS, inSTEP, inRR, inHASHPOS,
inCTX, inSP, inPC, inGAS, inMAXMEM, inHASHPOS, inSTEP, inRR, inHASHPOS, inRCX,
inCntArith, inCntBinary, inCntKeccakF, inCntMemAlign, inCntPaddingPG, inCntPoseidonG,
operations, offset, incStack, binOpcode, zkPC
} in {
Rom.CONST0, Rom.CONST1, Rom.CONST2, Rom.CONST3, Rom.CONST4, Rom.CONST5, Rom.CONST6, Rom.CONST7,
Rom.inA, Rom.inB, Rom.inC, Rom.inROTL_C, Rom.inD, Rom.inE, Rom.inSR, Rom.inFREE,
Rom.inCTX, Rom.inSP, Rom.inPC, Rom.inGAS, Rom.inMAXMEM, Rom.inHASHPOS, Rom.inSTEP, Rom.inRR, Rom.inHASHPOS,
Rom.inCTX, Rom.inSP, Rom.inPC, Rom.inGAS, Rom.inMAXMEM, Rom.inHASHPOS, Rom.inSTEP, Rom.inRR, Rom.inHASHPOS, Rom.inRCX,
Rom.inCntArith, Rom.inCntBinary, Rom.inCntKeccakF, Rom.inCntMemAlign, Rom.inCntPaddingPG, Rom.inCntPoseidonG,
Rom.operations, Rom.offset, Rom.incStack, Rom.binOpcode, Rom.line
};
Expand Down
15 changes: 8 additions & 7 deletions pil/rom.pil
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Rom(%N);
pol constant CONST7;
pol constant offset;

pol constant inA, inB, inC, inROTL_C, inD, inE, inSR, inFREE, inCTX, inSP, inPC, inGAS, inMAXMEM, inHASHPOS, inSTEP, inRR;
pol constant inA, inB, inC, inROTL_C, inD, inE, inSR, inFREE, inCTX, inSP, inPC, inGAS, inMAXMEM, inHASHPOS, inSTEP, inRR ,inRCX;
pol constant inCntArith, inCntBinary, inCntKeccakF, inCntMemAlign, inCntPaddingPG, inCntPoseidonG;
pol constant incStack;
pol constant binOpcode;
Expand All @@ -22,16 +22,17 @@ namespace Rom(%N);
/*

comment genereated with:
node tools/pil_pol_table/bits_compose.js "arithEq0,arithEq1,arithEq2,assert,bin,hashK,hashKDigest,hashKLen,hashP,hashPDigest,hashPLen,ind,indRR,isMem,isStack,JMP,JMPC,JMPN,memAlignRD,memAlignWR,memAlignWR8,mOp,mWR,setA,setB,setC,setCTX,setD,setE,setGAS,setHASHPOS,setMAXMEM,setPC,setRR,setSP,setSR,sRD,sWR,useCTX"
node tools/pil_pol_table/bits_compose.js "arithEq0,arithEq1,arithEq2,assert,bin,hashK,hashKDigest,hashKLen,hashP,hashPDigest,hashPLen,ind,indRR,isMem,isStack,JMP,JMPC,JMPN,memAlignRD,memAlignWR,memAlignWR8,mOp,mWR,repeat,setA,setB,setC,setCTX,setD,setE,setGAS,setHASHPOS,setMAXMEM,setPC,setRCX,setRR,setSP,setSR,sRD,sWR,useCTX"

operations = 2**0 * arithEq0 + 2**1 * arithEq1 + 2**2 * arithEq2 + 2**3 * assert
+ 2**4 * bin + 2**5 * hashK + 2**6 * hashKDigest + 2**7 * hashKLen
+ 2**8 * hashP + 2**9 * hashPDigest + 2**10 * hashPLen + 2**11 * ind
+ 2**12 * indRR + 2**13 * isMem + 2**14 * isStack + 2**15 * JMP
+ 2**16 * JMPC + 2**17 * JMPN + 2**18 * memAlignRD + 2**19 * memAlignWR
+ 2**20 * memAlignWR8 + 2**21 * mOp + 2**22 * mWR + 2**23 * setA
+ 2**24 * setB + 2**25 * setC + 2**26 * setCTX + 2**27 * setD
+ 2**28 * setE + 2**29 * setGAS + 2**30 * setHASHPOS + 2**31 * setMAXMEM
+ 2**32 * setPC + 2**33 * setRR + 2**34 * setSP + 2**35 * setSR
+ 2**36 * sRD + 2**37 * sWR + 2**38 * useCTX
+ 2**20 * memAlignWR8 + 2**21 * mOp + 2**22 * mWR + 2**23 * repeat
+ 2**24 * setA + 2**25 * setB + 2**26 * setC + 2**27 * setCTX
+ 2**28 * setD + 2**29 * setE + 2**30 * setGAS + 2**31 * setHASHPOS
+ 2**32 * setMAXMEM + 2**33 * setPC + 2**34 * setRCX + 2**35 * setRR
+ 2**36 * setSP + 2**37 * setSR + 2**38 * sRD + 2**39 * sWR
+ 2**40 * useCTX
*/
39 changes: 38 additions & 1 deletion src/sm/sm_main/sm_main_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ module.exports = async function execute(pols, input, rom, config = {}) {
let fastDebugExit = false;

let pendingCmds = false;
let previousRCX = 0n;
let previousRCVInv = 0n;

for (let step = 0; step < stepsN; step++) {
const i = step % N;
ctx.ln = Fr.toObject(pols.zkPC[i]);
Expand All @@ -124,6 +127,7 @@ module.exports = async function execute(pols, input, rom, config = {}) {
ctx.cntMemAlign = pols.cntMemAlign[i];
ctx.cntPoseidonG = pols.cntPoseidonG[i];
ctx.cntPaddingPG = pols.cntPaddingPG[i];
ctx.RCX = pols.RCX[i];

// evaluate commands "after" before start new line, but when new values of registers are ready.
if (pendingCmds) {
Expand Down Expand Up @@ -380,6 +384,14 @@ module.exports = async function execute(pols, input, rom, config = {}) {
pols.inROTL_C[i] = Fr.zero;
}

if (l.inRCX) {
op0 = Fr.add(op0, Fr.mul( Fr.e(l.inRCX), Fr.e(ctx.RCX)));
pols.inRCX[i] = Fr.e(l.inRCX);
} else {
pols.inRCX[i] = Fr.zero;
}


if ((!isNaN(l.CONSTL))&&(l.CONSTL)) {
[
pols.CONST0[i],
Expand Down Expand Up @@ -1336,6 +1348,12 @@ module.exports = async function execute(pols, input, rom, config = {}) {
pols.memAlignWR8[i] = 0n;
}

if (l.repeat) {
pols.repeat[i] = 1n;
} else {
pols.repeat[i] = 0n;
}

//////////
// SET NEXT REGISTERS
//////////
Expand Down Expand Up @@ -1605,6 +1623,23 @@ module.exports = async function execute(pols, input, rom, config = {}) {
pols.cntMemAlign[nexti] = pols.cntMemAlign[i];
}

if (l.setRCX == 1) {
pols.setRCX[i] = 1n;
pols.RCX[nexti] = BigInt(fe2n(Fr, op0, ctx));
} else {
pols.setRCX[i] = 0n;
pols.RCX[nexti] = Fr.add(pols.RCX[i], ((!Fr.isZero(pols.RCX[i]) && l.repeat == 1) ? Fr.negone:Fr.zero));
}

if (Fr.isZero(pols.RCX[nexti])) {
pols.RCXInv[nexti] = 0n;
} else {
if (!Fr.eq(previousRCX,pols.RCXInv[nexti])) {
previousRCX = pols.RCX[nexti];
previousRCVInv = Fr.inv(previousRCX);
}
pols.RCXInv[nexti] = previousRCVInv;
}

if (l.JMPN) {
const o = Fr.toObject(op0);
Expand Down Expand Up @@ -1651,7 +1686,7 @@ module.exports = async function execute(pols, input, rom, config = {}) {
pols.JMPC[i] = 0n;
} else {
pols.isNeg[i]=0n;
pols.zkPC[nexti] = pols.zkPC[i] + 1n;
pols.zkPC[nexti] = pols.zkPC[i] + ((l.repeat && !Fr.isZero(ctx.RCX)) ? 0n:1n);
pols.JMP[i] = 0n;
pols.JMPN[i] = 0n;
pols.JMPC[i] = 0n;
Expand Down Expand Up @@ -2020,6 +2055,8 @@ function initState(Fr, pols, ctx) {
pols.cntMemAlign[0] = 0n;
pols.cntPaddingPG[0] = 0n;
pols.cntPoseidonG[0] = 0n;
pols.RCX[0] = 0n;
pols.RCXInv[0] = 0n;
}

function evalCommands(ctx, cmds) {
Expand Down
Loading