Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(brillig): Handle Brillig overflow checks inside the VM #6425

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vezenovm
Copy link
Contributor

Description

Problem*

Part of a general effort to reduce Brillig bytecode size and execution traces

Summary*

This PR simply removes the add_overflow_check from brillig_gen and leaves it up to the VM to implement their own overflow checks if that is what is desired. Handling for the overflow checks done during Brillig gen was included in the Brillig VM.

Additional Context

In the following flamegraphs I search for jump and jump if opcodes. The total samples went down from 126k to 99k samples.

sha256_regression on master (126k samples, 29.5% of the program execution is jumps and jump ifs):
Screenshot 2024-10-31 at 3 10 55 PM

sha256_regression after this PR (99k samples, 25.9% of the program execution is jumps and jump ifs on a much more reduced trace):
Screenshot 2024-10-31 at 3 09 08 PM

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

Copy link
Contributor

github-actions bot commented Oct 31, 2024

Changes to Brillig bytecode sizes

Generated at commit: 802678e6f1ba9f3615e192e1e8147124fbda1c0f, compared to commit: b473d99b2b70b595596b8392617256dbaf5d5642

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
loop -16 ✅ -21.33%
brillig_cow_regression -524 ✅ -21.70%
6_array -96 ✅ -21.92%
6 -308 ✅ -21.92%
sha256_var_witness_const_regression -320 ✅ -22.19%
sha256 -476 ✅ -22.55%
brillig_acir_as_brillig -8 ✅ -23.53%
brillig_calls -8 ✅ -23.53%
brillig_loop -24 ✅ -23.53%
2_div -12 ✅ -25.00%
3_add -12 ✅ -25.53%
simple_bitwise -16 ✅ -28.57%
1_mul -32 ✅ -51.61%

