Skip to content

Commit

Permalink
Merge pull request #8 from anon-aadhaar/feat/implement_polygon_stack
Browse files Browse the repository at this point in the history
feat: implement polygon stack
  • Loading branch information
Meyanis95 authored Apr 10, 2024
2 parents d07d43c + 9e58ca3 commit c12c82a
Show file tree
Hide file tree
Showing 39 changed files with 1,017 additions and 1,964 deletions.
11 changes: 8 additions & 3 deletions anon-aadhaar-react-native.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ Pod::Spec.new do |s|
s.platforms = { :ios => min_ios_version_supported }
s.source = { :git => "https://github.com/Meyanis95/anon-aadhaar-react-native.git", :tag => "#{s.version}" }

s.vendored_frameworks = 'ios/Frameworks/MoproBindings.xcframework'
s.source_files = "ios/Frameworks/MoproBindings.xcframework/ios-arm64/Headers/*.{h,m,swift}"
s.static_framework = true
s.source_files = "ios/**/*.{h,m,mm,swift}"

s.preserve_path = 'ios/Frameworks/*'
s.vendored_frameworks = "ios/Frameworks/*.{xcframework}"
s.ios.vendored_frameworks = "ios/Frameworks/*.{xcframework}"
s.pod_target_xcconfig = { 'ONLY_ACTIVE_ARCH' => 'YES' }
s.user_target_xcconfig = { 'ONLY_ACTIVE_ARCH' => 'YES' }

# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
Expand All @@ -28,7 +34,6 @@ Pod::Spec.new do |s|
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
s.pod_target_xcconfig = {
'OTHER_LDFLAGS' => '-ObjC -lc++',
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
Expand Down
146 changes: 64 additions & 82 deletions example/ios/AnonAadhaarApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ target 'AnonAadhaarApp' do
:app_path => "#{Pod::Config.instance.installation_root}/.."
)

pod 'CircuitBindings', :path => '../../ios/Frameworks/CircuitBindings.podspec'

target 'AnonAadhaarAppTests' do
inherit! :complete
# Pods for testing
Expand Down
4 changes: 4 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
"pako": "^2.1.0",
"react": "18.2.0",
"react-native": "0.73.4",
"react-native-base64": "^0.2.1",
"react-native-blob-util": "^0.19.8",
"react-native-fs": "^2.20.0",
"react-native-safe-area-context": "^4.9.0",
"react-native-screens": "^3.29.0",
"react-native-svg": "^15.1.0",
"react-native-vision-camera": "^3.9.0",
"react-native-zip-archive": "^6.1.0",
"text-encoding": "^0.7.0",
"yarn": "^1.22.22"
},
"devDependencies": {
Expand Down
25 changes: 3 additions & 22 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
import React, { useEffect, useState } from 'react';
import { setupMopro } from '@anon-aadhaar/react-native';
import React, { useState } from 'react';
import { OnboardingScreen } from './OnboardingScreen';
import { MainScreen } from './MainScreen';
import BenchmarkView from './BenchmarkView';

export type Views = 'Onboarding' | 'Main' | 'Benchmark';

export default function App() {
const [setupReady, setSetupReady] = useState<boolean>(false);
const [currentScreen, setCurrentScreen] = useState<Views>('Onboarding');

useEffect(() => {
try {
if (!setupReady) {
setupMopro().then(() => {
setSetupReady(true);
});
}
} catch (e) {
console.log(e);
}
}, [setupReady]);

return (
<>
{currentScreen === 'Onboarding' && (
<OnboardingScreen
setupReady={setupReady}
setCurrentScreen={setCurrentScreen}
/>
<OnboardingScreen setCurrentScreen={setCurrentScreen} />
)}
{currentScreen === 'Main' && <MainScreen />}
{currentScreen === 'Benchmark' && (
<BenchmarkView setupReady={setupReady} />
)}
{currentScreen === 'Benchmark' && <BenchmarkView />}
</>
);
}
83 changes: 57 additions & 26 deletions example/src/BenchmarkView.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/* eslint-disable react-native/no-inline-styles */
import * as React from 'react';
import RNFS from 'react-native-fs';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
ActivityIndicator,
// Platform,
} from 'react-native';
import {
generateProof,
verifyProof,
type AnonAadhaarArgs,
groth16ProveWithZKeyFilePath,
AadhaarScanner,
verifySignature,
groth16Verify,
setupProver,
} from '@anon-aadhaar/react-native';
import { useEffect, useState } from 'react';
import { circuitInputsFromQR } from '../../src/generateInputs';
Expand All @@ -22,7 +25,17 @@ const Toast = ({ message }: { message: string }) => (
</View>
);

