/** * 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); }
/** * 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; }
/** * Generates a new signature key pair. Runs in a single thread. * @return a key pair */ public SignatureKeyPair generateKeyPairSingleThread() { SignaturePrivateKey priv = new SignaturePrivateKey(param); SignaturePublicKey pub = null; Basis pubBasis = generateBoundedBasis(); pub = new SignaturePublicKey(pubBasis.h, param.q); pubBasis.h = null; // remove the public polynomial h from the private key priv.add(pubBasis); for (int k = param.B; k > 0; k--) { Basis basis = generateBoundedBasis(); priv.add(basis); } SignatureKeyPair kp = new SignatureKeyPair(priv, pub); return(kp); }
//@Override public override bool Equals(object obj) { if (this == obj) { return(true); } if (obj == null) { return(false); } //if (getClass() != obj.getClass()) // return false; SignaturePrivateKey other = (SignaturePrivateKey)obj; if (bases == null) { if (other.bases != null) { return(false); } } if (bases.Count != other.bases.Count) { return(false); } for (int i = 0; i < bases.Count; i++) { if (bases[i].f == null) { if (other.bases[i].f != null) { return(false); } } else if (!bases[i].f.Equals(other.bases[i].f)) { return(false); } if (bases[i].h == null) { if (other.bases[i].h != null) { return(false); } } else if (!bases[i].h.Equals(other.bases[i].h)) { return(false); } if (bases[i].N == null) { if (other.bases[i].N != null) { return(false); } } else if (!bases[i].N.Equals(other.bases[i].N)) { return(false); } if (bases[i].q == null) { if (other.bases[i].q != null) { return(false); } } else if (!bases[i].q.Equals(other.bases[i].q)) { return(false); } } return(true); }
private void SignVerify(SignatureParameters param) { NtruSign ntru = new NtruSign(param); SignatureKeyPair kp = ntru.generateKeyPair(); Assert.Equals(param.B + 1, kp.priv.getNumBases()); Random rng = new Random(); byte[] msg = new byte[10 + rng.Next(1000)]; rng.NextBytes(msg); // sign and verify byte[] s = ntru.sign(msg, kp); bool valid = ntru.verify(msg, s, kp.pub); Assert.True(valid); // altering the signature should make it invalid s[rng.Next(param.N)] += 1; valid = ntru.verify(msg, s, kp.pub); Assert.False(valid); // test that a random signature fails rng.NextBytes(s); valid = ntru.verify(msg, s, kp.pub); Assert.False(valid); // encode, decode keypair, test SignaturePrivateKey priv = new SignaturePrivateKey(kp.priv.getEncoded()); SignaturePublicKey pub = new SignaturePublicKey(kp.pub.getEncoded()); kp = new SignatureKeyPair(priv, pub); s = ntru.sign(msg, kp); valid = ntru.verify(msg, s, kp.pub); Assert.True(valid); // altering the signature should make it invalid s[rng.Next(s.Length)] += 1; valid = ntru.verify(msg, s, kp.pub); Assert.False(valid); // sparse/dense param.sparse = !param.sparse; s = ntru.sign(msg, kp); valid = ntru.verify(msg, s, kp.pub); Assert.True(valid); s[rng.Next(s.Length)] += 1; valid = ntru.verify(msg, s, kp.pub); Assert.False(valid); param.sparse = !param.sparse; // decrease NormBound to force multiple signing attempts SignatureParameters params2 = param.Clone(); params2.normBoundSq *= (float)4.0 / 9; // works for APR2011_439_PROD but may need to be increased for different params params2.signFailTolerance = 10000; ntru = new NtruSign(params2); s = ntru.sign(msg, kp); valid = ntru.verify(msg, s, kp.pub); Assert.True(valid); // test KeyGenAlg.FLOAT (default=RESULTANT) params2 = param.Clone(); param.keyGenAlg = KeyGenAlg.FLOAT; ntru = new NtruSign(param); kp = ntru.generateKeyPair(); s = ntru.sign(msg, kp); valid = ntru.verify(msg, s, kp.pub); Assert.True(valid); s[rng.Next(s.Length)] += 1; valid = ntru.verify(msg, s, kp.pub); Assert.False(valid); }
/** * Generates a new signature key pair. Runs in a single thread. * @return a key pair */ public SignatureKeyPair generateKeyPairSingleThread() { SignaturePrivateKey priv = new SignaturePrivateKey(param); SignaturePublicKey pub = null; Basis pubBasis = generateBoundedBasis(); pub = new SignaturePublicKey(pubBasis.h, param.q); pubBasis.h = null; // remove the public polynomial h from the private key priv.add(pubBasis); for (int k = param.B; k > 0; k--) { Basis basis = generateBoundedBasis(); priv.add(basis); } SignatureKeyPair kp = new SignatureKeyPair(priv, pub); return kp; }
/** * Constructs a new key pair from an input stream * @param is an input stream * @throws NtruException if an {@link IOException} occurs */ public SignatureKeyPair(MemoryStream ins) { pub = new SignaturePublicKey(ins); priv = new SignaturePrivateKey(ins); }
/** * Constructs a new key pair. * @param priv a private key * @param pub a public key */ public SignatureKeyPair(SignaturePrivateKey priv, SignaturePublicKey pub) { this.priv = priv; this.pub = pub; }