Files
powdr/test_data/polygon-hermez/binary.pil
2023-04-21 17:25:16 +02:00

127 lines
5.7 KiB
Plaintext

/*
* LICENSE WARNING
*
* These files are from the [polygon-hermez zkEVM project](https://github.com/0xPolygonHermez/zkevm-proverjs)
* and were developed by Polygon. They are not covered by the MIT license of this repository.
* All rights reserved by Polygon.
*/
/*
PLOOKUP OPERATIONS
==================
PIL State Machine that compute 256 bits operations:
- [X] ADD (OPCODE = 0)
- [X] SUB (OPCODE = 1)
- [X] LT (OPCODE = 2)
- [X] SLT (OPCODE = 3)
- [X] EQ (OPCODE = 4)
- [X] AND (OPCODE = 5)
- [X] OR (OPCODE = 6)
- [X] XOR (OPCODE = 7)
*/
include "global.pil";
namespace Binary(%N);
// ##############################################################
// CONSTANT POLINOMIALS
// ##############################################################
// Plockup polinomials
// ==============================================================
// ==== IN ====
// P_OPCODE (3 bits) Operation code
// P_CIN (1 bits) Carry in
// P_LAST (1 bits) Last byte
// Global.BYTE_2A (8 bits) Input A
// Global.BYTE (8 bits) Input B
// ==== OUT ======
// P_C (8 bits) Output C
// P_COUT (1 bits) Carry out
// P_USE_CARRY (1 bits) Carry out
// ==== TOTAL ====
// 3 + 1 + 1 + 8 + 8 = 21 bits
// ==============================================================
// NAME | 0 | 1 | 2 | 3 | ... | 32 |
// ==============================================================
// FACTOR0 | 0x1 | 0x10000 | 0x0 | 0x0 | ... | 0x0 | 0x0
// FACTOR1 | 0x0 | 0x0 | 0x1 | 0x10000 | ... | 0x0 | 0x0
// ...
// FACTOR7 | 0x0 | 0x0 | 0x0 | 0x0 | ... | 0x1 | 0x10000
pol constant P_OPCODE, P_CIN, P_LAST, P_USE_CARRY;
pol constant P_C, P_COUT;
pol RESET = Global.CLK32[0] + Global.CLK32[16];
pol constant FACTOR[8];
// ############################################################
// COMMIT POLINOMIALS
// ############################################################
// opcode = (2 bits) Operation code
// ============================================================
// a0-a7, a0-a7, a0-a7
// 256 bits operations -> 32 Bytes / 4 Bytes (per registry) ->
// 8 Registries
// ============================================================
// freeInA, freeInB, freeInC -> 1 Byte input
// ============================================================
// cIn -> Carry In ; cOut -> Carry Out ; lCIn -> Latch Carry in
// ============================================================
pol commit freeInA[2], freeInB[2], freeInC[2];
pol commit a[8], b[8], c[8];
pol commit opcode;
pol commit cIn, cMiddle, cOut;
pol commit lCout,lOpcode;
pol commit useCarry;
pol commit resultBinOp;
pol commit resultValidRange;
(1 - RESET) * resultBinOp = 0;
(1 - RESET) * resultValidRange = 0;
resultBinOp * (1-resultBinOp) = 0;
resultValidRange * (1-resultValidRange) = 0;
resultValidRange * resultBinOp = 0;
(opcode' - opcode) * ( 1 - RESET' ) = 0;
(cOut - cIn') * (1 - RESET') = 0;
cIn * RESET = 0;
lCout' = cOut;
lOpcode' = opcode;
{0, opcode, freeInA[0], freeInB[0], cIn, 0, freeInC[0], cMiddle} in {P_LAST, P_OPCODE, Global.BYTE_2A, Global.BYTE, P_CIN, P_USE_CARRY, P_C, P_COUT};
{resultValidRange' + resultBinOp', opcode, freeInA[1], freeInB[1], cMiddle, useCarry ,freeInC[1], cOut} in {P_LAST, P_OPCODE, Global.BYTE_2A, Global.BYTE, P_CIN, P_USE_CARRY, P_C, P_COUT};
a[0]' = a[0] * (1 - RESET) + freeInA[0] * FACTOR[0] + 256 * freeInA[1] * FACTOR[0];
a[1]' = a[1] * (1 - RESET) + freeInA[0] * FACTOR[1] + 256 * freeInA[1] * FACTOR[1];
a[2]' = a[2] * (1 - RESET) + freeInA[0] * FACTOR[2] + 256 * freeInA[1] * FACTOR[2];
a[3]' = a[3] * (1 - RESET) + freeInA[0] * FACTOR[3] + 256 * freeInA[1] * FACTOR[3];
a[4]' = a[4] * (1 - RESET) + freeInA[0] * FACTOR[4] + 256 * freeInA[1] * FACTOR[4];
a[5]' = a[5] * (1 - RESET) + freeInA[0] * FACTOR[5] + 256 * freeInA[1] * FACTOR[5];
a[6]' = a[6] * (1 - RESET) + freeInA[0] * FACTOR[6] + 256 * freeInA[1] * FACTOR[6];
a[7]' = a[7] * (1 - RESET) + freeInA[0] * FACTOR[7] + 256 * freeInA[1] * FACTOR[7];
b[0]' = b[0] * (1 - RESET) + freeInB[0] * FACTOR[0] + 256 * freeInB[1] * FACTOR[0];
b[1]' = b[1] * (1 - RESET) + freeInB[0] * FACTOR[1] + 256 * freeInB[1] * FACTOR[1];
b[2]' = b[2] * (1 - RESET) + freeInB[0] * FACTOR[2] + 256 * freeInB[1] * FACTOR[2];
b[3]' = b[3] * (1 - RESET) + freeInB[0] * FACTOR[3] + 256 * freeInB[1] * FACTOR[3];
b[4]' = b[4] * (1 - RESET) + freeInB[0] * FACTOR[4] + 256 * freeInB[1] * FACTOR[4];
b[5]' = b[5] * (1 - RESET) + freeInB[0] * FACTOR[5] + 256 * freeInB[1] * FACTOR[5];
b[6]' = b[6] * (1 - RESET) + freeInB[0] * FACTOR[6] + 256 * freeInB[1] * FACTOR[6];
b[7]' = b[7] * (1 - RESET) + freeInB[0] * FACTOR[7] + 256 * freeInB[1] * FACTOR[7];
pol c0Temp = c[0] * (1 - RESET) + freeInC[0] * FACTOR[0] + 256 * freeInC[1] * FACTOR[0];
c[0]' = useCarry * (cOut - c0Temp ) + c0Temp;
c[1]' = c[1] * (1 - RESET) + freeInC[0] * FACTOR[1] + 256 * freeInC[1] * FACTOR[1];
c[2]' = c[2] * (1 - RESET) + freeInC[0] * FACTOR[2] + 256 * freeInC[1] * FACTOR[2];
c[3]' = c[3] * (1 - RESET) + freeInC[0] * FACTOR[3] + 256 * freeInC[1] * FACTOR[3];
c[4]' = c[4] * (1 - RESET) + freeInC[0] * FACTOR[4] + 256 * freeInC[1] * FACTOR[4];
c[5]' = c[5] * (1 - RESET) + freeInC[0] * FACTOR[5] + 256 * freeInC[1] * FACTOR[5];
c[6]' = c[6] * (1 - RESET) + freeInC[0] * FACTOR[6] + 256 * freeInC[1] * FACTOR[6];
pol c7Temp = c[7] * (1 - RESET) + freeInC[0] * FACTOR[7] + 256 * freeInC[1] * FACTOR[7];
c[7]' = (1 - useCarry) * c7Temp;