Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sha256): Perform compression per block and utilize ROM instead of…
… RAM when setting up the message block (#5760) # Description ## Problem\* Resolves #5761 Resolution to performance blow-up found with sha256_var. ## Summary\* ### Issue The crux of the blow-up was the result of calling `sha256_compression` inside of the same loop where we build the message block. In the current `sha256_var` algorithm we are looping over the entire message and conditionally checking a msg byte pointer (the pointer into the msg block) to determine whether we have filled up a msg block and should run the sha compression. However, in a circuit this leads to us calling the compression opcode `N` times where `N` is the size of the message. We also were utilize RAM to build our message block when we do not have to do so. We can instead construct our block outside of the circuit and verify that the block has been constructed as we expect with assertion that just require ROM. ### Improvements This PR produces a ~16x improvement in ACIR opcodes a >13x improvement in backend constraints for the following circuit: ```rust fn main(foo: [u8; 95], toggle: bool) { let size: Field = 93 + toggle as Field * 2; let hash = std::sha256::sha256_var(foo, size as u64); println(f"{hash}"); } ``` #### master nargo info: ``` +---------+----------------------------+----------------------+--------------+-----------------+ | Package | Function | Expression Width | ACIR Opcodes | Brillig Opcodes | +---------+----------------------------+----------------------+--------------+-----------------+ | sha256 | main | Bounded { width: 4 } | 125852 | 243 | +---------+----------------------------+----------------------+--------------+-----------------+ | sha256 | print_unconstrained | N/A | N/A | 230 | +---------+----------------------------+----------------------+--------------+-----------------+ | sha256 | directive_integer_quotient | N/A | N/A | 6 | +---------+----------------------------+----------------------+--------------+-----------------+ | sha256 | directive_invert | N/A | N/A | 7 | +---------+----------------------------+----------------------+--------------+-----------------+ ``` bb gates: ``` {"functions": [ { "acir_opcodes": 125852, "circuit_size": 597646, ``` #### This PR Output of nargo info: ``` +----------------------------+----------------------------+----------------------+--------------+-----------------+ | Package | Function | Expression Width | ACIR Opcodes | Brillig Opcodes | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | main | Bounded { width: 4 } | 7768 | 1041 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | build_msg_block_iter | N/A | N/A | 299 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | pad_msg_block | N/A | N/A | 201 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | attach_len_to_msg_block | N/A | N/A | 298 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | print_unconstrained | N/A | N/A | 230 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | directive_integer_quotient | N/A | N/A | 6 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ | sha256_var_size_regression | directive_invert | N/A | N/A | 7 | +----------------------------+----------------------------+----------------------+--------------+-----------------+ ``` bb gates output: ``` {"functions": [ { "acir_opcodes": 7768, "circuit_size": 44663, ``` ## Additional Context ## 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](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information