Skip to content

Commit

Permalink
Flatten sumcheck univariate array.
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Dec 13, 2024
1 parent 5288e99 commit 84d9858
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ def input_map(self) -> dict:
imap["p_public_inputs"] = (structs.u256Span, self.vk.public_inputs_size)
imap["p_public_inputs_offset"] = structs.u384

for i in range(self.vk.log_circuit_size):
imap[f"sumcheck_univariate_{i}"] = (
structs.u256Span,
hk.BATCHED_RELATION_PARTIAL_LENGTH,
)
imap["sumcheck_univariates_flat"] = (
structs.u256Span,
self.vk.log_circuit_size * hk.BATCHED_RELATION_PARTIAL_LENGTH,
)

imap["sumcheck_evaluations"] = (
structs.u256Span,
Expand Down Expand Up @@ -208,9 +207,16 @@ def _execute_circuit_logic(
vars["p_public_inputs_offset"],
)

sumcheck_univariates_flat = vars["sumcheck_univariates_flat"]
sumcheck_univariates = []
for i in range(self.vk.log_circuit_size):
sumcheck_univariates.append(vars[f"sumcheck_univariate_{i}"])
sumcheck_univariates.append(
sumcheck_univariates_flat[
i
* hk.BATCHED_RELATION_PARTIAL_LENGTH : (i + 1)
* hk.BATCHED_RELATION_PARTIAL_LENGTH
]
)

assert len(sumcheck_univariates) == self.vk.log_circuit_size
assert len(sumcheck_univariates[0]) == hk.BATCHED_RELATION_PARTIAL_LENGTH
Expand Down
23 changes: 9 additions & 14 deletions hydra/garaga/precompiled_circuits/honk.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,8 @@ def format_array(elements: list, span: bool = False) -> str:
code += f"lookup_read_tags: {g1_to_g1point256(self.lookup_read_tags)},\n"
code += f"lookup_inverses: {g1_to_g1point256(self.lookup_inverses)},\n"

# Format nested arrays for sumcheck_univariates
univariates_arrays = [
format_array(univariate, span=True)
for univariate in self.sumcheck_univariates
]
code += (
f"sumcheck_univariates: array![{','.join(univariates_arrays)}].span(),\n"
)
# Flatten sumcheck_univariates array
code += f"sumcheck_univariates: {format_array(io.flatten(self.sumcheck_univariates), span=True)},\n"

code += f"sumcheck_evaluations: {format_array(self.sumcheck_evaluations, span=True)},\n"
code += f"gemini_fold_comms: array![{', '.join(g1_to_g1point256(comm) for comm in self.gemini_fold_comms)}].span(),\n"
Expand Down Expand Up @@ -283,13 +277,14 @@ def serialize_G1Point256(g1_point: G1Point) -> list[int]:
cd.extend(serialize_G1Point256(self.lookup_read_counts))
cd.extend(serialize_G1Point256(self.lookup_read_tags))
cd.extend(serialize_G1Point256(self.lookup_inverses))
cd.append(len(self.sumcheck_univariates))
for univariate in self.sumcheck_univariates:
cd.extend(
io.bigint_split_array(
x=univariate, n_limbs=2, base=2**128, prepend_length=True
)
cd.extend(
io.bigint_split_array(
x=io.flatten(self.sumcheck_univariates),
n_limbs=2,
base=2**128,
prepend_length=True,
)
)

cd.extend(
io.bigint_split_array(
Expand Down
11 changes: 7 additions & 4 deletions hydra/garaga/starknet/honk_contract_generator/generator_honk.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def gen_honk_verifier(
use garaga::utils::neg_3;
use super::{{vk, precomputed_lines, {sumcheck_function_name}, {prepare_scalars_function_name}, {lhs_ecip_function_name}}};
use garaga::utils::noir::{{HonkProof, remove_unused_variables_sumcheck_evaluations, G2_POINT_KZG_1, G2_POINT_KZG_2}};
use garaga::utils::noir::keccak_transcript::{{HonkTranscriptTrait, Point256IntoCircuitPoint}};
use garaga::utils::noir::keccak_transcript::{{HonkTranscriptTrait, Point256IntoCircuitPoint, BATCHED_RELATION_PARTIAL_LENGTH}};
use garaga::core::circuit::U64IntoU384;
use core::num::traits::Zero;
use core::poseidon::hades_permutation;
Expand Down Expand Up @@ -202,7 +202,7 @@ def gen_honk_verifier(
let (sum_check_rlc, honk_check) = {sumcheck_function_name}(
p_public_inputs: full_proof.proof.public_inputs,
p_public_inputs_offset: full_proof.proof.public_inputs_offset.into(),
{', '.join([f'sumcheck_univariate_{i}: (*full_proof.proof.sumcheck_univariates.at({i}))' for i in range(vk.log_circuit_size)])},
sumcheck_univariates_flat: full_proof.proof.sumcheck_univariates.slice(0, log_n * BATCHED_RELATION_PARTIAL_LENGTH),
sumcheck_evaluations: remove_unused_variables_sumcheck_evaluations(
full_proof.proof.sumcheck_evaluations
),
Expand Down Expand Up @@ -383,7 +383,7 @@ def gen_honk_verifier(
);
let mod_bn = get_modulus(0);
let zk_ecip_batched_rhs = batch_3_mod_p(rhs_low, rhs_high, rhs_high_shifted, c0, mod_bn);
let zk_ecip_batched_rhs = batch_3_mod_p(rhs_low, rhs_high, rhs_high_shifted, base_rlc_coeff.into(), mod_bn);
let ecip_check = zk_ecip_batched_lhs == zk_ecip_batched_rhs;
Expand Down Expand Up @@ -448,11 +448,14 @@ def gen_honk_verifier(
VK_PATH = (
"hydra/garaga/starknet/honk_contract_generator/examples/vk_ultra_keccak.bin"
)

VK_LARGE_PATH = (
"hydra/garaga/starknet/honk_contract_generator/examples/vk_large.bin"
)
CONTRACTS_FOLDER = "src/contracts/" # Do not change this

FOLDER_NAME = (
"noir_ultra_keccak_honk_example" # '_curve_id' is appended in the end.
)

gen_honk_verifier(VK_PATH, CONTRACTS_FOLDER, FOLDER_NAME)
gen_honk_verifier(VK_LARGE_PATH, CONTRACTS_FOLDER, FOLDER_NAME + "_large")
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod UltraKeccakHonkVerifier {
MSMHintBatched, compute_rhs_ecip, derive_ec_point_from_X, SlopeInterceptOutput,
};
use garaga::ec_ops_g2::{G2PointTrait};
use garaga::basic_field_ops::{add_mod_p, mul_mod_p};
use garaga::basic_field_ops::{batch_3_mod_p};
use garaga::circuits::ec;
use garaga::utils::neg_3;
use super::{
Expand All @@ -32,7 +32,9 @@ mod UltraKeccakHonkVerifier {
use garaga::utils::noir::{
HonkProof, remove_unused_variables_sumcheck_evaluations, G2_POINT_KZG_1, G2_POINT_KZG_2,
};
use garaga::utils::noir::keccak_transcript::{HonkTranscriptTrait, Point256IntoCircuitPoint};
use garaga::utils::noir::keccak_transcript::{
HonkTranscriptTrait, Point256IntoCircuitPoint, BATCHED_RELATION_PARTIAL_LENGTH,
};
use garaga::core::circuit::U64IntoU384;
use core::num::traits::Zero;
use core::poseidon::hades_permutation;
Expand Down Expand Up @@ -69,11 +71,10 @@ mod UltraKeccakHonkVerifier {
let (sum_check_rlc, honk_check) = run_GRUMPKIN_HONK_SUMCHECK_SIZE_5_PUB_1_circuit(
p_public_inputs: full_proof.proof.public_inputs,
p_public_inputs_offset: full_proof.proof.public_inputs_offset.into(),
sumcheck_univariate_0: (*full_proof.proof.sumcheck_univariates.at(0)),
sumcheck_univariate_1: (*full_proof.proof.sumcheck_univariates.at(1)),
sumcheck_univariate_2: (*full_proof.proof.sumcheck_univariates.at(2)),
sumcheck_univariate_3: (*full_proof.proof.sumcheck_univariates.at(3)),
sumcheck_univariate_4: (*full_proof.proof.sumcheck_univariates.at(4)),
sumcheck_univariates_flat: full_proof
.proof
.sumcheck_univariates
.slice(0, log_n * BATCHED_RELATION_PARTIAL_LENGTH),
sumcheck_evaluations: remove_unused_variables_sumcheck_evaluations(
full_proof.proof.sumcheck_evaluations,
),
Expand Down Expand Up @@ -365,14 +366,8 @@ mod UltraKeccakHonkVerifier {
);

let mod_bn = get_modulus(0);
let c0: u384 = base_rlc_coeff.into();
let c1: u384 = mul_mod_p(c0, c0, mod_bn);
let c2 = mul_mod_p(c1, c0, mod_bn);

let zk_ecip_batched_rhs = add_mod_p(
add_mod_p(mul_mod_p(rhs_low, c0, mod_bn), mul_mod_p(rhs_high, c1, mod_bn), mod_bn),
mul_mod_p(rhs_high_shifted, c2, mod_bn),
mod_bn,
let zk_ecip_batched_rhs = batch_3_mod_p(
rhs_low, rhs_high, rhs_high_shifted, base_rlc_coeff.into(), mod_bn,
);

let ecip_check = zk_ecip_batched_lhs == zk_ecip_batched_rhs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ use core::option::Option;
pub fn run_GRUMPKIN_HONK_SUMCHECK_SIZE_5_PUB_1_circuit(
p_public_inputs: Span<u256>,
p_public_inputs_offset: u384,
sumcheck_univariate_0: Span<u256>,
sumcheck_univariate_1: Span<u256>,
sumcheck_univariate_2: Span<u256>,
sumcheck_univariate_3: Span<u256>,
sumcheck_univariate_4: Span<u256>,
sumcheck_univariates_flat: Span<u256>,
sumcheck_evaluations: Span<u256>,
tp_sum_check_u_challenges: Span<u128>,
tp_gate_challenges: Span<u128>,
Expand Down Expand Up @@ -875,30 +871,10 @@ pub fn run_GRUMPKIN_HONK_SUMCHECK_SIZE_5_PUB_1_circuit(

circuit_inputs = circuit_inputs.next_2(p_public_inputs_offset); // in30

let mut sumcheck_univariate_0 = sumcheck_univariate_0;
while let Option::Some(val) = sumcheck_univariate_0.pop_front() {
let mut sumcheck_univariates_flat = sumcheck_univariates_flat;
while let Option::Some(val) = sumcheck_univariates_flat.pop_front() {
circuit_inputs = circuit_inputs.next_u256(*val);
}; // in31 - in38

let mut sumcheck_univariate_1 = sumcheck_univariate_1;
while let Option::Some(val) = sumcheck_univariate_1.pop_front() {
circuit_inputs = circuit_inputs.next_u256(*val);
}; // in39 - in46

let mut sumcheck_univariate_2 = sumcheck_univariate_2;
while let Option::Some(val) = sumcheck_univariate_2.pop_front() {
circuit_inputs = circuit_inputs.next_u256(*val);
}; // in47 - in54

let mut sumcheck_univariate_3 = sumcheck_univariate_3;
while let Option::Some(val) = sumcheck_univariate_3.pop_front() {
circuit_inputs = circuit_inputs.next_u256(*val);
}; // in55 - in62

let mut sumcheck_univariate_4 = sumcheck_univariate_4;
while let Option::Some(val) = sumcheck_univariate_4.pop_front() {
circuit_inputs = circuit_inputs.next_u256(*val);
}; // in63 - in70
}; // in31 - in70

let mut sumcheck_evaluations = sumcheck_evaluations;
while let Option::Some(val) = sumcheck_evaluations.pop_front() {
Expand Down
5 changes: 2 additions & 3 deletions src/src/core/circuit.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::circuit::{
add_circuit_input, AddInputResult, CircuitData, IntoCircuitInputValue, CircuitDefinition,
init_circuit_data, CircuitInputAccumulator, into_u96_guarantee, U96Guarantee,
init_circuit_data, CircuitInputAccumulator, into_u96_guarantee, U96Guarantee, u384,
};
use core::panic_with_felt252;
use garaga::definitions::{E12D, G2Line, u384, u288};
use garaga::definitions::{E12D, G2Line, u288};
use garaga::utils::hashing::{hades_permutation, PoseidonState};
// use core::panics::panic;

Expand All @@ -23,7 +23,6 @@ impl u288IntoCircuitInputValue of IntoCircuitInputValue<u288> {
}
}


#[generate_trait]
pub impl AddInputResultImpl2<C> of AddInputResultTrait2<C> {
/// Adds an input to the accumulator.
Expand Down
Loading

0 comments on commit 84d9858

Please sign in to comment.