Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Add serialization methods for Fp2, Fp6, Fp12 and Gt (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Jul 14, 2022
1 parent a43c7bd commit 9880d28
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Fp12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public Fp12(in Fp6 c0, in Fp6 c1)
C1 = c1;
}

public static Fp12 FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
Fp6 c0 = Fp6.FromBytes(data[Fp6.Size..]);
Fp6 c1 = Fp6.FromBytes(data[..Fp6.Size]);
return new(in c0, in c1);
}

public static bool operator ==(in Fp12 a, in Fp12 b)
{
return a.C0 == b.C0 & a.C1 == b.C1;
Expand All @@ -70,6 +79,21 @@ public override int GetHashCode()
return C0.GetHashCode() ^ C1.GetHashCode();
}

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
TryWrite(result);
return result;
}

public bool TryWrite(Span<byte> buffer)
{
if (buffer.Length < Size) return false;
C0.TryWrite(buffer[Fp6.Size..Size]);
C1.TryWrite(buffer[0..Fp6.Size]);
return true;
}

public static Fp12 Random(RandomNumberGenerator rng)
{
return new(Fp6.Random(rng), Fp6.Random(rng));
Expand Down
24 changes: 24 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Fp2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public Fp2(in Fp c0, in Fp c1)
C1 = c1;
}

public static Fp2 FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
Fp c0 = Fp.FromBytes(data[Fp.Size..]);
Fp c1 = Fp.FromBytes(data[..Fp.Size]);
return new(in c0, in c1);
}

public static Fp2 Random(RandomNumberGenerator rng)
{
return new(Fp.Random(rng), Fp.Random(rng));
Expand Down Expand Up @@ -66,6 +75,21 @@ public override int GetHashCode()
return C0.GetHashCode() ^ C1.GetHashCode();
}

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
TryWrite(result);
return result;
}

public bool TryWrite(Span<byte> buffer)
{
if (buffer.Length < Size) return false;
C0.TryWrite(buffer[Fp.Size..Size]);
C1.TryWrite(buffer[0..Fp.Size]);
return true;
}

public Fp2 FrobeniusMap()
{
// This is always just a conjugation. If you're curious why, here's
Expand Down
26 changes: 26 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Fp6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public Fp6(in Fp2 c0, in Fp2 c1, in Fp2 c2)
C2 = c2;
}

public static Fp6 FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
Fp2 c0 = Fp2.FromBytes(data[(Fp2.Size * 2)..]);
Fp2 c1 = Fp2.FromBytes(data[Fp2.Size..(Fp2.Size * 2)]);
Fp2 c2 = Fp2.FromBytes(data[..Fp2.Size]);
return new(in c0, in c1, in c2);
}

public static bool operator ==(in Fp6 a, in Fp6 b)
{
return a.C0 == b.C0 & a.C1 == b.C1 & a.C2 == b.C2;
Expand All @@ -68,6 +78,22 @@ public override int GetHashCode()
return C0.GetHashCode() ^ C1.GetHashCode() ^ C2.GetHashCode();
}

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
TryWrite(result);
return result;
}

public bool TryWrite(Span<byte> buffer)
{
if (buffer.Length < Size) return false;
C0.TryWrite(buffer[(Fp2.Size * 2)..Size]);
C1.TryWrite(buffer[Fp2.Size..(Fp2.Size * 2)]);
C2.TryWrite(buffer[0..Fp2.Size]);
return true;
}

public static Fp6 Random(RandomNumberGenerator rng)
{
return new(Fp2.Random(rng), Fp2.Random(rng), Fp2.Random(rng));
Expand Down
15 changes: 15 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Gt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public Gt(in Fp12 f)
Value = f;
}

public static Gt FromBytes(ReadOnlySpan<byte> data)
{
return new(Fp12.FromBytes(data));
}

public static bool operator ==(in Gt a, in Gt b)
{
return a.Value == b.Value;
Expand All @@ -46,6 +51,16 @@ public override int GetHashCode()
return Value.GetHashCode();
}

public byte[] ToArray()
{
return Value.ToArray();
}

public bool TryWrite(Span<byte> buffer)
{
return Value.TryWrite(buffer);
}

public static Gt Random(RandomNumberGenerator rng)
{
while (true)
Expand Down

0 comments on commit 9880d28

Please sign in to comment.