home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / rfc / 00s / rfc70.txt < prev    next >
Text File  |  1997-06-23  |  13KB  |  508 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                      S. Crocker
  8. Request for Comments #70                                   UCLA
  9.                                                            15 October 70
  10.  
  11.                            A Note on Padding
  12.  
  13. The padding on a message is a string of the form 10*.  For Hosts with
  14. word lengths 16, 32, 48, etc., bits long, this string is necessarily in
  15. the last word received from the Imp.  For Hosts with word lengths which
  16. are not a multiple of 16 (but which are at least 16 bits long), the 1
  17. bit will be in either the last word or the next to last word.  Of
  18. course if the 1 bit is in the next to last word, the last word is all
  19. zero.
  20.  
  21. An unpleasant coding task is discovering the bit position of the 1 bit
  22. within its word.  One obvious technique is to repeatedly test the
  23. low-order bit, shifting the word right one bit position if the
  24. low-order bit is zero.  The following techniques are more pleasant.
  25.  
  26. Isolating the Low-Order Bit
  27.  
  28. Let W be a non-zero word, where the word length is n.  Then W is of the
  29. form
  30.  
  31.             x....x10....0
  32.             \__ __/\__ __/
  33.                V      V
  34.              n-k-1    k
  35.  
  36. where 0<=k<n
  37.  
  38. and the x's are arbitrary bits.
  39.  
  40. Assuming two's complement arithmetic,
  41.  
  42.             W-1    =       x....x01....1
  43.                            _    _
  44.              -W    =       x....x10....0
  45.               _            _    _
  46.               W    =       x....x01....1
  47.  
  48. By using AND, OR and exclusive OR with various pairs of these
  49. quantities, useful new forms are obtained.
  50.  
  51. For example,
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.                                                                 [Page 1]
  59.  
  60. Network Working Group      A Note on Padding                      RFC 70
  61.  
  62.  
  63.             W AND W-1       xx...x00....0
  64.                            \__ __/\__ __/
  65.                               V      V
  66.                             n-k-1    k
  67.  
  68. thus removing the low-order 1 bit;
  69.  
  70. also W AND -W =           0....010....0
  71.                          __ __/__ __/
  72.                             V      V
  73.                           n-k-1    k
  74.  
  75. thus isolating the low-order bit.
  76.  
  77. Below, we will focus solely on this last result; however, in a
  78. particular application it may be advantageous to use a variation.
  79.  
  80. Determining the Position of an Isolated Bit
  81.  
  82. The two obvious techniques for finding the bit position of an isolated
  83. bit are to shift repetitively with tests, as above, and to use floating
  84. normalization hardware.  On the PDP-10, in particular, the JFFO
  85. instruction is made to order*.  On machines with hexadecimal
  86. normalization, e.g. IBM 360's and XDS Sigma 7's, the normalization
  87. hardware may not be very convenient.  A different approach uses
  88. division and table look-up.
  89.                                                               k
  90. A word with a single bit on has an unsigned integer value of 2  for
  91.                                          k
  92. 0<=k<n.  If we choose a p such that mod(2 ,p) is distinct for each
  93.  
  94. 0<=k<n, we can make a table of length p which gives the correspondence
  95.              k
  96. between mod(2 ,p) and k.  The remainder of this paper is concerned with
  97.  
  98. the selection of an appropriate divisor p for each word length n.
  99.  
  100.  
  101.  
  102.  
  103. *Some of the CDC machines have a "population count" instruction which
  104.                                                k
  105. gives the number of bits in a word.  Note the 2 -1 has exactly k bits
  106.  
  107. on.
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.                                                                 [Page 2]
  115.  
  116. Network Working Group      A Note on Padding                      RFC 70
  117.  
  118.  
  119. Example
  120.  
  121.    Let n = 8 and p = 11
  122.  
  123.       Then
  124.  
  125.                  0
  126.             mod(2, 11)     =    1
  127.                  1
  128.             mod(2, 11)     =    2
  129.                  2
  130.             mod(2, 11)     =    4
  131.                  3
  132.             mod(2, 11)     =    8
  133.                  4
  134.             mod(2, 11)     =    5
  135.                  5
  136.             mod(2, 11)     =   10
  137.                  6
  138.             mod(2, 11)     =    9
  139.                  7
  140.             mod(2, 11)     =    7
  141.  
  142.       This yields a table of the form
  143.  
  144.          remainder             bit position
  145.  
  146.              0                       --
  147.  
  148.              1                        0
  149.  
  150.              2                        1
  151.  
  152.              3                       --
  153.  
  154.              4                        2
  155.  
  156.              5                        4
  157.  
  158.              6                       --
  159.  
  160.              7                        7
  161.  
  162.              8                        3
  163.  
  164.              9                        6
  165.  
  166.             10                        5
  167.  
  168.  
  169.  
  170.                                                                 [Page 3]
  171.  
  172. Network Working Group      A Note on Padding                      RFC 70
  173.  
  174.  
  175. Good Divisors
  176.  
  177. The divisor p should be as small as possible in order to minimize the
  178.  
  179. length of the table.  Since the divisor must generate n distinct
  180.  
  181. remainders, the divisor will certainly need to be at least n.  A
  182.  
  183. remainder of zero, however, can occur only if the divisor is a power of
  184.                                                j
  185. 2.  If the divisor is a small power of 2, say 2  for j < n-1, it will
  186.  
  187. not generate n distinct remainders; if the divisor is a larger power of
  188.                                        n-1     n
  189. 2, the correspondence table is either 2    or 2  in length.  We can
  190.  
  191. thus rule out zero as a remainder value, so the divisor must be at
  192.  
  193. least one more than the word length.  This bound is in fact achieved
  194.  
  195. for some word lengths.
  196.  
  197. Let R(p) be the number of distinct remainders p generates when divided
  198. into successively higher powers of 2.  The distinct remainders all occur
  199. for the R(p) lowest powers of 2.  Only odd p are interesting and the
  200. following table gives R(p) for odd p between 1 and 21.
  201.  
  202.       p     R(p)                                p     R(p)
  203.  
  204.       1      1                                 13     12
  205.  
  206.       3      2                                 15      4
  207.  
  208.       5      4                                 17      8
  209.  
  210.       7      3                                 19     18
  211.  
  212.       9      6                                 21      6
  213.  
  214.       11     10
  215.  
  216. This table shows that 7, 15, 17 and 21 are useless divisors because
  217. there are smaller divisors which generate a larger number of distinct
  218. remainders.  If we limit our attention to p such that p > p' =>
  219. R(p) > R(p'), we obtain the following table of useful divisors for
  220. p < 100.
  221.  
  222.  
  223.  
  224.  
  225.  
  226.                                                                 [Page 4]
  227.  
  228. Network Working Group      A Note on Padding                      RFC 70
  229.  
  230.  
  231.       p     R(p)                                p     R(p)
  232.  
  233.       1      1                                 29     28
  234.  
  235.       3      2                                 37     36
  236.  
  237.       5      4                                 53     52
  238.  
  239.       9      6                                 59     58
  240.  
  241.       11     10                                61     60
  242.  
  243.       13     12                                67     66
  244.  
  245.       19     18                                83     82
  246.  
  247.       25     20
  248.  
  249. Notice that 9 and 25 are useful divisors even though they generate only
  250. 6 and 20 remainders, respectively.
  251.  
  252. Determination of R(p)
  253.  
  254. If p is odd, the remainders
  255.  
  256.            0
  257.       mod(2 ,p)
  258.            1
  259.       mod(2 ,p)
  260.  
  261.            .
  262.            .
  263.            .
  264.                                                                t
  265. will be between 1 and p-1 inclusive.  At some power of 2, say 2 , there
  266.                                                        k    t
  267. will be a repeated remainder, so that for some k < t, 2  = 2  mod p.
  268.        t+1    k+1
  269. Since 2    = 2    mod p
  270.        t+2    k+2
  271. and   2    = 2    mod p
  272.  
  273.            .
  274.            .
  275.            .
  276.           etc.
  277.                                           0    t-1
  278. all of the distinct remainders occur for 2 ...2   .  Therefore, R(p)=t.
  279.  
  280.  
  281.  
  282.                                                                 [Page 5]
  283.  
  284. Network Working Group      A Note on Padding                      RFC 70
  285.  
  286.  
  287. Next we show that
  288.  
  289.       R(p)
  290.      2     = 1 mod p
  291.                       R(p)    k
  292. We already know that 2     = 2  mod p
  293.  
  294. for some 0<=k<R(p).  Let j=R(p)-k so 0<j<=R(p).  Then
  295.  
  296.       k+j           k
  297.      2           = 2  mod p
  298.       j  k          k
  299. or   2 *2        = 2  mod p
  300.        j     k
  301. or   (2 -1)*2    =  0 mod p
  302.                        k                                     j
  303. Now p does not divide 2  because p is odd, so p must divide 2 -1.  Thus
  304.  
  305.        j
  306.       2 -1        =  0 mod p
  307.        j
  308.       2           =  1 mod p
  309.  
  310. Since j is greater than 0 by hypothesis and since ther is no k other
  311. than 0 less than R(p) such that
  312.  
  313.        k    0
  314.       2  = 2  mod p,
  315.  
  316.                          R(p)
  317. we must have j=R(p), or 2     = 1 mod p.
  318.                                                        k
  319. We have thus shown that for odd p, the remainders mod(2 ,p) are unique
  320. for k = 0, 1,..., R(p)-1 and then repeat exactly, beginning with
  321.  
  322.        R(p)
  323.       2     = 1 mod p.
  324.  
  325. We now consider even p.  Let
  326.  
  327.               q
  328.       p = p'*2 ,
  329.                                 k                     k          k
  330. where p' is odd.  For k<q, mod(2 ,p) is clearly just 2  because 2 <p.
  331.  
  332. For k>=q,
  333.           k       q      k-q
  334.      mod(2 ,p) = 2 *mod(2   ,p').
  335.  
  336.  
  337.  
  338.                                                                 [Page 6]
  339.  
  340. Network Working Group      A Note on Padding                      RFC 70
  341.  
  342.  
  343. From this we can see that the sequence of remainders will have an
  344.                              q-1
  345. initial segment of 1, 2, ...2    of length q, and repeating segments of
  346.  
  347. length R(p').  Therefore, R(p) = q+R(p').  Since we normally expect
  348.  
  349.      R(p) ~ p,
  350.  
  351. even p generally will not be useful.
  352.  
  353. I don't know of a direct way of choosing a p for a given n, but the
  354. previous table was generated from the following Fortran program run
  355. under the SEX system at UCLA.
  356.  
  357.  
  358.  
  359.             0
  360.                     CALL IASSGN('OC ',56)
  361.             1       FORMAT(I3,I5)
  362.                     M=0
  363.                     DO 100 K=1,100,2
  364.                     K=1
  365.                     L=0
  366.             20      L=L+1
  367.                     N=MOD(2*N,K)
  368.                     IF(N.GT.1) GO TO 20
  369.                     IF(L.LE.M) GO TO 100
  370.                     M=L
  371.                     WRITE(56,1)K,L
  372.             100     CONTINUE
  373.                     STOP
  374.                     END
  375.  
  376.       Fortran program to computer useful divisors
  377.  
  378. In the program, K takes on trial values of p, N takes on the values of
  379. the successive remainders, L counts up to R(p), and M remembers the
  380. previous largest R(p).  Execution is quite speedy.
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.                                                                 [Page 7]
  395.  
  396. Network Working Group      A Note on Padding                      RFC 70
  397.  
  398.  
  399. Results from Number Theory
  400.  
  401. The quantity referred to above as R(p) is usually written Ord 2 and is
  402.                                                              p
  403. read "the order of 2 mod p".  The maximum value of Ord 2 is given by
  404.                                                       p
  405. Euler's phi-function, sometimes called the totient.  The totient of a
  406.  
  407. positive integer p is the number of integers less than p which are
  408.  
  409. relatively prime to p.  The totient is easy to compute from a
  410.  
  411. representation of p as a product of primes:
  412.  
  413.                n      n        n
  414.      Let p = p  1 * p  2 ... p  k
  415.               1      2        k
  416.  
  417. where the p  are distinct primes.  Then
  418.            i
  419.                           k -1               k -1                 k -1
  420.      phi(p) = (p - 1) * p  1   * (p - 1) * p  2   ... (p - 1) * p  k
  421.                 1        1         2        2           k        k
  422.  
  423. If p is prime, the totient of p is simply
  424.  
  425.      phi(p) = p-1.
  426.  
  427. If p is not prime, the totient is smaller.
  428.  
  429. If a is relatively prime to p, then Euler's generalization of Fermat's
  430. theorem states
  431.  
  432.       phi(m)
  433.      a       = 1 mod p.
  434.  
  435. It is this theorem which places an upper bound Ord 2, because Ord 2 is
  436.                                                   p              p
  437. the smallest value such that
  438.  
  439.       Ord 2
  440.      2   p  = 1 mod p
  441.  
  442. Moreover it is always true that phi(p) is divisible by Ord 2.
  443.                                                           p
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.                                                                 [Page 8]
  451.  
  452. Network Working Group      A Note on Padding                      RFC 70
  453.  
  454.  
  455. Acknowledgements
  456.  
  457. Bob Kahn read an early draft and made many comments which improved the
  458. exposition.  Alex Hurwitz assured me that a search technique is
  459. necessary to compute R(p), and supplied the names for the quantities
  460. and theorems I uncovered.
  461.  
  462.  
  463.        [ This RFC was put into machine readable form for entry ]
  464.        [ into the online RFC archives by Guillaume Lahaye 6/97 ]
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.                                                                 [Page 9]
  507.  
  508.