Polygon zkEVM中的常量多项式
Posted mutourend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Polygon zkEVM中的常量多项式相关的知识,希望对你有一定的参考价值。
1. 引言
具体见:
2. Global.pil中的常量多项式
Polygon zkEVM全局多项式Global.pil中包含3个constant多项式:
- 1)L1 constant多项式
- 2)BYTE constant多项式
- 3)BYTE2 constant多项式
namespace Global(%N);
pol constant L1; // 1, 0, 0, 0, 0,
pol constant BYTE;
pol constant BYTE2;
这些全局constant多项式的基本赋值情况为:
module.exports.buildConstants = async function (pols)
const F = new F1Field("0xFFFFFFFF00000001");
const N = pols.BYTE.length;
buidBYTE(pols.BYTE, F, N);
buidBYTE2(pols.BYTE2, F, N);
buildL1(pols.L1, F, N);
;
function buidBYTE2(pol, F, N)
const m = 1<<16;
if (N<m) throw new Error("GLOBAL.BYTE does not fit");
for (let i=0; i<m; i++)
pol[i] = BigInt(i);
for (let i=m; i<N; i++)
pol[i] = 0n;
function buidBYTE(pol, F, N)
if (N<256) throw new Error("GLOBAL.BYTE does not fit");
for (let i=0; i<256; i++)
pol[i] = BigInt(i);
for (let i=256; i<N; i++)
pol[i] = 0n;
function buildL1(pol, F, N)
pol[0] = 1n;
for ( let i=1; i<N; i++) pol[i] = 0n;
以 N = 2 31 N=2^31 N=231为例,这些全局常量多项式的具体赋值为:
index | L1 | BYTE | BYTE2 |
---|---|---|---|
0 | 1 | 0 | 0 |
1 | 0 | 1 | 1 |
2 | 0 | 2 | 2 |
3 | 0 | 3 | 3 |
⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ |
254 | 0 | 254 | 254 |
255 | 0 | 255 | 255 |
256 | 0 | 0 | 256 |
257 | 0 | 0 | 257 |
⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ |
2 16 − 1 2^16-1 216−1 | 0 | 0 | 2 16 − 1 2^16-1 216−1 |
2 16 2^16 216 | 0 | 0 | 0 |
2 16 + 1 2^16+1 216+1 | 0 | 0 | 0 |
⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ |
2 21 − 1 2^21-1 221−1 | 0 | 0 | 0 |
3. main.pil中的常量多项式
main.pil中包含一个常量多项式STEP:【注意,在zkasm.js中,将STEP定义为只读寄存器。】
/// Constant Polynomials
pol constant STEP; // 0, 1, 2, 3, .......
相应的赋值为:
module.exports = async function (pols)
const N = pols.STEP.length;
for ( let i=0; i<N; i++)
pols.STEP[i] = BigInt(i);
以 N = 2 31 N=2^31 N=231为例,该常量多项式的具体赋值为:
index | STEP |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
⋮ \\vdots ⋮ | ⋮ \\vdots ⋮ |
2 21 − 1 2^21-1 221−1 | 2 21 − 1 2^21-1 221−1 |
4. rom.pil中的常量多项式
rom.pil中包含的常量多项式有:
namespace Rom(%N);
pol constant CONST0;
pol constant CONST1;
pol constant CONST2;
pol constant CONST3;
pol constant CONST4;
pol constant CONST5;
pol constant CONST6;
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 setA, setB, setC, setD, setE, setSR, setCTX, setSP, setPC, setGAS, setMAXMEM, setHASHPOS, JMP, JMPN, JMPC, setRR;
pol constant incStack, incCode;
pol constant isStack;
pol constant isCode;
pol constant isMem;
pol constant ind, indRR;
pol constant useCTX;
pol constant mOp, mWR;
pol constant sWR, sRD;
pol constant arith;
pol constant arithEq0;
pol constant arithEq1;
pol constant arithEq2;
pol constant arithEq3;
pol constant memAlign, memAlignWR, memAlignWR8;
pol constant hashK, hashKLen, hashKDigest;
pol constant hashP, hashPLen, hashPDigest;
pol constant bin;
pol constant binOpcode;
pol constant assert;
pol constant line;
不过,rom.pil中的常量多项式的值不是固定的,而是根据zkasm编译出的json文件类设定:
module.exports.buildConstants = async function buildConstants(pols, rom)
const F = new F1Field("0xFFFFFFFF00000001");
const N = pols.inA.length;
const twoTo31 = Scalar.e(0x80000000);
const maxInt = 2147483647;
const minInt = -2147483648;
const maxUInt = 0xFFFFFFFF;
const minUInt = 0;
if (rom.program.length>N) throw new Error("Rom is too big for this N");
for (let i=0; i<rom.program.length; i++)
if (rom.program[i].CONST)
if (rom.program[i].CONSTL) throw new Error("Program mixed with long and short constants");
pols.CONST0[i] = rom.program[i].CONST ? F.e(rom.program[i].CONST) : F.zero;
pols.CONST1[i] = F.zero;
pols.CONST2[i] = F.zero;
pols.CONST3[i] = F.zero;
pols.CONST4[i] = F.zero;
pols.CONST5[i] = F.zero;
pols.CONST6[i] = F.zero;
pols.CONST7[i] = F.zero;
else if (rom.program[i].CONSTL)
[
pols.CONST0[i],
pols.CONST1[i],
pols.CONST2[i],
pols.CONST3[i],
pols.CONST4[i],
pols.CONST5[i],
pols.CONST6[i],
pols.CONST7[i],
] = scalar2fea(F, BigInt(rom.program[i].CONSTL));
else
pols.CONST0[i] = F.zero;
pols.CONST1[i] = F.zero;
pols.CONST2[i] = F.zero;
pols.CONST3[i] = F.zero;
pols.CONST4[i] = F.zero;
pols.CONST5[i] = F.zero;
pols.CONST6[i] = F.zero;
pols.CONST7[i] = F.zero;
pols.offset[i] = rom.program[i].offset ? BigInt(rom.program[i].offset) : 0n;
pols.inA[i] = rom.program[i].inA ? F.e(rom.program[i].inA) : F.zero;
pols.inB[i] = rom.program[i].inB ? F.e(rom.program[i].inB) : F.zero;
pols.inC[i] = rom.program[i].inC ? F.e(rom.program[i].inC) : F.zero;
pols.inD[i] = rom.program[i].inD ? F.e(rom.program[i].inD) : F.zero;
pols.inE[i] = rom.program[i].inE ? F.e(rom.program[i].inE) : F.zero;
pols.inSR[i] = rom.program[i].inSR ? F.e(rom.program[i].inSR) : F.zero;
pols.inCTX[i] = rom.program[i].inCTX ? F.e(rom.program[i].inCTX) : F.zero;
pols.inSP[i] = rom.program[i].inSP ? F.e(rom.program[i].inSP) : F.zero;
pols.inPC[i] = rom.program[i].inPC ? F.e(rom.program[i].inPC) : F.zero;
pols.inMAXMEM[i] = rom.program[i].inMAXMEM ? F.e(rom.program[i].inMAXMEM) : F.zero;
pols.inSTEP[i] = rom.program[i].inSTEP ? F.e(rom.program[i].inSTEP) : F.zero;
pols.inFREE[i] = rom.program[i].inFREE ? F.e(rom.program[i].inFREE) : F.zero;
pols.inGAS[i] = rom.program[i].inGAS ? F.e(rom.program[i].inGAS) : F.zero;
pols.inRR[i] = rom.program[i].inRR ? F.e(rom.program[i].inRR) : F.zero;
pols.inHASHPOS[i] = rom.program[i].inHASHPOS ? F.e(rom.program[i].inHASHPOS) : F.zero;
pols.inROTL_C[i] = rom.program[i].inROTL_C ? F.e(rom.program[i].inROTL_C) : F.zero;
pols.setA[i] = rom.program[i].setA ? 1n : 0n;
pols.setB[i] = rom.program[i].setB ? 1n : 0n;
pols.setC[i] = rom.program[i].setC ? 1n : 0n;
pols.setD[i] = rom.program[i].setD ? 1n : 0n;
pols.setE[i] = rom.program[i].setE ? 1n : 0n;
pols.setSR[i] = rom.programPolygon zkEVM zkASM语法