|
| 1 | +use std::rc::Rc; |
| 2 | + |
| 3 | +use acvm::{acir::AcirField, BlackBoxFunctionSolver, BlackBoxResolutionError, FieldElement}; |
| 4 | +use iter_extended::vecmap; |
| 5 | + |
| 6 | +use crate::ssa::ir::{ |
| 7 | + dfg::DataFlowGraph, instruction::SimplifyResult, types::Type, value::ValueId, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::{array_is_constant, make_constant_array, to_u8_vec}; |
| 11 | + |
| 12 | +pub(super) fn simplify_ec_add( |
| 13 | + dfg: &mut DataFlowGraph, |
| 14 | + solver: impl BlackBoxFunctionSolver<FieldElement>, |
| 15 | + arguments: &[ValueId], |
| 16 | +) -> SimplifyResult { |
| 17 | + match ( |
| 18 | + dfg.get_numeric_constant(arguments[0]), |
| 19 | + dfg.get_numeric_constant(arguments[1]), |
| 20 | + dfg.get_numeric_constant(arguments[2]), |
| 21 | + dfg.get_numeric_constant(arguments[3]), |
| 22 | + dfg.get_numeric_constant(arguments[4]), |
| 23 | + dfg.get_numeric_constant(arguments[5]), |
| 24 | + ) { |
| 25 | + ( |
| 26 | + Some(point1_x), |
| 27 | + Some(point1_y), |
| 28 | + Some(point1_is_infinity), |
| 29 | + Some(point2_x), |
| 30 | + Some(point2_y), |
| 31 | + Some(point2_is_infinity), |
| 32 | + ) => { |
| 33 | + let Ok((result_x, result_y, result_is_infinity)) = solver.ec_add( |
| 34 | + &point1_x, |
| 35 | + &point1_y, |
| 36 | + &point1_is_infinity, |
| 37 | + &point2_x, |
| 38 | + &point2_y, |
| 39 | + &point2_is_infinity, |
| 40 | + ) else { |
| 41 | + return SimplifyResult::None; |
| 42 | + }; |
| 43 | + |
| 44 | + let result_x = dfg.make_constant(result_x, Type::field()); |
| 45 | + let result_y = dfg.make_constant(result_y, Type::field()); |
| 46 | + let result_is_infinity = dfg.make_constant(result_is_infinity, Type::bool()); |
| 47 | + |
| 48 | + let typ = Type::Array(Rc::new(vec![Type::field()]), 3); |
| 49 | + let result_array = |
| 50 | + dfg.make_array(im::vector![result_x, result_y, result_is_infinity], typ); |
| 51 | + |
| 52 | + SimplifyResult::SimplifiedTo(result_array) |
| 53 | + } |
| 54 | + _ => SimplifyResult::None, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +pub(super) fn simplify_poseidon2_permutation( |
| 59 | + dfg: &mut DataFlowGraph, |
| 60 | + solver: impl BlackBoxFunctionSolver<FieldElement>, |
| 61 | + arguments: &[ValueId], |
| 62 | +) -> SimplifyResult { |
| 63 | + match (dfg.get_array_constant(arguments[0]), dfg.get_numeric_constant(arguments[1])) { |
| 64 | + (Some((state, _)), Some(state_length)) if array_is_constant(dfg, &state) => { |
| 65 | + let state: Vec<FieldElement> = state |
| 66 | + .iter() |
| 67 | + .map(|id| { |
| 68 | + dfg.get_numeric_constant(*id) |
| 69 | + .expect("value id from array should point at constant") |
| 70 | + }) |
| 71 | + .collect(); |
| 72 | + |
| 73 | + let Some(state_length) = state_length.try_to_u32() else { |
| 74 | + return SimplifyResult::None; |
| 75 | + }; |
| 76 | + |
| 77 | + let Ok(new_state) = solver.poseidon2_permutation(&state, state_length) else { |
| 78 | + return SimplifyResult::None; |
| 79 | + }; |
| 80 | + |
| 81 | + let result_array = make_constant_array(dfg, new_state, Type::field()); |
| 82 | + |
| 83 | + SimplifyResult::SimplifiedTo(result_array) |
| 84 | + } |
| 85 | + _ => SimplifyResult::None, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +pub(super) fn simplify_schnorr_verify( |
| 90 | + dfg: &mut DataFlowGraph, |
| 91 | + solver: impl BlackBoxFunctionSolver<FieldElement>, |
| 92 | + arguments: &[ValueId], |
| 93 | +) -> SimplifyResult { |
| 94 | + match ( |
| 95 | + dfg.get_numeric_constant(arguments[0]), |
| 96 | + dfg.get_numeric_constant(arguments[1]), |
| 97 | + dfg.get_array_constant(arguments[2]), |
| 98 | + dfg.get_array_constant(arguments[3]), |
| 99 | + ) { |
| 100 | + (Some(public_key_x), Some(public_key_y), Some((signature, _)), Some((message, _))) |
| 101 | + if array_is_constant(dfg, &signature) && array_is_constant(dfg, &message) => |
| 102 | + { |
| 103 | + let signature = to_u8_vec(dfg, signature); |
| 104 | + let signature: [u8; 64] = |
| 105 | + signature.try_into().expect("Compiler should produce correctly sized signature"); |
| 106 | + |
| 107 | + let message = to_u8_vec(dfg, message); |
| 108 | + |
| 109 | + let Ok(valid_signature) = |
| 110 | + solver.schnorr_verify(&public_key_x, &public_key_y, &signature, &message) |
| 111 | + else { |
| 112 | + return SimplifyResult::None; |
| 113 | + }; |
| 114 | + |
| 115 | + let valid_signature = dfg.make_constant(valid_signature.into(), Type::bool()); |
| 116 | + SimplifyResult::SimplifiedTo(valid_signature) |
| 117 | + } |
| 118 | + _ => SimplifyResult::None, |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +pub(super) fn simplify_hash( |
| 123 | + dfg: &mut DataFlowGraph, |
| 124 | + arguments: &[ValueId], |
| 125 | + hash_function: fn(&[u8]) -> Result<[u8; 32], BlackBoxResolutionError>, |
| 126 | +) -> SimplifyResult { |
| 127 | + match dfg.get_array_constant(arguments[0]) { |
| 128 | + Some((input, _)) if array_is_constant(dfg, &input) => { |
| 129 | + let input_bytes: Vec<u8> = to_u8_vec(dfg, input); |
| 130 | + |
| 131 | + let hash = hash_function(&input_bytes) |
| 132 | + .expect("Rust solvable black box function should not fail"); |
| 133 | + |
| 134 | + let hash_values = vecmap(hash, |byte| FieldElement::from_be_bytes_reduce(&[byte])); |
| 135 | + |
| 136 | + let result_array = make_constant_array(dfg, hash_values, Type::unsigned(8)); |
| 137 | + SimplifyResult::SimplifiedTo(result_array) |
| 138 | + } |
| 139 | + _ => SimplifyResult::None, |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +type ECDSASignatureVerifier = fn( |
| 144 | + hashed_msg: &[u8], |
| 145 | + public_key_x: &[u8; 32], |
| 146 | + public_key_y: &[u8; 32], |
| 147 | + signature: &[u8; 64], |
| 148 | +) -> Result<bool, BlackBoxResolutionError>; |
| 149 | + |
| 150 | +pub(super) fn simplify_signature( |
| 151 | + dfg: &mut DataFlowGraph, |
| 152 | + arguments: &[ValueId], |
| 153 | + signature_verifier: ECDSASignatureVerifier, |
| 154 | +) -> SimplifyResult { |
| 155 | + match ( |
| 156 | + dfg.get_array_constant(arguments[0]), |
| 157 | + dfg.get_array_constant(arguments[1]), |
| 158 | + dfg.get_array_constant(arguments[2]), |
| 159 | + dfg.get_array_constant(arguments[3]), |
| 160 | + ) { |
| 161 | + ( |
| 162 | + Some((public_key_x, _)), |
| 163 | + Some((public_key_y, _)), |
| 164 | + Some((signature, _)), |
| 165 | + Some((hashed_message, _)), |
| 166 | + ) if array_is_constant(dfg, &public_key_x) |
| 167 | + && array_is_constant(dfg, &public_key_y) |
| 168 | + && array_is_constant(dfg, &signature) |
| 169 | + && array_is_constant(dfg, &hashed_message) => |
| 170 | + { |
| 171 | + let public_key_x: [u8; 32] = to_u8_vec(dfg, public_key_x) |
| 172 | + .try_into() |
| 173 | + .expect("ECDSA public key fields are 32 bytes"); |
| 174 | + let public_key_y: [u8; 32] = to_u8_vec(dfg, public_key_y) |
| 175 | + .try_into() |
| 176 | + .expect("ECDSA public key fields are 32 bytes"); |
| 177 | + let signature: [u8; 64] = |
| 178 | + to_u8_vec(dfg, signature).try_into().expect("ECDSA signatures are 64 bytes"); |
| 179 | + let hashed_message: Vec<u8> = to_u8_vec(dfg, hashed_message); |
| 180 | + |
| 181 | + let valid_signature = |
| 182 | + signature_verifier(&hashed_message, &public_key_x, &public_key_y, &signature) |
| 183 | + .expect("Rust solvable black box function should not fail"); |
| 184 | + |
| 185 | + let valid_signature = dfg.make_constant(valid_signature.into(), Type::bool()); |
| 186 | + SimplifyResult::SimplifiedTo(valid_signature) |
| 187 | + } |
| 188 | + _ => SimplifyResult::None, |
| 189 | + } |
| 190 | +} |
0 commit comments