Skip to content

Commit

Permalink
Check that rom counters limits match the secondary state machine max …
Browse files Browse the repository at this point in the history
…inputs
  • Loading branch information
fractasy committed Dec 20, 2024
1 parent c0fb2cc commit 65e650d
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 24 deletions.
23 changes: 22 additions & 1 deletion src/executor/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,28 @@ class Executor
poseidonGExecutor(fr, poseidon),
memAlignExecutor(fr, config),
climbKeyExecutor(fr, config)
{};
{
if (PROVER_FORK_ID == 10)
{
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_ARITH_LIMIT == arithExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_BINARY_LIMIT == binaryExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_KECCAK_F_LIMIT == paddingKKExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_MEM_ALIGN_LIMIT == memAlignExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_PADDING_PG_LIMIT == paddingPGExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_POSEIDON_G_LIMIT == poseidonGExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_10.constants.MAX_CNT_SHA256_F_LIMIT == paddingSha256Executor.maxInputs);
}
else
{
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_ARITH_LIMIT == arithExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_BINARY_LIMIT == binaryExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_KECCAK_F_LIMIT == paddingKKExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_MEM_ALIGN_LIMIT == memAlignExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_PADDING_PG_LIMIT == paddingPGExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_POSEIDON_G_LIMIT == poseidonGExecutor.maxInputs);
zkassertpermanent(mainExecutor_fork_10.romBatch_11.constants.MAX_CNT_SHA256_F_LIMIT == paddingSha256Executor.maxInputs);
}
};

// Full version: all polynomials are evaluated, in all evaluations
void executeBatch (ProverRequest &proverRequest, PROVER_FORK_NAMESPACE::CommitPols & commitPols);
Expand Down
4 changes: 2 additions & 2 deletions src/sm/arith/arith_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ void ArithExecutor::execute (vector<ArithAction> &action, ArithCommitPols &pols)
pBN254++;

// Check that we have enough room in polynomials TODO: Do this check in JS
if (action.size()*32 > N)
if (action.size() > maxInputs)
{
zklog.error("ArithExecutor::execute() Too many Arith entries=" + to_string(action.size()) + " > N/32=" + to_string(N/32));
zklog.error("ArithExecutor::execute() Too many Arith entries=" + to_string(action.size()) + " > maxInputs=N/32=" + to_string(maxInputs));
exitProcess();
}

Expand Down
5 changes: 4 additions & 1 deletion src/sm/arith/arith_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ class ArithExecutor
const Config &config;
const uint64_t N;
mpz_class pFec;
public:
const uint64_t maxInputs;

