Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

feat(stdlib): Add fallback implementation of SHA256 black box function #407

Merged
merged 45 commits into from
Jul 11, 2023
Merged
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
db1433a
initial
Ethan-000 Jun 22, 2023
b97e105
impl padding
Ethan-000 Jun 22, 2023
ed1596b
fix bytes
Ethan-000 Jun 24, 2023
fbba92b
padding
Ethan-000 Jun 28, 2023
2f01ea2
sha256u32
Ethan-000 Jun 29, 2023
32612da
unfinished
Ethan-000 Jul 1, 2023
c574ad1
.
Ethan-000 Jul 2, 2023
4c4e1df
Merge branch 'master' into sha256_fallback
Ethan-000 Jul 2, 2023
52ca59d
clippy
Ethan-000 Jul 2, 2023
868832b
.
Ethan-000 Jul 2, 2023
6b79031
.
Ethan-000 Jul 2, 2023
810b73b
.
Ethan-000 Jul 3, 2023
e24cbfc
.
Ethan-000 Jul 4, 2023
49b6b45
.
Ethan-000 Jul 4, 2023
1d51cf3
Merge branch 'master' into sha256_fallback
Ethan-000 Jul 4, 2023
895941d
cleanup
Ethan-000 Jul 5, 2023
9bc1fbd
Merge branch 'master' into sha256_fallback
Ethan-000 Jul 5, 2023
528e746
.
Ethan-000 Jul 5, 2023
ec66851
.
Ethan-000 Jul 5, 2023
4c011ac
.
Ethan-000 Jul 5, 2023
4ad60f1
.
Ethan-000 Jul 5, 2023
b6a7759
.
Ethan-000 Jul 5, 2023
79cbec1
.
Ethan-000 Jul 5, 2023
bba8196
.
Ethan-000 Jul 5, 2023
75ea62c
.
Ethan-000 Jul 5, 2023
eba6e5b
.
Ethan-000 Jul 5, 2023
09d59da
Merge branch 'noir-lang:master' into sha256_fallback
Ethan-000 Jul 8, 2023
0b1097e
Merge branch 'master' into sha256_fallback
Ethan-000 Jul 9, 2023
adf7a61
.
Ethan-000 Jul 9, 2023
6ccc6ca
change wu32 -> uint32
Ethan-000 Jul 10, 2023
0eef099
change visibility
Ethan-000 Jul 10, 2023
d35c79c
move logic fallbacks to blackbox_fallbacks folder
Ethan-000 Jul 10, 2023
a8253d9
remove split & add Copy to uint32
Ethan-000 Jul 10, 2023
cb92432
.
Ethan-000 Jul 10, 2023
1a90301
fix based on review
Ethan-000 Jul 10, 2023
e57e9ce
move bit_decomposition
Ethan-000 Jul 10, 2023
48f571c
.
Ethan-000 Jul 10, 2023
122816c
.
Ethan-000 Jul 10, 2023
4843037
.
Ethan-000 Jul 10, 2023
2a37f28
.
Ethan-000 Jul 10, 2023
87179b9
sha256 test
Ethan-000 Jul 10, 2023
682cdbf
remove genetated file
Ethan-000 Jul 10, 2023
acdcbc9
Merge branch 'master' into sha256_fallback
kevaundray Jul 11, 2023
22ae2f3
Merge branch 'master' into sha256_fallback
Ethan-000 Jul 11, 2023
e339999
update with master
Ethan-000 Jul 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
impl padding
Ethan-000 committed Jun 22, 2023
commit b97e10501e4dce9b0e82ae9bd26e26d94c30620a
52 changes: 37 additions & 15 deletions stdlib/src/custom_gate_fallbacks/sha256.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use acir::{
FieldElement,
};

use crate::helpers::VariableStore;
use crate::{fallback::range, helpers::VariableStore};

use super::utils::{radix_decomposition, round_to_nearest_byte};

@@ -15,35 +15,57 @@ pub fn sha256(
) -> (u32, Vec<Opcode>) {
let mut new_gates = Vec::new();

let mut calculate_total_bytes_exprs = Vec::new();
let mut variables = VariableStore::new(&mut num_witness);
let total_bytes_witness = variables.new_variable();
let mut total_bytes = Expression::default();
for (_, num_bits) in &inputs {
let num_bytes = round_to_nearest_byte(*num_bits);
total_bytes.push_addition_term(FieldElement::from(num_bytes as u128), total_bytes_witness);
}
calculate_total_bytes_exprs.push(Opcode::Arithmetic(total_bytes));
let mut num_witness = variables.finalize();

new_gates.extend(calculate_total_bytes_exprs);
let mut total_num_bytes = 0;

for (witness, num_bits) in &inputs {
let num_bytes = round_to_nearest_byte(*num_bits);
total_num_bytes += num_bytes;
let (extra_gates, _, updated_witness_counter) =
radix_decomposition(witness.clone(), num_bytes, 256, num_witness);
new_gates.extend(extra_gates);
num_witness = updated_witness_counter;
}

let output_bytes = create_sha256_constraint(inputs, total_bytes_witness, num_witness);
let output_bytes = create_sha256_constraint(inputs, total_num_bytes, num_witness);
(0, Vec::new())
}

fn create_sha256_constraint(
input: Vec<(Expression, u32)>,
total_bytes_witness: Witness,
total_num_bytes: u32,
mut num_witness: u32,
) {
let mut new_gates = Vec::new();
let mut variables = VariableStore::new(&mut num_witness);

let message_bits = total_num_bytes * 8;
let (num_witness, gates) = pad(128, 8, num_witness);
new_gates.extend(gates);

let bytes_per_block = 64;
let num_bytes = total_num_bytes + 8;
let num_blocks = num_bytes / bytes_per_block + ((num_bytes % bytes_per_block != 0) as u32);

let num_total_bytes = num_blocks * bytes_per_block;
for _ in num_bytes..num_total_bytes {
let (num_witness, gates) = pad(0, 8, num_witness);
new_gates.extend(gates);
}

let (num_witness, gates) = pad(message_bits, 64, num_witness);
}

fn pad(number: u32, size: u32, mut num_witness: u32) -> (u32, Vec<Opcode>) {
let mut new_gates = Vec::new();
let mut variables = VariableStore::new(&mut num_witness);

let pad = variables.new_variable();
let mut pad_expr = Expression::default();
pad_expr.push_addition_term(FieldElement::from(number as u128), pad);
new_gates.push(Opcode::Arithmetic(pad_expr.clone()));
let num_witness = variables.finalize();
let (num_witness, gates) = range(pad_expr, size, num_witness);
new_gates.extend(gates);

(num_witness, new_gates)
}