remainder/
circuit_layout.rs1use std::collections::{hash_map::Entry, HashMap};
4use std::fmt::Debug;
5use std::hash::Hash;
6
7use serde::{Deserialize, Serialize};
8use shared_types::Field;
9
10use crate::{
11 layer::LayerId,
12 mle::{dense::DenseMle, evals::MultilinearExtension, mle_description::MleDescription},
13};
14
15use anyhow::{anyhow, Result};
16
17#[derive(Debug)]
20pub struct CircuitEvalMap<F: Field>(pub(crate) HashMap<CircuitLocation, MultilinearExtension<F>>);
21pub type LayerMap<F> = HashMap<LayerId, Vec<DenseMle<F>>>;
25
26impl<F: Field> CircuitEvalMap<F> {
27 pub fn new() -> Self {
30 Self(HashMap::new())
31 }
32
33 pub fn get_data_from_circuit_mle(
36 &self,
37 circuit_mle: &MleDescription<F>,
38 ) -> Result<&MultilinearExtension<F>> {
39 let circuit_location =
40 CircuitLocation::new(circuit_mle.layer_id(), circuit_mle.prefix_bits());
41 let result = self
42 .0
43 .get(&circuit_location)
44 .ok_or(anyhow!("Circuit location not found!"));
45 if let Ok(actual_result) = result {
46 assert_eq!(actual_result.num_vars(), circuit_mle.num_free_vars());
47 }
48 result
49 }
50
51 pub fn add_node(&mut self, circuit_location: CircuitLocation, value: MultilinearExtension<F>) {
53 self.0.insert(circuit_location, value);
54 }
55
56 pub fn convert_to_layer_map(mut self) -> LayerMap<F> {
61 let mut layer_map = HashMap::<LayerId, Vec<DenseMle<F>>>::new();
62 self.0.drain().for_each(|(circuit_location, data)| {
63 let corresponding_mle = DenseMle::new_with_prefix_bits(
64 data,
65 circuit_location.layer_id,
66 circuit_location.prefix_bits,
67 );
68 if let Entry::Vacant(e) = layer_map.entry(circuit_location.layer_id) {
69 e.insert(vec![corresponding_mle]);
70 } else {
71 layer_map
72 .get_mut(&circuit_location.layer_id)
73 .unwrap()
74 .push(corresponding_mle);
75 }
76 });
77 layer_map
78 }
79}
80
81impl<F: Field> Default for CircuitEvalMap<F> {
82 fn default() -> Self {
83 Self::new()
84 }
85}
86
87#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
89pub struct CircuitLocation {
90 pub layer_id: LayerId,
92 pub prefix_bits: Vec<bool>,
95}
96
97impl CircuitLocation {
98 pub fn new(layer_id: LayerId, prefix_bits: Vec<bool>) -> Self {
100 Self {
101 layer_id,
102 prefix_bits,
103 }
104 }
105}