Skip to content

Commit

Permalink
feat: add multi instance signer support
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Oct 3, 2023
1 parent 9877fbc commit 8105cec
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 115 deletions.
Binary file modified tss-client-android/libs/arm64-v8a/libdkls.a
Binary file not shown.
Binary file modified tss-client-android/libs/armeabi-v7a/libdkls.a
Binary file not shown.
135 changes: 60 additions & 75 deletions tss-client-android/libs/include/dkls.h
Original file line number Diff line number Diff line change
@@ -1,78 +1,63 @@
#ifndef __DKLS_H__
#define __DKLS_H__ // Include guard
#ifdef __cplusplus // Required for C++ compiler
extern "C" {
#endif
//Includes
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

//Forward declarations
struct Sigfrags;
struct Counterparties;
struct DKLSMsgComm;
struct ChaChaRng;
struct ThresholdSigner;
struct Precompute;

//Methods
//Signature Fragments
struct Sigfrags *signature_fragments_from_string(const char *input, int *error_code);
const char *signature_fragments_to_string(struct Sigfrags *fragments, int *error_code);
void signature_fragments_free(struct Sigfrags *fragments);

//Counterparties
struct Counterparties *counterparties_from_string(const char *parties, int *error_code);
const char *counterparties_to_string(struct Counterparties *fragments, int *error_code);
void counterparties_free(struct Counterparties *parties);

//MsgComm
struct DKLSMsgComm *dkls_comm(int index, int parties, const char *session,
const char *(*read_msg_callback)(const char *, unsigned long long int,
unsigned long long int,
const char *),
bool (*send_msg_callback)(const char *, unsigned long long int,
unsigned long long int, const char *,
const char *), int *error_code);
void dkls_comm_free(struct DKLSMsgComm *comm);

//Random Generator
struct ChaChaRng *random_generator(const char *state, int *error_code);
void random_generator_free(struct ChaChaRng *rng);

//Precompute
const char *precompute_to_string(struct Precompute *precompute, int *error_code);
struct Precompute *precompute_from_string(const char *input, int *error_code);
const char *get_r_from_precompute(const char *precompute, int *error_code);
void precompute_free(struct Precompute *precompute);

//Threshold Signer
struct ThresholdSigner *
threshold_signer(const char *session, int player_index, int parties, int threshold,
const char *share, const char *pubkey, int *error_code);
void threshold_signer_free(struct ThresholdSigner *signer);
bool threshold_signer_setup(struct ThresholdSigner *signer, struct ChaChaRng *rng,
struct DKLSMsgComm *comm);
const char *
threshold_signer_precompute(struct Counterparties *parties, struct ThresholdSigner *signer,
struct ChaChaRng *rng, struct DKLSMsgComm *comm, int *error_code);
const char *
threshold_signer_sign(struct Counterparties *counterparties, const char *msg, bool hash_only,
struct ThresholdSigner *signer, struct ChaChaRng *rng,
struct DKLSMsgComm *comm, int *error_code);

//Utilities
int dkls_batch_size(int *error_code);
const char *dkls_hash_encode(const char *msg, int *error_code);
const char *
dkls_local_sign(const char *msg, bool hash_only, const char *precompute, int *error_code);
const char *
dkls_local_verify(const char *msg, bool hash_only, const char *r, struct Sigfrags *sig_frags,
const char *pubkey, int *error_code);
void dkls_string_free(char *ptr);
#ifdef __cplusplus
} // extern "C"
#endif
#ifdef __cplusplus // Required for C++ compiler
extern "C" {
#endif
//Includes
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

//Forward declarations
struct Sigfrags;
struct Counterparties;
struct DKLSMsgComm;
struct ChaChaRng;
struct ThresholdSigner;
struct Precompute;

//Methods
//Signature Fragments
struct Sigfrags *signature_fragments_from_string(const char* input, int* error_code);
const char* signature_fragments_to_string(struct Sigfrags* fragments, int* error_code);
void signature_fragments_free(struct Sigfrags* fragments);

//Counterparties
struct Counterparties *counterparties_from_string(const char* parties, int* error_code);
const char* counterparties_to_string(struct Counterparties* fragments, int* error_code);
void counterparties_free(struct Counterparties* parties);

//MsgComm
struct DKLSMsgComm* dkls_comm(int index,int parties,const char* session,
const char* (*read_msg_callback)(const char*,unsigned long long int,unsigned long long int,const char*, const void*),
bool (*send_msg_callback)(const char*,unsigned long long int,unsigned long long int,const char*,const char*, const void*), const void* parent_instance_ref, int* error_code);
const void* dkls_comm_free(struct DKLSMsgComm* comm);

//Random Generator
struct ChaChaRng* random_generator(const char* state, int* error_code);
void random_generator_free(struct ChaChaRng* rng);

//Precompute
const char* precompute_to_string(struct Precompute* precompute, int* error_code);
struct Precompute* precompute_from_string(const char* input, int* error_code);
const char* get_r_from_precompute(const char* precompute, int* error_code);
void precompute_free(struct Precompute* precompute);

//Threshold Signer
struct ThresholdSigner* threshold_signer(const char* session, int player_index, int parties, int threshold, const char* share, const char* pubkey, int* error_code);
void threshold_signer_free(struct ThresholdSigner* signer);
bool threshold_signer_setup(struct ThresholdSigner* signer, struct ChaChaRng* rng, struct DKLSMsgComm* comm);
const char* threshold_signer_precompute(struct Counterparties* parties, struct ThresholdSigner* signer, struct ChaChaRng* rng, struct DKLSMsgComm* comm, int* error_code);
const char* threshold_signer_sign(struct Counterparties* counterparties, const char* msg, bool hash_only, struct ThresholdSigner* signer, struct ChaChaRng* rng, struct DKLSMsgComm* comm, int* error_code);

//Utilities
int dkls_batch_size(int* error_code);
const char* dkls_hash_encode(const char* msg, int* error_code);
const char* dkls_local_sign(const char* msg, bool hash_only, const char* precompute, int* error_code);
const char* dkls_local_verify(const char* msg, bool hash_only, const char* r, struct Sigfrags* sig_frags, const char* pubkey, int* error_code);
void dkls_string_free(char* ptr);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // __DKLS_H__
Binary file modified tss-client-android/libs/x86/libdkls.a
Binary file not shown.
Binary file modified tss-client-android/libs/x86_64/libdkls.a
Binary file not shown.
3 changes: 0 additions & 3 deletions tss-client-android/src/main/cpp/jniChaChaRng.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand Down
3 changes: 0 additions & 3 deletions tss-client-android/src/main/cpp/jniCounterparties.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand Down
48 changes: 26 additions & 22 deletions tss-client-android/src/main/cpp/jniDKLSComm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand All @@ -14,18 +11,14 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return JNI_VERSION_1_6;
}

