BigInt module (SSE2)

Share your advanced PureBasic knowledge/code with the community.
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: BigInt module (SSE2)

Post by AZJIO »

Can this module be made available in SpiderBasic? I used this in a password generator and would like the program to be available on the phone.
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: BigInt module (SSE2)

Post by mk-soft »

No,

its intel assembler code. No java or no arm processors ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: BigInt module (SSE2)

Post by jack »

doesn't javascript have a bigint type?
I say this because of https://pi-calculator.netlify.app/
it works offline in the browser
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: BigInt module (SSE2)

Post by AZJIO »

There is a BigNum library on AutoIt3, it generally works with a string. That is, it performs step-by-step calculations, and the result is written to a string. It works slowly, but it works. I would like it to be possible to get even a slowly working, but the same result. On asm it works instantly, on AutoIt3 - half a second. But this is not critical for my program.
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: BigInt module (SSE2)

Post by Al_the_dutch »

Thx for all the great work. I ran into the problem that I get unexpected results. :o Can anyone help? Thanx!

Code: Select all

IncludeFile("..\bigint.pbi")

UseModule BigInt

EnableExplicit

Procedure Hex2Dec(hex$)
   ProcedureReturn Val("$"+hex$)
EndProcedure

; http://www.javascripter.net/math/calculators/100digitbigintcalculator.htm
Define.BigInt h1, h2, h3, h4, h5, h4_hex$

SetValue(h1, 9)
SetValue(h2, 27)
 
Debug "Modulus Exponentiation: 9 ^ 27 mod 27 = 3*9, I expect 0 but get 20?!"
ModPow(h4, h1, h2, h2)
h4_hex$ = GetHex(h4)
Debug "ModPow(h4, 9, 27, 27) = " + Hex2Dec(h4_hex$)

SetValue(h1, 9)
SetValue(h2, 81)

Debug "Modulus Exponentiation: 9 ^ 81 mod 81 = 9*9, I expect 0 but get 72?!"
ModPow(h4, h1, h2, h2)
h4_hex$ = GetHex(h4)
Debug "ModPow(h4, 9, 27, 27) = " + Hex2Dec(h4_hex$)

End; EndItAll
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: BigInt module (SSE2)

Post by Little John »

Al_the_dutch wrote: Tue Feb 28, 2023 4:19 pm Thx for all the great work. I ran into the problem that I get unexpected results. :o Can anyone help? Thanx!
Unfortunately, you did not write what PB version and what operating system you used when you got those results.

I tested your above code with PB 6.00 LTS (x64) and PB 6.01 beta 4 (x64) on Windows 11, and I always got the result "0", as expected.

BTW:
Near the end of your code, it reads
Debug "ModPow(h4, 9, 27, 27) = " + Hex2Dec(h4_hex$)
but it should be
Debug "ModPow(h4, 9, 81, 81) = " + Hex2Dec(h4_hex$)
:)
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: BigInt module (SSE2)

Post by Al_the_dutch »

Both good points. I use PureBasic 6.00 LTS (Windows - x64) on Windows 10 Home... Strange! :o

CPU: Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz 3.30 GHz

Some more searching: I get the expected zero's with
#BigIntBits = 512; not with 2048
in BigInt.pbi. It was on 2048, a multiple of 512.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: BigInt module (SSE2)

Post by Little John »

Yes, that's strange.
I used Wilbert's code as is in the first post of this thread, i.e.
#BigIntBits = 2048.

CPU: Intel(R) Core(TM) i5-1035G4 CPU @ 1.10GHz 64 bit
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: BigInt module (SSE2)

Post by Al_the_dutch »

The problem only seems to be on my PC with #BigIntBits = 2048, I tried 512,1024 and 4096 without a problem.
You even don't have a problem with #BigIntBits = 2048 and we both use Windows x64.
I wonder if there's anyone who knows the possible reason for it. But I am fine now, I have a workaround. Thx!
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: BigInt module (SSE2)

Post by Al_the_dutch »

I did a test with many calculations and compared the output between runs with 512,1024, 2048 and 4096 as #BigIntBits. All were the same with the exception of 2048 on many cases. As long as nobody has an explanation, I would suggest that if you use BigInt, you compare the output with the output using another #BigInts to be sure that this issue don't effect your situation. Above all I am happy to be able to use BigInt! :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: BigInt module (SSE2)

Post by wilbert »

Unfortunately I also haven't got a clue.
Does this happen only with ModPow or also with ModMul ?
Windows (x64)
Raspberry Pi OS (Arm64)
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: BigInt module (SSE2)

Post by Al_the_dutch »

Thx wilbert.
I did not use ModMul, so I wrote a tester with it and I did not get any differences in a wide range between running with #BigIntBits 2048 and 4096.
I also tested with 216*46656 mod 9 = 6^9 mod 9 should be 0 and it is. With ModPow 6^9 mod 9 and #BigIntBits 2048 it gives not 0 on my PC.
So the problem seems only related to ModPow with #BigIntBits 2048 on my PC. I am almost thinking it might be a specific CPU problem.
@wilbert, what is the influence of #BigIntBits in general?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: BigInt module (SSE2)

Post by wilbert »

Al_the_dutch wrote: Sat Mar 04, 2023 11:20 am So the problem seems only related to ModPow with #BigIntBits 2048 on my PC. I am almost thinking it might be a specific CPU problem.
Unfortunately I don't know since I can't reproduce the problem.
Al_the_dutch wrote: Sat Mar 04, 2023 11:20 am @wilbert, what is the influence of #BigIntBits in general?
It determines the amount of bits for a number.
The two biggest native types PB supports are 32 and 64 bit numbers.
When you set #BigIntBits to 512, it's 512 bit numbers etc.
The bigger values for #BigIntBits you choose, the slower it will be.
Windows (x64)
Raspberry Pi OS (Arm64)
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: BigInt module (SSE2)

Post by AZJIO »

Can you adapt to version 6.03?
*vn -> vn3 ?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: BigInt module (SSE2)

Post by wilbert »

AZJIO wrote: Sun Dec 03, 2023 8:39 am Can you adapt to version 6.03?
*vn -> vn3 ?
Try moving the *vn declaration inside the divmod_private procedure to the line above like this.

Code: Select all

    Protected.BigInt un, vn, *vn
    Protected.i qhat, rhat, *un.Quad
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply