Skip to content

Commit aa6b4ee

Browse files
refactor(sol): reduce local variables in SortMergeJoinExec (#1107)
Please be sure to look over the pull request guidelines here: https://github.com/spaceandtimelabs/sxt-proof-of-sql/blob/main/CONTRIBUTING.md#submit-pr. # Please go through the following checklist - [ ] The PR title and commit messages adhere to guidelines here: https://github.com/spaceandtimelabs/sxt-proof-of-sql/blob/main/CONTRIBUTING.md. In particular `!` is used if and only if at least one breaking change has been introduced. - [ ] I have run the ci check script with `source scripts/run_ci_checks.sh`. - [ ] I have run the clean commit check script with `source scripts/check_commits.sh`, and the commit history is certified to follow clean commit guidelines as described here: https://github.com/spaceandtimelabs/sxt-proof-of-sql/blob/main/COMMIT_GUIDELINES.md - [ ] The latest changes from `main` have been incorporated to this PR by simple rebase if possible, if not, then conflicts are resolved appropriately. # Rationale for this change The proof of sql contracts repo needs there to be fewer local variables in `SortMergeJoinExec`. # What changes are included in this PR? Adding s struct-like object within `SortMergeJoinExec` to reduce local variables. # Are these changes tested? Yes.
2 parents cd0f901 + 4312e52 commit aa6b4ee

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

solidity/src/proof_plans/SortMergeJoinExec.presl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ library SortMergeJoinExec {
188188

189189
// Assembles the collection of output columns that will be compared to the right hat collection.
190190
// Additionally, computes the i_eval
191-
function consume_right_evals_for_join(builder_ptr, num_join_columns, hat_evals, res_chi_eval, chi_eval, left_right_and_output_evaluations) -> i_eval, right_output_column_evals {
191+
function consume_right_evals_for_join(builder_ptr, num_join_columns, hat_evals, left_right_and_output_evaluations) -> i_eval, right_output_column_evals {
192192
// The length of hat_evals is the length of the collection which will be used in the membership check
193193
let num_hat_columns := mload(hat_evals)
194194
right_output_column_evals := mload(FREE_PTR)
@@ -250,11 +250,11 @@ library SortMergeJoinExec {
250250
// Consumes the output evaluations from the first round builder for any output columns that belong only to the right plan
251251
// and verifies that all output rows from the right plan are all members of the right hat collection.
252252
// Additionally, this function retrieves i_eval
253-
function consume_and_membership_check_right_column_evals(builder_ptr, alpha_and_beta, num_join_columns, hat_evals, res_chi_eval, chi_eval, left_right_and_output_evaluations) -> i_eval {
253+
function consume_and_membership_check_right_column_evals(builder_ptr, alpha_and_beta, num_join_columns, hat_evals, right_and_output_chi_evals, left_right_and_output_evaluations) -> i_eval {
254254
let right_column_evals
255255
i_eval, right_column_evals :=
256256
consume_right_evals_for_join(
257-
builder_ptr, num_join_columns, hat_evals, res_chi_eval, chi_eval, left_right_and_output_evaluations
257+
builder_ptr, num_join_columns, hat_evals, left_right_and_output_evaluations
258258
)
259259

260260
// Finally, we can do our membership check
@@ -263,8 +263,8 @@ library SortMergeJoinExec {
263263
builder_ptr,
264264
mload(alpha_and_beta),
265265
mload(add(alpha_and_beta, WORD_SIZE)),
266-
chi_eval,
267-
res_chi_eval,
266+
mload(right_and_output_chi_evals),
267+
mload(add(right_and_output_chi_evals, WORD_SIZE)),
268268
hat_evals,
269269
right_column_evals
270270
)
@@ -290,7 +290,7 @@ library SortMergeJoinExec {
290290
// for any columns that belong only to the right plan, and verifies that all output rows from the right plan
291291
// are all members of the right hat collection.
292292
// Additionally, this function checks the monotonicity of the i_eval
293-
function evaluate_consume_and_check_right_join_evals(plan_ptr, builder_ptr, alpha_and_beta, res_chi_eval, left_right_and_output_evaluations, chi_eval) -> plan_ptr_out {
293+
function evaluate_consume_and_check_right_join_evals(plan_ptr, builder_ptr, alpha_and_beta, left_right_and_output_evaluations, right_and_output_chi_evals) -> plan_ptr_out {
294294
let hat_evals, i_eval, join_evals
295295
plan_ptr, hat_evals, join_evals :=
296296
evaluate_input_plans(plan_ptr, builder_ptr, mload(add(left_right_and_output_evaluations, WORD_SIZE)))
@@ -300,8 +300,7 @@ library SortMergeJoinExec {
300300
alpha_and_beta,
301301
mload(join_evals),
302302
hat_evals,
303-
res_chi_eval,
304-
chi_eval,
303+
right_and_output_chi_evals,
305304
left_right_and_output_evaluations
306305
)
307306
mstore(add(left_right_and_output_evaluations, WORD_SIZE), join_evals)
@@ -311,7 +310,7 @@ library SortMergeJoinExec {
311310
mload(alpha_and_beta),
312311
mload(add(alpha_and_beta, WORD_SIZE)),
313312
i_eval,
314-
res_chi_eval,
313+
mload(add(right_and_output_chi_evals, WORD_SIZE)),
315314
1,
316315
1
317316
)
@@ -350,9 +349,10 @@ library SortMergeJoinExec {
350349
}
351350

352351
// This function is effectively any verification that happens on the evaluations of the left and right input plans
353-
function evaluate_sort_merge_join_outputs(plan_ptr, builder_ptr, left_right_and_output_evaluations, left_chi_eval, right_chi_eval, output_chi_eval) -> plan_ptr_out {
352+
function evaluate_sort_merge_join_outputs(plan_ptr, builder_ptr, left_right_and_output_evaluations, left_chi_eval, right_and_output_chi_evals) -> plan_ptr_out {
354353
// In order to save on local variables, we save alpha and beta in one location in memory
355354
let alpha_and_beta := mload(FREE_PTR)
355+
let output_chi_eval := mload(add(right_and_output_chi_evals, WORD_SIZE))
356356
mstore(FREE_PTR, add(alpha_and_beta, WORDX2_SIZE))
357357
mstore(alpha_and_beta, builder_consume_challenge(builder_ptr))
358358
mstore(add(alpha_and_beta, WORD_SIZE), builder_consume_challenge(builder_ptr))
@@ -370,9 +370,8 @@ library SortMergeJoinExec {
370370
plan_ptr,
371371
builder_ptr,
372372
alpha_and_beta,
373-
output_chi_eval,
374373
left_right_and_output_evaluations,
375-
right_chi_eval
374+
right_and_output_chi_evals
376375
)
377376

378377
let w_eval
@@ -383,7 +382,7 @@ library SortMergeJoinExec {
383382
alpha_and_beta,
384383
left_right_and_output_evaluations,
385384
left_chi_eval,
386-
right_chi_eval
385+
mload(right_and_output_chi_evals)
387386
)
388387
// sum w_eval - output_chi_eval = 0
389388
builder_produce_zerosum_constraint(builder_ptr, submod_bn254(w_eval, output_chi_eval), 2)
@@ -408,28 +407,31 @@ library SortMergeJoinExec {
408407
let left_right_and_output_evaluations := mload(FREE_PTR)
409408
mstore(FREE_PTR, add(left_right_and_output_evaluations, WORDX3_SIZE))
410409

411-
let left_chi_eval, right_chi_eval
410+
let left_chi_eval
412411
{
413412
let left_evaluations, left_output_length
414413
plan_ptr, left_evaluations, left_output_length, left_chi_eval :=
415414
proof_plan_evaluate(plan_ptr, builder_ptr)
416415
mstore(left_right_and_output_evaluations, left_evaluations)
417416
}
417+
let right_and_output_chi_evals := mload(FREE_PTR)
418+
mstore(FREE_PTR, add(right_and_output_chi_evals, WORDX2_SIZE))
418419
{
419-
let right_evaluations, right_output_length
420+
let right_evaluations, right_output_length, right_chi_eval
420421
plan_ptr, right_evaluations, right_output_length, right_chi_eval :=
421422
proof_plan_evaluate(plan_ptr, builder_ptr)
423+
mstore(right_and_output_chi_evals, right_chi_eval)
422424
mstore(add(left_right_and_output_evaluations, WORD_SIZE), right_evaluations)
423425
}
424426
output_length, output_chi_eval := builder_consume_chi_evaluation_with_length(builder_ptr)
427+
mstore(add(right_and_output_chi_evals, WORD_SIZE), output_chi_eval)
425428
plan_ptr :=
426429
evaluate_sort_merge_join_outputs(
427430
plan_ptr,
428431
builder_ptr,
429432
left_right_and_output_evaluations,
430433
left_chi_eval,
431-
right_chi_eval,
432-
output_chi_eval
434+
right_and_output_chi_evals
433435
)
434436
evaluations_ptr := mload(add(left_right_and_output_evaluations, WORDX2_SIZE))
435437

0 commit comments

Comments
 (0)