Full diff report 👇
Program Brillig opcodes (+/-) %
regression_4709 133,725 (-8) -0.01%
slice_dynamic_index 2,840 (-29) -1.01%
brillig_slices 532 (-8) -1.48%
modulus 1,745 (-32) -1.80%
slices 2,255 (-49) -2.13%
nested_dyn_array_regression_5782 141 (-4) -2.76%
simple_2d_array 136 (-4) -2.86%
brillig_blake2s 135 (-4) -2.88%
blake3 135 (-4) -2.88%
7 135 (-4) -2.88%
brillig_blake3 135 (-4) -2.88%
brillig_cow_assign 130 (-4) -2.99%
databus_two_calldata 259 (-8) -3.00%
brillig_constant_reference_regression 128 (-4) -3.03%
to_bytes_consistent 126 (-4) -3.08%
strings 1,007 (-32) -3.08%
signed_arithmetic 247 (-8) -3.14%
to_le_bytes 123 (-4) -3.15%
regression_4202 114 (-4) -3.39%
databus_two_calldata_simple 113 (-4) -3.42%
fold_call_witness_condition 110 (-4) -3.51%
array_to_slice 983 (-36) -3.53%
array_if_cond_simple 103 (-4) -3.74%
bigint 2,160 (-84) -3.74%
binary_operator_overloading 405 (-16) -3.80%
signed_cmp 93 (-4) -4.12%
array_neq 92 (-4) -4.17%
array_eq 90 (-4) -4.26%
poseidon2 328 (-16) -4.65%
bench_2_to_17 321 (-16) -4.75%
poseidon_bn254_hash 5,410 (-280) -4.92%
poseidon_bn254_hash_width_3 5,410 (-280) -4.92%
slice_regex 2,449 (-128) -4.97%
fold_numeric_generic_poseidon 729 (-40) -5.20%
no_predicates_numeric_generic_poseidon 729 (-40) -5.20%
array_sort 290 (-16) -5.23%
struct_inputs 288 (-16) -5.26%
brillig_loop_size_regression 70 (-4) -5.41%
fold_2_to_17 555 (-32) -5.45%
check_large_field_bits 271 (-16) -5.57%
wildcard_type 269 (-16) -5.61%
brillig_oracle 334 (-20) -5.65%
to_be_bytes 194 (-12) -5.83%
eddsa 10,502 (-660) -5.91%
regression_5252 4,582 (-296) -6.07%
poseidonsponge_x5_254 4,224 (-276) -6.13%
side_effects_constrain_array 115 (-8) -6.50%
databus_composite_calldata 511 (-36) -6.58%
brillig_rc_regression_6123 161 (-12) -6.94%
array_dynamic 304 (-24) -7.32%
7_function 591 (-48) -7.51%
brillig_cow 392 (-32) -7.55%
higher_order_functions 683 (-56) -7.58%
databus_in_fn_with_empty_arr 48 (-4) -7.69%
regression 664 (-56) -7.78%
slice_loop 324 (-28) -7.95%
to_bits 182 (-16) -8.08%
pedersen_commitment 181 (-16) -8.12%
references 268 (-24) -8.22%
databus 87 (-8) -8.42%
global_consts 209 (-20) -8.73%
generics 192 (-20) -9.43%
regression_struct_array_conditional 494 (-52) -9.52%
pedersen_hash 322 (-36) -10.06%
simple_shield 782 (-88) -10.11%
conditional_regression_547 35 (-4) -10.26%
merkle_insert 698 (-80) -10.28%
brillig_hash_to_field 138 (-16) -10.39%
hash_to_field 138 (-16) -10.39%
import 130 (-16) -10.96%
schnorr 1,300 (-160) -10.96%
cast_and_shift_global 63 (-8) -11.27%
reference_only_used_as_alias 220 (-28) -11.29%
aes128_encrypt 493 (-64) -11.49%
signed_div 154 (-20) -11.49%
tuple_inputs 333 (-44) -11.67%
pedersen_check 657 (-88) -11.81%
brillig_pedersen 657 (-88) -11.81%
fold_complex_outputs 498 (-68) -12.01%
fold_distinct_return 27 (-4) -12.90%
break_and_continue 53 (-8) -13.11%
u128 2,533 (-384) -13.16%
uhashmap 14,268 (-2,340) -14.09%
ecdsa_secp256k1 876 (-156) -15.12%
conditional_regression_661 110 (-20) -15.38%
conditional_1 1,142 (-208) -15.41%
acir_inside_brillig_recursion 65 (-12) -15.58%
brillig_keccak 1,486 (-284) -16.05%
keccak256 1,486 (-284) -16.05%
sha2_byte 2,807 (-540) -16.13%
brillig_nested_arrays 144 (-28) -16.28%
regression_3607 41 (-8) -16.33%
sha256_regression 6,191 (-1,220) -16.46%
fold_fibonacci 60 (-12) -16.67%
brillig_recursion 60 (-12) -16.67%
array_dynamic_nested_blackbox_input 831 (-176) -17.48%
hashmap 22,331 (-4,752) -17.55%
nested_array_in_slice 1,108 (-236) -17.56%
array_dynamic_blackbox_input 981 (-212) -17.77%
brillig_sha256 659 (-156) -19.14%
regression_4449 702 (-176) -20.05%
ram_blowup_regression 818 (-212) -20.58%
brillig_calls_array 77 (-20) -20.62%
conditional_regression_short_circuit 1,167 (-308) -20.88%
sha256_var_padding_regression 4,242 (-1,132) -21.06%
sha256_var_size_regression 1,645 (-444) -21.25%
nested_array_dynamic 2,008 (-544) -21.32%
loop 59 (-16) -21.33%
brillig_cow_regression 1,891 (-524) -21.70%
6_array 342 (-96) -21.92%
6 1,097 (-308) -21.92%
sha256_var_witness_const_regression 1,122 (-320) -22.19%
sha256 1,635 (-476) -22.55%
brillig_acir_as_brillig 26 (-8) -23.53%
brillig_calls 26 (-8) -23.53%
brillig_loop 78 (-24) -23.53%
2_div 36 (-12) -25.00%
3_add 35 (-12) -25.53%
simple_bitwise 40 (-16) -28.57%
1_mul 30 (-32) -51.61%

