Calculating sqrt and arcTan in javacard without float type

258 views Asked by At

i want to calculate sqrt and arctangent in javacard. i haven't any math lib to do this for me and i haven't float type to calculate it manually. I have some questions in my mind:

1- Can i use float number in byte array form and working on it? how? 2- Usually how these operations is calculated in javacard?

I found some links but i couldn't help me: http://stackoverflow.com/questions/15363244/math-library-for-javacard http://javacardos.com/javacardforum/viewtopic.php?t=437

I should mention that i have to calculate these operation on card. Thank you very much if anyone can help me.

2

There are 2 answers

5
Lutz Lehmann On BEST ANSWER

The idea for CORDIC in the computation of atan is to have a table of values

angle[i] = atan(pow(2,-i));

It does not matter if the angles are precomputed in radians or degrees. Then use the tangent addition theorem

tan(a+b)=(tan(a)+tan(b) ) / ( 1-tan(a)*tan(b) )

to successively reduce the given tangent value

tan(x) {
    if(x<0) return -atan(-x);
    if(x>1) return 2*angle[0]-atan(1/x);
    pow2=1.0;
    phi=0;
    for(i=0;i<10; i++) {
        if(x>pow2) {
            phi += angle[i];
            x = (x-pow2)/(1+pow2*x);
        }
        pow2 /= 2;
    }
    return phi+x;

Now one needs to translate these operations and constants into using some kind of fixed point format.

5
AudioBubble On

The integer square root can be computed by the Babylonian method, if integer division is available.

Just iterate

R' = (R + S / R) / 2

with a suitable initial R.

Such a value can be found with

R= 1
while S > 2:
  R*= 2
  S/= 4

(preferably implemented with shifts, if available).

You can stop the iterations when the value of R stabilizes (you can also determine a priori a constant number of iterations that yields sufficient accuracy).