home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / cug / softt-12.lbr / PWRMOD.RAT < prev    next >
Text File  |  1984-07-05  |  768b  |  24 lines

  1. #-h-  pwrmod.r                    860  local   01/05/81  21:33:56
  2. # pwrmod --- calculate an exponential, modulo a given modulus
  3. #     (function result is p ** E mod n)
  4.  
  5.    long_int function pwrmod (p, E, n)
  6.    long_int p, E, n
  7.  
  8.    integer i
  9.    long_int result
  10.    define(bit_i,rt (rs (E, BITS_PER_LONG_WORD - i), 1)) # i'th bit of E
  11.       # 'rt' is right-truncate, 'rs' is right-shift
  12.  
  13.    result = 1
  14.    for (i = 1; i <= BITS_PER_LONG_WORD; i += 1)
  15.       if (bit_i == 0)
  16.          result = mod (result * result, n)
  17.       else
  18.          result = mod (mod (result * result, n) * p, n)
  19.  
  20.    return (result)
  21.    end
  22. #-t-  pwrmod.r                    860  local   01/05/81  21:33:56
  23. #-t-  pwrmod                     1867  local   01/05/81  21:38:53
  24.