Skip to content

Commit

Permalink
Implement FullTracer::fillInReadWriteAddresses in all forks. Change v…
Browse files Browse the repository at this point in the history
…ersion to v6.0.2-RC1.
  • Loading branch information
fractasy committed Apr 17, 2024
1 parent b358641 commit 6b10393
Show file tree
Hide file tree
Showing 48 changed files with 655 additions and 143 deletions.
2 changes: 1 addition & 1 deletion src/config/version.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef ZKEVM_PROVER_VERSION_HPP
#define ZKEVM_PROVER_VERSION_HPP

#define ZKEVM_PROVER_VERSION "v6.0.1"
#define ZKEVM_PROVER_VERSION "v6.0.2-RC1"

#endif
8 changes: 4 additions & 4 deletions src/main_generator/main_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ code += " #endif\n";

if (bFastMode)
{
code += " zkResult = eval_addReadWriteAddress(ctx, smtGetResult.value);\n";
code += " zkResult = eval_addReadWriteAddress(ctx, smtGetResult.value, key);\n";
code += " if (zkResult != ZKR_SUCCESS)\n";
code += " {\n";
code += " proverRequest.result = zkResult;\n";
Expand Down Expand Up @@ -1646,7 +1646,7 @@ code += " #endif\n";

if (bFastMode)
{
code += " zkResult = eval_addReadWriteAddress(ctx, value);\n";
code += " zkResult = eval_addReadWriteAddress(ctx, value, ctx.lastSWrite.key);\n";
code += " if (zkResult != ZKR_SUCCESS)\n";
code += " {\n";
code += " proverRequest.result = zkResult;\n";
Expand Down Expand Up @@ -2827,7 +2827,7 @@ code += " #endif\n";

if (bFastMode)
{
code += " zkResult = eval_addReadWriteAddress(ctx, value);\n";
code += " zkResult = eval_addReadWriteAddress(ctx, value, key);\n";
code += " if (zkResult != ZKR_SUCCESS)\n";
code += " {\n";
code += " proverRequest.result = zkResult;\n";
Expand Down Expand Up @@ -3015,7 +3015,7 @@ code += " #endif\n";

if (bFastMode)
{
code += " zkResult = eval_addReadWriteAddress(ctx, scalarD);\n";
code += " zkResult = eval_addReadWriteAddress(ctx, scalarD, ctx.lastSWrite.key);\n";
code += " if (zkResult != ZKR_SUCCESS)\n";
code += " {\n";
code += " proverRequest.result = zkResult;\n";
Expand Down
5 changes: 3 additions & 2 deletions src/main_sm/fork_1/main/eval_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2063,13 +2063,14 @@ void eval_AddPointEc (Context &ctx, const RomCommand &cmd, bool dbl, RawFec::Ele
ctx.fec.sub(y3, aux1, y1);
}

void eval_addReadWriteAddress (Context &ctx, const mpz_class value)
void eval_addReadWriteAddress (Context &ctx, const mpz_class value, const Goldilocks::Element (&key)[4])
{
zkassert(ctx.proverRequest.input.publicInputsExtended.publicInputs.forkID == 1);
((fork_1::FullTracer *)ctx.proverRequest.pFullTracer)->addReadWriteAddress(
ctx.pols.A0[0], ctx.pols.A1[0], ctx.pols.A2[0], ctx.pols.A3[0], ctx.pols.A4[0], ctx.pols.A5[0], ctx.pols.A6[0], ctx.pols.A7[0],
ctx.pols.B0[0], ctx.pols.B1[0], ctx.pols.B2[0], ctx.pols.B3[0], ctx.pols.B4[0], ctx.pols.B5[0], ctx.pols.B6[0], ctx.pols.B7[0],
value);
value,
key);
}

} // namespace
2 changes: 1 addition & 1 deletion src/main_sm/fork_1/main/eval_command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void eval_storeLog (Context &ctx, const RomCommand &cmd, CommandResul
void eval_memAlignWR_W0 (Context &ctx, const RomCommand &cmd, CommandResult &cr);
void eval_memAlignWR_W1 (Context &ctx, const RomCommand &cmd, CommandResult &cr);
void eval_memAlignWR8_W0 (Context &ctx, const RomCommand &cmd, CommandResult &cr);
void eval_addReadWriteAddress (Context &ctx, const mpz_class value);
void eval_addReadWriteAddress (Context &ctx, const mpz_class value, const Goldilocks::Element (&key)[4]);

} // namespace

Expand Down
54 changes: 53 additions & 1 deletion src/main_sm/fork_1/main/full_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,8 @@ void FullTracer::onOpcode(Context &ctx, const RomCommand &cmd)

void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, const Goldilocks::Element &address1, const Goldilocks::Element &address2, const Goldilocks::Element &address3, const Goldilocks::Element &address4, const Goldilocks::Element &address5, const Goldilocks::Element &address6, const Goldilocks::Element &address7,
const Goldilocks::Element &keyType0, const Goldilocks::Element &keyType1, const Goldilocks::Element &keyType2, const Goldilocks::Element &keyType3, const Goldilocks::Element &keyType4, const Goldilocks::Element &keyType5, const Goldilocks::Element &keyType6, const Goldilocks::Element &keyType7,
const mpz_class &value )
const mpz_class &value,
const Goldilocks::Element (&key)[4] )
{
#ifdef LOG_TIME_STATISTICS
gettimeofday(&t, NULL);
Expand All @@ -1349,6 +1350,10 @@ void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, cons
{
InfoReadWrite infoReadWrite;
infoReadWrite.balance = value.get_str();
infoReadWrite.balanceKey[0]= key[0];
infoReadWrite.balanceKey[1]= key[1];
infoReadWrite.balanceKey[2]= key[2];
infoReadWrite.balanceKey[3]= key[3];
read_write_addresses[addressHex] = infoReadWrite;
}
else
Expand All @@ -1363,6 +1368,10 @@ void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, cons
{
InfoReadWrite infoReadWrite;
infoReadWrite.nonce = value.get_str();
infoReadWrite.nonceKey[0]= key[0];
infoReadWrite.nonceKey[1]= key[1];
infoReadWrite.nonceKey[2]= key[2];
infoReadWrite.nonceKey[3]= key[3];
read_write_addresses[addressHex] = infoReadWrite;
}
else
Expand All @@ -1376,4 +1385,47 @@ void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, cons
#endif
}