export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
// const zkeyChunksFolderPath = RNFS.DocumentDirectoryPath + '/chunked';
const zkeyFilePath = RNFS.DocumentDirectoryPath + '/circuit_final.zkey';
const DatFilePath = RNFS.DocumentDirectoryPath + '/aadhaar-verifier.dat';

function getVerificationKey(): Promise<string> {
const path = RNFS.DocumentDirectoryPath + '/vkey.json';
return RNFS.readFile(path, 'utf8');
}

export default function BenchmarkView({}) {
const [ready, setReady] = useState<boolean>(false);
const [complexProof, setComplexProof] = useState<string | null>(null);
const [publicInputs, setPublicInputs] = useState<string | null>(null);
const [proofVerified, setProofVerified] = useState<boolean>(false);
Expand All @@ -31,22 +44,12 @@ export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
const [isVerifyingSig, setIsVerifyingSig] = useState<boolean>(false);
const [isQrScanned, setIsQrScanned] = useState<boolean>(false);
const [sigVerified, setSigVerified] = useState<boolean>(false);
// const [chunkPaths, setChunkPaths] = useState<string[]>([]);
const [errorToastMessage, setErrorToastMessage] = useState<string | null>(
null
);
const [anonAadhaarArgs, setAnonAadhaarArgs] = useState<{
qrDataPadded: string[];
qrDataPaddedLength: string[];
nonPaddedDataLength: string[];
delimiterIndices: string[];
signature: string[];
pubKey: string[];
signalHash: string[];
revealGender: string[];
revealAgeAbove18: string[];
revealState: string[];
revealPinCode: string[];
} | null>(null);
const [anonAadhaarArgs, setAnonAadhaarArgs] =
useState<AnonAadhaarArgs | null>(null);
const [qrCodeValue, setQrCodeValue] = useState<string>('');
const [executionTime, setExecutionTime] = useState<{
setup: number;
Expand Down Expand Up @@ -74,6 +77,14 @@ export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
}
}, [qrCodeValue]);

if (!ready) {
const startSetup = Date.now();
setupProver().then(() => {
setReady(true);
setExecutionTime((prev) => ({ ...prev, setup: Date.now() - startSetup }));
});
}

