Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Proposal of VM-UT #80

Merged
merged 8 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/neo-vm.Tests/Converters/ScriptConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Neo.Test.Extensions;
using Newtonsoft.Json;

namespace Neo.Test.Converters
{
internal class ScriptConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(byte[]) || objectType == typeof(string);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value is byte[] data) return data;
if (!(reader.Value is string str)) throw new FormatException();

if (str.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
{
data = str.FromHexString();
}
else
{
data = Convert.FromBase64String(str);
}

return data;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is byte[] data)
{
writer.WriteValue(data.ToHexString());
}
else
{
throw new FormatException();
}
}
}
}
40 changes: 40 additions & 0 deletions tests/neo-vm.Tests/Converters/StateConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Neo.VM;
using Newtonsoft.Json;

namespace Neo.Test.Converters
{
internal class StateConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (!(reader.Value is string str)) throw new FormatException();

VMState ret = VMState.NONE;

foreach (var split in str.Split("|", StringSplitOptions.RemoveEmptyEntries))
{
ret |= Enum.Parse<VMState>(split.Trim().ToUpperInvariant());
}

return ret;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is VMState data)
{
writer.WriteValue(data.ToString().Replace(" ", ""));
}
else
{
throw new FormatException();
}
}
}
}
44 changes: 44 additions & 0 deletions tests/neo-vm.Tests/Cryptography/ECC/ECCurve.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Globalization;
using System.Numerics;
using Neo.Test.Extensions;

namespace Neo.Cryptography.ECC
{
public class ECCurve
{
internal readonly BigInteger Q;
internal readonly ECFieldElement A;
internal readonly ECFieldElement B;
internal readonly BigInteger N;
public readonly ECPoint Infinity;
public readonly ECPoint G;

private ECCurve(BigInteger Q, BigInteger A, BigInteger B, BigInteger N, byte[] G)
{
this.Q = Q;
this.A = new ECFieldElement(A, this);
this.B = new ECFieldElement(B, this);
this.N = N;
this.Infinity = new ECPoint(null, null, this);
this.G = ECPoint.DecodePoint(G, this);
}

public static readonly ECCurve Secp256k1 = new ECCurve
(
BigInteger.Parse("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", NumberStyles.AllowHexSpecifier),
BigInteger.Zero,
7,
BigInteger.Parse("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", NumberStyles.AllowHexSpecifier),
("04" + "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" + "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8").FromHexString()
);

public static readonly ECCurve Secp256r1 = new ECCurve
(
BigInteger.Parse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.AllowHexSpecifier),
BigInteger.Parse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", NumberStyles.AllowHexSpecifier),
BigInteger.Parse("005AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", NumberStyles.AllowHexSpecifier),
BigInteger.Parse("00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", NumberStyles.AllowHexSpecifier),
("04" + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5").FromHexString()
);
}
}
109 changes: 109 additions & 0 deletions tests/neo-vm.Tests/Cryptography/ECC/ECDsa.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using Neo.Test.Extensions;

namespace Neo.Cryptography.ECC
{
public class ECDsa
{
private readonly byte[] privateKey;
private readonly ECPoint publicKey;
private readonly ECCurve curve;

public ECDsa(byte[] privateKey, ECCurve curve)
: this(curve.G * privateKey)
{
this.privateKey = privateKey;
}

public ECDsa(ECPoint publicKey)
{
this.publicKey = publicKey;
this.curve = publicKey.Curve;
}

private BigInteger CalculateE(BigInteger n, byte[] message)
{
int messageBitLength = message.Length * 8;
BigInteger trunc = new BigInteger(message.Reverse().Concat(new byte[1]).ToArray());
if (n.GetBitLength() < messageBitLength)
{
trunc >>= messageBitLength - n.GetBitLength();
}
return trunc;
}

public BigInteger[] GenerateSignature(byte[] message)
{
if (privateKey == null) throw new InvalidOperationException();
BigInteger e = CalculateE(curve.N, message);
BigInteger d = new BigInteger(privateKey.Reverse().Concat(new byte[1]).ToArray());
BigInteger r, s;
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
do
{
BigInteger k;
do
{
do
{
k = rng.NextBigInteger(curve.N.GetBitLength());
}
while (k.Sign == 0 || k.CompareTo(curve.N) >= 0);
ECPoint p = ECPoint.Multiply(curve.G, k);
BigInteger x = p.X.Value;
r = x.Mod(curve.N);
}
while (r.Sign == 0);
s = (k.ModInverse(curve.N) * (e + d * r)).Mod(curve.N);
if (s > curve.N / 2)
{
s = curve.N - s;
}
}
while (s.Sign == 0);
}
return new BigInteger[] { r, s };
}

private static ECPoint SumOfTwoMultiplies(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
{
int m = Math.Max(k.GetBitLength(), l.GetBitLength());
ECPoint Z = P + Q;
ECPoint R = P.Curve.Infinity;
for (int i = m - 1; i >= 0; --i)
{
R = R.Twice();
if (k.TestBit(i))
{
if (l.TestBit(i))
R = R + Z;
else
R = R + P;
}
else
{
if (l.TestBit(i))
R = R + Q;
}
}
return R;
}

public bool VerifySignature(byte[] message, BigInteger r, BigInteger s)
{
if (r.Sign < 1 || s.Sign < 1 || r.CompareTo(curve.N) >= 0 || s.CompareTo(curve.N) >= 0)
return false;
BigInteger e = CalculateE(curve.N, message);
BigInteger c = s.ModInverse(curve.N);
BigInteger u1 = (e * c).Mod(curve.N);
BigInteger u2 = (r * c).Mod(curve.N);
ECPoint point = SumOfTwoMultiplies(curve.G, u1, publicKey, u2);
BigInteger v = point.X.Value.Mod(curve.N);
return v.Equals(r);
}
}
}
Loading