2

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.

Mohsen Gorgani
  • 420
  • 4
  • 18
  • What exactly are the inputs? What is their data format, (implied) precision etc. What is expected of the results, how exact do they have to be? Do you know the manual square root algorithm? The CORDIC algorithms? – Lutz Lehmann Dec 23 '16 at 19:05
  • Thanks for reply. Doesn't the algorithm work with float data type? I haven't float data type in card. – Mohsen Gorgani Dec 23 '16 at 19:18
  • That is why I asked about the input and output data format. If you can't get the data in, and have no structure to put the result, then the "how" of the computation becomes rather secondary. – Lutz Lehmann Dec 23 '16 at 19:22
  • Actually input data are pictures in format of minutia(format for storing fingerprint in javacard) and output is a score in float format that indicates fingerprints how much similar to each other. I have to calculate some mathematical operation on two picture and calculate the score. – Mohsen Gorgani Dec 23 '16 at 19:33
  • And everything with 80kB code and 2kB data? So you have to compute a scalar product, two vector norms and an arcus cosine? Or is it sufficient to give the correlation as percent? – Lutz Lehmann Dec 23 '16 at 19:38
  • If i understand your question correctly Input data in minutia format for each picture is about 200 bytes. Minutia is not picture but are extracted from picture. For computing the correlation of two picture i have to calculate some algorithm that use sqrt and arctangent and finally show the correlation as percent. – Mohsen Gorgani Dec 23 '16 at 19:51
  • Also test your algorithm description if you actually need the angle or just the normalized direction vector. Even angle operations are often faster using the trigonometric identities on the direction vectors. – Lutz Lehmann Dec 23 '16 at 22:46
  • 1
    If this is not for academic purposes then you should look into existing products – Paul Bastian Dec 24 '16 at 09:02

2 Answers2

4

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).

vojta
  • 5,591
  • 2
  • 24
  • 64
2

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.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
  • This is a strange CORDIC implementation. It is not much efficient due to the division in the loop. A better solution with only additions and shifts exists. –  Dec 23 '16 at 22:15
  • Yes, this was just written down without checking other sources. Searching the internet finds http://www.convict.lu/Jeunes/Math/arctan.htm which has a probably sufficient quadratic polynomial approximation and the general CORDIC idea. – Lutz Lehmann Dec 23 '16 at 22:39
  • Thank you Lutzl for your answer. Is there a way to represent float type in byte array and working on it? – Mohsen Gorgani Dec 24 '16 at 11:15
  • Yes, multi-precision formats are constructed like that, see for instance Pari, there was a whole book written about how to build a CAS that way. -- But for your purposes, a fixed point format would be easier to handle. For instance, all numbers are 32bit and are understood to be interpreted as x/2^16. Thus the product would be `(x/2^16)*(y/2^16)=(x*y/2^16)/2^16` etc. This would also better fit with the implementation using shifts. – Lutz Lehmann Dec 24 '16 at 11:21
  • Thanks Lutzl. I test it and will notify you about the result. – Mohsen Gorgani Dec 24 '16 at 11:26