public:
ArithExecutor (Goldilocks &fr, const Config &config) :
fr(fr),
config(config),
N(getForkN(PROVER_FORK_ID))
N(getForkN(PROVER_FORK_ID)),
maxInputs(N/32)
{
// Calculate the prime number
fec2scalar(fec, fec.negOne(), pFec);
Expand Down
7 changes: 4 additions & 3 deletions src/sm/binary/binary_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ using json = nlohmann::json;
BinaryExecutor::BinaryExecutor (Goldilocks &fr, const Config &config) :
fr(fr),
config(config),
N(getForkN(PROVER_FORK_ID))
N(getForkN(PROVER_FORK_ID)),
maxInputs(N/LATCH_SIZE)
{
TimerStart(BINARY_EXECUTOR);

Expand Down Expand Up @@ -83,9 +84,9 @@ void BinaryExecutor::buildReset (void)
void BinaryExecutor::execute (vector<BinaryAction> &action, BinaryCommitPols &pols)
{
// Check that we have enough room in polynomials TODO: Do this check in JS
if (action.size()*LATCH_SIZE > N)
if (action.size() > maxInputs)
{
zklog.error("BinaryExecutor::execute() Too many Binary entries=" + to_string(action.size()) + " > N/LATCH_SIZE=" + to_string(N/LATCH_SIZE));
zklog.error("BinaryExecutor::execute() Too many Binary entries=" + to_string(action.size()) + " > maxInputs=N/LATCH_SIZE=" + to_string(maxInputs));
exitProcess();
}

Expand Down
2 changes: 2 additions & 0 deletions src/sm/binary/binary_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class BinaryExecutor
const uint64_t N;
vector<vector<uint64_t>> FACTOR;
vector<uint64_t> RESET;
public:
const uint64_t maxInputs;

public:
BinaryExecutor (Goldilocks &fr, const Config &config);
Expand Down
4 changes: 2 additions & 2 deletions src/sm/mem_align/mem_align_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ uint8_t getByte (mpz_class value, uint8_t index) {
void MemAlignExecutor::execute (vector<MemAlignAction> &input, MemAlignCommitPols &pols)
{
// Check input size
if (input.size()*32 > N)
if (input.size() > maxInputs)
{
zklog.error("MemAlignExecutor::execute() Too many entries input.size()=" + to_string(input.size()) + " > N/32=" + to_string(N/32));
zklog.error("MemAlignExecutor::execute() Too many entries input.size()=" + to_string(input.size()) + " > maxInputs=N/32=" + to_string(maxInputs));
exitProcess();
}

Expand Down
5 changes: 4 additions & 1 deletion src/sm/mem_align/mem_align_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ class MemAlignExecutor
Goldilocks &fr;
const Config &config;
const uint64_t N;
public:
const uint64_t maxInputs;

public:
MemAlignExecutor (Goldilocks &fr, const Config &config) :
fr(fr),
config(config),
N(getForkN(PROVER_FORK_ID)) {}
N(getForkN(PROVER_FORK_ID)),
maxInputs(N/32) {}
void execute (vector<MemAlignAction> &input, PROVER_FORK_NAMESPACE::MemAlignCommitPols &pols);
};

Expand Down
4 changes: 2 additions & 2 deletions src/sm/padding_kk/padding_kk_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void PaddingKKExecutor::execute (vector<PaddingKKExecutorInput> &input, PaddingK
uint64_t totalInputBytes = prepareInput(input);

// Check input size
if (totalInputBytes > (44*bytesPerBlock*(N/blockSize)))
if (totalInputBytes > (bytesPerBlock*maxInputs))
{
zklog.error("PaddingKKExecutor::execute() Too many entries input.size()=" + to_string(input.size()) + " totalInputBytes=" + to_string(totalInputBytes) + " > 44*bytesPerBlock*(N/blockSize)=" + to_string(44*bytesPerBlock*(N/blockSize)));
zklog.error("PaddingKKExecutor::execute() Too many entries input.size()=" + to_string(input.size()) + " totalInputBytes=" + to_string(totalInputBytes) + " > 44*bytesPerBlock*(N/blockSize)=" + to_string(bytesPerBlock*maxInputs));
exitProcess();
}

Expand Down
7 changes: 5 additions & 2 deletions src/sm/padding_kk/padding_kk_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class PaddingKKExecutor
const uint64_t blockSize;
const uint64_t bytesPerBlock;
const uint64_t N;

public:
const uint64_t maxInputs;
private:
/* Hash of an empty/zero message */
mpz_class hashZeroScalar;
Goldilocks::Element hash0[8];
Expand All @@ -52,7 +54,8 @@ uint64_t prepareInput (vector<PaddingKKExecutorInput> &input);
fr(fr),
blockSize(155286),
bytesPerBlock(136),
N(getForkN(PROVER_FORK_ID))
N(getForkN(PROVER_FORK_ID)),
maxInputs(44*(N/blockSize))
{
keccak256(NULL, 0, hashZeroScalar);
scalar2fea(fr, hashZeroScalar, hash0);
Expand Down
7 changes: 5 additions & 2 deletions src/sm/padding_pg/padding_pg_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class PaddingPGExecutor
const uint64_t nElements;
const uint64_t bytesPerBlock;
const uint64_t N;

public:
const uint64_t maxInputs;
private:
uint64_t prepareInput (vector<PaddingPGExecutorInput> &input);

public:
Expand All @@ -46,7 +48,8 @@ uint64_t prepareInput (vector<PaddingPGExecutorInput> &input);
bytesPerElement(7),
nElements(8),
bytesPerBlock(bytesPerElement*nElements),
N(getForkN(PROVER_FORK_ID)) {};
N(getForkN(PROVER_FORK_ID)),
maxInputs(N/bytesPerBlock) {};
void execute (vector<PaddingPGExecutorInput> &input, PROVER_FORK_NAMESPACE::PaddingPGCommitPols &pols, vector<array<Goldilocks::Element, 17>> &required);
};

Expand Down
4 changes: 2 additions & 2 deletions src/sm/padding_sha256/padding_sha256_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ void PaddingSha256Executor::execute (vector<PaddingSha256ExecutorInput> &input,

// Check input size: totalInputBytes/bitsPerElement <= bytesPerBlock* maxBlocks; maxBlocks=N/blockSize
// this condition depends on all the SM used to evaluated the Sha256 hash
if (totalInputBytes * blockSize > bitsPerElement*bytesPerBlock*N)
if (totalInputBytes > maxInputs*bytesPerBlock)
{
zklog.error("PaddingKKExecutor::execute() Too many entries input.size()=" + to_string(input.size()) + " totalInputBytes=" + to_string(totalInputBytes) + " > bitsPerElement*bytesPerBlock*(N/blockSize)=" + to_string(bitsPerElement*bytesPerBlock*(N/blockSize)));
zklog.error("PaddingKKExecutor::execute() Too many entries input.size()=" + to_string(input.size()) + " totalInputBytes=" + to_string(totalInputBytes) + " > bitsPerElement*bytesPerBlock*(N/blockSize)=" + to_string(maxInputs*bytesPerBlock));
exitProcess();
}

Expand Down
7 changes: 5 additions & 2 deletions src/sm/padding_sha256/padding_sha256_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class PaddingSha256Executor
const uint64_t bytesPerBlock;
const uint64_t bitsPerElement;
const uint64_t N;

public:
const uint64_t maxInputs;
private:
/* Hash of an empty/zero message */
mpz_class hashZeroScalar;
Goldilocks::Element hash0[8];
Expand All @@ -55,7 +57,8 @@ uint64_t prepareInput (vector<PaddingSha256ExecutorInput> &input);
blockSize(31488),
bytesPerBlock(64),
bitsPerElement(7),
N(getForkN(PROVER_FORK_ID))
N(getForkN(PROVER_FORK_ID)),
maxInputs(bitsPerElement*(N/blockSize))
{
SHA256(NULL, 0, hashZeroScalar);
scalar2fea(fr, hashZeroScalar, hash0);
Expand Down
4 changes: 2 additions & 2 deletions src/sm/poseidon_g/poseidon_g_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ void PoseidonGExecutor::execute ( vector<array<Goldilocks::Element, 17>> &inpu
uint64_t size = sizeMain + sizePadding + sizeStorage;

// Check input size
if (size > maxHashes)
if (size > maxInputs)
{
zklog.error("PoseidonGExecutor::execute() Not enough Poseidon slots inputs size=" + to_string(size) + " > maxHashes=" + to_string(maxHashes));
zklog.error("PoseidonGExecutor::execute() Not enough Poseidon slots inputs size=" + to_string(size) + " > maxInputs=" + to_string(maxInputs));
exitProcess();
}

Expand Down
6 changes: 4 additions & 2 deletions src/sm/poseidon_g/poseidon_g_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class PoseidonGExecutor
const uint64_t t;
const uint64_t nRoundsF;
const uint64_t nRoundsP;
const uint64_t maxHashes;
public:
const uint64_t maxInputs;
private:
const array<Goldilocks::Element,12> MCIRC;
const array<Goldilocks::Element,12> MDIAG;
array<array<Goldilocks::Element,12>,12> M;
Expand All @@ -34,7 +36,7 @@ class PoseidonGExecutor
t(12),
nRoundsF(8),
nRoundsP(22),
maxHashes(N / (nRoundsF + nRoundsP + 1)),
maxInputs(N / (nRoundsF + nRoundsP + 1)),
MCIRC({17, 15, 41, 16, 2, 28, 13, 13, 39, 18, 34, 20}),
MDIAG({8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
{
Expand Down

0 comments on commit 65e650d

Please sign in to comment.