Page 1 of 1

Porting NTL to iOS (iPhone/iPad)

PostPosted: Wed Jun 01, 2016 1:41 pm
by neteali
Hi, I try to compile NTL for armv7, armv7s and arm64 processors to use in a iPhone, but only can do it with i386 and x86_64 processors.
Is possible compile NTL for iOS?

Re: Porting NTL to iOS (iPhone/iPad)

PostPosted: Wed Jun 01, 2016 2:56 pm
by victorshoup
Hi!
I've gotten requests now and again from people who want to do something like this.
While I can't do this myself, I'm willing to help.
If you want to try to make it happen, it would be very nice...

Here are some initial comments.

First, NTL's build system is not set up very well for cross compilation.
In particular, various steps (like building "mach_desc.h") should be done on
the target machine. Likewise, there is also the "performance tuning Wizard".
That said, it should not be too hard to set these things by hand...

Second, I hope you can get GMP setup on your target. NTL runs much faster there.

Let me know how I can help...
It should be possible to do this...but it will just be slightly painful...

Re: Porting NTL to iOS (iPhone/iPad)

PostPosted: Thu Dec 08, 2016 4:07 pm
by swbrenneis
I have started working on this. Here is where it is at the moment...

GMP has been successfully built for the iPhone simulator. It fails a couple of the make check tests. I will try to dig into that, but the library seems to be usable. Several people have successfully built GMP on both Mac OS and iOS.

Building NTL for the simulator is stuck on its dependence on a shared library. It seems to be libm.

Some notes:

All libraries that NTL depends on have to be static and they have to be built specifically for the simulator and whichever arm processors will be necessary. At the moment, that would be arm6 and arm7. It may be necessary to build libm specifically for this. Many years ago, it was possible to build some of the glibc libraries without building all of them on Linux, but Apple products are based on BSD. I haven't done anything like that for a long time, so I will have to investigate. If there are code changes possible to avoid that dependency it would be helpful.

As you mentioned, all of the performance tuning would have to be done manually for the moment. It would be possible to build an iOS application that does this, but it would have to go through all of the vetting that Apple does to get it into the App Store. Building for the simulator is going to optimize it for the processor that runs the simulator. Is there documentation on what is in the machine description header? I have looked through it, but I would need help to know what each macro does. We might be able to create one based on knowledge of the processor architectures.

I am using NTL version 9.7.0 for this. When I get a working build, I will put it on github and post a link here. The optimal end result will be for us to get this into a Cocoa Touch Framework. Once a build is generated for the simulator, this shouldn't be a problem. There will be some tedious work renaming the files. Clang++ doesn't like source files to have the ".c" extension.

Thank you for this invaluable library. I think that many people will use it on iOS. The state of user space cryptography on iOS is pretty dreadful. This should help immensely.

Re: Porting NTL to iOS (iPhone/iPad)

PostPosted: Tue Dec 20, 2016 5:36 pm
by swbrenneis
Update.

I have successfully created a Cocoa Touch framework for NTL. The only class fully implemented so far is ZZ. All problems described above were mitigated by simply importing the source files to XCode. The mach_def.h file I'm using at the moment is the one that was created when I ran the makefile on my MacBook. This will probably not be sufficient when it goes on a native arm processor. I am using the ZZ class for a BigInteger implementation and generating primes for an RSA key pair. The performance on the simulator is reasonable. I can consistently generate 2048 bit RSA keys in under 30 seconds. Most of the time, it takes 10 seconds or less. The secure RNG on iOS is based on Fortuna, so I have a lot of confidence in the primes being generated. I have not had to modify any core code to make this work.

You can view the github repository here:

https://www.github.com/swbrenneis/NTL_iOS

It is a public repository, so you can clone it at will. The repository contains everything needed to use it in XCode.