From 0c422ded80e3a2d1c8982e3e5bfe47d0c9a2c316 Mon Sep 17 00:00:00 2001 From: Theo Madzou Date: Sat, 21 Dec 2024 14:04:53 +0100 Subject: [PATCH] upgrade to Noir 1.0.0-beta.0 --- README.md | 14 ++---- lib/build.gradle.kts | 4 +- .../main/java/com/noirandroid/lib/Circuit.kt | 12 ++--- lib/src/main/java/com/noirandroid/lib/Noir.kt | 6 +-- lib/src/main/java/noir_java/Cargo.toml | 2 +- lib/src/main/java/noir_java/src/lib.rs | 44 +++++++------------ 6 files changed, 31 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 8355753..e50e34c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ maven { url 'https://jitpack.io' } Then, in your app level `build.gradle` file, add the following in your `dependencies` block: ```gradle -implementation("com.github.madztheo:noir_android:0.36.0-1") +implementation("com.github.madztheo:noir_android:1.0.0-beta.0-1") ``` After this your project should be able to use the library. @@ -64,13 +64,9 @@ inputs["a"] = 5 inputs["b"] = 3 inputs["result"] = 15 -// For UltraPlonk proofs -val proof: Proof = circuit.prove(inputs, "plonk") +val proof: Proof = circuit.prove(inputs) Log.d("Proof", proof.proof) Log.d("Verification key", proof.vk) - -// For Honk proofs -val proof: Proof = circuit.prove(inputs, "honk") ``` ### Verify a proof @@ -78,9 +74,5 @@ val proof: Proof = circuit.prove(inputs, "honk") To verify a proof, you can call the `verify` method and pass in the proof object and the proof type. It will return a boolean indicating whether the proof is valid or not. ```kotlin -// For UltraPlonk proofs -val isValid = circuit.verify(proof, "plonk") - -// For Honk proofs -val isValid = circuit.verify(proof, "honk") +val isValid = circuit.verify(proof) ``` diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 1932d7c..125bd7a 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -58,7 +58,7 @@ afterEvaluate { from(components["release"]) groupId = "com.github.madztheo" artifactId = "noir_android" - version = "v0.36.0-1" + version = "v1.0.0-beta.0-1" } } } @@ -114,7 +114,7 @@ tasks.register("copyRustLibs") { } else { // Download the .so file from the GitHub release download.run { - src("https://github.com/madztheo/noir_android/releases/download/v0.36.0-1/libnoir_java_arm64-v8a.so") + src("https://github.com/madztheo/noir_android/releases/download/v1.0.0-beta.0-1/libnoir_java_arm64-v8a.so") dest("src/main/jniLibs/arm64-v8a/libnoir_java.so") overwrite(false) } diff --git a/lib/src/main/java/com/noirandroid/lib/Circuit.kt b/lib/src/main/java/com/noirandroid/lib/Circuit.kt index bef95cb..38b4e1a 100644 --- a/lib/src/main/java/com/noirandroid/lib/Circuit.kt +++ b/lib/src/main/java/com/noirandroid/lib/Circuit.kt @@ -67,23 +67,23 @@ class Circuit(public val bytecode: String, public val manifest: CircuitManifest, System.loadLibrary("noir_java"); } - fun setupSrs(srs_path: String?) { - num_points = Noir.setup_srs(bytecode, srs_path) + fun setupSrs(srs_path: String?, recursive: Boolean?) { + num_points = Noir.setup_srs(bytecode, srs_path, recursive) } - fun prove(initialWitness: Map, proofType: String): Proof { + fun prove(initialWitness: Map, proofType: String?, recursive: Boolean?): Proof { if (num_points == 0) { throw IllegalArgumentException("SRS not set up") } val witness = generateWitnessMap(initialWitness, manifest.abi.parameters, 0) - return Noir.prove(bytecode, witness, proofType, num_points.toString()) + return Noir.prove(bytecode, witness, proofType ?: "honk", recursive) } - fun verify(proof: Proof, proofType: String): Boolean { + fun verify(proof: Proof, proofType: String?): Boolean { if (num_points == 0) { throw IllegalArgumentException("SRS not set up") } - return Noir.verify(proof, proofType, num_points.toString()) + return Noir.verify(proof, proofType ?: "honk") } private fun flattenMultiDimensionalArray(array: List): List { diff --git a/lib/src/main/java/com/noirandroid/lib/Noir.kt b/lib/src/main/java/com/noirandroid/lib/Noir.kt index 02326fc..204b152 100644 --- a/lib/src/main/java/com/noirandroid/lib/Noir.kt +++ b/lib/src/main/java/com/noirandroid/lib/Noir.kt @@ -2,10 +2,10 @@ package com.noirandroid.lib class Noir { companion object { - external fun prove(circuitBytecode: String, initialWitness: Map, proofType: String, numPoints: String): Proof + external fun prove(circuitBytecode: String, initialWitness: Map, proofType: String?, recursive: Boolean?): Proof - external fun verify(proof: Proof, proofType: String, numPoints: String): Boolean + external fun verify(proof: Proof, proofType: String?): Boolean - external fun setup_srs(circuitBytecode: String, srsPath: String?): Int + external fun setup_srs(circuitBytecode: String, srsPath: String?, recursive: Boolean?): Int } } \ No newline at end of file diff --git a/lib/src/main/java/noir_java/Cargo.toml b/lib/src/main/java/noir_java/Cargo.toml index d8ffd7e..ec9fdef 100644 --- a/lib/src/main/java/noir_java/Cargo.toml +++ b/lib/src/main/java/noir_java/Cargo.toml @@ -11,7 +11,7 @@ crate-type = ["cdylib"] [dependencies] jni = "0.21.1" -noir_rs = { git = "https://github.com/zkpassport/noir_rs.git", branch = "v0.36.0", package = "noir_rs" } +noir_rs = { git = "https://github.com/zkpassport/noir_rs.git", branch = "v1.0.0-beta.0", features = ["barretenberg"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" hex = "0.4.3" diff --git a/lib/src/main/java/noir_java/src/lib.rs b/lib/src/main/java/noir_java/src/lib.rs index 7271126..9f56b77 100644 --- a/lib/src/main/java/noir_java/src/lib.rs +++ b/lib/src/main/java/noir_java/src/lib.rs @@ -5,11 +5,11 @@ use jni::sys::{jboolean, jobject, jint}; use jni::JNIEnv; use noir_rs::{ native_types::{Witness, WitnessMap}, - prove::prove, - prove::prove_honk, - verify::verify, - verify::verify_honk, - srs::setup_srs, + barretenberg::{ + prove::prove_ultra_honk, + verify::verify_ultra_honk, + srs::setup_srs, + }, FieldElement }; @@ -19,6 +19,7 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_setup_1srs<' _class: JClass<'local>, circuit_bytecode_jstr: JString<'local>, srs_path_jstr: JString<'local>, + recursive: jboolean, ) -> jint { let circuit_bytecode = env .get_string(&circuit_bytecode_jstr) @@ -38,7 +39,9 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_setup_1srs<' ), }; - let num_points = setup_srs(circuit_bytecode, srs_path.as_deref()).expect("Failed to setup srs"); + let recursive_bool = recursive == 1; + + let num_points = setup_srs(&circuit_bytecode, srs_path.as_deref(), recursive_bool).expect("Failed to setup srs"); jint::try_from(num_points).unwrap() } @@ -51,7 +54,7 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_prove<'local circuit_bytecode_jstr: JString<'local>, witness_jobject: JObject<'local>, proof_type_jstr: JString<'local>, - num_points_jstr: JString<'local>, + recursive: jboolean, ) -> jobject { // Use more descriptive variable names and handle errors gracefully let witness_map = match env.get_map(&witness_jobject) { @@ -76,13 +79,7 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_prove<'local .expect("Failed to convert proof type to Rust string") .to_owned(); - let num_points = env - .get_string(&num_points_jstr) - .expect("Failed to get num_points string") - .to_str() - .expect("Failed to convert num_points to Rust string") - .parse() - .expect("Failed to parse num_points"); + let recursive_bool = recursive == 1; let mut witness_map = WitnessMap::new(); @@ -109,9 +106,9 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_prove<'local } let (proof, vk) = if proof_type == "honk" { - prove_honk(circuit_bytecode, witness_map).expect("Proof generation failed") + prove_ultra_honk(&circuit_bytecode, witness_map, recursive_bool).expect("Proof generation failed") } else { - prove(circuit_bytecode, witness_map, num_points).expect("Proof generation failed") + panic!("Honk is the only proof type supported for now"); }; let proof_str = hex::encode(proof); @@ -141,8 +138,7 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_verify<'loca mut env: JNIEnv<'local>, _class: JClass<'local>, mut proof_jobject: JObject<'local>, - proof_type_jstr: JString<'local>, - num_points_jstr: JString<'local>, + proof_type_jstr: JString<'local> ) -> jboolean { let proof_field = env .get_field(&mut proof_jobject, "proof", "Ljava/lang/String;") @@ -181,18 +177,10 @@ pub extern "system" fn Java_com_noirandroid_lib_Noir_00024Companion_verify<'loca .expect("Failed to convert proof type to Rust string") .to_owned(); - let num_points = env - .get_string(&num_points_jstr) - .expect("Failed to get num_points string") - .to_str() - .expect("Failed to convert num_points to Rust string") - .parse() - .expect("Failed to parse num_points"); - let verdict = if proof_type == "honk" { - verify_honk(proof, verification_key).expect("Verification failed") + verify_ultra_honk(proof, verification_key).expect("Verification failed") } else { - verify(proof, verification_key, num_points).expect("Verification failed") + panic!("Ultra honk is the only proof type supported for now"); }; jboolean::from(verdict)