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

Commit

Permalink
Add array length checks (#12)
Browse files Browse the repository at this point in the history
* Add length checks

* Change should to must
  • Loading branch information
shargon committed Jan 8, 2024
1 parent 844bc3a commit 3f0726b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/Neo.Cryptography.BLS12_381/Fp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Neo.Cryptography.BLS12_381;
public static Fp FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
throw new FormatException($"The argument `{nameof(data)}` must contain {Size} bytes.");

Span<ulong> tmp = stackalloc ulong[SizeL];
BinaryPrimitives.TryReadUInt64BigEndian(data[0..8], out tmp[5]);
Expand Down Expand Up @@ -63,6 +63,9 @@ public static Fp FromBytes(ReadOnlySpan<byte> data)

internal static Fp FromRawUnchecked(ulong[] values)
{
if (values.Length != SizeL)
throw new FormatException($"The argument `{nameof(values)}` must contain {SizeL} entries.");

return MemoryMarshal.Cast<ulong, Fp>(values)[0];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/Fp12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Fp12(in Fp6 c0, in Fp6 c1)
public static Fp12 FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
throw new FormatException($"The argument `{nameof(data)}` must contain {Size} bytes.");
Fp6 c0 = Fp6.FromBytes(data[Fp6.Size..]);
Fp6 c1 = Fp6.FromBytes(data[..Fp6.Size]);
return new(in c0, in c1);
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/Fp2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Fp2(in Fp c0, in Fp c1)
public static Fp2 FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
throw new FormatException($"The argument `{nameof(data)}` must contain {Size} bytes.");
Fp c0 = Fp.FromBytes(data[Fp.Size..]);
Fp c1 = Fp.FromBytes(data[..Fp.Size]);
return new(in c0, in c1);
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/Fp6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Fp6(in Fp2 c0, in Fp2 c1, in Fp2 c2)
public static Fp6 FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
throw new FormatException($"The argument `{nameof(data)}` must 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]);
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/G1Projective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public G1Projective Double()
{
int length = b.Length;
if (length != 32)
throw new ArgumentException($"The argument {nameof(b)} should be 32 bytes.");
throw new ArgumentException($"The argument {nameof(b)} must be 32 bytes.");

G1Projective acc = Identity;

Expand Down
12 changes: 9 additions & 3 deletions src/Neo.Cryptography.BLS12_381/Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ namespace Neo.Cryptography.BLS12_381;

internal Scalar(ulong[] values)
{
if (values.Length != SizeL)
throw new FormatException($"The argument `{nameof(values)}` must contain {SizeL} entries.");

// This internal method is only used by the constants classes.
// The data must be in the correct format.
// So, there is no need to do any checks.
// So, there is no need to do any additional checks.
this = MemoryMarshal.AsRef<Scalar>(MemoryMarshal.Cast<ulong, byte>(values));
}

Expand All @@ -47,7 +50,7 @@ public Scalar(RandomNumberGenerator rng)
public static Scalar FromBytes(ReadOnlySpan<byte> data)
{
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size} bytes.");
throw new FormatException($"The argument `{nameof(data)}` must contain {Size} bytes.");

ref readonly Scalar ref_ = ref MemoryMarshal.AsRef<Scalar>(data);

Expand Down Expand Up @@ -77,14 +80,17 @@ public static Scalar FromBytes(ReadOnlySpan<byte> data)
public static Scalar FromBytesWide(ReadOnlySpan<byte> data)
{
if (data.Length != Size * 2)
throw new FormatException($"The argument `{nameof(data)}` should contain {Size * 2} bytes.");
throw new FormatException($"The argument `{nameof(data)}` must contain {Size * 2} bytes.");

ReadOnlySpan<Scalar> d = MemoryMarshal.Cast<byte, Scalar>(data);
return d[0] * R2 + d[1] * R3;
}

public static Scalar FromRaw(ReadOnlySpan<ulong> data)
{
if (data.Length != SizeL)
throw new FormatException($"The argument `{nameof(data)}` must contain {SizeL} entries.");

ReadOnlySpan<Scalar> span = MemoryMarshal.Cast<ulong, Scalar>(data);
return span[0] * R2;
}
Expand Down

0 comments on commit 3f0726b

Please sign in to comment.