Skip to content

Commit

Permalink
Updated Android implementation and usage example.
Browse files Browse the repository at this point in the history
  • Loading branch information
5eeman committed Dec 19, 2024
1 parent fda8bc8 commit f4fbc6d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {calculateWitness} from 'react-native-circom-witnesscalc';

// ...

const inputs = readFileFromFS('path/to/inputs.json').encode('base64');
const graph = readFileFromFS('path/to/graph.wcd').encode('base64');
const inputs = RNFS.readFile('path/to/inputs.json', 'utf8');
const graph = RNFS.readFile('path/to/graph.wcd', "base64");

const result = await calculateWitness(inputs, graph);

Expand Down
26 changes: 22 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ buildscript {

def reactNativeArchitectures() {
def value = rootProject.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
return value ? value.split(",") : ["x86_64", "arm64-v8a"]
}

def isNewArchitectureEnabled() {
Expand Down Expand Up @@ -49,7 +49,7 @@ def supportsNamespace() {

android {
if (supportsNamespace()) {
namespace "com.circomwitnesscalc"
namespace "io.iden3.rn_circom_witnesscalc"

sourceSets {
main {
Expand All @@ -58,14 +58,23 @@ android {
}
}

ndkVersion getExtOrDefault("ndkVersion")
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
ndk {
abiFilters "x86_64", "arm64-v8a"
}
externalNativeBuild {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
abiFilters "x86_64", "arm64-v8a"
}
}
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -95,5 +104,14 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

implementation "io.iden3.circomwitnesscalc:0.0.1-alpha.1"
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "CircomWitnesscalc"
codegenJavaPackageName = "com.circomwitnesscalc"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import io.iden3.circomwitnesscalc.CircomWitnesscalc

class CircomWitnesscalcModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
Expand All @@ -15,7 +16,24 @@ class CircomWitnesscalcModule(reactContext: ReactApplicationContext) :
// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun multiply(a: Double, b: Double, promise: Promise) {
fun calculateWitness(inputs: String, graph: String, promise: Promise) {
try {
// Decode base64
byte[] zkeyBytes = Base64.decode(graph, Base64.DEFAULT);

ByteArray witness = CircomWitnesscalc.calculateWitness(
inputs,
graph
);

// Encode base64
String witnessBase64 = Base64.encodeToString(witness, Base64.DEFAULT);

promise.resolve(witnessBase64);
} catch (RapidsnarkError e) {
promise.reject(String.valueOf(e.getCode()), e.getMessage());
}

promise.resolve(a * b)
}

Expand Down

0 comments on commit f4fbc6d

Please sign in to comment.