Large integers and cousins

Asked

Viewed 673 times

3

Is there any function that generates prime and integer numbers in considerably large sizes?

If not, how to do it using Javascript?

  • 1

    You want someone to do it for you?

  • 6

    Do you really need to generate the numbers? They are known, I would mount an array with primes from any table (like this) and draw an array position.

  • @Lucas no, I don’t really know how to work with generating numbers of this type with Javascript just using php gmp, if you have any idea how much you’d appreciate the goodwill.

  • @bfavaretto yes if they were predefined would be easy, but the case is actually generate numbers of this type, I can not have a pre molded array, these will be used for DH key exchange..

  • Why are you implementing your own encryption? And why are you doing crypto in JS?

  • @hugomg itself? I am only using existing ideas, communication between two points without interference from the server and third parties.

  • 2

    I’m talking about the implementation. In general it’s more reliable to use existing libraries for crypto rather than to program everything again yourself. And if your crypto is running on a broser, it has the problem of an adversary interfering with the Javascript code the client receives

  • @hugomg on the server side I have these features, on the client side not have everything ready on the web, have to make I just interlude everything to make it work, already revised enough for what I want, but let’s get back to the focus of this.

  • 2

    You had already asked the same thing about PHP and I had given the same suggestion, and other users also (the question no longer exists, you deleted). I can not understand why generate, the prime numbers are all known (those that are not, is not a browser that will be able to calculate...). I do not understand why you think they would be more secret if you calculate. Anyone with a table of cousins can try an attack Brut-force, if that’s your concern.

  • Otherwise, remember that the largest integer allowed in JS is 9007199254740992. You will never be able to calculate larger primes than this. So using a table of strings representing the numbers would allow you to use even larger primes.

  • @bfavaretto would be interesting to see the generation of these, but I will follow an answer I found in Soen that explains a means, obg, I will erase this soon

  • @bfavaretto don’t take this the wrong way, I just don’t want to rely only on an array that will be exposed, it would be something like "take the numbers for you to try" even if it is possible to do brute force otherwise, I think that will trick.

  • 1

    Although the question as it was written gives room for interpretation, I personally [who have an interest in cryptography, although I am not an expert] when I read "very large cousins" I already assume it is something that goes beyond representation in int, long or double, something that requires an integer library of arbitrary precision. And the "default" solution to this problem is always the same - generating random numbers and testing whether they are probably primes. The fact that these cousins are "known" has no relevance in the case, because their number is too large to test one by one.

Show 8 more comments

4 answers

8


A solution making use of Forge:

var bits = 1024;
forge.prime.generateProbablePrime(bits, function(err, num) {
    console.log('número primo aleatório', num.toString(16));
});

Solution credit for @dlongley in this answer in the SOEN.

  • Interesting how this works (it is described in the SOEN answer you indicated). He generates a cousin candidate efficiently, and then tests if he’s a cousin. If he’s not, try another candidate. And this Forge library seems to be exactly what AP needs to solve its biggest problem (which is encryption).

  • I love this one, I’m just trying to extract this function with its dependencies but I can’t, it’s suggestive?

6

I already I insisted enough on the comments for you to use a pre-defined primes table. Generating Javascript primes is not at all efficient, especially if you only need to draw one. If you need numbers with multiple digits then, it will be well slow.

An adaptation of the implementation of the Sieve of Eratosthenes posted by the user Guffa on the site Code Review:

function primosAte(n) {
  var i, j;
  var prime = new Array(n);
  for (i = 2; i < n ; i++) prime[i] = true;
  
  for (i = 2; i * i < n ; i++) {
    if (prime[i]) {
      for (j = 0; i * i + i * j < n ; j++) {
        prime[i * i + i * j] = false;
      }
    }
  }
  var primes = [];
  for (i = 2 ; i < n ; i++) {
    if (prime[i]){
      primes.push(i);
    }
  }
  return primes;
}
 
var primos = primosAte(1000);
document.body.innerHTML = primos.toString(', ');

2

You can use one of the libraries on this page to solve your problem:

-- Translated transcription of a part --

The jsbn library API is very reminiscent of classes java.math.BigInteger java. For example:

