Skip to content

Commit a4fdb2a

Browse files
committed
init_graph
1 parent 57d8b62 commit a4fdb2a

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/lib.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ pub struct HashSignalInfo {
1818
pub signalsize: u64,
1919
}
2020

21+
pub struct Graph {
22+
pub nodes: Vec<Node>,
23+
pub signals: Vec<usize>,
24+
pub input_mapping: Vec<HashSignalInfo>,
25+
}
26+
2127
fn fnv1a(s: &str) -> u64 {
2228
let mut hash: u64 = 0xCBF29CE484222325;
2329
for c in s.bytes() {
@@ -39,18 +45,27 @@ fn set_input_signal_eval(
3945
signal_values[si] = val;
4046
}
4147

48+
/// Loads the graph from bytes
49+
pub fn init_graph(graph_bytes: &[u8]) -> eyre::Result<Graph> {
50+
let (nodes, signals, input_mapping): (Vec<Node>, Vec<usize>, Vec<HashSignalInfo>) =
51+
postcard::from_bytes(graph_bytes)?;
52+
53+
Ok(Graph {
54+
nodes,
55+
signals,
56+
input_mapping,
57+
})
58+
}
59+
4260
/// Calculate witness based on serialized graph and inputs
4361
pub fn calculate_witness(
4462
input_list: HashMap<String, Vec<U256>>,
45-
graph_bytes: &[u8],
63+
graph: &Graph,
4664
) -> eyre::Result<Vec<U256>> {
47-
let (nodes, signals, input_mapping): (Vec<Node>, Vec<usize>, Vec<HashSignalInfo>) =
48-
postcard::from_bytes(graph_bytes)?;
49-
5065
// Calculate number of inputs from graph
5166
let mut start = false;
5267
let mut max_index = 0usize;
53-
for &node in nodes.iter() {
68+
for &node in graph.nodes.iter() {
5469
if let Node::Input(i) = node {
5570
if i > max_index {
5671
max_index = i;
@@ -69,12 +84,12 @@ pub fn calculate_witness(
6984
for (key, value) in input_list {
7085
let h = fnv1a(key.as_str());
7186
for (idx, item) in value.into_iter().enumerate() {
72-
set_input_signal_eval(input_mapping.clone(), &mut inputs, h, idx as u64, item);
87+
set_input_signal_eval(graph.input_mapping.clone(), &mut inputs, h, idx as u64, item);
7388
}
7489
}
7590

7691
// Calculate witness
77-
let witness = graph::evaluate(&nodes, &inputs, &signals);
92+
let witness = graph::evaluate(&graph.nodes, &inputs, &graph.signals);
7893

7994
Ok(witness)
8095
}

0 commit comments

Comments
 (0)