Android Open Source - asecrypto-goes-mobile-app Carmichael Finder 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.carmichael;
/*from   w  w w  . j  av a  2s .c om*/
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 Carmichael number generation.
 */
public class CarmichaelFinderTask extends AsyncTask<Integer, Void, CarmichaelResult> {

    private static int numOfPrimeTests = 40; // how often the primality test has to be passed in order to be accepted as a prime

    private final TaskFinishedCallable<CarmichaelResult> finishedCallable;

    private StopWatch watch;

    public CarmichaelFinderTask(TaskFinishedCallable<CarmichaelResult> finishedCallable) {
        this.finishedCallable = finishedCallable;

        watch = new StopWatch();
    }

    @Override
    protected CarmichaelResult doInBackground(Integer... numbers) {
        if (numbers.length != 1) {
            throw new RuntimeException("supply only one Integer for the bit number");
        }

        if (numbers[0] == null) {
            throw new NullPointerException("numbers[0] must be non-null");
        }

        watch.start();
        AseInteger[] result = createCMNumber(numbers[0]);

        watch.stop();

        if (result == null) { // only if cancelled
            return null;
        }

        return new CarmichaelResult(watch.getElapsedTime(), result[0], result[1], result[2], result[3]);
    }

    public AseInteger[] createCMNumber(int bits) {

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // How to produce a Carmichael Number?
        // Use the method of Chernick:
        //
        // If 6m + 1, 12m + 1 und 18m + 1 are prime numbers, then (6m + 1)(12m + 1)(18m + 1) is a Carmichael-Number!
        //
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        AseInteger six = AseInteger.valueOf(6);
        AseInteger twelve = AseInteger.valueOf(12);
        AseInteger eighteen = AseInteger.valueOf(18);

        AseInteger m = new AseInteger(bits, new Random());
        AseInteger counter = AseInteger.ZERO; // used to determine when to output information
        AseInteger p1, p2, p3, phiCm;

        // increase m by one until all terms are prime
        do {
            counter = counter.add(AseInteger.ONE);
            m = m.add(AseInteger.ONE);

            p1 = six.multiply(m).add(AseInteger.ONE);
            p2 = twelve.multiply(m).add(AseInteger.ONE);
            p3 = eighteen.multiply(m).add(AseInteger.ONE);

            if (isCancelled()) {
                return null; // get out straight away
            }

        } while (!(
                p1.isProbablePrime(numOfPrimeTests) && p2.isProbablePrime(numOfPrimeTests)
                        && p3.isProbablePrime(numOfPrimeTests)
        ));

        // the carmichael number is found!
        AseInteger carmichael = p1.multiply(p2).multiply(p3);
        watch.stop();

        // calculate phi(carmichael) and charmichael-phi to output probability information
        phiCm = p1.subtract(AseInteger.ONE).multiply(p2.subtract(AseInteger.ONE))
                .multiply(p3.subtract(AseInteger.ONE));
        AseInteger antiPhiCm = carmichael.subtract(AseInteger.ONE).subtract(phiCm);

        return new AseInteger[] {
                carmichael,
                antiPhiCm,
                phiCm,
                carmichael.subtract(AseInteger.ONE).divide(antiPhiCm)
        };
    }

    @Override
    protected void onPostExecute(CarmichaelResult carmichaelResult) {
        super.onPostExecute(carmichaelResult);
        this.finishedCallable.onAsyncTaskFinished(this, carmichaelResult);
    }

    @Override
    protected void onCancelled(CarmichaelResult carmichaelResult) {
        super.onCancelled(carmichaelResult);
        this.finishedCallable.onAsyncTaskFinished(this, null);
    }
}




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