예제 #1
0
        /**
         * Generates a new signature key pair. Uses up to <code>B+1</code> threads
         * if multiple processors are available.
         * @return a key pair
         */
        public SignatureKeyPair generateKeyPair()
        {
            int processors           = Environment.ProcessorCount;
            SignaturePrivateKey priv = new SignaturePrivateKey(param);
            int B = param.B;

            //if (processors == 1)
            // generate all B+1 bases in the current thread
            for (int k = B; k >= 0; k--)
            {
                priv.add(generateBoundedBasis());
            }

            /*else {
             *  List<Future<Basis>> bases = new ArrayList<Future<Basis>>();
             *
             *  // start up to processors-1 new threads and generate B bases
             *  int numThreads = Math.min(B, processors-1);
             *  if (numThreads > 0) {
             *      ExecutorService executor = Executors.newFixedThreadPool(numThreads);
             *      for (int k=B-1; k>=0; k--)
             *          bases.add(executor.submit(new BasisGenerationTask()));
             *      executor.shutdown();
             *  }
             *
             *  // generate the remaining basis in the current thread
             *  Basis basis0 = generateBoundedBasis();
             *
             *  // build the private key
             *  for (Future<Basis> basis: bases)
             *      try {
             *          priv.add(basis.get());
             *      } catch (Exception e) {
             *          throw new NtruException(e);
             *      }
             *  priv.add(basis0);
             * }*/

            int q = param.q;
            SignaturePublicKey pub = new SignaturePublicKey(priv.getBasis(0).h, q);

            priv.getBasis(0).h = null;   // remove the public polynomial h from the private key

            SignatureKeyPair kp = new SignatureKeyPair(priv, pub);

            return(kp);
        }
예제 #2
0
        /**
         * Tests if the key pair is valid.
         * @return <code>true</code> if the key pair is valid, <code>false</code> otherwise
         */
        public bool isValid()
        {
            if (priv.N != pub.h.Coeffs.Length)
            {
                return(false);
            }
            if (priv.q != pub.q)
            {
                return(false);
            }

            int B = priv.getNumBases() - 1;

            for (int i = 0; i <= B; i++)
            {
                Basis basis = priv.getBasis(i);
                if (!basis.isValid(i == 0 ? pub.h : basis.h))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #3
0
        /**
         * Generates a new signature key pair. Uses up to <code>B+1</code> threads
         * if multiple processors are available.
         * @return a key pair
         */
        public SignatureKeyPair generateKeyPair()
        {
            int processors = Environment.ProcessorCount;
            SignaturePrivateKey priv = new SignaturePrivateKey(param);
            int B = param.B;

            //if (processors == 1)
            // generate all B+1 bases in the current thread
            for (int k = B; k >= 0; k--)
                priv.add(generateBoundedBasis());
            /*else {
                List<Future<Basis>> bases = new ArrayList<Future<Basis>>();
            
                // start up to processors-1 new threads and generate B bases
                int numThreads = Math.min(B, processors-1);
                if (numThreads > 0) {
                    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
                    for (int k=B-1; k>=0; k--)
                        bases.add(executor.submit(new BasisGenerationTask()));
                    executor.shutdown();
                }
            
                // generate the remaining basis in the current thread
                Basis basis0 = generateBoundedBasis();
            
                // build the private key
                for (Future<Basis> basis: bases)
                    try {
                        priv.add(basis.get());
                    } catch (Exception e) {
                        throw new NtruException(e);
                    }
                priv.add(basis0);
            }*/

            int q = param.q;
            SignaturePublicKey pub = new SignaturePublicKey(priv.getBasis(0).h, q);
            priv.getBasis(0).h = null;   // remove the public polynomial h from the private key

            SignatureKeyPair kp = new SignatureKeyPair(priv, pub);
            return kp;
        }