-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce30b43
commit dabc9e4
Showing
8 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub fn set_panic_hook() { | ||
// When the `console_error_panic_hook` feature is enabled, we can call the | ||
// `set_panic_hook` function at least once during initialization, and then | ||
// we will get better error messages if our code ever panics. | ||
// | ||
// For more details see | ||
// https://github.com/rustwasm/console_error_panic_hook#readme | ||
#[cfg(feature = "console_error_panic_hook")] | ||
console_error_panic_hook::set_once(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::utils; | ||
use std::collections::HashMap; | ||
|
||
use plonk_fibonacci; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use serde_wasm_bindgen::{from_value, to_value}; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
// fn alert(s: &str); | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn generate_proof( | ||
srs_key: &[u8], | ||
proving_key: &[u8], | ||
input: JsValue, | ||
) -> Result<JsValue, JsValue> { | ||
let input: HashMap<String, Vec<String>> = from_value(input) | ||
.map_err(|e| JsValue::from_str(&format!("Failed to parse input: {}", e)))?; | ||
|
||
// Generate proof | ||
let (proof, public_input) = plonk_fibonacci::prove(srs_key, proving_key, input) | ||
.map_err(|e| JsValue::from_str(&format!("Proof generation failed: {}", e)))?; | ||
|
||
// Serialize the output back into JsValue | ||
to_value(&(proof, public_input)) | ||
.map_err(|e| JsValue::from_str(&format!("Serialization failed: {}", e))) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn verify_proof( | ||
srs_key: &[u8], | ||
verifying_key: &[u8], | ||
proof: JsValue, | ||
public_inputs: JsValue, | ||
) -> Result<JsValue, JsValue> { | ||
let proof: Vec<u8> = from_value(proof) | ||
.map_err(|e| JsValue::from_str(&format!("Failed to parse proof: {}", e)))?; | ||
let public_inputs: Vec<u8> = from_value(public_inputs) | ||
.map_err(|e| JsValue::from_str(&format!("Failed to parse public_inputs: {}", e)))?; | ||
|
||
// Verify proof | ||
let is_valid = plonk_fibonacci::verify(srs_key, verifying_key, proof, public_inputs) | ||
.map_err(|e| JsValue::from_str(&format!("Proof verification failed: {}", e)))?; | ||
|
||
// Convert result to JsValue | ||
to_value(&is_valid).map_err(|e| JsValue::from_str(&format!("Serialization failed: {}", e))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { generate_proof, verify_proof } from "./pkg/mopro_ffi.js"; | ||
import fs from "fs"; | ||
// Function to convert file content at a path to Uint8Array | ||
function pathToUint8Array(filePath) { | ||
const buffer = fs.readFileSync(filePath); // Read the file as a Buffer | ||
return new Uint8Array(buffer); // Convert the Buffer to a Uint8Array | ||
} | ||
|
||
async function run() { | ||
const srs = "../test-vectors/halo2/plonk_fibonacci_srs.bin"; | ||
const pk = "../test-vectors/halo2/plonk_fibonacci_pk.bin"; | ||
const vk = "../test-vectors/halo2/plonk_fibonacci_vk.bin"; | ||
const input = { | ||
out: ["55"], | ||
}; | ||
const start = Date.now(); | ||
const output = generate_proof( | ||
pathToUint8Array(srs), | ||
pathToUint8Array(pk), | ||
input | ||
); | ||
const end = Date.now(); | ||
console.log(`Proof time: ${end - start} ms`); | ||
const proof = output[0]; | ||
const publicInputs = output[1]; | ||
|
||
const valid = verify_proof( | ||
pathToUint8Array(srs), | ||
pathToUint8Array(vk), | ||
proof, | ||
publicInputs | ||
); | ||
|
||
return valid; | ||
} | ||
|
||
run().then((t) => console.log(t)); |