Skip to content

Commit

Permalink
OPTIMIZER_REPORTS_NODES
Browse files Browse the repository at this point in the history
  • Loading branch information
rudy-6-4 committed Apr 3, 2024
1 parent 7e8d75c commit 14c3890
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ fn convert_to_circuit_solution(sol: &ffi::DagSolution, dag: &OperationDag) -> ff
global_p_error: sol.global_p_error,
is_feasible,
error_msg,
error_nodes: vec![],
}
}

Expand All @@ -332,6 +333,7 @@ impl From<CircuitSolution> for ffi::CircuitSolution {
global_p_error: v.global_p_error,
is_feasible: v.is_feasible,
error_msg: v.error_msg,
error_nodes: v.error_nodes,
}
}
}
Expand Down Expand Up @@ -950,6 +952,7 @@ mod ffi {
pub global_p_error: f64,
pub is_feasible: bool,
pub error_msg: String,
pub error_nodes: Vec<usize>,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ struct CircuitSolution final {
double global_p_error;
bool is_feasible;
::rust::String error_msg;
::rust::Vec<::std::size_t> error_nodes;

::rust::String dump() const noexcept;
::rust::String short_dump() const noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ struct CircuitSolution final {
double global_p_error;
bool is_feasible;
::rust::String error_msg;
::rust::Vec<::std::size_t> error_nodes;

::rust::String dump() const noexcept;
::rust::String short_dump() const noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ fn variance_constraint(
nb_constraints,
safe_variance_bound,
variance,
nodes: vec![op_i],
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Feasible {
}
let compress = |c: &VarianceConstraint| VarianceConstraint {
variance: c.variance.compress(&detect_used),
..(*c)
..c.clone()
};
let constraints = self.constraints.iter().map(compress).collect();
let undominated_constraints = self.undominated_constraints.iter().map(compress).collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,18 @@ pub struct CircuitSolution {
pub crt_decomposition: Vec<u64>, // empty in native case
pub is_feasible: bool,
pub error_msg: String,
pub error_nodes: Vec<usize>
}

impl CircuitSolution {
pub fn no_solution(error_msg: impl Into<String>) -> Self {
pub fn no_solution(error_msg: impl Into<String>, error_nodes: Vec<usize>) -> Self {
Self {
is_feasible: false,
complexity: f64::INFINITY,
p_error: 1.0,
global_p_error: 1.0,
error_msg: error_msg.into(),
error_nodes: error_nodes,
..Self::default()
}
}
Expand Down Expand Up @@ -216,6 +218,7 @@ impl CircuitSolution {
crt_decomposition: sol.crt_decomposition,
is_feasible: true,
error_msg,
error_nodes: vec![],
}
}

Expand Down Expand Up @@ -260,6 +263,7 @@ impl CircuitSolution {
global_p_error: sol.global_p_error,
is_feasible,
error_msg,
error_nodes: vec![],
};
}
let small_key = SecretLweKey {
Expand Down Expand Up @@ -315,6 +319,7 @@ impl CircuitSolution {
global_p_error: sol.global_p_error,
is_feasible,
error_msg,
error_nodes: vec![],
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct Parameters {
pub p_error: f64,
pub global_p_error: f64,
pub complexity: f64,
pub worst_nodes: Vec<usize>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -778,6 +779,8 @@ fn optimize_macro(
continue;
}
best_partition_p_error = partition_p_error;
// retain non satisfied constraint
let (_, _, worst_constraint) = feasible.worst_constraint(&operations.variance);
let p_error = feasible.p_error(&operations.variance);
let global_p_error = feasible.global_p_error(&operations.variance);
let mut pbs = init_parameters.micro_params.pbs.clone();
Expand All @@ -795,6 +798,7 @@ fn optimize_macro(
macro_params,
is_lower_bound: true,
is_feasible: false,
worst_nodes: worst_constraint.nodes.clone(),
};
continue;
}
Expand Down Expand Up @@ -880,6 +884,7 @@ fn optimize_macro(
macro_params,
is_lower_bound,
is_feasible: true,
worst_nodes: vec![],
};
} else {
// the macro parameters are feasible
Expand Down Expand Up @@ -941,6 +946,7 @@ pub fn optimize(
p_error: 1.0,
global_p_error: 1.0,
complexity: f64::INFINITY,
worst_nodes: vec![],
};

let mut params = init_parameters;
Expand Down Expand Up @@ -973,7 +979,7 @@ pub fn optimize(
params = new_params;
if !params.is_feasible {
if nb_partitions == 1 {
return Err(NoParametersFound);
return Err(NoParametersFound(params.worst_nodes));
}
if DEBUG {
eprintln!(
Expand Down Expand Up @@ -1021,7 +1027,7 @@ pub fn optimize(
fix_point = params.clone();
}
if best_params.is_none() {
return Err(NoParametersFound);
return Err(NoParametersFound(params.worst_nodes));
}
let best_params = best_params.unwrap();
sanity_check(
Expand Down Expand Up @@ -1167,14 +1173,16 @@ pub fn optimize_to_circuit_solution(
if config.composable {
return keys_spec::CircuitSolution::no_solution(
NotComposable("No luts in the circuit.".into()).to_string(),
vec![],
);
}
let nb_instr = dag.operators.len();
if let Some(sol) = optimize_mono(dag, config, search_space, persistent_caches).best_solution
{
return keys_spec::CircuitSolution::from_native_solution(sol, nb_instr);
}
return keys_spec::CircuitSolution::no_solution(NoParametersFound.to_string());
let err = NoParametersFound(vec![]);
return keys_spec::CircuitSolution::no_solution(err.to_string(), err.error_nodes());
}
let default_partition = 0;
let dag_and_params = optimize(
Expand All @@ -1187,7 +1195,7 @@ pub fn optimize_to_circuit_solution(
);
#[allow(clippy::option_if_let_else)]
match dag_and_params {
Err(e) => keys_spec::CircuitSolution::no_solution(e.to_string()),
Err(e) => keys_spec::CircuitSolution::no_solution(e.to_string(), e.error_nodes()),
Ok((dag, params)) => {
let ext_keys = keys_spec::ExpandedCircuitKeys::of(&params);
let instructions_keys = analyze::original_instrs_partition(&dag, &ext_keys);
Expand All @@ -1209,6 +1217,7 @@ pub fn optimize_to_circuit_solution(
global_p_error: params.global_p_error,
is_feasible: true,
error_msg: String::default(),
error_nodes: vec![],
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn crt_optimize(
if analyze::has_round(dag) || analyze::has_unsafe_cast(dag) {
return CircuitSolution::no_solution(
"Crt does not support round/reinterpret_precision operator",
vec![], // TODO: report round and reinterpret
);
} // TODO: dag to params
let max_precision = max_precision(dag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct VarianceConstraint {
pub nb_constraints: u64,
pub safe_variance_bound: f64,
pub variance: SymbolicVariance,
pub nodes: Vec<usize>,
}

impl fmt::Display for VarianceConstraint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ pub mod wop_atomic_pattern;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Err {
NotComposable(String),
NoParametersFound,
NoParametersFound(Vec<usize>),
}

impl Err {
fn error_nodes(&self) -> Vec<usize> {
match self {
Self::NotComposable(_details) => vec![],
Self::NoParametersFound(nodes) => nodes.clone(),
}
}
}

impl std::fmt::Display for Err {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::NotComposable(details) => write!(f, "Program can not be composed: {details}"),
Self::NoParametersFound => write!(f, "No crypto parameters could be found"),
Self::NoParametersFound(_) => write!(f, "No crypto parameters could be found"),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ pub fn optimize_to_circuit_solution(
let Ok(coprimes) = crt_decomposition::default_coprimes(precision as Precision) else {
return keys_spec::CircuitSolution::no_solution(
"Crt decomposition is not unknown for {precision}:bits",
vec![],
);
};
let n_functions = 1;
Expand All @@ -569,7 +570,7 @@ pub fn optimize_to_circuit_solution(
} else {
keys_spec::CircuitSolution {
crt_decomposition: coprimes,
..keys_spec::CircuitSolution::no_solution("No crypto parameters")
..keys_spec::CircuitSolution::no_solution("No crypto parameters", vec![])
}
}
}

0 comments on commit 14c3890

Please sign in to comment.