jobject handler = nullptr;
jmethodID sendMsgCallbackID;
jmethodID readMsgCallbackID;

// Note:
// readMsgCallback(session, index, remote, msg_type) -> msg_data
// sendMsgCallback(session, index, recipient, msg_type, msg_data) -> Bool

const char* readMsgCallback(const char* session, unsigned long long int index, unsigned long long int remote, const char* msg_type) {
const char* readMsgCallback(const char* session, unsigned long long int index, unsigned long long int remote, const char* msg_type, const void *obj_ref) {
JNIEnv *jniEnv;

if (g_vm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6) != JNI_OK || handler == nullptr) {
if (g_vm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6) != JNI_OK || obj_ref == nullptr) {
return "";
}

Expand All @@ -35,10 +28,13 @@ const char* readMsgCallback(const char* session, unsigned long long int index, u
jstring jmsgType = jniEnv->NewStringUTF(msg_type);
dkls_string_free(const_cast<char*>(msg_type));

auto jparent_ref = reinterpret_cast<jobject>(handler);
auto jparent_ref = reinterpret_cast<jobject>(const_cast<void*>(obj_ref));
auto index_bytes = getBytesFromUnsignedLongLong(jniEnv, index);
auto remote_bytes = getBytesFromUnsignedLongLong(jniEnv, remote);

auto readMsgCallbackID = reinterpret_cast<jmethodID>(GetPointerField(jniEnv,
jparent_ref,
"read_callback_id"));
auto result = (jstring) jniEnv->CallObjectMethod(
jparent_ref,
readMsgCallbackID,
Expand All @@ -48,10 +44,10 @@ const char* readMsgCallback(const char* session, unsigned long long int index, u
return res;
}

bool sendMsgCallback(const char* session, unsigned long long int index, unsigned long long int remote, const char* msg_type, const char* msg_data) {
bool sendMsgCallback(const char* session, unsigned long long int index, unsigned long long int remote, const char* msg_type, const char* msg_data, const void *obj_ref) {
JNIEnv *jniEnv;

if (g_vm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6) != JNI_OK || handler == nullptr) {
if (g_vm->GetEnv((void **) &jniEnv, JNI_VERSION_1_6) != JNI_OK || obj_ref == nullptr) {
return false;
}

Expand All @@ -67,7 +63,10 @@ bool sendMsgCallback(const char* session, unsigned long long int index, unsigned
auto index_bytes = getBytesFromUnsignedLongLong(jniEnv, index);
auto remote_bytes = getBytesFromUnsignedLongLong(jniEnv, remote);

auto jparent_ref = reinterpret_cast<jobject>(handler);
auto jparent_ref = reinterpret_cast<jobject>(const_cast<void*>(obj_ref));
auto sendMsgCallbackID = reinterpret_cast<jmethodID>(GetPointerField(jniEnv,
jparent_ref,
"send_callback_id"));
auto result = jniEnv->CallBooleanMethod(
jparent_ref,
sendMsgCallbackID,
Expand All @@ -88,20 +87,23 @@ Java_com_web3auth_tss_1client_1android_dkls_DKLSComm_jniDklsComm(JNIEnv *env, jo
jthrowable dkls_error) {
int errorCode = 0;
int *error_ptr = &errorCode;
handler = env->NewGlobalRef(thiz);
sendMsgCallbackID = getMethodId(
jobject instance = env->NewGlobalRef(thiz);

jmethodID sendMsgCallbackID = getMethodId(
env,
thiz,
instance,
send_msg_callback,
send_msg_callback_sig);
readMsgCallbackID = getMethodId(
SetPointerField(env, instance, reinterpret_cast<jlong>(sendMsgCallbackID), "send_callback_id");
jmethodID readMsgCallbackID = getMethodId(
env,
thiz,
instance,
read_msg_callback,
read_msg_callback_sig);
SetPointerField(env, instance, reinterpret_cast<jlong>(readMsgCallbackID), "read_callback_id");

const char *pSession = env->GetStringUTFChars(session, JNI_FALSE);
auto *pResult = dkls_comm(index,parties,pSession,readMsgCallback,sendMsgCallback,error_ptr);
auto *pResult = dkls_comm(index,parties,pSession,readMsgCallback,sendMsgCallback,instance,error_ptr);
env->ReleaseStringUTFChars(session, pSession);
setErrorCode(env, dkls_error, errorCode);
return reinterpret_cast<jlong>(pResult);
Expand All @@ -110,9 +112,11 @@ Java_com_web3auth_tss_1client_1android_dkls_DKLSComm_jniDklsComm(JNIEnv *env, jo
extern "C"
JNIEXPORT void JNICALL
Java_com_web3auth_tss_1client_1android_dkls_DKLSComm_jniDklsCommFree(JNIEnv *env, jobject thiz) {
env->DeleteGlobalRef(handler);
handler = nullptr;
jlong pObject = GetPointerField(env, thiz);
auto pObj = reinterpret_cast<DKLSMsgComm *>(pObject);
dkls_comm_free(pObj);
auto instance = dkls_comm_free(pObj);
if (instance != nullptr) {
auto obj_ref = reinterpret_cast<jobject>(const_cast<void*>(instance));
env->DeleteGlobalRef(obj_ref);
}
}
3 changes: 0 additions & 3 deletions tss-client-android/src/main/cpp/jniPrecompute.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand Down
3 changes: 0 additions & 3 deletions tss-client-android/src/main/cpp/jniSignatureFragments.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand Down
3 changes: 0 additions & 3 deletions tss-client-android/src/main/cpp/jniThresholdSigner.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand Down
3 changes: 0 additions & 3 deletions tss-client-android/src/main/cpp/jniUtilities.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//
// Created by grvgo on 28-07-2023.
//
#include <jni.h>
#include "include/dkls.h"
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public final class DKLSComm {
@SuppressWarnings("all")
private final long pointer;

@SuppressWarnings("unused") // linter cannot detect that this is called from the JNI
final long read_callback_id = 0;
@SuppressWarnings("unused") // linter cannot detect that this is called from the JNI
final long send_callback_id = 0;

@SuppressWarnings("unused") // linter cannot detect that this is called from the JNI
private String readMsg(String session, byte[] index_bytes, byte[] remote_bytes, String msgType) {
if ("ga1_worker_support".equals(msgType)) {
Expand Down

0 comments on commit 8105cec

Please sign in to comment.