Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code dupe for WASM env #17

Open
nalinbhardwaj opened this issue May 18, 2023 · 0 comments
Open

Fix code dupe for WASM env #17

nalinbhardwaj opened this issue May 18, 2023 · 0 comments

Comments

@nalinbhardwaj
Copy link
Owner

nalinbhardwaj commented May 18, 2023

This entire function is duped and can be deduped with some compile-time conditionals inside the function:

Nova-Scotia/src/lib.rs

Lines 149 to 238 in 1bc7e39

#[cfg(target_family = "wasm")]
pub async fn create_recursive_circuit(
witness_generator_file: FileLocation,
r1cs: R1CS<F1>,
private_inputs: Vec<HashMap<String, Value>>,
start_public_input: Vec<F1>,
pp: &PublicParams<G1, G2, C1, C2>,
) -> Result<RecursiveSNARK<G1, G2, C1, C2>, std::io::Error> {
let iteration_count = private_inputs.len();
let mut circuit_iterations = Vec::with_capacity(iteration_count);
let start_public_input_hex = start_public_input
.iter()
.map(|&x| format!("{:?}", x).strip_prefix("0x").unwrap().to_string())
.collect::<Vec<String>>();
let mut current_public_input = start_public_input_hex.clone();
for i in 0..iteration_count {
let decimal_stringified_input: Vec<String> = current_public_input
.iter()
.map(|x| BigInt::from_str_radix(x, 16).unwrap().to_str_radix(10))
.collect();
let input = CircomInput {
step_in: decimal_stringified_input.clone(),
extra: private_inputs[i].clone(),
};
let input_json = serde_json::to_string(&input).unwrap();
let is_wasm = match &witness_generator_file {
FileLocation::PathBuf(path) => path.extension().unwrap_or_default() == "wasm",
FileLocation::URL(_) => true,
};
let witness = if is_wasm {
generate_witness_from_wasm::<<G1 as Group>::Scalar>(
&witness_generator_file,
&input_json,
Path::new(""),
)
.await
} else {
let witness_generator_file = match &witness_generator_file {
FileLocation::PathBuf(path) => path,
FileLocation::URL(_) => panic!("unreachable"),
};
generate_witness_from_bin::<<G1 as Group>::Scalar>(
&witness_generator_file,
&input_json,
Path::new(""),
)
};
let circuit = CircomCircuit {
r1cs: r1cs.clone(),
witness: Some(witness),
};
let current_public_output = circuit.get_public_outputs();
circuit_iterations.push(circuit);
current_public_input = current_public_output
.iter()
.map(|&x| format!("{:?}", x).strip_prefix("0x").unwrap().to_string())
.collect();
}
let circuit_secondary = TrivialTestCircuit::default();
let mut recursive_snark: Option<RecursiveSNARK<G1, G2, C1, C2>> = None;
let z0_secondary = vec![<G2 as Group>::Scalar::zero()];
for i in 0..iteration_count {
let res = RecursiveSNARK::prove_step(
&pp,
recursive_snark,
circuit_iterations[i].clone(),
circuit_secondary.clone(),
start_public_input.clone(),
z0_secondary.clone(),
);
assert!(res.is_ok());
recursive_snark = Some(res.unwrap());
}
let recursive_snark = recursive_snark.unwrap();
Ok(recursive_snark)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant