SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rs;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. public class InverseTable {
  7.  
  8.     public static void main(String[] args) {
  9.         // x^8 + x^4 + x^3 + x + 1
  10.         int poly = 0b100011011;
  11.  
  12.         Set<Integer> invs = new HashSet<>();
  13.  
  14.         for (int high = 0; high <= 0xF; high++) {
  15.             System.out.printf("%x \t", high);
  16.  
  17.             for (int low = 0; low <= 0xF; low++) {
  18.                 int elem = (high << 4) | low;
  19.  
  20.                 if (elem == 0) {
  21.                     System.out.print("- \t");
  22.                 } else {
  23.                     final int inv = inv(elem, poly);
  24.                     System.out.printf("%02x \t", inv);
  25.  
  26.                     invs.add(inv);
  27.                 }
  28.             }
  29.  
  30.             System.out.println();
  31.         }
  32.  
  33.         System.out.println(invs.size());
  34.     }
  35.  
  36.     private static int inv(int a, int p) {
  37.         for (int b = 1; b <= 0xFF; b++) {
  38.             int prod = mult(a, b);
  39.             int r = div(prod, p)[1];
  40.  
  41.             if (r == 1) {
  42.                 return b;
  43.             }
  44.         }
  45.  
  46.         throw new IllegalArgumentException("Can't find inverse for " + Integer.toBinaryString(a));
  47.     }
  48.  
  49.     private static int mult(int p1, int p2) {
  50.         int result = 0;
  51.  
  52.         for (int i = 31; i >= 0; i--) {
  53.             if (((p2 >>> i) & 1) != 0) {
  54.                 result ^= (p1 << i);
  55.             }
  56.         }
  57.  
  58.         return result;
  59.     }
  60.  
  61.     private static int[] div(int dividend, int divisor) {
  62.         int divDeg = deg(divisor);
  63.  
  64.         int q = 0;
  65.         int r = dividend;
  66.  
  67.         while (deg(r) >= divDeg) {
  68.             int degDif = deg(r) - divDeg;
  69.  
  70.             r ^= (divisor << degDif);
  71.             q ^= (1 << degDif);
  72.         }
  73.  
  74.         return new int[]{q, r};
  75.     }
  76.  
  77.     private static int deg(int pol) {
  78.         return 32 - Integer.numberOfLeadingZeros(pol) - 1;
  79.     }
  80.  
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top