public static BigRational ComputeP(int k, int n, int p) { // the P(n) equation BigRational pnNumerator = BigRational.Pow(p, n); BigRational pnDenominator = BigRational.Pow(k, (n - k)) * Factorial(k); // the P(0) equation //---the right side of "+" sign in Denominator BigRational pk = BigRational.Pow(p, k); BigRational factorialK = Factorial(k); // CHANGED: Don't cast to double here (loses precision) BigRational lastPart = (BigRational.Subtract(1, BigRational.Divide(p, k))); BigRational factorialAndLastPart = BigRational.Multiply(factorialK, lastPart); BigRational fullRightSide = BigRational.Divide(pk, factorialAndLastPart); //---the left side of "+" sign in Denominator BigRational series = Series(k, p); BigRational p0Denominator = series + fullRightSide; BigRational p0Result = BigRational.Divide(1, p0Denominator); BigRational pNResult = BigRational.Divide((pnNumerator * p0Result), pnDenominator); return(pNResult); }
public void TestMultiplication() { BigRational sevenTwoths = new BigRational(BigInteger.Zero, new Fraction(7, 2)); BigRational sevenFifths = new BigRational(BigInteger.Zero, new Fraction(7, 5)); BigRational expected = BigRational.Reduce(new BigRational(BigInteger.Zero, 49, 10)); BigRational result = BigRational.Multiply(sevenTwoths, sevenFifths); Assert.AreEqual(expected, result); }
public static BigRational Factorial(int k) { if (k <= 1) { return(1); } else { return(BigRational.Multiply(k, Factorial(k - 1))); } }
public override byte[] Split(byte[] secretClear, int shareCount, int threshold) { // var primeArr = ComputeRandomePrime(); // var prime = new BigInteger(primeArr); var primeMinusOne = _prime - 1; var number = new BigInteger(secretClear); var coef = new BigInteger[threshold]; coef[0] = number; // TODO: rewrite this to use cryptographically-secure RNG var rng = new Random(); var pmo = new BigRational(primeMinusOne); for (int c = 1; c < threshold; ++c) { coef[c] = BigRational.Multiply(pmo, new BigRational(rng.NextDouble())).GetWholePart(); } var shares = new Tuple <int, BigInteger> [shareCount]; for (var x = 1; x <= shareCount; ++x) { System.Console.WriteLine("X: " + x); var accum = coef[0]; for (int exp = 1; exp < threshold; ++exp) { // accum = (accum + (coef[exp] * (Math.pow(x, exp) % prime) % prime)) % prime; var a = new BigInteger(Math.Pow(x, exp)) % _prime; // (Math.pow(x, exp) % prime) var b = (coef[exp] * a) % _prime; // (coef[exp] * a % prime) var c = (accum + b) % _prime; // (accum + b) % prime; accum = c; } shares[x - 1] = Tuple.Create(x, accum); } Shares = shares.Select(x => { var index = BitConverter.GetBytes(x.Item1); var biarr = x.Item2.ToByteArray(); var bytes = new byte[INT_ARR_LEN + biarr.Length]; Array.Copy(index, 0, bytes, 0, INT_ARR_LEN); Array.Copy(biarr, 0, bytes, INT_ARR_LEN, biarr.Length); return(bytes); }); // The original secret value is fully encoded in the distributed shares so there // is no need to return any version of the original secreted in encrypted form return(new byte[0]); }
public void big_rational_static_methods() { Assert.True(0.5 == BigRational.Abs(0.5d)); Assert.True(0.5 == BigRational.Abs(-0.5d)); Assert.False(0.5 == BigRational.Abs(-1)); Assert.False(0.5 == BigRational.Abs(1)); Assert.True(-0.5 == BigRational.Negate(0.5d)); Assert.True(0.5 == BigRational.Negate(-0.5d)); Assert.False(0.5 == BigRational.Negate(-1)); Assert.False(-0.5 == BigRational.Negate(1)); //Doesn't work great... testing with equals doubles fails Assert.True(new BigRational(1, 2) == BigRational.Invert(new BigRational(2, 1))); Assert.True(new BigRational(2, 1) == BigRational.Invert(new BigRational(1, 2))); Assert.True(new BigRational(-1, 2) == BigRational.Invert(new BigRational(-2, 1))); Assert.True(new BigRational(-2, 1) == BigRational.Invert(new BigRational(-1, 2))); Assert.True(new BigRational(BigInteger.One) + new BigRational(BigInteger.One) == 2); Assert.True(BigRational.Add(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 2); //Not working for me //BigRational big1 = new BigRational(1.0); //big1++; //Assert.AreEqual(new BigRational(2, 1), big1); //Not working for me //BigRational big2 = new BigRational(1.0); //big2--; //Assert.True(0 == big2); Assert.True(new BigRational(BigInteger.One) - new BigRational(BigInteger.One) == 0); Assert.True(BigRational.Subtract(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 0); BigRational big3 = new BigRational(1.0); Assert.True(-1.0 == -big3); BigRational big4 = new BigRational(1.0); Assert.True(1.0 == +big3); Assert.True(new BigRational(BigInteger.One) * new BigRational(BigInteger.One) == 1); Assert.True(BigRational.Multiply(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 1); Assert.True(new BigRational(BigInteger.One) * new BigRational(2.0) == 2.0); Assert.True(BigRational.Multiply(new BigRational(BigInteger.One), new BigRational(2.0)) == 2.0); Assert.True(new BigRational(BigInteger.One) / new BigRational(BigInteger.One) == 1); Assert.True(BigRational.Divide(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 1); Assert.AreEqual(new BigRational(1, 2), new BigRational(new BigInteger(1)) / new BigRational(new BigInteger(2))); //Again, mixing consructors doesn't work well. Assert.AreEqual(new BigRational(1, 2), BigRational.Divide(new BigRational(new BigInteger(1)), new BigRational(new BigInteger(2)))); //Again, mixing consructors doesn't work well. Testing with equals double 0.5 fails //Assert.True(new BigRational(BigInteger.One) < new BigRational(2.0)); //Don't mix the constructors for comparison!!! Assert.True(new BigRational(1.0) < new BigRational(2.0)); Assert.False(new BigRational(2.0) < new BigRational(1.0)); Assert.False(new BigRational(1.0) < new BigRational(1.0)); Assert.True(new BigRational(1.0) <= new BigRational(2.0)); Assert.True(new BigRational(1.0) <= new BigRational(1.0)); Assert.False(new BigRational(2.0) <= new BigRational(1.0)); Assert.True(new BigRational(1.0) == new BigRational(1.0)); Assert.True(new BigRational(1.0) != new BigRational(2.0)); Assert.False(new BigRational(1.0) > new BigRational(2.0)); Assert.True(new BigRational(2.0) > new BigRational(1.0)); Assert.False(new BigRational(BigInteger.One) > new BigRational(BigInteger.One)); Assert.False(new BigRational(1.0) >= new BigRational(2.0)); Assert.True(new BigRational(BigInteger.One) >= new BigRational(BigInteger.One)); Assert.True(new BigRational(2.0) >= new BigRational(1.0)); //Modulus doesn't seem to work well //Assert.AreEqual(BigRational.Zero, new BigRational(2.0) % new BigRational(1.0)); //Assert.AreEqual(BigRational.One, new BigRational(3.0) % new BigRational(2.0)); //Assert.True(new BigRational(1.5) % new BigRational(1.0) == new BigRational(0.5)); //Assert.True(new BigRational(1.0) % new BigRational(2.0) == 0); }