Page 1 of 1

Bignum library - a must

Posted: Sun Mar 10, 2013 11:44 pm
by PeterH
Hi everyone.

Been tossing and turning during the nights for quite some time now. I'm thinking about buying a license, but everything seems to fall on two main issues.
  1. 1) RSA... No built-in bignum in PureBasic, and the few examples I have found and understood are painfully slow when it comes to implementing and testing to encrypt/decrypt with 2048+ bit keys. Can't live without it, and preferrably in native commands IF POSSIBLE since I REALLY need the portability to OSX and Linux including not needing to redo the whole shebang when switching compiling mode between x86 and x64.
  1. 2) The "SendNetworkFile()" was apparently removed due to possible exploitation. Are there any ready-to-use commands on the way? It seemed really handy.
Any help, code examples, multiplatform libraries (preferrably with source since there might come new platforms later on which needs this as well), etc appreciated.

//Peter

Re: Bignum library - a must

Posted: Sun Mar 10, 2013 11:50 pm
by LuCiFeR[SD]
solutions are available if you use the search feature.... but to save you some time

http://www.purebasic.fr/english/viewtop ... rksendfile

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 2:23 am
by tj1010
Couple of notes:

1. Anything with ReceiveNetworkData needs session id per stream per user. I've yet to see this done in any code ever posted here... If you don't know why, go write anything that handles >1 streams from any single client and actually use it...

2. RSA up to 4096 can be done cross platform with API in very little code. Don't hold your breath on PB functions for it...

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 11:19 am
by PeterH
LuCiFeR[SD] wrote:solutions are available if you use the search feature.... but to save you some time

http://www.purebasic.fr/english/viewtop ... rksendfile
Well, I feel quite dumb now. I must've read nearly every other post except that one. That one is brilliant! Thank you!

Regarding the other issue, I thought it should be possible to write a decent RSA-algorithm without using API just to simplify the cross platform code. The functions needed are quite few, and a friend gave me an example quickly thrown together in C++ that I don't understand and can't port but that covers the essential functions needed for RSA. Hopefully it might help others:

Code: Select all

        std::vector<uint32_t> v;
 
        bigint operator +(const bigint &rhs) const {
                std::vector<uint32_t> s;
                size_t n;
                if (v.size() < rhs.v.size()) {
                        s = rhs.v;
                        n = v.size();
                } else {
                        s = v;
                        n = rhs.v.size();
                }
                bool c = false;
                for (size_t i = 0; i < n; i++) {
                        s[i] = v[i] + rhs.v[i];
                        if (s[i] < v[i]) {
                                if (c) {
                                        s[i]++;
                                }
                                c = true;
                        } else if (c) {
                                s[i]++;
                                c = (s[i] == 0);
                        }
                }
                for (size_t i = n; c && i < s.size(); i++) {
                        s[i]++;
                        c = (s[i] == 0);
                }
                if (c) {
                        s.push_back(1);
                }
                return bigint(s);
        }
 
        bigint operator *(const bigint &rhs) const {
                bigint b(*this);
                bigint p(0);
                for (size_t i = 0; i < 32 * rhs.v.size(); i++) {
                        if (rhs.testbit(i)) {
                                p = p + b;
                        }
                        b = b + b;
                }
                return p;
        }
 
        bigint modpow(const bigint &exponent, const bigint &modulus) const {
                bigint b(*this);
                bigint r(1);
                for (size_t i = 0; i < 32 * exponent.v.size(); i++) {
                        if (exponent.testbit(i)) {
                                r = (r * b) % modulus;
                        }
                        b = (b * b) % modulus;
                }
                return r;
        }
 
        bool testbit(size_t n) const {
                size_t i = n >> 5;
                size_t b = n & 31;
                return ((v[i] >> b) & 1);
        }

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 4:11 pm
by LuCiFeR[SD]
you might need to do some additional work to update the code to current versions... maybe some additional features to add too, but again the forum search is your friend.

http://www.purebasic.fr/english/viewtopic.php?t=37047
http://www.purebasic.fr/english/viewtopic.php?t=37186

if nothing more, it will kind of give you an idea whether the language is for you :) Unfortunately, encryption is not my strong point and I am not really the best man to ask, but I guess the authors of the respective code may have already done updates or have come up with better solutions?

Just some things to bare in mind though. PB is a fun language, the syntax does change from time to time and code can get broken. especially if you haven't had to update the code for a few versions :). but on the whole it is easy enough to fix, or people create their own solutions and post them for the benefit of others.

People here are mostly friendly and helpful... some like me can be a little short tempered and irritating :).

but as languages go.. the price is good, the free updates for life is good. But just remember, like any language, it has strengths and weaknesses. It isn't the perfect tool for every task, but it really does have a damn good go at being that tool :)

oh, and by the way... Welcome to the forums.

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 4:42 pm
by PeterH
I have looked around the forum and the codes that I can even grasp and use seems to be too slow to be useful. I have a friend testing them out and they're taking several seconds to complete. Took a wild guess and wrote a guy privately to see if he ever got RSA working. Will see if I get a reply. I'm still interested in the language I must admit, but without these features I don't know if I can motivate the purchase to the wife. ;)

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 4:53 pm
by LuCiFeR[SD]
Just remember... never try to do a speed test with the debugger on. use a message requester or something at the very end of the code to display the results. it is a lot more meaningful when you try to compare then. Also, external dlls can be used as can static libs. takes a little work to get things going sometimes, but that is the beauty of a place like this, people have probably already done it. you might not get an instant reply and in rare cases people just don't know :)

also you can use inline asm, which if it is written correctly will work across the platforms.

but, hell, my job is not to persuade you to use the language, the choice is yours... :)

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 8:50 pm
by davido
PeterH:

Will any of the routines in my post help you?

http://www.purebasic.fr/english/viewtop ... 27&t=52813

I, too would like Bignum but I am not skilled enough to write a wrapper nor strong enough to twist the arms of another!

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 10:00 pm
by PeterH
LuCiFeR[SD] wrote:Just remember... never try to do a speed test with the debugger on. use a message requester or something at the very end of the code to display the results. it is a lot more meaningful when you try to compare then. Also, external dlls can be used as can static libs. takes a little work to get things going sometimes, but that is the beauty of a place like this, people have probably already done it. you might not get an instant reply and in rare cases people just don't know :)

also you can use inline asm, which if it is written correctly will work across the platforms.

but, hell, my job is not to persuade you to use the language, the choice is yours... :)
Yes, I have disabled the debugger. Took me a while at first to figure out that I had done that mistake but even after so it's still too slow with the implementations I've tried. Worth noting that my math skills are somewhat similar to a 9-year old.

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 10:05 pm
by PeterH
davido wrote:PeterH:

Will any of the routines in my post help you?

http://www.purebasic.fr/english/viewtop ... 27&t=52813

I, too would like Bignum but I am not skilled enough to write a wrapper nor strong enough to twist the arms of another!
Actually, that one was one that I've experimented the most with and it's also the reason that one button on my laptop is only partially working. No joke. Got so pissed when I couldn't figure out how to implement a ModPow (the Schneier-method described above) and screwed up the sourcecode too many times. One falcon punch later... well.

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 10:09 pm
by jack
the LibTomMath and LibTomCrypt may be of interest http://libtom.org/ , it's open source under the WTFPL license.
it's easy to use in PB.

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 11:07 pm
by MachineCode
PeterH wrote:I have looked around the forum and the codes that I can even grasp and use seems to be too slow to be useful.
In my experience, good fast code depends on the coder. I recently had to do some string manipulations that were taking around 30 seconds to complete, despite contributions from several people. Then, an amazing guy posted his version which did what I wanted in about 20 ms! So, the speed is there if you know your stuff. :)

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 11:33 pm
by STARGÅTE
@PeterH:

If you only need a code for memory arithmetic (add, multiply, divide ... by unlimited memory blocks, binary without conversion to decimal system) then I can give you an include (based on ASM) of me. (PN me)
For example:

Code: Select all

MemoryArithmetic_Multiplication(*Destination, *Source1, 24, *Source2, 16)
This Procedure multipy *Source1 (with 24 Bytes) and *Source2 (with 16 Bytes) and store the result (40 Bytes) in *Destination.

Re: Bignum library - a must

Posted: Mon Mar 11, 2013 11:55 pm
by PeterH
jack: I would prefer not using an external library just so that it could be compiled for other operating systems as well in the future. If inlineasm, then at least it'll compile on operating systems of the same architecture. Built-in commands will just be way easier, but possibly not fast enough for my purposes.

MachineCode: You're absolutely right, and the issue is simply being not skilled enough to write my own, and even if I could it'd probably still be too slow unless I'm using inline assembler for all the time critical loops.

STARGÅTE: You got mail! :) Thanks!

Re: Bignum library - a must

Posted: Thu Mar 14, 2013 2:09 pm
by PeterH
STARGÅTE wrote:@PeterH:

If you only need a code for memory arithmetic (add, multiply, divide ... by unlimited memory blocks, binary without conversion to decimal system) then I can give you an include (based on ASM) of me. (PN me)
For example:

Code: Select all

MemoryArithmetic_Multiplication(*Destination, *Source1, 24, *Source2, 16)
This Procedure multipy *Source1 (with 24 Bytes) and *Source2 (with 16 Bytes) and store the result (40 Bytes) in *Destination.
Slight misunderstanding. Not an include. A compiled library. Rules out compatibility for cross-compiling as well as the possibility to rework it slightly to support the "Schneier-modpow". Thank you anyway.