Skip to content

Commit

Permalink
init_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
philsippl committed Nov 18, 2023
1 parent 57d8b62 commit a4fdb2a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub struct HashSignalInfo {
pub signalsize: u64,
}

pub struct Graph {
pub nodes: Vec<Node>,
pub signals: Vec<usize>,
pub input_mapping: Vec<HashSignalInfo>,
}

fn fnv1a(s: &str) -> u64 {
let mut hash: u64 = 0xCBF29CE484222325;
for c in s.bytes() {
Expand All @@ -39,18 +45,27 @@ fn set_input_signal_eval(
signal_values[si] = val;
}

/// Loads the graph from bytes
pub fn init_graph(graph_bytes: &[u8]) -> eyre::Result<Graph> {
let (nodes, signals, input_mapping): (Vec<Node>, Vec<usize>, Vec<HashSignalInfo>) =
postcard::from_bytes(graph_bytes)?;

Ok(Graph {
nodes,
signals,
input_mapping,
})
}

/// Calculate witness based on serialized graph and inputs
pub fn calculate_witness(
input_list: HashMap<String, Vec<U256>>,
graph_bytes: &[u8],
graph: &Graph,
) -> eyre::Result<Vec<U256>> {
let (nodes, signals, input_mapping): (Vec<Node>, Vec<usize>, Vec<HashSignalInfo>) =
postcard::from_bytes(graph_bytes)?;

// Calculate number of inputs from graph
let mut start = false;
let mut max_index = 0usize;
for &node in nodes.iter() {
for &node in graph.nodes.iter() {
if let Node::Input(i) = node {
if i > max_index {
max_index = i;
Expand All @@ -69,12 +84,12 @@ pub fn calculate_witness(
for (key, value) in input_list {
let h = fnv1a(key.as_str());
for (idx, item) in value.into_iter().enumerate() {
set_input_signal_eval(input_mapping.clone(), &mut inputs, h, idx as u64, item);
set_input_signal_eval(graph.input_mapping.clone(), &mut inputs, h, idx as u64, item);
}
}

// Calculate witness
let witness = graph::evaluate(&nodes, &inputs, &signals);
let witness = graph::evaluate(&graph.nodes, &inputs, &graph.signals);

Ok(witness)
}

0 comments on commit a4fdb2a

Please sign in to comment.