miracl / MIRACL

MIRACL Cryptographic SDK: Multiprecision Integer and Rational Arithmetic Cryptographic Library is a C software library that is widely regarded by developers as the gold standard open source SDK for elliptic curve cryptography (ECC).
https://miracl.com
653 stars 242 forks source link

Any guide for using the library on Android? #45

Closed YangRongAtGit closed 7 years ago

YangRongAtGit commented 7 years ago

Anywhere I could find a guide about use the library on Android studio 2.3? Thanks [SLOVED]

YangRongAtGit commented 7 years ago

environment The target android SDK version is 25 android studio version 2.3

What I did is copied header and source files into android project and use CMake to generate project files containing all sources and hearders. And to generate a shared library for an android application

During that process, I have following compile issues. In the file mrmuldv.c (renamed from mrmuldv.g64 or for 32 bit system mrmuldv.gcc)

mr_small muldvm(mr_small a,mr_small c,mr_small m,mr_small *rp)
{
    mr_small q;
    __asm__ __volatile__ (
    "movl %1,%%edx\n"
    "movl %2,%%eax\n"
    "divl %3\n"
    "movl %4,%%ebx\n"
    "movl %%edx,(%%ebx)\n"
    "movl %%eax,%0\n"
    : "=m"(q)
    : "m"(a),"m"(c),"m"(m),"m"(rp)
    : "eax","ebx","memory"
    );        
    return q;
}

I tried both version of the mrmuldv.c the 32 bit version has following error Error:(76, 7) error: unknown register name 'rax' in asm

the amd64 version has following error Error:(21, 7) error: unknown register name 'eax' in asm

I also tried to remove the mrmuldv.c from the project. It turns out mrarth1.c, mrarth2.c, mrbuild.c and other files are calling function in the mrmuldv.c.

Is there a way I could disable the usage of assembly language in MIRACL? e.g using MR_NOASM?

It seems I should use mrmuldv.ccc. Will give it a try soon.

All suggestions are welcome

Thanks

Rong

mcarrickscott commented 7 years ago

Hello,

Problem here is that you are attempting to build for an Intel x86 target, whereas your processor is probably an ARM. Its probably best not to start with any assembly language.

Best to start with a standard C build of the library.

Mike

On Wed, Mar 29, 2017 at 11:30 PM, r0nG notifications@github.com wrote:

environment The target android SDK version is 25 android studio version 2.3

I tried both version of the mrmuldv.c the 32 bit version has following error Error:(76, 7) error: unknown register name 'rax' in asm

the amd64 version has following error Error:(21, 7) error: unknown register name 'eax' in asm

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/miracl/MIRACL/issues/45#issuecomment-290246138, or mute the thread https://github.com/notifications/unsubscribe-auth/ACm8jrSoI2Dkz7tZRlB_pibPXxm-z6KZks5rqttzgaJpZM4MrFmw .

YangRongAtGit commented 7 years ago

Hello Mike, Thanks for your suggestion. I realised that there is a ARM.txt file in the repository. I was using incorrect setting file.

Thanks

mcarrickscott commented 7 years ago

Sure, but that file contains in-line assembly language. You need to rename mrmuldv.ccc to mrmuldv.c

Alternatively follow instructions in ARM.TXT

Mike

On Thu, Mar 30, 2017 at 1:14 AM, r0nG notifications@github.com wrote:

Hello Mike, The error is from mrmuldv.c which is renamed from mrmuldv.g64. It is in the MIRACL library. I will edit my comment to make it clear.

Thanks

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/miracl/MIRACL/issues/45#issuecomment-290264246, or mute the thread https://github.com/notifications/unsubscribe-auth/ACm8jo9uY2K_dbBpuYcbQWb8O5PZZasLks5rqvPggaJpZM4MrFmw .

npj008 commented 7 years ago

@YangRongAtGit Can you tell me steps to use this library in android project. I am very much new to android so. Thanks in advance.

mcarrickscott commented 7 years ago

Sorry, I have no experience of using MIRACL with Android. However as I indicated it can be configured as a pure C library.

So its just programming...

Mike

On Thu, Mar 30, 2017 at 6:56 PM, npj008 notifications@github.com wrote:

@YangRongAtGit https://github.com/YangRongAtGit Can you tell me steps to use this library in android project. I am very much new to android so. Thanks in advance.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/miracl/MIRACL/issues/45#issuecomment-290491232, or mute the thread https://github.com/notifications/unsubscribe-auth/ACm8jvi1P2d2oGXQL4fZS_J7iBOGXwy4ks5rq-zHgaJpZM4MrFmw .

YangRongAtGit commented 7 years ago

For the developers who is using android studio 2.3 here is the CMake file for MIRACL. If the cmake file already in the project, I am sorry for the duplicated post :)

set (MIRACLE_SOURCE_DIR src/main/cpp/miracl/)

include_directories(${MIRACLE_HEADER_DIR})

file(GLOB MIRACLE_HEADER_FILES ${MIRACLE_SOURCE_DIR}/*.h)
file(GLOB MIRACLE_SOURCE_FILES ${MIRACLE_SOURCE_DIR}/*.c)

add_library( # Sets the name of the library.
             mrc-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/mrc-lib.cpp
             ${MIRACLE_HEADER_FILES}
             ${MIRACLE_SOURCE_FILES}
             )

find_library(log-lib log)

target_link_libraries(mrc-lib ${log-lib} )
YangRongAtGit commented 7 years ago

Another thing is it seems google fixed the bug mentioned in the ARM.txt. I run a test in a glue function as the doc stated:

    unsigned long long x;
    unsigned long a,b;
    a=0;
    b=0xFFFFFFFF;
    x=(unsigned long long)a-b;

The result is :

(lldb) p x
(unsigned long long) $1 = 18446744069414584321
(lldb) p/x x
(unsigned long long) $2 = 0xffffffff00000001
YangRongAtGit commented 7 years ago

@npj008 I am not an expert of android studio 2.3. By some reason I really dislike Java. Although I spent all my time in uni using Java as my main programming language. Anyway, the way I integrate MIRACL into an android studio 2.3 project is:

  1. New project, tick the option 'Include C++ support' in the first wizard page.
  2. In the last step of the wizard, make sure the C++ standard is 'Toolchain Default' (C11 might work I just didn't test). I hate exceptions, so no exception used in the project. rtti is slow, so rtti is out as well.
  3. A default shared c library (native-lib) will be generated alone with a lot all android stuff after you click the button 'Finish'.
  4. Now you have the code stub for a shared library. If you want to modify the name of the library and the generated cpp file (native-lib.cpp), you need to do following things. Otherwise just skip this step. 4.1 Right click the cpp file -> In the popup menu, select 'Refactor' -> 'Rename' 4.2 Find the CMake file. When you use the 'Android' view, the cmake file is under the folder 'External Build files'.
  5. Copy the MIRACL header / source files mentioned in the ARM.txt to where ever you want. Or just use the MIRACL default repository by changing the value of 'MIRACLE_SOURCE_DIR' in CMake .
  6. In the android studio, click build -> Refresh Linked C++ Projects
  7. Click the button 'Sync project with Gradle Files'. (Should not need this since the Gradle stuff is not been touched but I had strange problem fixed by sync gradle files).
  8. Write some test code in a block in the renamed / generated cpp file
    
    extern "C"
    {
    ...
    shs256_hash(...);

}


9. Clean & rebuild-> RUN your test
npj008 commented 7 years ago

Thanks for your super steps. I will try your step today. If you have any working demo project of android studio, then please attach at joshinikunj008 at gmaiel dot com Thank you...

simeon-aladjem commented 7 years ago

You might want to take a look at https://github.com/miracl/incubator-milagro-mfa-sdk-android. It is an Android SDK that wraps some crypto functionality. The crypto lib is not MIRACL, but its successor. In any case, you can see how the C/C++ code is wrapped in an Android/Java code. It also builds with Android Studio, so it will be helpful for you.