const showToast = (message: string) => {
setErrorToastMessage(message);
setTimeout(() => setErrorToastMessage(null), 3000); // hide after 3 seconds
Expand All @@ -83,12 +94,20 @@ export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
try {
setIsProving(true);
const startProof = Date.now();
const { proof, inputs } = await generateProof(anonAadhaarArgs);
setComplexProof(proof);
setPublicInputs(inputs);
if (!anonAadhaarArgs) throw Error('You must generate arguments first');
const { proof, pub_signals } = await groth16ProveWithZKeyFilePath(
zkeyFilePath,
DatFilePath,
anonAadhaarArgs
);
setExecutionTime((prev) => ({ ...prev, proof: Date.now() - startProof }));
setComplexProof(proof);
console.log('Complex Proof received: ', proof);
setPublicInputs(pub_signals);
console.log('Public Inputs received: ', pub_signals);
setIsProving(false);
} catch (e) {
setIsProving(false);
if (e instanceof Error) {
showToast(e.message);
} else {
Expand All @@ -97,10 +116,14 @@ export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
}
};

const verifProof = async (_proof: any, _publicInputs: any) => {
const verifProof = async (_proof: string, _publicInputs: string) => {
try {
const startVerif = Date.now();
const res = await verifyProof(_proof, _publicInputs);
const res = await groth16Verify(
_proof,
_publicInputs,
await getVerificationKey()
);
console.log('Verification result: ', res);
setProofVerified(res);
setExecutionTime((prev) => ({
Expand All @@ -125,12 +148,16 @@ export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
<View
style={[
styles.statusIndicator,
{ backgroundColor: setupReady ? 'green' : 'red' },
{ backgroundColor: ready ? 'green' : 'red' },

Check warning on line 151 in example/src/BenchmarkView.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { backgroundColor: "ready ? 'green' : 'red'" }
]}
/>
<Text>Prover State: {String(setupReady)}</Text>
<Text>Prover State: {String(ready)}</Text>
{ready ? (
<Text>Proof Execution Time: {executionTime.setup}ms</Text>
) : (
<></>
)}
</View>
<Text>Setup Execution Time: {executionTime.setup}ms</Text>
{!isQrScanned && (
<TouchableOpacity
style={styles.button}
Expand Down Expand Up @@ -177,7 +204,11 @@ export default function BenchmarkView({ setupReady }: { setupReady: boolean }) {
styles.button,
complexProof ? styles.buttonEnabled : styles.buttonDisabled,
]}
onPress={() => verifProof(complexProof, publicInputs)}
onPress={() =>
complexProof && publicInputs
? verifProof(complexProof, publicInputs)
: null
}
>
<Text style={styles.buttonText}>Verify</Text>
</TouchableOpacity>
Expand Down
75 changes: 41 additions & 34 deletions example/src/ProofModal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
AadhaarScanner,
circuitInputsFromQR,
generateProof,
verifyProof,
// groth16ProveWithZKeyFilePath,
// groth16Verify,
verifySignature,
} from '@anon-aadhaar/react-native';
import React, { useEffect, useState } from 'react';
Expand Down Expand Up @@ -86,24 +86,28 @@ const Screen2 = ({
);
};

const ProveScreen = ({
anonAadhaarArgs,
setProofVerified,
setProofs,
}: {
anonAadhaarArgs: any;
setProofVerified: any;
setProofs: any;
}) => {
const ProveScreen = (
{
// anonAadhaarArgs,
// setProofVerified,
// setProofs,
}: {
// anonAadhaarArgs: any;
setProofVerified: any;
setProofs: any;
}
) => {
const [isProving, setIsProving] = useState<boolean>(false);

const genProof = async () => {
setIsProving(true);
try {
const { proof, inputs } = await generateProof(anonAadhaarArgs);
const res = await verifyProof(proof, inputs);
setProofs({ proof, inputs });
setProofVerified(res);
// TODO Get path of zkey and witness
// const { proof, pub_signals } = await groth16ProveWithZKeyFilePath('', '');
// // TODO Get path of the vk
// const res = await groth16Verify(proof, pub_signals, '');
// setProofs({ proof, pub_signals });
// setProofVerified(res);
setIsProving(false);
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -144,19 +148,19 @@ export const ProofModal = ({
const [currentScreen, setCurrentScreen] = useState('screen1');
const [qrCodeValue, setQrCodeValue] = useState<string>('');
const [proofVerified, setProofVerified] = useState<boolean>(false);
const [anonAadhaarArgs, setAnonAadhaarArgs] = useState<{
qrDataPadded: string[];
qrDataPaddedLength: string[];
nonPaddedDataLength: string[];
delimiterIndices: string[];
signature: string[];
pubKey: string[];
signalHash: string[];
revealGender: string[];
revealAgeAbove18: string[];
revealState: string[];
revealPinCode: string[];
} | null>(null);
// const [anonAadhaarArgs, setAnonAadhaarArgs] = useState<{
// qrDataPadded: string[];
// qrDataPaddedLength: string[];
// nonPaddedDataLength: string[];
// delimiterIndices: string[];
// signature: string[];
// pubKey: string[];
// signalHash: string[];
// revealGender: string[];
// revealAgeAbove18: string[];
// revealState: string[];
// revealPinCode: string[];
// } | null>(null);

useEffect(() => {
if (proofVerified) onCloseModal();
Expand All @@ -172,17 +176,20 @@ export const ProofModal = ({
verifySignature(qrCodeValue)
.then((isVerified) => {
if (isVerified) {
circuitInputsFromQR(qrCodeValue).then((args) => {
setAnonAadhaarArgs(args);
setCurrentScreen('sigVerified');
});
circuitInputsFromQR(qrCodeValue).then(() =>
// args
{
// setAnonAadhaarArgs(args);
setCurrentScreen('sigVerified');
}
);
}
})
.catch((e) => {
console.error(e);
});
}
}, [qrCodeValue, setAnonAadhaarArgs, setCurrentScreen]);
}, [qrCodeValue, setCurrentScreen]);

return (
<View style={styles.centeredView}>
Expand Down Expand Up @@ -215,7 +222,7 @@ export const ProofModal = ({
return (
<ProveScreen
setProofVerified={setProofVerified}
anonAadhaarArgs={anonAadhaarArgs}
// anonAadhaarArgs={anonAadhaarArgs}
setProofs={setProofs}
/>
);
Expand Down
12 changes: 0 additions & 12 deletions ios/Frameworks/CircuitBindings.podspec

This file was deleted.

Binary file not shown.
Loading

0 comments on commit c12c82a

Please sign in to comment.