The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Astro on March 05, 2010, 09:40:35 PM

Title: Modulo Arithmetic
Post by: Astro on March 05, 2010, 09:40:35 PM
Hi,

I guess I need the FPU for this?

Is there a listing of FPU op codes, like opcodes.chm in the MASM help folder?

Best regards,
Robin.
Title: Re: Modulo Arithmetic
Post by: dedndave on March 05, 2010, 10:11:28 PM
use the forum search tool
if you use advanced search - look for modulo division (i think) - responses from E^cube only
he was asking about this some time ago
Title: Re: Modulo Arithmetic
Post by: clive on March 05, 2010, 10:33:33 PM
There isn't a modulo function in floating point. ie you get a fractional result, not a remainder.

A quick grab of the 'F' opcodes gives

f2xm1
fabs
fadd
faddp
fbld
fbstp
fchs
fcmovb
fcmovbe
fcmove
fcmovnb
fcmovnbe
fcmovne
fcmovnu
fcmovu
fcom
fcom2
fcomi
fcomip
fcomp
fcomp3
fcomp5
fcompp
fcos
fdecstp
fdisi
fdiv
fdivp
fdivr
fdivrp
femms
feni
ffree
ffreep
fiadd
ficom
ficomp
fidiv
fidivr
fild
fimul
fincstp
fist
fistp
fisttp
fisub
fisubr
fld
fld1
fldcw
fldenv
fldl2e
fldl2t
fldlg2
fldln2
fldpi
fldz
fmul
fmulp
fnclex
fninit
fnop
fnsave
fnstcw
fnstenv
fnstsw
fpatan
fprem
fprem1
fptan
frndint
frstor
fscale
fsetpm
fsin
fsincos
fsqrt
fst
fstp
fstp1
fstp8
fstp9
fsub
fsubp
fsubr
fsubrp
ftst
fucom
fucomi
fucomip
fucomp
fucompp
fxam
fxch
fxch4
fxch7
fxrstor
fxsave
fxtract
fyl2x
fyl2xp1


-Clive
Title: Re: Modulo Arithmetic
Post by: oex on March 05, 2010, 10:38:27 PM
xor edx, edx
mov eax, 10
mov ecx, 3
div ecx

eax contains 3
edx contains 1
Title: Re: Modulo Arithmetic
Post by: clive on March 05, 2010, 10:49:05 PM
I guess we'd need to guess at the point/usage being looked for. Usually, I'd be interested in separating the integer and fractional parts, and not screwing up the rounding.
For example with 59.9999999 seconds, and "large" values of 0.9999999 becoming 1.0 when it is technically still smaller. (There are precision issues representing base 10 numbers in base 2, removing the most significant digits suddenly causes additional precision to appear in the least significant ones)

-Clive
Title: Re: Modulo Arithmetic
Post by: qWord on March 06, 2010, 12:24:50 AM
Quote from: Astro on March 05, 2010, 09:40:35 PM
Is there a listing of FPU op codes
AMD64 Architecture Programmer's Manual Volume 5: 64-Bit Media and x87 Floating-Point Instructions (http://support.amd.com/us/Processor_TechDocs/26569.pdf)
Title: Re: Modulo Arithmetic
Post by: Astro on March 06, 2010, 02:04:26 AM
Hi,

Great!

I want to do arbitrary precision math.

Quoteuse the forum search tool
Good point! I visit more forums where it is broken or disabled than where they work so I don't tend to think about it.

Quoteif you use advanced search - look for modulo division (i think) - responses from E^cube only
he was asking about this some time ago
OK!

Best regards,
Robin.
Title: Re: Modulo Arithmetic
Post by: dedndave on March 06, 2010, 02:59:56 AM
Drizz has a bignum library on his webpage - not sure about modulo division
but, if you wanted to, you could divide, then modulus = dividend - (quotient x divisor)
Title: Re: Modulo Arithmetic
Post by: raymond on March 06, 2010, 03:14:11 AM
QuoteI want to do arbitrary precision math.

The FPU will give you at most 64 bits of precision (equivalent to approx. 19 decimal digits). That precision can be less depending on the precision of the input and the type/quantity of operations performed to the input.

Arbitrary precision is generaly regarded as requiring significantly more than the 64 bits of precision, such that the use of the FPU cannot be sufficiently precise. You then have to rely on integer operations to provide the required precision.

If you only need the basic arithmetic operations such as addition, subtraction and multiplications, this can be achieve easily. Divisions by large numbers will be a little more difficult. Computing logarithms, exponentials and trigonometric functions is considerably more difficult.

I don't know what your objectives are but I wish you the best in your endeavour.
Title: Re: Modulo Arithmetic
Post by: Astro on March 07, 2010, 07:49:56 AM
I want to implement the Blum Blum Shub algorithm - it is a CSPRNG based on the same principles as RSA.

http://en.wikipedia.org/wiki/Blum_blum_shub

Best regards,
Robin.
Title: Re: Modulo Arithmetic
Post by: Eddy on March 07, 2010, 09:51:41 PM
Blum-Blum-Shub PRNG requires the use of big numbers, so the FPU will be not much good to you.
You need to use a bignum library as Dave already suggested.

Kind regards
Title: Re: Modulo Arithmetic
Post by: dedndave on March 07, 2010, 11:50:02 PM
i remember seeing Blum Blum Shub in here before - you might try the forum search tool
Title: Re: Modulo Arithmetic
Post by: raymond on March 08, 2010, 03:53:55 AM
QuoteI want to implement the Blum Blum Shub algorithm

That algo is designed to generate pseudo random numbers. You have not mentioned the range you intend to generate.

If that range is within the range of a dword (less than 32 bits), you don't need a bignum library, and you need not use the FPU. If your intention is for encryption with ultra-high security, that is a totally different ball game; then you need to use a bignum library of functions which you either write yourself or adapt from someone else's work.
Title: Re: Modulo Arithmetic
Post by: dsouza123 on March 13, 2010, 04:16:53 AM
A version of Blum Blum Shub with dword sized p and q.
Title: Re: Modulo Arithmetic
Post by: Astro on March 21, 2010, 03:57:30 AM
Hi,

Thanks dsouza123!

The range I'm looking at is large - upwards of 2048-bit. After posting here I actually starting taking issue with the fact it was pseudo, and contemplated finding a hardware source. I was also reading that B-B-S may have a serious weakness in its algorithm due to the way the numbers are generated, and that it seems it might be possible to influence the output or determine its state.

The more I read the less I liked about any of it, and wondered if it was worth the hassle.

I did wonder about an alternating step generator with a very long period. It seems to be as solid as anything, and the start state can be generated easily, and it seems, securely, if a hash of the output is fed back in as an input. It could then be initialized by running it for a random length of time before taking bits from it so its state is unknown. It also simplifies it as I don't have to mess around finding primes.

* Generate random data
* hash it
* hash the output several times, adding random data to the mix
* insert this output as the start state of the LFSR
* run the LFSR for a time
* start taking bits from it

Best regards,
Robin.