amazon-archives / amazon-cognito-identity-js

Amazon Cognito Identity SDK for JavaScript
Other
985 stars 451 forks source link

authenticateUser takes around 10 seconds. #452

Closed torrens closed 7 years ago

torrens commented 7 years ago

I've written a react-native Expo application using v1.19.0 of amazon-cognito-identity.js. You can find a working sample of what I've done on GitHub.

https://github.com/torrens/react-native-aws-cognito-expo

When authenticateUser is executed on the IOS Simulator, it takes less than a second. When authenticateUser is executed on a physical device, it takes about 10 seconds.

I would appreciate any help getting this to run faster on a physical device.

itrestian commented 7 years ago

As noted in the issue https://github.com/aws/amazon-cognito-identity-js/issues/404, the slowness of authenticateUser comes from the modpow operation.

Most other computations are really fast for SRP, however the modpow operation is so to speak the main part of SRP (raising to a power and getting modulus of big integers). modpow in our case is performed using the JSBN library which uses montgomery modular multiplication http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js

In our case, the size of the integers is fixed by the service so for the integers our service deals with (recommended by security), montgomery multiplication is the best way to go (other methods won't perform as fast).

JSBN outperforms other implementations of modpow as you can see here https://github.com/bitwiseshiftleft/sjcl/issues/172

I looked at the JSBN implementation and it seems it uses certain optimizations for specific browsers. I would assume react falls in the last if statement. You could change BigInteger to use any of the other branches and see if there's any speedup.

https://github.com/aws/amazon-cognito-identity-js/blob/master/src/BigInteger.js#L113

karthiksaligrama commented 7 years ago

we recently released a sample that demonstrates how to use native bridges to use Cognito User Pools in a react native app @ https://github.com/awslabs/aws-sdk-react-native. This should speed up the login.