Skip to content

Commit

Permalink
Challenges in global constraints (#93)
Browse files Browse the repository at this point in the history
* Challenges in global constraints
  • Loading branch information
RogerTaule authored Oct 31, 2024
1 parent 42c6469 commit 2645c3a
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jobs:
with:
repository: 0xPolygonHermez/pil2-proofman-js
token: ${{ secrets.ZISK_CI_TOKEN }}
ref: 0.0.4
ref: 0.0.5
path: pil2-proofman-js

- name: Install pil2-proofman-js dependencies
Expand Down
1 change: 1 addition & 0 deletions examples/fibonacci-square/pil/fibonaccisq.pil
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ proofval value2;
private function checkProofValues()
{
mod * value1 - value2 === 0;
std_alpha - std_alpha === 0;
}

on final proof checkProofValues();
Expand Down
4 changes: 2 additions & 2 deletions pil2-stark/lib/include/starks_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@

// Global constraints
// =================================================================================
bool verify_global_constraints(void *globalBin, void *publics, void *proofValues, void **airgroupValues);
void *get_hint_field_global_constraints(void *globalBin, void *publics, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression);
bool verify_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues);
void *get_hint_field_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression);
uint64_t set_hint_field_global_constraints(void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName);

// Debug functions
Expand Down
8 changes: 4 additions & 4 deletions pil2-stark/src/api/starks_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,13 @@ void *verify_constraints(void *pSetupCtx, void* stepsParams)

// Global Constraints
// =================================================================================
bool verify_global_constraints(void* p_globalinfo_bin, void *publics, void *proofValues, void **airgroupValues) {
return verifyGlobalConstraints(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues);
bool verify_global_constraints(void* p_globalinfo_bin, void *publics, void *challenges, void *proofValues, void **airgroupValues) {
return verifyGlobalConstraints(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)challenges, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues);
}

void *get_hint_field_global_constraints(void* p_globalinfo_bin, void *publics, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression)
void *get_hint_field_global_constraints(void* p_globalinfo_bin, void *publics, void *challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression)
{
HintFieldValues hintFieldValues = getHintFieldGlobalConstraint(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues, hintId, string(hintFieldName), print_expression);
HintFieldValues hintFieldValues = getHintFieldGlobalConstraint(*(ExpressionsBin*)p_globalinfo_bin, (Goldilocks::Element *)publics, (Goldilocks::Element *)challenges, (Goldilocks::Element *)proofValues, (Goldilocks::Element **)airgroupValues, hintId, string(hintFieldName), print_expression);
return new HintFieldValues(hintFieldValues);
}

Expand Down
4 changes: 2 additions & 2 deletions pil2-stark/src/api/starks_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@

// Global constraints
// =================================================================================
bool verify_global_constraints(void *globalBin, void *publics, void *proofValues, void **airgroupValues);
void *get_hint_field_global_constraints(void *globalBin, void *publics, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression);
bool verify_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues);
void *get_hint_field_global_constraints(void *globalBin, void *publics, void* challenges, void *proofValues, void **airgroupValues, uint64_t hintId, char *hintFieldName, bool print_expression);
uint64_t set_hint_field_global_constraints(void* p_globalinfo_bin, void *proofValues, void *values, uint64_t hintId, char *hintFieldName);

// Debug functions
Expand Down
66 changes: 54 additions & 12 deletions pil2-stark/src/starkpil/global_constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using json = nlohmann::json;

