Skip to content

Commit

Permalink
upgrade to Noir 1.0.0-beta.0
Browse files Browse the repository at this point in the history
  • Loading branch information
madztheo committed Dec 21, 2024
1 parent 644e65b commit 0c422de
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 51 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -64,23 +64,15 @@ 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

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)
```
4 changes: 2 additions & 2 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ tasks.register<Copy>("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)
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/main/java/com/noirandroid/lib/Circuit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any>, proofType: String): Proof {
fun prove(initialWitness: Map<String, Any>, 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<Any>): List<Any> {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/com/noirandroid/lib/Noir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.noirandroid.lib

class Noir {
companion object {
external fun prove(circuitBytecode: String, initialWitness: Map<String, String>, proofType: String, numPoints: String): Proof
external fun prove(circuitBytecode: String, initialWitness: Map<String, String>, 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
}
}
2 changes: 1 addition & 1 deletion lib/src/main/java/noir_java/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
44 changes: 16 additions & 28 deletions lib/src/main/java/noir_java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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)
Expand All @@ -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()
}
Expand All @@ -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) {
Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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;")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0c422de

Please sign in to comment.