1

I'm trying to implement a basic procedure to generate an RSA key. The procedure accepts a range of numbers a and b. It has to check that the intervall between a and b is "five digits".

So I came up with a solution:

with (numtheory);
gen_rsa := proc(a, b)
  local p, q, len_p, len_q, larger;
  # the two prime-numbers
  p:=safeprime(round(RandomTools[Generate](integer(range=a .. b))-1/2));
  q:=safeprime(round(RandomTools[Generate](integer(rande=a .. b))-1/2));
  if( evalb(log10(p) > log10(q)+5 ) 
  [...]

Thing is: Maple seems to understand p and q as variables of the type function. I want to use the log10 to find out how many digits the prime-number has in order to calculate a safe RSA key. So evalb fails, because it cannot determine the two logarithms??

wishi
  • 7,188
  • 17
  • 64
  • 103
  • In addition to acer's excellent answer: I think that for your application, ilog10 is sufficient. Essentially, `ilog10(n) = floor(log10(n))`, so you lose some information, but in return it immediately evaluates to a number, whereas log10(n) will have a remaining unevaluated part for numbers that aren't exact powers of 10. An effect of this is that the evaluation problem that you have for log10 does not occur for ilog10. – Erik P. Feb 19 '11 at 01:03

1 Answers1

2

You shouldn't load the package outside the proc definition -- it's not good practice.

You don't need a call to evalb, when using if...then, as it automatically does that.

You could either use is instead, or evalf both quantities so that the inequality can be tested.

For example,

gen_rsa := proc(a, b)
local p, q, len_p, len_q, larger;
uses numtheory, RandomTools;
   randomize();
   # the two prime-numbers
   p:=safeprime(round(Generate(integer(range=a .. b))-1/2));
   q:=safeprime(round(Generate(integer(range=a .. b))-1/2));
   if is(log10(p) > log10(q)+5) then
      hi;
   else
      bye;
   end if;
end proc:

or you could replace that is call by applying evalf to both sides of the < inequality conditional. (The is command can actually utilize evalf internally, possibly via shake, to figure it out.)

What you mean by the "interval" between p and q being "5 digits" is not clear. If you mean that one must have five more decimal digits that the other then you might want to round or trunc those log10 calls separately. It's hard to say, as the wording is fuzzy.

ps. I also corrected the misspelling "rande" for "range", and removed the inappropriate open-parenthesis right after the if. And the randomize call will make the RandomTools command produce different answers after each restart or in each fresh session.

acer
  • 6,671
  • 15
  • 15