working with arkworks zkrsa

This commit is contained in:
0xturboblitz
2023-09-21 17:05:57 +02:00
parent d6de6b8a66
commit 3c91122bb4
11 changed files with 3172 additions and 9 deletions

View File

@@ -56,11 +56,11 @@ dependencies {
apply plugin: 'org.mozilla.rust-android-gradle.rust-android'
cargo {
module = "../../../halo2-passport" // this works
module = "../../../ark-circom-rsa" // this works
// module = "/Users/turboblitz/code/my-code/passport-sbt/app/halo2-passport"
libname = "halo2_passport" // Or whatever matches Cargo.toml's [package] name.
libname = "ark_circom_rsa" // Or whatever matches Cargo.toml's [package] name.
// targets = ["arm", "x86"] // failing
targets = ["arm64", "darwin-aarch64"] // Those work
apiLevel = 29
profile = 'release'
}
profile = 'debug'
}

View File

@@ -551,7 +551,7 @@ class RNPassportReaderModule(private val reactContext: ReactApplicationContext)
//-------------functions related to calling rust lib----------------//
// Declare native method
external fun callRustCode(): Int
external fun callRustCode(): String
@ReactMethod
fun callRustLib(callback: Callback) {
@@ -562,12 +562,12 @@ class RNPassportReaderModule(private val reactContext: ReactApplicationContext)
callback.invoke(null, resultFromRust)
}
external fun proveInRust(): Int
external fun proveRSAInRust(): Int
@ReactMethod
fun proveRust(callback: Callback) {
// Call the Rust function
val resultFromProof = proveInRust()
val resultFromProof = proveRSAInRust()
// Return the result to JavaScript through the callback
callback.invoke(null, resultFromProof)
@@ -583,7 +583,7 @@ class RNPassportReaderModule(private val reactContext: ReactApplicationContext)
private const val KEY_IS_SUPPORTED = "isSupported"
var instance: RNPassportReaderModule? = null
init {
System.loadLibrary("halo2_passport")
System.loadLibrary("ark_circom_rsa")
}
}
}

1
app/ark-circom-rsa/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

2997
app/ark-circom-rsa/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,20 @@
[package]
name = "ark_circom_rsa"
version = "0.1.0"
edition = "2021"
[lib]
name = "ark_circom_rsa"
path = "src/rsa.rs"
crate-type = ["cdylib"]
[dependencies]
ark-circom = { git = "https://github.com/0xturboblitz/circom-compat.git" }
ark-bn254 = { version = "=0.4.0" }
ark-groth16 = { version = "=0.4.0", default-features = false, features = ["parallel"] }
ark-std = { version = "=0.4.0", default-features = false, features = ["parallel"] }
ark-crypto-primitives = { version = "=0.4.0" }
color-eyre = "=0.6.2"
jni = "0.18" # Choose the version that best fits your needs

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
version = "1.67.0"

View File

@@ -0,0 +1,142 @@
use ark_circom::{CircomBuilder, CircomConfig};
use ark_std::rand::thread_rng;
use color_eyre::Result;
use std::os::raw::c_int;
use ark_bn254::Bn254;
use ark_crypto_primitives::snark::SNARK;
use ark_groth16::Groth16;
use std::time::Instant;
type GrothBn = Groth16<Bn254>;
extern crate jni;
use jni::objects::JClass;
use jni::JNIEnv;
#[no_mangle]
pub extern "C" fn Java_io_tradle_nfc_RNPassportReaderModule_callRustCode(
env: JNIEnv,
_: JClass,
) -> jni::sys::jstring {
let current_dir = std::env::current_dir().unwrap();
let path_str = current_dir.to_str().unwrap();
let output = env.new_string(path_str).expect("Couldn't create java string!");
output.into_inner()
}
#[no_mangle]
pub extern "C" fn Java_io_tradle_nfc_RNPassportReaderModule_proveRSAInRust(
_: JNIEnv,
_: JClass,
) -> c_int {
fn run() -> Result<u128, Box<dyn std::error::Error>> {
println!("log before imports");
const MAIN_WASM: &'static [u8] = include_bytes!("../rsa/main.wasm");
const MAIN_R1CS: &'static [u8] = include_bytes!("../rsa/main.r1cs");
let cfg = CircomConfig::<Bn254>::from_bytes(MAIN_WASM, MAIN_R1CS)?;
let mut builder = CircomBuilder::new(cfg);
let signature: [u128; 32] = [
4993543337487904319, 5039260395924778555,
16044715263198697509, 6517674227143205114,
9783381675666809188, 7797234981612410535,
9712659746244703685, 8223984644219552691,
5746171858797010138, 16352708903743190663,
11557514992480971638, 13495509591487042457,
11156826800435483355, 7934676927345641909,
17671838456179191719, 15427313345670295171,
3979639931302305273, 10870708508897347751,
17325747030660864416, 4196229958717243275,
8295837152932404523, 5206285193355768709,
16500962385150574058, 45927554409508738,
11056427006453546685, 3610340837562714815,
2914954158206709664, 9941999032204203280,
3682966980231699250, 1089954850805856847,
12801803660741250853, 6643401487810361365
];
let modulus: [u128; 32] = [
14637485623069577853, 7482098129440337882,
9329095990282353414, 13124250581866537330,
18349306516477384309, 3633589540637627345,
756443621693602880, 9532268969225926567,
10797289495421403158, 8716880397646489088,
16390100705849925925, 4946748147388408397,
5159237052852568257, 4383482229078465345,
17440536203309797881, 9244726556354794825,
13954964489103323762, 12859274108738823253,
15430872548874177827, 8078236913810864353,
13311543254088155939, 6627932043456339426,
10937476704429447948, 4860889415451015006,
4549761793924050171, 1117773587704762559,
13984923195668836033, 5179232650854575709,
16174751231280536837, 9625446134615655537,
6169436660688221259, 13128400207083283532
];
let base_message: [u128; 32] = [
3626324085499461436, 15137430623782848370,
13410089559264023318, 7272337899472972005,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0
];
for &elem in signature.iter() {
builder.push_input("signature", elem);
}
for &elem in modulus.iter() {
builder.push_input("modulus", elem);
}
for &elem in base_message.iter() {
builder.push_input("base_message", elem);
}
// create an empty instance for setting it up
let circom = builder.setup();
let mut rng = thread_rng();
let params = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng)?;
let circom = builder.build()?;
println!("circuit built");
let inputs = circom.get_public_inputs().unwrap();
let start1 = Instant::now();
let proof = GrothBn::prove(&params, circom, &mut rng)?;
let duration1 = start1.elapsed();
println!("proof generated. Took: {:?}", duration1);
let start2 = Instant::now();
let pvk = GrothBn::process_vk(&params.vk).unwrap();
let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof)?;
let duration2 = start2.elapsed();
println!("proof verified. Took: {:?}", duration2);
assert!(verified);
Ok(duration1.as_millis())
}
match run() {
Ok(elapsed_millis) => elapsed_millis as i32, // Assuming the elapsed time will fit in an i32
Err(_) => -1, // return -1 or some other error code when there's an error
}
}

View File

@@ -6,4 +6,4 @@ cd ../android
cd ..
mkdir -p android/react-native-passport-reader/android/src/main/jniLibs/arm64/
cp halo2-passport/target/aarch64-linux-android/release/libhalo2_passport.so android/react-native-passport-reader/android/src/main/jniLibs/arm64/
cp ark-circom-rsa/target/aarch64-linux-android/release/libark_circom_rsa.so android/react-native-passport-reader/android/src/main/jniLibs/arm64/