The Non-adjacent_form (NAF) of a number is a unique signed-digit representation. It is useful when building scalar multipliers of Elliptic Curve points, for which the cost of doubling the point is significantly less, compared to the cost of point addition and subtraction. There can be other applications as well, e.g. for efficiently implementing an algebra with similar relative costs of operations.

In J the NAF can be built with the verb

   NAF =: (}:"1@:-/@([: #: (_1 _3)&(*/))) :. #.

It works as

   NAF 7
1 0 0 _1
   NAF i.8
0 0  0  0
0 0  0  1
0 0  1  0
0 1  0 _1
0 1  0  0
0 1  0  1
1 0 _1  0
1 0  0 _1
   |&.NAF i.8
0 1 2 5 4 5 10 9

This implementation supports array arguments and has obverse defined. Thanks to Oleg Kobchenko for array support and to Raul Miller for mentioning that #. is inverse of NAF.


Comments

You can use @SIG@ to sign your comments. This section is possibly temporary(?).



Contributed by Konstantin Metlov.

Essays/NAF (last edited 2008-12-08 10:45:50 by )