Skip to content

Commit

Permalink
Fix hints custom commits (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerTaule authored Nov 8, 2024
1 parent 3fd2d24 commit 1609bbf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/fibonacci-square/pil/fibonaccisq.pil
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ airtemplate FibonacciSquare(const int N = 2**8) {

line - (flags + 1) === 0;

@test_hint{ custom_col: line };

permutation_assumes(MODULE_ID, [a*a + b*b, b'], 1 - L1');
}
3 changes: 3 additions & 0 deletions pil2-stark/src/starkpil/expressions_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ void ExpressionsBin::loadExpressionsBin(BinFileUtils::BinFile *expressionsBin) {
if(hintFieldValue.operand == opType::tmp) {
hintFieldValue.dim = expressionsBin->readU32LE();
}
if(hintFieldValue.operand == opType::custom) {
hintFieldValue.commitId = expressionsBin->readU32LE();
}
uint64_t nPos = expressionsBin->readU32LE();
for(uint64_t p = 0; p < nPos; ++p) {
uint32_t pos = expressionsBin->readU32LE();
Expand Down
1 change: 1 addition & 0 deletions pil2-stark/src/starkpil/expressions_bin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const int GLOBAL_HINTS_SECTION = 3;
struct HintFieldValue {
opType operand;
uint64_t id;
uint64_t commitId;
uint64_t dim;
uint64_t value;
string stringValue;
Expand Down
38 changes: 30 additions & 8 deletions pil2-stark/src/starkpil/hints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ struct HintFieldOptions {
};


void getPolynomial(SetupCtx& setupCtx, Goldilocks::Element *buffer, Goldilocks::Element *dest, bool committed, uint64_t idPol, bool domainExtended) {
PolMap polInfo = committed ? setupCtx.starkInfo.cmPolsMap[idPol] : setupCtx.starkInfo.constPolsMap[idPol];
void getPolynomial(SetupCtx& setupCtx, Goldilocks::Element *buffer, Goldilocks::Element *dest, PolMap& polInfo, string type, bool domainExtended) {
uint64_t deg = domainExtended ? 1 << setupCtx.starkInfo.starkStruct.nBitsExt : 1 << setupCtx.starkInfo.starkStruct.nBits;
uint64_t dim = polInfo.dim;
std::string stage = committed ? "cm" + to_string(polInfo.stage) : "const";
std::string stage = type == "cm" ? "cm" + to_string(polInfo.stage) : type == "custom" ? setupCtx.starkInfo.customCommits[polInfo.commitId].name + "0" : "const";
uint64_t nCols = setupCtx.starkInfo.mapSectionsN[stage];
uint64_t offset = setupCtx.starkInfo.mapOffsets[std::make_pair(stage, domainExtended)];
offset += polInfo.stagePos;
Polinomial pol = Polinomial(&buffer[offset], deg, dim, nCols, std::to_string(idPol));
Polinomial pol = Polinomial(&buffer[offset], deg, dim, nCols);
#pragma omp parallel for
for(uint64_t j = 0; j < deg; ++j) {
std::memcpy(&dest[j*dim], pol[j], dim * sizeof(Goldilocks::Element));
Expand Down Expand Up @@ -166,7 +165,7 @@ HintFieldInfo printByName(SetupCtx& setupCtx, StepsParams& params, string name,
hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size];
hintFieldInfo.fieldType = cmPol.dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended;
hintFieldInfo.offset = cmPol.dim;
getPolynomial(setupCtx, params.pols, hintFieldInfo.values, true, i, false);
getPolynomial(setupCtx, params.pols, hintFieldInfo.values, setupCtx.starkInfo.cmPolsMap[i], "cm", false);
}
return hintFieldInfo;
}
Expand All @@ -192,7 +191,7 @@ HintFieldInfo printByName(SetupCtx& setupCtx, StepsParams& params, string name,
hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size];
hintFieldInfo.fieldType = HintFieldType::Column;
hintFieldInfo.offset = 1;
getPolynomial(setupCtx, params.pConstPolsAddress, hintFieldInfo.values, false, i, false);
getPolynomial(setupCtx, params.pConstPolsAddress, hintFieldInfo.values, setupCtx.starkInfo.constPolsMap[i], "const", false);
}
return hintFieldInfo;
}
Expand Down Expand Up @@ -317,14 +316,37 @@ HintFieldValues getHintField(
cout << endl;
}
if(!hintOptions.dest) {
getPolynomial(setupCtx, params.pols, hintFieldInfo.values, true, hintFieldVal.id, false);
getPolynomial(setupCtx, params.pols, hintFieldInfo.values, setupCtx.starkInfo.cmPolsMap[hintFieldVal.id], "cm", false);
if(hintOptions.inverse) {
zklog.error("Inverse not supported still for polynomials");
exitProcess();
}
} else if(hintOptions.initialize_zeros) {
memset((uint8_t *)hintFieldInfo.values, 0, hintFieldInfo.size * sizeof(Goldilocks::Element));
}
} else if(hintFieldVal.operand == opType::custom) {
uint64_t dim = setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].dim;
hintFieldInfo.size = deg*dim;
hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size];
hintFieldInfo.fieldType = dim == 1 ? HintFieldType::Column : HintFieldType::ColumnExtended;
hintFieldInfo.offset = dim;
if(hintOptions.print_expression) {
cout << "witness col " << setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].name;
if(setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size() > 0) {
cout << "[";
for(uint64_t i = 0; i < setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size(); ++i) {
cout << setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths[i];
if(i != setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id].lengths.size() - 1) cout << ", ";
}
cout << "]";
}
cout << endl;
}
getPolynomial(setupCtx, params.pols, hintFieldInfo.values, setupCtx.starkInfo.customCommitsMap[hintFieldVal.commitId][hintFieldVal.id], "custom", false);
if(hintOptions.inverse) {
zklog.error("Inverse not supported still for polynomials");
exitProcess();
}
} else if(hintFieldVal.operand == opType::const_) {
uint64_t dim = setupCtx.starkInfo.constPolsMap[hintFieldVal.id].dim;
hintFieldInfo.size = deg*dim;
Expand All @@ -341,7 +363,7 @@ HintFieldValues getHintField(
cout << "]";
}
cout << endl;
getPolynomial(setupCtx, params.pConstPolsAddress, hintFieldInfo.values, false, hintFieldVal.id, false);
getPolynomial(setupCtx, params.pConstPolsAddress, hintFieldInfo.values, setupCtx.starkInfo.constPolsMap[hintFieldVal.id], "const", false);
if(hintOptions.inverse) {
zklog.error("Inverse not supported still for polynomials");
exitProcess();
Expand Down
2 changes: 2 additions & 0 deletions pil2-stark/src/starkpil/stark_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ opType string2opType(const string s)
return string_;
if(s == "airvalue")
return airvalue;
if(s == "custom")
return custom;
zklog.error("string2opType() found invalid string=" + s);
exitProcess();
exit(-1);
Expand Down
1 change: 1 addition & 0 deletions pil2-stark/src/starkpil/stark_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef enum
string_ = 7,
airvalue = 8,
proofvalue = 9,
custom = 10,
} opType;


Expand Down

0 comments on commit 1609bbf

Please sign in to comment.