/// <summary> /// Create a permutation using an encoded permutation /// </summary> /// /// <param name="Encoded">The encoded permutation</param> public Permutation(byte[] Encoded) { if (Encoded.Length <= 4) { throw new ArgumentException("Permutation: Invalid encoding!"); } int n = LittleEndian.OctetsToInt(Encoded, 0); int size = BigMath.CeilLog256(n - 1); if (Encoded.Length != 4 + n * size) { throw new ArgumentException("Permutation: Invalid encoding!"); } _perm = new int[n]; for (int i = 0; i < n; i++) { _perm[i] = LittleEndian.OctetsToInt(Encoded, 4 + i * size, size); } if (!IsPermutation(_perm)) { throw new ArgumentException("Permutation: Invalid encoding!"); } }
/// <summary> /// Encode this permutation as byte array /// </summary> /// /// <returns>The encoded permutation</returns> public byte[] GetEncoded() { int n = _perm.Length; int size = BigMath.CeilLog256(n - 1); byte[] result = new byte[4 + n * size]; LittleEndian.IntToOctets(n, result, 0); for (int i = 0; i < n; i++) { LittleEndian.IntToOctets(_perm[i], result, 4 + i * size, size); } return(result); }