zkresult FullTracer::fillInReadWriteAddresses (Context &ctx)
{
zkresult zkr;

// For all entries in read_write_addresses
unordered_map<string, InfoReadWrite>::iterator it;
for (it = read_write_addresses.begin(); it != read_write_addresses.end(); it++)
{
Goldilocks::Element newStateRoot[4];
string2fea(fr, finalTrace.new_state_root, newStateRoot);

// Re-read balance for this state root
if (!it->second.balance.empty())
{
zkassert(!fr.isZero(it->second.balanceKey[0]) || !fr.isZero(it->second.balanceKey[1]) || !fr.isZero(it->second.balanceKey[2]) || !fr.isZero(it->second.balanceKey[3]));
mpz_class balance;
zkr = ctx.pHashDB->get(ctx.proverRequest.uuid, newStateRoot, it->second.balanceKey, balance, NULL, NULL);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::fillInReadWriteAddresses() failed calling ctx.pHashDB->get(balance) result=" + zkresult2string(zkr));
return zkr;
}
it->second.balance = balance.get_str();
}

// Re-read nonce for this state root
if (!it->second.nonce.empty())
{
zkassert(!fr.isZero(it->second.nonceKey[0]) || !fr.isZero(it->second.nonceKey[1]) || !fr.isZero(it->second.nonceKey[2]) || !fr.isZero(it->second.nonceKey[3]));
mpz_class nonce;
zkr = ctx.pHashDB->get(ctx.proverRequest.uuid, newStateRoot, it->second.nonceKey, nonce, NULL, NULL);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::fillInReadWriteAddresses() failed calling ctx.pHashDB->get(nonce) result=" + zkresult2string(zkr));
return zkr;
}
it->second.nonce = nonce.get_str();
}
}

return ZKR_SUCCESS;
}

} // namespace
4 changes: 3 additions & 1 deletion src/main_sm/fork_1/main/full_tracer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class FullTracer: public FullTracerInterface
void onOpcode (Context &ctx, const RomCommand &cmd);
void addReadWriteAddress ( const Goldilocks::Element &address0, const Goldilocks::Element &address1, const Goldilocks::Element &address2, const Goldilocks::Element &faddress3, const Goldilocks::Element &address4, const Goldilocks::Element &address5, const Goldilocks::Element &address6, const Goldilocks::Element &address7,
const Goldilocks::Element &keyType0, const Goldilocks::Element &keyType1, const Goldilocks::Element &keyType2, const Goldilocks::Element &keyType3, const Goldilocks::Element &keyType4, const Goldilocks::Element &keyType5, const Goldilocks::Element &keyType6, const Goldilocks::Element &keyType7,
const mpz_class &value );
const mpz_class &value,
const Goldilocks::Element (&key)[4] );
zkresult fillInReadWriteAddresses (Context &ctx);

FullTracer(Goldilocks &fr) : fr(fr), depth(1), prevCTX(0), initGas(0), txCount(0), txTime(0), accBatchGas(0), numberOfOpcodesInThisTx(0), lastErrorOpcode(0) { };
~FullTracer()
Expand Down
14 changes: 7 additions & 7 deletions src/main_sm/fork_1/main/main_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,11 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
#ifdef LOG_TIME_STATISTICS_MAIN_EXECUTOR
mainMetrics.add("SMT Get", TimeDiff(t));
#endif
}

if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, smtGetResult.value);
if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, smtGetResult.value, key);
}
}

scalar2fea(fr, smtGetResult.value, fi0, fi1, fi2, fi3, fi4, fi5, fi6, fi7);
Expand Down Expand Up @@ -1258,7 +1258,7 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
}
if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, value);
eval_addReadWriteAddress(ctx, value, ctx.lastSWrite.key);
}

// If we just modified a balance
Expand Down Expand Up @@ -1965,7 +1965,7 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,

if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, smtGetResult.value);
eval_addReadWriteAddress(ctx, smtGetResult.value, key);
}

#ifdef LOG_TIME_STATISTICS_MAIN_EXECUTOR
Expand Down Expand Up @@ -2107,7 +2107,7 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,

if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, scalarD);
eval_addReadWriteAddress(ctx, scalarD, ctx.lastSWrite.key);
}

// If we just modified a balance
Expand Down
5 changes: 3 additions & 2 deletions src/main_sm/fork_2/main/eval_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2063,13 +2063,14 @@ void eval_AddPointEc (Context &ctx, const RomCommand &cmd, bool dbl, RawFec::Ele
ctx.fec.sub(y3, aux1, y1);
}

void eval_addReadWriteAddress (Context &ctx, const mpz_class value)
void eval_addReadWriteAddress (Context &ctx, const mpz_class value, const Goldilocks::Element (&key)[4])
{
zkassert(ctx.proverRequest.input.publicInputsExtended.publicInputs.forkID == 2); // fork_2
((fork_2::FullTracer *)ctx.proverRequest.pFullTracer)->addReadWriteAddress(
ctx.pols.A0[0], ctx.pols.A1[0], ctx.pols.A2[0], ctx.pols.A3[0], ctx.pols.A4[0], ctx.pols.A5[0], ctx.pols.A6[0], ctx.pols.A7[0],
ctx.pols.B0[0], ctx.pols.B1[0], ctx.pols.B2[0], ctx.pols.B3[0], ctx.pols.B4[0], ctx.pols.B5[0], ctx.pols.B6[0], ctx.pols.B7[0],
value);
value,
key);
}

} // namespace
2 changes: 1 addition & 1 deletion src/main_sm/fork_2/main/eval_command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void eval_storeLog (Context &ctx, const RomCommand &cmd, CommandResul
void eval_memAlignWR_W0 (Context &ctx, const RomCommand &cmd, CommandResult &cr);
void eval_memAlignWR_W1 (Context &ctx, const RomCommand &cmd, CommandResult &cr);
void eval_memAlignWR8_W0 (Context &ctx, const RomCommand &cmd, CommandResult &cr);
void eval_addReadWriteAddress (Context &ctx, const mpz_class value);
void eval_addReadWriteAddress (Context &ctx, const mpz_class value, const Goldilocks::Element (&key)[4]);

} // namespace