void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* publics, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) {
void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* publics, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) {

uint8_t* ops = &parserArgs.ops[parserParams.opsOffset];
uint16_t* args = &parserArgs.args[parserParams.argsOffset];
Expand Down Expand Up @@ -114,41 +114,83 @@ void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* p
break;
}
case 15: {
// OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: tmp1
Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &tmp1[args[i_args + 3]]);
i_args += 4;
break;
}
case 16: {
// OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: public
Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &publics[args[i_args + 3]]);
i_args += 4;
break;
}
case 17: {
// OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: number
Goldilocks3::op_31_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &numbers_[args[i_args + 3]]);
i_args += 4;
break;
}
case 18: {
// OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: tmp3
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &tmp3[args[i_args + 3] * FIELD_EXTENSION]);
i_args += 4;
break;
}
case 16: {
case 19: {
// OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: airgroupvalue
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &airgroupValues[args[i_args + 3]][args[i_args + 4] * FIELD_EXTENSION]);
i_args += 5;
break;
}
case 17: {
case 20: {
// OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: proofvalue
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]);
i_args += 4;
break;
}
case 18: {
case 21: {
// OPERATION WITH DEST: tmp3 - SRC0: tmp3 - SRC1: challenge
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &tmp3[args[i_args + 2] * FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION]);
i_args += 4;
break;
}
case 22: {
// OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: airgroupvalue
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &airgroupValues[args[i_args + 4]][args[i_args + 5] * FIELD_EXTENSION]);
i_args += 6;
break;
}
case 19: {
case 23: {
// OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: proofvalue
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &proofValues[args[i_args + 4] * FIELD_EXTENSION]);
i_args += 5;
break;
}
case 20: {
case 24: {
// OPERATION WITH DEST: tmp3 - SRC0: airgroupvalue - SRC1: challenge
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &airgroupValues[args[i_args + 2]][args[i_args + 3] * FIELD_EXTENSION], &challenges[args[i_args + 4]*FIELD_EXTENSION]);
i_args += 5;
break;
}
case 25: {
// OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: proofvalue
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &proofValues[args[i_args + 3] * FIELD_EXTENSION]);
i_args += 4;
break;
}
case 26: {
// OPERATION WITH DEST: tmp3 - SRC0: proofvalue - SRC1: challenge
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &proofValues[args[i_args + 2] * FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION]);
i_args += 4;
break;
}
case 27: {
// OPERATION WITH DEST: tmp3 - SRC0: challenge - SRC1: challenge
Goldilocks3::op_pack(1, args[i_args], &tmp3[args[i_args + 1] * FIELD_EXTENSION], &challenges[args[i_args + 2]*FIELD_EXTENSION], &challenges[args[i_args + 3]*FIELD_EXTENSION]);
i_args += 4;
break;
}
default: {
std::cout << " Wrong operation!" << std::endl;
exit(1);
Expand All @@ -167,11 +209,11 @@ void calculateGlobalExpression(Goldilocks::Element* dest, Goldilocks::Element* p
}


bool verifyGlobalConstraint(Goldilocks::Element* publics, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) {
bool verifyGlobalConstraint(Goldilocks::Element* publics, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, ParserArgs &parserArgs, ParserParams &parserParams) {

Goldilocks::Element dest[parserParams.destDim];

calculateGlobalExpression(dest, publics, proofValues, airgroupValues, parserArgs, parserParams);
calculateGlobalExpression(dest, publics, challenges, proofValues, airgroupValues, parserArgs, parserParams);

bool isValidConstraint = true;
if(parserParams.destDim == 1) {
Expand Down Expand Up @@ -199,7 +241,7 @@ bool verifyGlobalConstraint(Goldilocks::Element* publics, Goldilocks::Element* p
}


bool verifyGlobalConstraints(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues)
bool verifyGlobalConstraints(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues)
{

std::vector<ParserParams> globalConstraintsInfo = globalConstraintsBin.constraintsInfoDebug;
Expand All @@ -210,7 +252,7 @@ bool verifyGlobalConstraints(ExpressionsBin &globalConstraintsBin, Goldilocks::E
cout << "--------------------------------------------------------" << endl;
cout << globalConstraintsInfo[i].line << endl;
cout << "--------------------------------------------------------" << endl;
if(!verifyGlobalConstraint(publicInputs, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsConstraints, globalConstraintsInfo[i])) {
if(!verifyGlobalConstraint(publicInputs, challenges, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsConstraints, globalConstraintsInfo[i])) {
validGlobalConstraints = false;
};
}
Expand All @@ -219,7 +261,7 @@ bool verifyGlobalConstraints(ExpressionsBin &globalConstraintsBin, Goldilocks::E
}


HintFieldValues getHintFieldGlobalConstraint(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, uint64_t hintId, std::string hintFieldName, bool print_expression) {
HintFieldValues getHintFieldGlobalConstraint(ExpressionsBin &globalConstraintsBin, Goldilocks::Element* publicInputs, Goldilocks::Element* challenges, Goldilocks::Element* proofValues, Goldilocks::Element** airgroupValues, uint64_t hintId, std::string hintFieldName, bool print_expression) {


if(globalConstraintsBin.hints.size() == 0) {
Expand Down Expand Up @@ -263,7 +305,7 @@ HintFieldValues getHintFieldGlobalConstraint(ExpressionsBin &globalConstraintsBi
cout << "the expression with id: " << hintFieldVal.id << " " << globalConstraintsBin.expressionsInfo[hintFieldVal.id].line << endl;
}

calculateGlobalExpression(hintFieldInfo.values, publicInputs, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsExpressions, globalConstraintsBin.expressionsInfo[hintFieldVal.id]);
calculateGlobalExpression(hintFieldInfo.values, publicInputs, challenges, proofValues, airgroupValues, globalConstraintsBin.expressionsBinArgsExpressions, globalConstraintsBin.expressionsInfo[hintFieldVal.id]);
} else if (hintFieldVal.operand == opType::public_) {
hintFieldInfo.size = 1;
hintFieldInfo.values = new Goldilocks::Element[hintFieldInfo.size];
Expand Down
16 changes: 16 additions & 0 deletions proofman/src/global_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub fn verify_global_constraints_proof<F: Field>(pctx: Arc<ProofCtx<F>>, sctx: A
let public_inputs_guard = pctx.public_inputs.inputs.read().unwrap();
let public_inputs = (*public_inputs_guard).as_ptr() as *mut c_void;

let challenges_guard = pctx.challenges.challenges.read().unwrap();
let challenges = (*challenges_guard).as_ptr() as *mut c_void;

let proof_values_guard = pctx.proof_values.values.read().unwrap();
let proof_values = (*proof_values_guard).as_ptr() as *mut c_void;

Expand All @@ -79,6 +82,7 @@ pub fn verify_global_constraints_proof<F: Field>(pctx: Arc<ProofCtx<F>>, sctx: A
let global_constraints_verified = verify_global_constraints_c(
sctx.get_global_bin(),
public_inputs,
challenges,
proof_values,
airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void,
);
Expand Down Expand Up @@ -113,12 +117,16 @@ pub fn get_hint_field_gc<F: Field>(
.map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec
.collect();

let challenges_guard = pctx.challenges.challenges.read().unwrap();
let challenges = (*challenges_guard).as_ptr() as *mut c_void;

let proof_values_guard = pctx.proof_values.values.read().unwrap();
let proof_values = (*proof_values_guard).as_ptr() as *mut c_void;

let raw_ptr = get_hint_field_global_constraints_c(
sctx.get_global_bin(),
public_inputs_ptr,
challenges,
proof_values,
airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void,
hint_id,
Expand Down Expand Up @@ -151,12 +159,16 @@ pub fn get_hint_field_gc_a<F: Field>(
.map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec
.collect();

let challenges_guard = pctx.challenges.challenges.read().unwrap();
let challenges = (*challenges_guard).as_ptr() as *mut c_void;

let proof_values_guard = pctx.proof_values.values.read().unwrap();
let proof_values = (*proof_values_guard).as_ptr() as *mut c_void;

let raw_ptr = get_hint_field_global_constraints_c(
sctx.get_global_bin(),
public_inputs_ptr,
challenges,
proof_values,
airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void,
hint_id,
Expand Down Expand Up @@ -195,12 +207,16 @@ pub fn get_hint_field_gc_m<F: Field>(
.map(|inner_vec| inner_vec.as_mut_ptr()) // Get a raw pointer to each inner Vec
.collect();

let challenges_guard = pctx.challenges.challenges.read().unwrap();
let challenges = (*challenges_guard).as_ptr() as *mut c_void;

let proof_values_guard = pctx.proof_values.values.read().unwrap();
let proof_values = (*proof_values_guard).as_ptr() as *mut c_void;

let raw_ptr = get_hint_field_global_constraints_c(
sctx.get_global_bin(),
public_inputs_ptr,
challenges,
proof_values,
airgroup_values_ptrs.as_mut_ptr() as *mut *mut c_void,
hint_id,
Expand Down
6 changes: 4 additions & 2 deletions provers/starks-lib-c/bindings_starks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,19 +459,21 @@ extern "C" {
) -> *mut ::std::os::raw::c_void;
}
extern "C" {
#[link_name = "\u{1}_Z25verify_global_constraintsPvS_S_PS_"]
#[link_name = "\u{1}_Z25verify_global_constraintsPvS_S_S_PS_"]
pub fn verify_global_constraints(
globalBin: *mut ::std::os::raw::c_void,
publics: *mut ::std::os::raw::c_void,
challenges: *mut ::std::os::raw::c_void,
proofValues: *mut ::std::os::raw::c_void,
airgroupValues: *mut *mut ::std::os::raw::c_void,
) -> bool;
}
extern "C" {
#[link_name = "\u{1}_Z33get_hint_field_global_constraintsPvS_S_PS_mPcb"]
#[link_name = "\u{1}_Z33get_hint_field_global_constraintsPvS_S_S_PS_mPcb"]
pub fn get_hint_field_global_constraints(
globalBin: *mut ::std::os::raw::c_void,
publics: *mut ::std::os::raw::c_void,
challenges: *mut ::std::os::raw::c_void,
proofValues: *mut ::std::os::raw::c_void,
airgroupValues: *mut *mut ::std::os::raw::c_void,
hintId: u64,
Expand Down
11 changes: 9 additions & 2 deletions provers/starks-lib-c/src/ffi_starks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,16 +600,19 @@ pub fn verify_constraints_c(p_setup: *mut c_void, p_steps_params: *mut c_void) -
pub fn verify_global_constraints_c(
p_global_constraints_bin: *mut c_void,
publics: *mut c_void,
challenges: *mut c_void,
proof_values: *mut c_void,
airgroupvalues: *mut *mut c_void,
) -> bool {
unsafe { verify_global_constraints(p_global_constraints_bin, publics, proof_values, airgroupvalues) }
unsafe { verify_global_constraints(p_global_constraints_bin, publics, challenges, proof_values, airgroupvalues) }
}

#[cfg(not(feature = "no_lib_link"))]
#[allow(clippy::too_many_arguments)]
pub fn get_hint_field_global_constraints_c(
p_global_constraints_bin: *mut c_void,
publics: *mut c_void,
challenges: *mut c_void,
proof_values: *mut c_void,
airgroupvalues: *mut *mut c_void,
hint_id: u64,
Expand All @@ -621,6 +624,7 @@ pub fn get_hint_field_global_constraints_c(
get_hint_field_global_constraints(
p_global_constraints_bin,
publics,
challenges,
proof_values,
airgroupvalues,
hint_id,
Expand Down Expand Up @@ -1265,6 +1269,7 @@ pub fn verify_constraints_c(_p_setup: *mut c_void, _p_steps_params: *mut c_void)
pub fn verify_global_constraints_c(
_p_global_constraints_bin: *mut c_void,
_publics: *mut c_void,
_challenges: *mut c_void,
_proof_values: *mut c_void,
_airgroupvalues: *mut *mut c_void,
) -> bool {
Expand All @@ -1277,9 +1282,11 @@ pub fn verify_global_constraints_c(
}

#[cfg(feature = "no_lib_link")]
#[allow(clippy::too_many_arguments)]
pub fn get_hint_field_global_constraints_c(
_p_global_constraints_bin: *mut c_void,
_publics: *mut c_void,
_challenges: *mut c_void,
_proof_values: *mut c_void,
_airgroupvalues: *mut *mut c_void,
_hint_id: u64,
Expand All @@ -1305,7 +1312,7 @@ pub fn set_hint_field_global_constraints_c(
trace!(
"{}: ··· {}",
"ffi ",
"get_hint_field_global_constraints_c: This is a mock call because there is no linked library"
"set_hint_field_global_constraints_c: This is a mock call because there is no linked library"
);
100000
}
Expand Down

0 comments on commit 2645c3a

Please sign in to comment.