//Checks to see if two polynoms are equal by comparing their values public override bool Equals(object ob) { IrreduciblePolynom poly = ob as IrreduciblePolynom; if (poly == null) { return(base.Equals(ob)); } return(PolynomValue.Equals(poly.PolynomValue)); }
//Generates enumerable for random finite field polynoms using a given Irreducible polynom and size private static IEnumerable <FFPolynom> GenerateRandomPolynom(IrreduciblePolynom irreduciblePolynomial, int total) { RandomNumberGenerator rng = RandomNumberGenerator.Create(); for (int i = 0; i < total; i++) { //Size in bytes = Degree/8 to accomodate the length of the secret phrase byte[] randomCoeffs = new byte[irreduciblePolynomial.SizeInBytes]; rng.GetBytes(randomCoeffs); yield return(new FFPolynom(irreduciblePolynomial, randomCoeffs.FromLittleEndUnsignedBytesToBigInt())); } }
//Checks for matching with Regex and proccess the , and returns FiniteFieldPoint if success; otherwise null. internal static bool TryParse(Match matchedShare, out FFPoint res) { if (!matchedShare.Success) { res = null; return(false); } try { //Parse the share string: x - number by order, y - the string after '.' sign //Gets the string matched by the particular expression. Change to lowcase and apply casing invariant string xStr = matchedShare.Groups["x"].Value.ToLowerInvariant(); string yStr = matchedShare.Groups["y"].Value.ToLowerInvariant(); //Remove initial 0s to compare with ordinal = 4 while (xStr.StartsWith("0", StringComparison.Ordinal)) { xStr = xStr.Substring(1); } // 1hexChar = 4 bits, so degree in bits = degree * 4 int polynomialDegree = yStr.Length * 4; IrreduciblePolynom irp = new IrreduciblePolynom(polynomialDegree); FFPolynom x = new FFPolynom(irp, BigInteger.Parse(xStr)); // Create an array of bites (big endian) with the length in bytes as initial phrase, initialize to 0 byte[] yArr = new byte[yStr.Length / 2]; for (int i = 0; i < yStr.Length; i += 2) { //Convert a hex-string to byte representation (1hex = 4 bits).. that's why i/2 yArr[i / 2] = Byte.Parse(yStr.Substring(i, 2), NumberStyles.HexNumber); } //Convert yArr to BigInteger and create a finite field polynom FFPolynom y = new FFPolynom(irp, yArr.FromBigEndUnsignedBytesToBigInt()); res = new FFPoint(x, y); return(true); } catch (Exception e) { res = null; return(false); } }
//Shares the secret respectfully to number of shares and threshold protected virtual SharedSecret ShareImpl(byte[] secret, int threshold, int numberOfShares) { //Define irred polynom for a secret IrreduciblePolynom irreduciblePolynom = IrreduciblePolynom.GiveFromBytes(secret.Length); BigInteger rawSecret = secret.FromBigEndUnsignedBytesToBigInt(); FFPolynom secretCoeff = new FFPolynom(irreduciblePolynom, rawSecret); //Generate random polynom with corresponding irred. polynom and given threshold IEnumerable <FFPolynom> randPolynom = GenerateRandomPolynom(irreduciblePolynom, threshold - 1); //Construct an array of all coefficients represented as finite field polynoms FFPolynom[] consTerm = new[] { secretCoeff }; //Concatenate previously created random coefficients converting them as an array FFPolynom[] allCoefficients = consTerm.Concat(randPolynom).ToArray(); return(new SharedSecret(threshold, irreduciblePolynom, allCoefficients)); }
// internal FFPolynom[] AllCoefficients { get { return _allCoefficients; } } public SharedSecret(int threshold, IrreduciblePolynom irreduciblePolynomial, FFPolynom[] allCoefficients) { Threshold = threshold; _irreduciblePolynom = irreduciblePolynomial; _allCoefficients = allCoefficients; }
//Constructor to create FiniteFieldPolynom object passing the irreducible polynom and its polyn value public FFPolynom(IrreduciblePolynom primePoly, BigInteger poly) { _polynomCoefficients = poly; PrimePolynom = primePoly; }