Expand Down
54 changes: 53 additions & 1 deletion src/main_sm/fork_2/main/full_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,8 @@ void FullTracer::onOpcode(Context &ctx, const RomCommand &cmd)

void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, const Goldilocks::Element &address1, const Goldilocks::Element &address2, const Goldilocks::Element &address3, const Goldilocks::Element &address4, const Goldilocks::Element &address5, const Goldilocks::Element &address6, const Goldilocks::Element &address7,
const Goldilocks::Element &keyType0, const Goldilocks::Element &keyType1, const Goldilocks::Element &keyType2, const Goldilocks::Element &keyType3, const Goldilocks::Element &keyType4, const Goldilocks::Element &keyType5, const Goldilocks::Element &keyType6, const Goldilocks::Element &keyType7,
const mpz_class &value )
const mpz_class &value,
const Goldilocks::Element (&key)[4] )
{
#ifdef LOG_TIME_STATISTICS
gettimeofday(&t, NULL);
Expand All @@ -1353,6 +1354,10 @@ void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, cons
{
InfoReadWrite infoReadWrite;
infoReadWrite.balance = value.get_str();
infoReadWrite.balanceKey[0]= key[0];
infoReadWrite.balanceKey[1]= key[1];
infoReadWrite.balanceKey[2]= key[2];
infoReadWrite.balanceKey[3]= key[3];
read_write_addresses[addressHex] = infoReadWrite;
}
else
Expand All @@ -1367,6 +1372,10 @@ void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, cons
{
InfoReadWrite infoReadWrite;
infoReadWrite.nonce = value.get_str();
infoReadWrite.nonceKey[0]= key[0];
infoReadWrite.nonceKey[1]= key[1];
infoReadWrite.nonceKey[2]= key[2];
infoReadWrite.nonceKey[3]= key[3];
read_write_addresses[addressHex] = infoReadWrite;
}
else
Expand All @@ -1380,4 +1389,47 @@ void FullTracer::addReadWriteAddress ( const Goldilocks::Element &address0, cons
#endif
}

zkresult FullTracer::fillInReadWriteAddresses (Context &ctx)
{
zkresult zkr;

// For all entries in read_write_addresses
unordered_map<string, InfoReadWrite>::iterator it;
for (it = read_write_addresses.begin(); it != read_write_addresses.end(); it++)
{
Goldilocks::Element newStateRoot[4];
string2fea(fr, finalTrace.new_state_root, newStateRoot);

// Re-read balance for this state root
if (!it->second.balance.empty())
{
zkassert(!fr.isZero(it->second.balanceKey[0]) || !fr.isZero(it->second.balanceKey[1]) || !fr.isZero(it->second.balanceKey[2]) || !fr.isZero(it->second.balanceKey[3]));
mpz_class balance;
zkr = ctx.pHashDB->get(ctx.proverRequest.uuid, newStateRoot, it->second.balanceKey, balance, NULL, NULL);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::fillInReadWriteAddresses() failed calling ctx.pHashDB->get(balance) result=" + zkresult2string(zkr));
return zkr;
}
it->second.balance = balance.get_str();
}

// Re-read nonce for this state root
if (!it->second.nonce.empty())
{
zkassert(!fr.isZero(it->second.nonceKey[0]) || !fr.isZero(it->second.nonceKey[1]) || !fr.isZero(it->second.nonceKey[2]) || !fr.isZero(it->second.nonceKey[3]));
mpz_class nonce;
zkr = ctx.pHashDB->get(ctx.proverRequest.uuid, newStateRoot, it->second.nonceKey, nonce, NULL, NULL);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::fillInReadWriteAddresses() failed calling ctx.pHashDB->get(nonce) result=" + zkresult2string(zkr));
return zkr;
}
it->second.nonce = nonce.get_str();
}
}

return ZKR_SUCCESS;
}

} // namespace
4 changes: 3 additions & 1 deletion src/main_sm/fork_2/main/full_tracer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class FullTracer: public FullTracerInterface
void onOpcode (Context &ctx, const RomCommand &cmd);
void addReadWriteAddress ( const Goldilocks::Element &address0, const Goldilocks::Element &address1, const Goldilocks::Element &address2, const Goldilocks::Element &faddress3, const Goldilocks::Element &address4, const Goldilocks::Element &address5, const Goldilocks::Element &address6, const Goldilocks::Element &address7,
const Goldilocks::Element &keyType0, const Goldilocks::Element &keyType1, const Goldilocks::Element &keyType2, const Goldilocks::Element &keyType3, const Goldilocks::Element &keyType4, const Goldilocks::Element &keyType5, const Goldilocks::Element &keyType6, const Goldilocks::Element &keyType7,
const mpz_class &value );
const mpz_class &value,
const Goldilocks::Element (&key)[4] );
zkresult fillInReadWriteAddresses (Context &ctx);

FullTracer(Goldilocks &fr) : fr(fr), depth(1), prevCTX(0), initGas(0), txCount(0), txTime(0), accBatchGas(0), numberOfOpcodesInThisTx(0), lastErrorOpcode(0) { };
~FullTracer()
Expand Down
14 changes: 7 additions & 7 deletions src/main_sm/fork_2/main/main_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,11 +1023,11 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
#ifdef LOG_TIME_STATISTICS_MAIN_EXECUTOR
mainMetrics.add("SMT Get", TimeDiff(t));
#endif
}

if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, smtGetResult.value);
if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, smtGetResult.value, key);
}
}

#ifdef LOG_TIME_STATISTICS_MAIN_EXECUTOR
Expand Down Expand Up @@ -1253,7 +1253,7 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
}
if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, value);
eval_addReadWriteAddress(ctx, value, ctx.lastSWrite.key);
}

// If we just modified a balance
Expand Down Expand Up @@ -1960,7 +1960,7 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,

if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, smtGetResult.value);
eval_addReadWriteAddress(ctx, smtGetResult.value, key);
}

#ifdef LOG_TIME_STATISTICS_MAIN_EXECUTOR
Expand Down Expand Up @@ -2102,7 +2102,7 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,

if (bProcessBatch)
{
eval_addReadWriteAddress(ctx, scalarD);
eval_addReadWriteAddress(ctx, scalarD, ctx.lastSWrite.key);
}

// If we just modified a balance
Expand Down
5 changes: 3 additions & 2 deletions src/main_sm/fork_3/main/eval_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2063,13 +2063,14 @@ void eval_AddPointEc (Context &ctx, const RomCommand &cmd, bool dbl, RawFec::Ele
ctx.fec.sub(y3, aux1, y1);
}

void eval_addReadWriteAddress (Context &ctx, const mpz_class value)
void eval_addReadWriteAddress (Context &ctx, const mpz_class value, const Goldilocks::Element (&key)[4])
{
zkassert(ctx.proverRequest.input.publicInputsExtended.publicInputs.forkID == 3); // fork_3
((fork_3::FullTracer *)ctx.proverRequest.pFullTracer)->addReadWriteAddress(
ctx.pols.A0[0], ctx.pols.A1[0], ctx.pols.A2[0], ctx.pols.A3[0], ctx.pols.A4[0], ctx.pols.A5[0], ctx.pols.A6[0], ctx.pols.A7[0],
ctx.pols.B0[0], ctx.pols.B1[0], ctx.pols.B2[0], ctx.pols.B3[0], ctx.pols.B4[0], ctx.pols.B5[0], ctx.pols.B6[0], ctx.pols.B7[0],
value);
value,
key);
}

} // namespace
Loading

0 comments on commit 6b10393

Please sign in to comment.