PHP's bcmath in bmax?

BlitzMax Forums/BlitzMax Programming/PHP's bcmath in bmax?

kronholm(Posted 2008) [#1]
Hey guys, been a while since I posted :) Hope everyone is doing alright!

I have a problem :p I'm trying to port aochat (found here http://aocchat.googlecode.com/svn/trunk/AOChat.php) which is a php class that makes it possible to communicate with Age of Conan chat servers.



Problem is, the class I'm trying to port relies heavily on the bcmath lib that's builtin to php. There's a page here explaining what bcmath is: http://mgccl.com/2007/01/26/introduction-to-bcmath

Does anyone know if there's libraries available for blitzmax to perform similar operations such as bcadd, bcmod etc.? I'm by no means a math geek, so it's impossible for me to understand the concepts, sadly.


Brucey(Posted 2008) [#2]
How's about : MAPM?


kronholm(Posted 2008) [#3]
Thanks a bunch Brucey! Time to test that baby out! :D


kronholm(Posted 2008) [#4]
Ugh, either I'm sleepy or I'm not getting how to use your module properly Brucey.

The place I'm stuck at is the following:

	function generate_login_key($servkey, $username, $password)
	{

                        $dhY = "0x9c32cc23d559ca90fc31be72df817d0e124769e809f936bc14360ff4bed758f260a0d596584eacbbc2b88bdd410416163e11dbf62173393fbc0c6fefb2d855f1a03dec8e9f105bbad91b3437d8eb73fe2f44159597aa4053cf788d2f9d7012fb8d7c4ce3876f7d6cd5d0c31754f4cd96166708641958de54a6def5657b9f2e92";
		$dhN = "0xeca2e8c85d863dcdc26a429a71a9815ad052f6139669dd659f98ae159d313d13c6bf2838e10a69b6478b64a24bd054ba8248e8fa778703b418408249440b2c1edd28853e240d8a7e49540b76d120d3b1ad2878b1b99490eb4a2a5e84caa8a91cecbdb1aa7c816e8be343246f80c637abc653b893fd91686cf8d32d6cfe5f2a6f";
		$dhG = "0x5";
		$dhx = "0x".$this->get_random_hex_key(256);

		$dhX = $this->bcmath_powm($dhG, $dhx, $dhN);
		$dhK = $this->bcmath_powm($dhY, $dhx, $dhN);


Basically, I can't figure out how to use tmapm.pow() for this. I've tried a lot of things, but can't seem to wrap my head around it :( I'll go sleep on it, but would really appreciate if you could give me a hint! :D


Brucey(Posted 2008) [#5]
You need to convert the rather large hex number into a looooooong integer representation - just like what AOChat does...

eg.
...
	function bcmath_powm($base, $exp, $mod)
	{
		$base = $this->bighexdec($base);
		$exp  = $this->bighexdec($exp);
		$mod  = $this->bighexdec($mod);
...

and
	function bighexdec($x)
	{
		if(substr($x, 0, 2) != "0x")
		{
			return $x;
		}
		$r = "0";
		for($p = $q = strlen($x) - 1; $p >= 2; $p--)
		{
			$r = bcadd($r, bcmul(hexdec($x[$p]), bcpow(16, $q - $p)));
		}
		return $r;
	}


The rest should be easy to port..

;-)


kronholm(Posted 2008) [#6]
The thing I'm stuck on, and I mean seriously stuck on, is this:

$r = bcadd($r, bcmul(hexdec($x[$p]), bcpow(16, $q - $p)));


How would I even begin to write that with your module? I've tried breaking it up in seperate pieces, but with frustrating results :) I'm sure, again, that I'm missing something basic?

The bcpow part (bcpow(16, $q - $p)));), as I understand it, should be something like this:
		Local sixteen:TMAPM = New TMAPM.Create("16")
		Local bcpow:TMAPM = New TMAPM
		bcpow = bcpow.pow(sixteen,q-p)
		Print bcpow.tostring()



kronholm(Posted 2008) [#7]
Two hours later :p Finally got it working, the bighexdec that is :) Thanks again Brucey, your module saved my day!

Local key$ = "0xc48840545531b0c3fee627c46f13bc5a36c05e120685150f2fc8aff7a9f50f45"
Print bighexdec(key)

Function bighexdec$(x$)
	If Mid(x,0,3) <> "0x" Print "Start of key aint '0x'." ; Return
	Local r:TMAPM = New TMAPM
	Local q = Len(x)-1
	For Local p = q To 2 Step -1
		Local power:TMAPM = New TMAPM.Create(q-p)
		Local powresult:TMAPM = New TMAPM.Create(16)
		powresult = powresult.pow(power,160)
		Local decresult = hexdec(Chr(x[p]))
		Local mulresult:TMAPM = New TMAPM.Create(decresult)
		mulresult = mulresult.multiply(powresult)
		Local addvalue:TMAPM = New TMAPM.Create(r.tostring())
		r = addvalue.add(mulresult)
	Next
	Local result$ = r.tostring()
	Return Left(result, Len(result)-2)	'return result with no '.0'
EndFunction



kronholm(Posted 2008) [#8]
Any reason there's no modulus function in MAPM, Brucey? Could really use it :p


Brucey(Posted 2008) [#9]
Glad you got there in the end :-)

I only implemented the API that MAPM provides... (means I don't have to think too hard!)

Anyhoo, how's about this for now :
SuperStrict
Framework BaH.MAPM
Import BRL.StandardIO

Function modulo:TMAPM(x:TMAPM, modY:TMAPM)

	Local a:TMAPM = x.IntegerDivide(modY)
	Local b:TMAPM = a.Multiply(modY)
	Return x.Subtract(b)

End Function


Function showMod(x:String, y:String)
	Print x + " mod " + y + " = " + modulo(New TMAPM.Create(x), New TMAPM.Create(y)).ToIntString()
End Function

showMod(20, 7)
showMod(7, 20)
showMod(144, 12)
showMod(360, 17)



I suppose we could have a built-in version of it :-)


kronholm(Posted 2008) [#10]
Thanks Brucey, again :) The modulo definitely works and helped me out greatly!


kronholm(Posted 2008) [#11]
Hey Brucey? Don't suppose you have a ported version of php's pack and unpack for BlitzMax? :p