public void Deserialize(BinaryReader reader) { FiniteFieldPolynomial x, y; DeserializeFrom(reader, out x, out y); this.X = x; this.Y = y; }
private static void DeserializeFrom(BinaryReader reader, out FiniteFieldPolynomial x, out FiniteFieldPolynomial y) { int x_i = (int)reader.ReadVarInt(); int expectedByteCount = (int)reader.ReadVarInt(); byte[] y_b = reader.ReadBytes(expectedByteCount); IrreduciblePolynomial irp = new IrreduciblePolynomial(expectedByteCount * 8); x = new FiniteFieldPolynomial(irp, x_i); y = new FiniteFieldPolynomial(irp, y_b.ToBigIntegerFromBigEndianUnsignedBytes()); }
private static void DeserializeFrom(BinaryReader reader, out FiniteFieldPolynomial x, out FiniteFieldPolynomial y) { int x_i = (int)reader.ReadVarInt(int.MaxValue); byte[] y_b = reader.ReadVarBytes(); IrreduciblePolynomial irp = new IrreduciblePolynomial(y_b.Length * 8); x = new FiniteFieldPolynomial(irp, x_i); y = new FiniteFieldPolynomial(irp, y_b.ToBigIntegerFromBigEndianUnsignedBytes()); }
private static FiniteFieldPoint AdjustPoint(int totalPoints, FiniteFieldPoint point) { var correction = new FiniteFieldPolynomial(point.Y.PrimePolynomial, BigInteger.One); var correctionMultiplier = point.X; for (int i = 1; i <= totalPoints; i++) { correction = correction * correctionMultiplier; } var newY = point.Y + correction; return new FiniteFieldPoint(point.X, newY); }
private static FiniteFieldPoint AdjustPoint(int totalPoints, FiniteFieldPoint point) { var correction = new FiniteFieldPolynomial(point.Y.PrimePolynomial, BigInteger.One); var correctionMultiplier = point.X; for (int i = 1; i <= totalPoints; i++) { correction = correction * correctionMultiplier; } var newY = point.Y + correction; return(new FiniteFieldPoint(point.X, newY)); }
public static SplitSecret Split(byte[] secret, int threshold) { var irreduciblePolynomial = IrreduciblePolynomial.CreateOfByteSize(secret.Length); var rawSecret = secret.ToBigIntegerFromBigEndianUnsignedBytes(); var diffusedSecret = DefaultDiffuser.Scramble(rawSecret, secret.Length); var secretCoefficient = new FiniteFieldPolynomial(irreduciblePolynomial, diffusedSecret); var allCoefficients = new[] { secretCoefficient } .Concat( GetRandomPolynomials( irreduciblePolynomial, threshold - 1) ) .ToArray(); return new SplitSecret(threshold, irreduciblePolynomial, allCoefficients); }
public static FiniteFieldPolynomial EvaluateAt(long x, FiniteFieldPolynomial[] coefficients) { // Use Horner's Scheme: http://en.wikipedia.org/wiki/Horner_scheme FiniteFieldPolynomial xAsPoly = coefficients[0].GetValueInField(x); // assume the coefficient for highest monomial is 1 FiniteFieldPolynomial result = xAsPoly.Clone(); for (int i = coefficients.Length - 1; i > 0; i--) { result = result + coefficients[i]; result = result * xAsPoly; } result = result + coefficients[0]; return result; }
public static FiniteFieldPolynomial EvaluateAt(long x, FiniteFieldPolynomial[] coefficients) { // Use Horner's Scheme: http://en.wikipedia.org/wiki/Horner_scheme FiniteFieldPolynomial xAsPoly = coefficients[0].GetValueInField(x); // assume the coefficient for highest monomial is 1 FiniteFieldPolynomial result = xAsPoly.Clone(); for (int i = coefficients.Length - 1; i > 0; i--) { result = result + coefficients[i]; result = result * xAsPoly; } result = result + coefficients[0]; return(result); }
public static bool TryParse(string s, out FiniteFieldPoint result) { var match = Regex.Match(s, @"(?<x>[0-9]+)-(?<y>[0-9a-fA-F]+)"); if (!match.Success) { result = null; return(false); } try { var xString = match.Groups["x"].Value.ToLowerInvariant(); var yString = match.Groups["y"].Value.ToLowerInvariant(); // Each hex letter makes up 4 bits, so to get the degree in bits // we multiply by 4 int polynomialDegree = yString.Length * 4; var irp = new IrreduciblePolynomial(polynomialDegree); var x = new FiniteFieldPolynomial(irp, BigInteger.Parse(xString)); // get bytes var bigEndianBytes = new byte[yString.Length / 2]; for (int i = 0; i < yString.Length; i += 2) { bigEndianBytes[i / 2] = Byte.Parse(yString.Substring(i, 2), NumberStyles.HexNumber); } var y = new FiniteFieldPolynomial(irp, bigEndianBytes.ToBigIntegerFromBigEndianUnsignedBytes()); result = new FiniteFieldPoint(x, y); return(true); } catch { result = null; return(false); } }
public static bool TryParse(string s, out FiniteFieldPoint result) { var match = Regex.Match(s, @"(?<x>[0-9]+)-(?<y>[0-9a-fA-F]+)"); if (!match.Success) { result = null; return false; } try { var xString = match.Groups["x"].Value.ToLowerInvariant(); var yString = match.Groups["y"].Value.ToLowerInvariant(); // Each hex letter makes up 4 bits, so to get the degree in bits // we multiply by 4 int polynomialDegree = yString.Length * 4; var irp = new IrreduciblePolynomial(polynomialDegree); var x = new FiniteFieldPolynomial(irp, BigInteger.Parse(xString)); // get bytes var bigEndianBytes = new byte[yString.Length / 2]; for (int i = 0; i < yString.Length; i += 2) { bigEndianBytes[i / 2] = Byte.Parse(yString.Substring(i, 2), NumberStyles.HexNumber); } var y = new FiniteFieldPolynomial(irp, bigEndianBytes.ToBigIntegerFromBigEndianUnsignedBytes()); result = new FiniteFieldPoint(x, y); return true; } catch { result = null; return false; } }
public FiniteFieldPoint GetShare(int n) { var xPoly = new FiniteFieldPolynomial(_IrreduciblePolynomial, new BigInteger(n)); var y = FiniteFieldPolynomial.EvaluateAt(n, _AllCoefficients); return new FiniteFieldPoint(xPoly, y); }
public FiniteFieldPoint(FiniteFieldPolynomial x, FiniteFieldPolynomial y) { X = x; Y = y; }
public void Deserialize(BinaryReader reader) { int x = (int)reader.ReadVarInt(); int expectedByteCount = (int)reader.ReadVarInt(); byte[] y = reader.ReadBytes(expectedByteCount); IrreduciblePolynomial irp = new IrreduciblePolynomial(expectedByteCount * 8); this.X = new FiniteFieldPolynomial(irp, x); this.Y = new FiniteFieldPolynomial(irp, y.ToBigIntegerFromBigEndianUnsignedBytes()); }
public SplitSecret(int threshold, IrreduciblePolynomial irreduciblePolynomial, FiniteFieldPolynomial[] allCoefficients) { Threshold = threshold; _IrreduciblePolynomial = irreduciblePolynomial; _AllCoefficients = allCoefficients; }