@vezenovm vezenovm marked this pull request as draft November 1, 2024 03:44
Copy link
Contributor

github-actions bot commented Nov 1, 2024

Changes to number of Brillig opcodes executed

Generated at commit: 802678e6f1ba9f3615e192e1e8147124fbda1c0f, compared to commit: b473d99b2b70b595596b8392617256dbaf5d5642

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
regression_4449 -37,584 ✅ -17.09%
array_dynamic_blackbox_input -3,322 ✅ -17.14%
simple_bitwise -8 ✅ -17.39%
sha256_var_padding_regression -43,284 ✅ -18.42%
sha256_var_size_regression -3,776 ✅ -20.57%
2_div -8 ✅ -21.05%
sha256_regression -26,967 ✅ -21.34%
3_add -8 ✅ -21.62%
brillig_cow_regression -133,777 ✅ -22.92%
ram_blowup_regression -203,013 ✅ -23.13%
loop -32 ✅ -23.36%
brillig_loop -48 ✅ -24.49%
1_mul -24 ✅ -48.00%

Full diff report 👇
Program Brillig opcodes (+/-) %
bench_2_to_17 144,664,522 (-26,138) -0.02%
fold_2_to_17 131,420,107 (-49,716) -0.04%
slice_dynamic_index 4,299 (-21) -0.49%
array_if_cond_simple 303 (-2) -0.66%
databus_two_calldata_simple 163 (-2) -1.21%
brillig_slices 709 (-10) -1.39%
nested_dyn_array_regression_5782 124 (-2) -1.59%
slices 3,820 (-62) -1.60%
signed_arithmetic 199 (-4) -1.97%
array_to_slice 2,308 (-50) -2.12%
brillig_constant_reference_regression 178 (-4) -2.20%
simple_2d_array 176 (-4) -2.22%
databus_two_calldata 479 (-12) -2.44%
signed_cmp 77 (-2) -2.53%
slice_regex 3,749 (-100) -2.60%
databus_composite_calldata 740 (-22) -2.89%
array_sort 530 (-16) -2.93%
brillig_loop_size_regression 66 (-2) -2.94%
databus 119 (-4) -3.25%
brillig_rc_regression_6123 272 (-10) -3.55%
poseidon2 682 (-26) -3.67%
brillig_cow_assign 512 (-20) -3.76%
regression_4202 204 (-8) -3.77%
regression_struct_array_conditional 1,241 (-50) -3.87%
fold_numeric_generic_poseidon 5,048 (-212) -4.03%
no_predicates_numeric_generic_poseidon 5,048 (-212) -4.03%
struct_inputs 622 (-28) -4.31%
array_dynamic 477 (-22) -4.41%
strings 1,804 (-84) -4.45%
fold_call_witness_condition 85 (-4) -4.49%
slice_loop 1,218 (-62) -4.84%
databus_in_fn_with_empty_arr 39 (-2) -4.88%
brillig_cow 1,218 (-64) -4.99%
binary_operator_overloading 187 (-10) -5.08%
conditional_regression_661 112 (-6) -5.08%
array_neq 1,186 (-64) -5.12%
array_eq 1,184 (-64) -5.13%
to_le_bytes 1,119 (-62) -5.25%
higher_order_functions 1,438 (-80) -5.27%
conditional_regression_547 33 (-2) -5.71%
regression_3607 33 (-2) -5.71%
7 1,040 (-64) -5.80%
blake3 1,040 (-64) -5.80%
brillig_blake2s 1,040 (-64) -5.80%
brillig_blake3 1,040 (-64) -5.80%
wildcard_type 646 (-40) -5.83%
u128 24,549 (-1,608) -6.15%
reference_only_used_as_alias 229 (-16) -6.53%
uhashmap 145,074 (-10,146) -6.54%
import 134 (-10) -6.94%
pedersen_commitment 258 (-20) -7.19%
7_function 2,608 (-204) -7.25%
to_be_bytes 2,293 (-186) -7.50%
generics 220 (-18) -7.56%
brillig_oracle 2,416 (-200) -7.65%
pedersen_hash 531 (-44) -7.65%
global_consts 1,635 (-136) -7.68%
brillig_pedersen 1,047 (-88) -7.75%
pedersen_check 1,047 (-88) -7.75%
fold_distinct_return 23 (-2) -8.00%
simple_shield 2,371 (-208) -8.07%
merkle_insert 3,080 (-276) -8.22%
to_bytes_consistent 690 (-62) -8.24%
regression 2,466 (-222) -8.26%
tuple_inputs 583 (-54) -8.48%
side_effects_constrain_array 64 (-6) -8.57%
break_and_continue 101 (-10) -9.01%
references 383 (-38) -9.03%
aes128_encrypt 4,166 (-416) -9.08%
fold_complex_outputs 778 (-78) -9.11%
eddsa 670,438 (-67,460) -9.14%
acir_inside_brillig_recursion 237 (-24) -9.20%
modulus 17,930 (-1,844) -9.33%
schnorr 9,204 (-980) -9.62%
brillig_hash_to_field 857 (-98) -10.26%
hash_to_field 857 (-98) -10.26%
nested_array_dynamic 2,957 (-344) -10.42%
nested_array_in_slice 1,369 (-162) -10.58%
brillig_calls_array 83 (-10) -10.75%
brillig_keccak 32,773 (-4,294) -11.58%
keccak256 32,773 (-4,294) -11.58%
brillig_recursion 3,999 (-528) -11.66%
fold_fibonacci 3,999 (-528) -11.66%
array_dynamic_nested_blackbox_input 3,965 (-543) -12.05%
check_large_field_bits 2,400 (-338) -12.34%
brillig_nested_arrays 126 (-18) -12.50%
conditional_1 5,113 (-732) -12.52%
poseidonsponge_x5_254 166,999 (-23,918) -12.53%
regression_5252 831,420 (-119,826) -12.60%
poseidon_bn254_hash 147,854 (-21,470) -12.68%
poseidon_bn254_hash_width_3 147,854 (-21,470) -12.68%
brillig_sha256 3,659 (-550) -13.07%
to_bits 532 (-80) -13.07%
sha2_byte 41,025 (-6,178) -13.09%
hashmap 49,770 (-7,619) -13.28%
ecdsa_secp256k1 6,102 (-982) -13.86%
signed_div 1,112 (-180) -13.93%
sha256 9,276 (-1,582) -14.57%
sha256_var_witness_const_regression 6,009 (-1,032) -14.66%
cast_and_shift_global 174 (-30) -14.71%
conditional_regression_short_circuit 6,318 (-1,132) -15.19%
6 6,243 (-1,132) -15.35%
brillig_acir_as_brillig 22 (-4) -15.38%
brillig_calls 22 (-4) -15.38%
6_array 1,531 (-314) -17.02%
regression_4449 182,318 (-37,584) -17.09%
array_dynamic_blackbox_input 16,064 (-3,322) -17.14%
simple_bitwise 38 (-8) -17.39%
sha256_var_padding_regression 191,657 (-43,284) -18.42%
sha256_var_size_regression 14,578 (-3,776) -20.57%
2_div 30 (-8) -21.05%
sha256_regression 99,386 (-26,967) -21.34%
3_add 29 (-8) -21.62%
brillig_cow_regression 449,996 (-133,777) -22.92%
ram_blowup_regression 674,649 (-203,013) -23.13%
loop 105 (-32) -23.36%
brillig_loop 148 (-48) -24.49%
1_mul 26 (-24) -48.00%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant