Android Open Source - asecrypto-goes-mobile-app Primitive Root Lookup Task






From Project

Back to project page asecrypto-goes-mobile-app.

License

The source code is released under:

GNU General Public License

If you think the Android project asecrypto-goes-mobile-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots;
//from  w  w  w . j a v  a 2  s . c  o m
import android.os.AsyncTask;

import java.util.Random;

import at.fhj.gaar.asecrypto.mobile.crypto.AseInteger;
import at.fhj.gaar.asecrypto.mobile.ui.TaskFinishedCallable;
import at.fhj.gaar.asecrypto.mobile.util.StopWatch;

/**
 * Executes the search for a primitive root.
 */
public class PrimitiveRootLookupTask extends AsyncTask<FinderArguments, Void, PrimitiveRootResult> {

    private final TaskFinishedCallable<PrimitiveRootResult> finishedCallable;

    private StopWatch watch;

    public PrimitiveRootLookupTask(TaskFinishedCallable<PrimitiveRootResult> finishedCallable) {
        this.finishedCallable = finishedCallable;

        watch = new StopWatch();
    }


    @Override
    protected PrimitiveRootResult doInBackground(FinderArguments... parameters) {
        if (parameters.length != 1) {
            throw new RuntimeException("supply one instance of FinderArguments");
        }

        FinderArguments args = parameters[0];
        if (args == null) {
            throw new NullPointerException("invald args");
        }

        watch.start();
        AseInteger result[] = findPrimitiveRoot(args.getBitCount(), args.getNumberOfTestRuns());
        watch.stop();

        if (result == null) {
            return null;
        }

        return new PrimitiveRootResult(watch.getElapsedTime(), result[0], result[1]);
    }

    private AseInteger[] findPrimitiveRoot(int bits, int testRuns) {
        AseInteger[] result = findPrimeCandidate(bits, testRuns);

        if (result == null) {
            return null;
        }

        AseInteger finalModulus = result[0];
        AseInteger finalPrimeQ = result[1];

        return calculateFinalRoot(bits, finalModulus, finalPrimeQ);
    }

    private AseInteger[] calculateFinalRoot(int bits, AseInteger modulus, AseInteger prime) {
        while (true) {
            if (isCancelled()) {
                return null;
            }

            // create a random AseInteger as a candidate
            AseInteger r = new AseInteger(bits, new Random());

            // make sure the candidate is smaller than the modulus number
            if (r.compareTo(modulus) >= 0) {
                continue;
            }

            // check the order of the candidate
            if (r.modPow(AseInteger.TWO, modulus).compareTo(AseInteger.ONE) == 0) {
                continue;
            }

            if (r.modPow(prime, modulus).compareTo(AseInteger.ONE) == 0) {
                continue;
            }

            return new AseInteger[] {
                    modulus,
                    r
            };
        }
    }

    private AseInteger[] findPrimeCandidate(int bits, int testRuns) {
        // do until you found a matching prime
        while (true) {
            if (isCancelled()) {
                return null;
            }

            // generate a random prime (using a suitable AseInteger constructor)
            AseInteger mod = new AseInteger(bits, testRuns, new Random());

            // check if this (prime-1)/2 is another prime (AseInteger provides the isProbablePrime() method for this)
            AseInteger q = mod.subtract(AseInteger.ONE).divide(AseInteger.TWO);
            if (q.isProbablePrime(testRuns)) {
                // verify the contract that modulus - 1 = 2*primeQ
                if (mod.subtract(AseInteger.ONE).divide(AseInteger.TWO).compareTo(q) != 0) {
                    throw new IllegalArgumentException("Your primes do not stick to the formula:" +
                            " \"modulus - 1 = 2*primeQ\"");
                }

                return new AseInteger[] {
                        mod,
                        mod.subtract(AseInteger.ONE).divide(AseInteger.TWO)
                };
            }
        }
    }

    @Override
    protected void onPostExecute(PrimitiveRootResult primitiveRootResultData) {
        super.onPostExecute(primitiveRootResultData);

        finishedCallable.onAsyncTaskFinished(this, primitiveRootResultData);
    }
}




Java Source Code List

at.fhj.gaar.asecrypto.mobile.crypto.AseInteger.java
at.fhj.gaar.asecrypto.mobile.crypto.RSAHelper.java
at.fhj.gaar.asecrypto.mobile.ui.FragmentFactory.java
at.fhj.gaar.asecrypto.mobile.ui.MainActivity.java
at.fhj.gaar.asecrypto.mobile.ui.SectionAttachable.java
at.fhj.gaar.asecrypto.mobile.ui.TaskFinishedCallable.java
at.fhj.gaar.asecrypto.mobile.ui.TaskIntermediateCallable.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.BaseFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutIterativeTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutRecursiveTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.bezout.BezoutResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.carmichael.CarmichaelFinderTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.carmichael.CarmichaelGeneratorFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.carmichael.CarmichaelResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidFactorialTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidIterativeTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidRecursiveTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.euclid.EuclidResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.ExponentiationResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.FastExponentiationFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.FastExponentiationTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.SlowExponentiationFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.exponentiation.SlowExponentiationTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatProgress.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatTaskArguments.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.fermat.FermatTestFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinArguments.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinProgress.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.millerrabin.MillerRabinTestFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.numbercounter.NumberCounterFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.numbercounter.NumberCounterTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.FinderArguments.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.PrimitiveRootFinderFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.PrimitiveRootLookupTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.primitiveroots.PrimitiveRootResult.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.ParameterCalculationHelperTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSACalculationFragment.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSADecryptionParameters.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSADecryptionTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSAEncryptionParameters.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSAEncryptionTask.java
at.fhj.gaar.asecrypto.mobile.ui.apptasks.rsa.RSAResult.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.DrawerItemIdentifiers.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.DrawerItem.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.NavigationDrawerCallable.java
at.fhj.gaar.asecrypto.mobile.ui.navigation.NavigationDrawerFragment.java
at.fhj.gaar.asecrypto.mobile.util.NumberChoiceSelector.java
at.fhj.gaar.asecrypto.mobile.util.NumberHelper.java
at.fhj.gaar.asecrypto.mobile.util.StopWatch.java