This JavaScript is driving me -mad-.

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

This JavaScript is driving me -mad-.

Postby WarDaft » Thu Oct 27, 2011 5:13 am UTC

So I'm trying to use a JS bignum library, despite not knowing JS. That's probably my first mistake. I'm using JSBN which is allegedly in Google's JS benchmark.

I've narrowed it down to a single line not working. Unfortunately, it's the only line that I actually need the JavaScript for. I've checked and rechecked the library, and the method I should want is .modPowInt(). As in base.modPowInt(exponent,modulus). The base, exponent, and modulus are all properly defined bignums, and I can use the other methods defined in the library on them fine. There's a demo on their site that uses .modPowInt() exactly the way I am, and it's working there, in the same browser, with the same loaded scripts.

The line is, exactly, no more or less than:
var ret = base.modPowInt(exp, mod);
... base, exp, and mod are all perfectly fine bignums, that other bignum functions from the same library work on.

But if I try to use it, the script goes from working to failing silently. I've tried everything I can think of.

If I get any more frustrated, I might start pulling out my hair.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1562
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: This JavaScript is driving me -mad-.

Postby Laguana » Thu Oct 27, 2011 8:04 am UTC

This might be obvious, but have you tried using firebug (a firefox extension) to see what your javascript is actually doing? It should also allow you to play with things on-the-fly so you can say "what _is_ x at this point here" and try to track down exactly where things break.
Laguana
 
Posts: 49
Joined: Sat Jan 19, 2008 10:13 pm UTC

Re: This JavaScript is driving me -mad-.

Postby WarDaft » Thu Oct 27, 2011 1:11 pm UTC

Not even having firefox installed, no I haven't tried that. I guess I shall. I don't see how much it can help, as I'm pretty sure that they're all valid bignums, but I don't have anything else to try at this point.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1562
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: This JavaScript is driving me -mad-.

Postby thedufer » Thu Oct 27, 2011 2:36 pm UTC

Alternatively, if you're in Chrome there's a fantastic Javascript console built in. Go to [wrench]->Tools->JavaScript Console (command-option-J on OS X).
User avatar
thedufer
 
Posts: 263
Joined: Mon Aug 06, 2007 2:11 am UTC
Location: Northern VA (not to be confused with VA)

Re: This JavaScript is driving me -mad-.

Postby mr-mitch » Thu Oct 27, 2011 3:35 pm UTC

thedufer wrote:Alternatively, if you're in Chrome there's a fantastic Javascript console built in. Go to [wrench]->Tools->JavaScript Console (command-option-J on OS X).


Ctrl Shift J on Windows.

Some people also say 'http://code.google.com/webtoolkit/speedtracer/' is a useful extension for chrome, but I've never used it.
mr-mitch
 
Posts: 480
Joined: Sun Jul 05, 2009 6:56 pm UTC

Re: This JavaScript is driving me -mad-.

Postby eta oin shrdlu » Thu Oct 27, 2011 6:21 pm UTC

WarDaft wrote:The line is, exactly, no more or less than:
var ret = base.modPowInt(exp, mod);
... base, exp, and mod are all perfectly fine bignums, that other bignum functions from the same library work on.
I think exp is supposed to be a plain integer for that function, not a bignum. Try it and see if it works.
User avatar
eta oin shrdlu
 
Posts: 431
Joined: Sat Jan 19, 2008 4:25 am UTC

Re: This JavaScript is driving me -mad-.

Postby WarDaft » Fri Oct 28, 2011 3:31 am UTC

eta oin shrdlu wrote:I think exp is supposed to be a plain integer for that function, not a bignum. Try it and see if it works.

That... would surprise me.

Actually no, no it wouldn't.

...

And you're exactly right. Guess I'm writing my own modular exponentiation function.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1562
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: This JavaScript is driving me -mad-.

Postby eta oin shrdlu » Fri Oct 28, 2011 3:54 am UTC

WarDaft wrote:
eta oin shrdlu wrote:That... would surprise me.

Actually no, no it wouldn't.
Heh.

... Actually, looking at this further it looks like the function you want is in jsbn2.js, as BigInteger.modPow.
User avatar
eta oin shrdlu
 
Posts: 431
Joined: Sat Jan 19, 2008 4:25 am UTC

Re: This JavaScript is driving me -mad-.

Postby WarDaft » Fri Oct 28, 2011 5:42 am UTC

I tried using jsbn2.js, but then it wouldn't let me make bigints at all.

I wrote my own modExp easily enough though.

I'm nearly done now, though I probably won't finish tonight. For the curious, it's part of the authentication process for a bootstrapping php page. Not bootstrapping as it seems to be used in regards to php, but rather a simple php page that lets you log in and edit page sources directly on the server. I know there's probably something very much like it but better, but I wanted to write my own. I also finally decided to write a secure login, but didn't want to mess around with https yet. I Googled for one and found people doing things like pre-hashing passwords and sending that, which is essentially just sending a different password in plaintext, and some schemes even stored that hash directly in a database and computing a new one with a salt, leaving the effective password in plaintext! I took a modified Diffie-Hellman key exchange scheme, in that the server maintains a single exponent E, which can be broken into e1*e2 in zillions of ways (as it has 64 factors), and it passes 7^e1 mod m to the client, which computes (7^e1)^shp mod m where shp is the sha512 of their password (had to be done locally, that's what the JS is for), returns it to the server which computes sha512(((7^e1)^shp)^e2) = sha512(7^(E*shp)) and uses that for authentication purposes. One of my inquiries earlier on the CS forum informed me that modular roots are as hard as factoring the modulus, and with a modulus the product of two 150 digit primes the whole thing should be totally secure - barring the clients computer being compromised or the client getting phished obviously.

Thanks for the help guys, I probably would have just had to give up otherwise.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1562
Joined: Thu Jul 30, 2009 3:16 pm UTC


Return to Coding

Who is online

Users browsing this forum: Breakfast, EvanED and 9 guests