x = new BigInteger("abcd1234", 16);
y = new BigInteger("beef", 16);
z = x.mod(y);
alert(z.toString(16));

will print b60c.

Core Library

  • jsbn.js - basic implmentation of BigInteger, enough for RSA encryption and not much more.
  • jsbn2.js - the rest of the library, including most public methods of BigInteger.

RSA

  • rsa.js - implementation of RSA encryption, does not require jsbn2.js.
  • rsa2.js - rest of the RSA algorithm, including decryption and key generation.

ECC

  • Ec.js - elliptic curves math, depends on jsbn.js and jsbn2.js
  • sec.js - standard parameters of elliptic curves

Utilities

  • Rng.js - RNG rudimentary entropy collector interface, requires a PRNG backend to define prng_newstate().
  • prng4.js - ARC4-based PRNG backend for Rng.js, very small.
  • Base64.js - Base64 encoding and decoding routines.
  • sha1.js - SHA-1 hash function, only required for IBE demo.

-- End of transcription --

 

Algorithms to generate very large primes

I found a implementation of a python algorithm

  • 1

    pardon, I didn’t understand the context of the question-answer link.

  • These are libraries to use large numbers in Javascript. Since there is a part of the library that generates keys, then you should find the part that generates very large prime numbers there.

  • I edited with a Python implementation, but I do not know if I will have time to translate for Javascript... is a lot of code.

  • I don’t understand python tricks but thank you very much!

  • 1

    @user3163662 This last algorithm quoted is for finding numbers that are guaranteed cousins. Formerly, while this problem was still open, one could only determine [in a timely manner] whether a number was probably prime, and this remained "good enough" for many practical applications (even after the discovery of a polynomial algorithm that gives an exact answer). As you do not quote in the question what the purpose of these cousins, I have no way of saying whether in your case is or is not important this guarantee (probably is not).

  • 1

    I’m trying to translate the code... even if it’s not important to this issue, it seems like it might be something useful.

  • @mgibsonbr the intention of these cousins is to make the DH exchange, I tried to extract the api that generates it from the library Forge but did not succeed, vc has an apparent solution?

  • @mgibsonbr is not necessary for me extremely large numbers, only that they are suitable for use in normal DH,

  • 1

    @user3163662 I know very little of Diffie-Hellman (except theory), but as far as I know the primes used need be big, huge, even, but I can’t tell you whether or not they need to be secret. I know that at least one of your parameters is not secret, there is even a recommended list of these parameters. Anyway, as this subject is very popular lately (you are the 3rd or 4th person who appears with doubts of this type), I am writing a question-answer pair to help clarify some of the finer details of DH in Javascript.

  • @mgibsonbr Poisé, my question of how to generate prime numbers remains 'pending'

  • 1

    @user3163662 Does Zuul’s answer not suit you? Nor do any of the libraries mentioned in this answer? The basic process is this: generate random numbers and pass a primality test (exact or probabilistic). Implement this by hand is bone (I studied in college, but as exercise only), better seek an implementation already ready.

  • 1

    @user3163662 I gave a full example of DH in that question-answer, using Webcrypto (yet is not 100% cross-browsers but it is almost there), that is, without the need for any external library. If it is to be used in practice (and not just for study), I strongly recommend using it instead of by hand.

  • @Miguelangelo Though not useful for the AP, would be useful to question yes, if you are really willing to translate the code (which should not be easy task...). I just haven’t given you +1 yet because you just quoted a bunch of libraries from bigamist, which in itself does not answer what was asked...

Show 8 more comments

-1


function primosAte(n) { var i, j; var prime = new Array(n); for (i = 2; i < n ; i++) prime[i] = true;

for (i = 2; i * i < n ; i++) {
if (prime[i]) {
  for (j = 0; i * i + i * j < n ; j++) {
    prime[i * i + i * j] = false;
  }
}
}
var primes = [];
for (i = 2 ; i < n ; i++) {
  if (prime[i]){
  primes.push(i);
}
}
return primes;

}

var cousins = primosAte(100000000000000000000000000000000000000000000000000000000000000); Document.body.innerHTML = cousins.toString(', ');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.