/// <summary> /// Runs the EEA on two BigIntegers /// </summary> /// <param name="A">Quotient A</param> /// <param name="B">Quotient B</param> /// <returns>Return a BigIntEuclidean object that contains the result in the variables X, Y, and GCD</returns> /// /// <remarks> /// Implemented from pseudocode on <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm"/>Wikipedia /// </remarks> public static IntEuclidean Calculate(int A, int B) { int x = 0; int lastX = 1; int y = 1; int lastY = 0; while (B != 0) { int quotient = A / B; int temp = A; A = B; B = temp % B; temp = x; x = lastX - quotient * x; lastX = temp; temp = y; y = lastY - quotient * y; lastY = temp; } IntEuclidean result = new IntEuclidean(); result.X = lastX; result.Y = lastY; result.GCD = A; return(result); }
/// <summary> /// Runs the EEA on two BigIntegers /// </summary> /// <param name="A">Quotient A</param> /// <param name="B">Quotient B</param> /// <returns>Return a BigIntEuclidean object that contains the result in the variables X, Y, and GCD</returns> /// /// <remarks> /// Implemented from pseudocode on <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm"/>Wikipedia /// </remarks> public static IntEuclidean Calculate(int A, int B) { int x = 0; int lastX = 1; int y = 1; int lastY = 0; while (B != 0) { int quotient = A / B; int temp = A; A = B; B = temp % B; temp = x; x = lastX - quotient * x; lastX = temp; temp = y; y = lastY - quotient * y; lastY = temp; } IntEuclidean result = new IntEuclidean(); result.X = lastX; result.Y = lastY; result.GCD = A; return result; }
private void IsValid() { // test valid key pairs NtruSign ntru = null; SignatureKeyPair kp = null; SignatureParameters[] paramSets = new SignatureParameters[] { SignatureParameters.TEST157, SignatureParameters.TEST157_PROD }; foreach (SignatureParameters param in paramSets) { ntru = new NtruSign(param); kp = ntru.generateKeyPair(); Assert.True(kp.isValid()); } // test an invalid key pair int q = kp.pub.q; kp.pub.h.Multiply(101); // make h invalid kp.pub.h.ModPositive(q); Assert.False(kp.isValid()); int inv101 = IntEuclidean.Calculate(101, q).X; kp.pub.h.Multiply(inv101); // restore h kp.pub.h.ModPositive(q); IntegerPolynomial f = kp.priv.getBasis(0).f.ToIntegerPolynomial(); f.Multiply(3); // make f invalid kp.priv.getBasis(0).f = f; Assert.False(kp.isValid()); }
/** * Runs the EEA on two <code>int</code>s<br> * Implemented from pseudocode on <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">Wikipedia</a>. * * @param a * @param b * @return a <code>IntEuclidean</code> object that contains the result in the variables <code>x</code>, <code>y</code>, and <code>gcd</code> */ public static IntEuclidean Calculate(int a, int b) { int x = 0; int lastx = 1; int y = 1; int lasty = 0; while (b != 0) { int quotient = a / b; int temp = a; a = b; b = temp % b; temp = x; x = lastx - quotient * x; lastx = temp; temp = y; y = lasty - quotient * y; lasty = temp; } IntEuclidean result = new IntEuclidean(); result.x = lastx; result.y = lasty; result.gcd = a; return(result); }
//private static volatile bool IS_64_BITNESS_KNOWN; //private static volatile bool IS_64_BIT_JVM; /** * Calculates the inverse of n mod modulus */ public static int Invert(int n, int modulus) { n %= modulus; if (n < 0) { n += modulus; } return(IntEuclidean.Calculate(n, modulus).x); }
public override void PerformTest() { IntEuclidean r = IntEuclidean.Calculate(120, 23); Assert.AreEqual(-9, r.x); Assert.AreEqual(47, r.y); Assert.AreEqual(1, r.gcd); r = IntEuclidean.Calculate(126, 231); Assert.AreEqual(2, r.x); Assert.AreEqual(-1, r.y); Assert.AreEqual(21, r.gcd); }
/// <summary> /// IntEuclidean tests /// </summary> /// /// <returns>State</returns> public string Test() { try { IntEuclidean r = IntEuclidean.Calculate(120, 23); if (!r.X.Equals(-9)) { throw new Exception("IntEuclidean failed r.X!"); } if (!r.Y.Equals(47)) { throw new Exception("IntEuclidean failed r.Y!"); } if (!r.GCD.Equals(1)) { throw new Exception("IntEuclidean failed r.GCD!"); } OnProgress(new TestEventArgs("Passed round 1 X, Y and GCD value comparisons")); r = IntEuclidean.Calculate(126, 231); if (!r.X.Equals(2)) { throw new Exception("IntEuclidean failed r.X!"); } if (!r.Y.Equals(-1)) { throw new Exception("IntEuclidean failed r.Y!"); } if (!r.GCD.Equals(21)) { throw new Exception("IntEuclidean failed r.GCD!"); } OnProgress(new TestEventArgs("Passed round 2 X, Y and GCD value comparisons")); return(SUCCESS); } catch (Exception Ex) { string message = Ex.Message == null ? "" : Ex.Message; throw new Exception(FAILURE + message); } }
/// <summary> /// Tests if the key pair is valid. /// <para>See IEEE 1363.1 section 9.2.4.1.</para> /// </summary> /// /// <returns>if the key pair is valid, <c>true</c> otherwise false</returns> public bool IsValid() { int N = ((NTRUPrivateKey)PrivateKey).N; int q = ((NTRUPrivateKey)PrivateKey).Q; TernaryPolynomialType polyType = ((NTRUPrivateKey)PrivateKey).PolyType; if (((NTRUPublicKey)PublicKey).N != N) { return(false); } if (((NTRUPublicKey)PublicKey).Q != q) { return(false); } if (((NTRUPrivateKey)PrivateKey).T.ToIntegerPolynomial().Coeffs.Length != N) { return(false); } IntegerPolynomial h = ((NTRUPublicKey)PublicKey).H.ToIntegerPolynomial(); if (h.Coeffs.Length != N) { return(false); } if (!h.IsReduced(q)) { return(false); } IntegerPolynomial f = ((NTRUPrivateKey)PrivateKey).T.ToIntegerPolynomial(); if (polyType == TernaryPolynomialType.SIMPLE && !f.IsTernary()) { return(false); } // if t is a ProductFormPolynomial, ternarity of f1,f2,f3 doesn't need to be verified if (polyType == TernaryPolynomialType.PRODUCT && !(((NTRUPrivateKey)PrivateKey).T.GetType().Equals(typeof(ProductFormPolynomial)))) { return(false); } if (polyType == TernaryPolynomialType.PRODUCT) { f.Multiply(3); f.Coeffs[0] += 1; f.ModPositive(q); } // the key generator pre-multiplies h by 3, so divide by 9 instead of 3 int inv9 = IntEuclidean.Calculate(9, q).X; // 9^-1 mod q IntegerPolynomial g = f.Multiply(h, q); g.Multiply(inv9); g.ModCenter(q); if (!g.IsTernary()) { return(false); } int dg = N / 3; // see EncryptionParameters.Initialize() if (g.Count(1) != dg) { return(false); } if (g.Count(-1) != dg - 1) { return(false); } return(true); }