/// <summary> /// Computes the bits required for a given encoding of an elliptic curve point. /// </summary> /// <returns>The integer bit length of the encoded points.</returns> /// <param name="encoding">The point encoding.</param> /// <param name="elementBitLength">The bit length elements in the underlying field of the curve.</param> public static int GetEncodingBitLength(PointEncoding encoding, int elementBitLength) { switch (encoding) { case PointEncoding.Compressed: return(elementBitLength + 8); default: return(2 * elementBitLength + 8); } }
/// <summary> /// Encodes the curve point in a byte buffer. /// </summary> /// <param name="encoding">The encoding scheme used to encode the point in the buffer.</param> /// <returns>Byte buffer containing the encoded curve point.</returns> public byte[] ToBytes(PointEncoding encoding = PointEncoding.Compressed) { using (var ctx = BigNumberContextHandle.Create()) { int requiredBufferLength = ECPointHandle.ToByteBuffer(_ecGroup, Handle, encoding, null, ctx); byte[] buffer = new byte[requiredBufferLength]; int writtenBytes = ECPointHandle.ToByteBuffer(_ecGroup, Handle, encoding, buffer, ctx); Debug.Assert(writtenBytes == requiredBufferLength); return(buffer); } }
public static int ToByteBuffer(ECGroupHandle group, ECPointHandle p, PointEncoding form, byte[]?buffer, BigNumberContextHandle ctx) { Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>"); Debug.Assert(!p.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(p)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); int bufferLen = 0; if (buffer != null) { bufferLen = buffer.Length; } bufferLen = EC_POINT_point2oct(group, p, form, buffer, bufferLen, ctx); if (bufferLen == 0) { throw new OpenSslNativeException(); } return(bufferLen); }
private extern static int EC_POINT_point2oct(ECGroupHandle group, ECPointHandle p, PointEncoding form, byte[]?buffer, int bufferLen, BigNumberContextHandle contextHandle);