Merge pull request #1131 from Zokrates/format-proof
Add proof formatting utility to zokrates-js
This commit is contained in:
commit
63a2fc864f
5 changed files with 81 additions and 3 deletions
1
changelogs/unreleased/1131-dark64
Normal file
1
changelogs/unreleased/1131-dark64
Normal file
|
@ -0,0 +1 @@
|
|||
Add proof formatting utility to zokrates-js
|
|
@ -115,8 +115,10 @@ fn cli_print_proof<T: SolidityCompatibleField, S: SolidityCompatibleScheme<T>>(
|
|||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
);
|
||||
print!(",");
|
||||
print!("{}", inputs);
|
||||
if !proof.inputs.is_empty() {
|
||||
print!(",");
|
||||
print!("{}", inputs);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
_ => unreachable!(),
|
||||
|
|
3
zokrates_js/index.d.ts
vendored
3
zokrates_js/index.d.ts
vendored
|
@ -76,6 +76,9 @@ declare module "zokrates-js" {
|
|||
): Proof;
|
||||
verify(verificationKey: VerificationKey, proof: Proof): boolean;
|
||||
exportSolidityVerifier(verificationKey: VerificationKey): string;
|
||||
utils: {
|
||||
formatProof(proof: Proof): any[];
|
||||
}
|
||||
}
|
||||
|
||||
export interface Metadata {
|
||||
|
|
|
@ -18,7 +18,7 @@ use zokrates_core::proof_system::ark::Ark;
|
|||
use zokrates_core::proof_system::groth16::G16;
|
||||
use zokrates_core::proof_system::{
|
||||
Backend, Marlin, NonUniversalBackend, NonUniversalScheme, Proof, Scheme,
|
||||
SolidityCompatibleScheme, UniversalBackend, UniversalScheme, GM17,
|
||||
SolidityCompatibleField, SolidityCompatibleScheme, UniversalBackend, UniversalScheme, GM17,
|
||||
};
|
||||
use zokrates_core::typed_absy::abi::Abi;
|
||||
use zokrates_core::typed_absy::types::{ConcreteSignature, ConcreteType};
|
||||
|
@ -237,6 +237,34 @@ mod internal {
|
|||
let result = B::verify(vk, proof);
|
||||
Ok(JsValue::from_serde(&result).unwrap())
|
||||
}
|
||||
|
||||
pub fn format_proof<T: SolidityCompatibleField, S: SolidityCompatibleScheme<T>>(
|
||||
proof: JsValue,
|
||||
) -> Result<JsValue, JsValue> {
|
||||
use serde_json::json;
|
||||
|
||||
let proof: Proof<T, S> = proof
|
||||
.into_serde()
|
||||
.map_err(|err| JsValue::from_str(&format!("{}", err)))?;
|
||||
|
||||
let res = S::Proof::from(proof.proof);
|
||||
|
||||
let proof_object = serde_json::to_value(&res).unwrap();
|
||||
let proof_vec = proof_object
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(_, value)| value)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let result = if !proof.inputs.is_empty() {
|
||||
json!([proof_vec, proof.inputs])
|
||||
} else {
|
||||
json!([proof_vec])
|
||||
};
|
||||
|
||||
Ok(JsValue::from_serde(&result).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
@ -307,6 +335,11 @@ pub fn export_solidity_verifier(vk: JsValue, options: JsValue) -> Result<JsValue
|
|||
>>::export_solidity_verifier(
|
||||
vk.into_serde().unwrap()
|
||||
)),
|
||||
(CurveParameter::Bn128, SchemeParameter::MARLIN) => Ok(
|
||||
<Marlin as SolidityCompatibleScheme<Bn128Field>>::export_solidity_verifier(
|
||||
vk.into_serde().unwrap(),
|
||||
),
|
||||
),
|
||||
_ => Err(JsValue::from_str("Not supported")),
|
||||
}?;
|
||||
|
||||
|
@ -493,6 +526,37 @@ pub fn verify(vk: JsValue, proof: JsValue, options: JsValue) -> Result<JsValue,
|
|||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn format_proof(proof: JsValue, options: JsValue) -> Result<JsValue, JsValue> {
|
||||
let options: serde_json::Value = options.into_serde().unwrap();
|
||||
let curve = CurveParameter::try_from(
|
||||
options["curve"]
|
||||
.as_str()
|
||||
.ok_or_else(|| JsValue::from_str("Invalid options: missing field `curve`"))?,
|
||||
)
|
||||
.map_err(|e| JsValue::from_str(&e))?;
|
||||
|
||||
let scheme = SchemeParameter::try_from(
|
||||
options["scheme"]
|
||||
.as_str()
|
||||
.ok_or_else(|| JsValue::from_str("Invalid options: missing field `scheme`"))?,
|
||||
)
|
||||
.map_err(|e| JsValue::from_str(&e))?;
|
||||
|
||||
match (curve, scheme) {
|
||||
(CurveParameter::Bn128, SchemeParameter::G16) => {
|
||||
internal::format_proof::<Bn128Field, G16>(proof)
|
||||
}
|
||||
(CurveParameter::Bn128, SchemeParameter::GM17) => {
|
||||
internal::format_proof::<Bn128Field, GM17>(proof)
|
||||
}
|
||||
(CurveParameter::Bn128, SchemeParameter::MARLIN) => {
|
||||
internal::format_proof::<Bn128Field, Marlin>(proof)
|
||||
}
|
||||
_ => Err(JsValue::from_str("Unsupported options")),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn main_js() -> Result<(), JsValue> {
|
||||
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||
|
|
|
@ -80,6 +80,11 @@ module.exports = (dep) => {
|
|||
exportSolidityVerifier: (vk, options) => {
|
||||
return zokrates.export_solidity_verifier(vk, options);
|
||||
},
|
||||
utils: {
|
||||
formatProof: (proof, options) => {
|
||||
return zokrates.format_proof(proof, options);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const withOptions = (options) => {
|
||||
|
@ -102,6 +107,9 @@ module.exports = (dep) => {
|
|||
verify: (vk, proof) => defaultProvider.verify(vk, proof, options),
|
||||
exportSolidityVerifier: (vk) =>
|
||||
defaultProvider.exportSolidityVerifier(vk, options),
|
||||
utils: {
|
||||
formatProof: (proof) => defaultProvider.utils.formatProof(proof, options),
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue