Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.math.BigInteger;

public class Main {
    public static byte[] generateJSF(BigInteger g, BigInteger h) {
        int digits = Math.max(g.bitLength(), h.bitLength()) + 1;
        byte[] jsf = new byte[digits];

        BigInteger k0 = g, k1 = h;
        int j = 0, d0 = 0, d1 = 0;

        while (k0.signum() > 0 || k1.signum() > 0 || d0 > 0 || d1 > 0) {
            int n0 = (k0.intValue() + d0) & 7, n1 = (k1.intValue() + d1) & 7;

            int u0 = n0 & 1;
            if (u0 != 0) {
                u0 -= (n0 & 2);
                if ((n0 + u0) == 4 && (n1 & 3) == 2) {
                    u0 = -u0;
                }
            }

            int u1 = n1 & 1;
            if (u1 != 0) {
                u1 -= (n1 & 2);
                if ((n1 + u1) == 4 && (n0 & 3) == 2) {
                    u1 = -u1;
                }
            }

            if ((d0 << 1) == 1 + u0) {
                d0 = 1 - d0;
            }
            if ((d1 << 1) == 1 + u1) {
                d1 = 1 - d1;
            }

            k0 = k0.shiftRight(1);
            k1 = k1.shiftRight(1);

            jsf[j++] = (byte) ((u0 << 4) | (u1 & 0xF));
        }

        // Reduce the JSF array to its actual length
        if (jsf.length > j) {
            jsf = trim(jsf, j);
        }

        return jsf;
    }

    private static byte[] trim(byte[] a, int length) {
        byte[] result = new byte[length];
        System.arraycopy(a, 0, result, 0, result.length);
        return result;
    }

    private static int[] trim(int[] a, int length) {
        int[] result = new int[length];
        System.arraycopy(a, 0, result, 0, result.length);
        return result;
    }
}