public void TestFromBytes() { byte[] input1 = { 0 }; Action action = () => ECPoint.FromBytes(input1, ECCurve.Secp256k1); action.ShouldThrow <FormatException>(); byte[] input2 = { 4, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152, 72, 58, 218, 119, 38, 163, 196, 101, 93, 164, 251, 252, 14, 17, 8, 168, 253, 23, 180, 72, 166, 133, 84, 25, 156, 71, 208, 143, 251, 16, 212, 184 }; ECPoint.FromBytes(input2, ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); byte[] input3 = { 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152 }; ECPoint.FromBytes(input3, ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); ECPoint.FromBytes(input2.Skip(1).ToArray(), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); byte[] input4 = generatePrivateKey(72); ECPoint.FromBytes(input4, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("3634473727541135791764834762056624681715094789735830699031648" + "273128038409767"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("18165245710263168158644330920009617039772504630129940696140050972160274286151"), ECCurve.Secp256k1), ECCurve.Secp256k1)); byte[] input5 = generatePrivateKey(96); ECPoint.FromBytes(input5, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("1780731860627700044960722568376592200742329637303199754547598" + "369979440671"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("14532552714582660066924456880521368950258152170031413196862950297402215317055"), ECCurve.Secp256k1), ECCurve.Secp256k1)); byte[] input6 = generatePrivateKey(104); ECPoint.FromBytes(input6, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("3634473727541135791764834762056624681715094789735830699031648" + "273128038409767"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("18165245710263168158644330920009617039772504630129940696140050972160274286151"), ECCurve.Secp256k1), ECCurve.Secp256k1)); }
public void TestOpMultiply() { ECPoint p = null; byte[] n = new byte[] { 1 }; Action action = () => p = p * n; action.ShouldThrow <ArgumentNullException>(); p = ECCurve.Secp256k1.G; n = null; action.ShouldThrow <ArgumentNullException>(); n = new byte[] { 1 }; action.ShouldThrow <ArgumentException>(); p = ECCurve.Secp256k1.Infinity; n = new byte[32]; (p * n).Should().Be(p); p = ECCurve.Secp256k1.G; (p * n).Should().Be(ECCurve.Secp256k1.Infinity); n[0] = 1; (p * n).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("63395642421589016740518975608504846303065672135176650115036476193363423546538"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("29236048674093813394523910922582374630829081423043497254162533033164154049666"), ECCurve.Secp256k1), ECCurve.Secp256k1)); }
public void TestTryParse() { ECPoint.TryParse("00", ECCurve.Secp256k1, out ECPoint result).Should().BeFalse(); result.Should().BeNull(); ECPoint.TryParse("0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8", ECCurve.Secp256k1, out result).Should().BeTrue(); result.Should().Be(ECCurve.Secp256k1.G); }
public void TestSerialize() { MemoryStream stream = new MemoryStream(); ECPoint point = new ECPoint(null, null, ECCurve.Secp256k1); ISerializable serializable = point; serializable.Serialize(new BinaryWriter(stream)); stream.ToArray().Should().BeEquivalentTo(new byte[] { 0 }); }
public void TestDeserialize() { ECPoint point = new ECPoint(null, null, ECCurve.Secp256k1); ISerializable serializable = point; byte[] input = { 4, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152, 72, 58, 218, 119, 38, 163, 196, 101, 93, 164, 251, 252, 14, 17, 8, 168, 253, 23, 180, 72, 166, 133, 84, 25, 156, 71, 208, 143, 251, 16, 212, 184 }; serializable.Deserialize(new BinaryReader(new MemoryStream(input))); point.X.Should().Be(ECCurve.Secp256k1.G.X); point.Y.Should().Be(ECCurve.Secp256k1.G.Y); }
public void TestCompareTo() { ECFieldElement X1 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256k1); ECFieldElement Y1 = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1); ECFieldElement X2 = new ECFieldElement(new BigInteger(300), ECCurve.Secp256k1); ECFieldElement Y2 = new ECFieldElement(new BigInteger(400), ECCurve.Secp256k1); ECPoint point1 = new ECPoint(X1, Y1, ECCurve.Secp256k1); ECPoint point2 = new ECPoint(X2, Y1, ECCurve.Secp256k1); ECPoint point3 = new ECPoint(X1, Y2, ECCurve.Secp256k1); point1.CompareTo(point1).Should().Be(0); point1.CompareTo(point2).Should().Be(-1); point2.CompareTo(point1).Should().Be(1); point1.CompareTo(point3).Should().Be(-1); point3.CompareTo(point1).Should().Be(1); }
public void TestEncodePoint() { ECPoint point = new ECPoint(null, null, ECCurve.Secp256k1); byte[] result1 = { 0 }; point.EncodePoint(true).Should().BeEquivalentTo(result1); point = ECCurve.Secp256k1.G; byte[] result2 = { 4, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152, 72, 58, 218, 119, 38, 163, 196, 101, 93, 164, 251, 252, 14, 17, 8, 168, 253, 23, 180, 72, 166, 133, 84, 25, 156, 71, 208, 143, 251, 16, 212, 184 }; point.EncodePoint(false).Should().BeEquivalentTo(result2); byte[] result3 = { 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152 }; point.EncodePoint(true).Should().BeEquivalentTo(result3); point = ECCurve.Secp256r1.G; byte[] result4 = { 3, 107, 23, 209, 242, 225, 44, 66, 71, 248, 188, 230, 229, 99, 164, 64, 242, 119, 3, 125, 129, 45, 235, 51, 160, 244, 161, 57, 69, 216, 152, 194, 150 }; point.EncodePoint(true).Should().BeEquivalentTo(result4); }
public void TestDeserializeFrom() { byte[] input1 = { 0 }; Action action = () => ECPoint.DeserializeFrom(new BinaryReader(new MemoryStream(input1)), ECCurve.Secp256k1); action.ShouldThrow <FormatException>(); byte[] input2 = { 4, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152, 72, 58, 218, 119, 38, 163, 196, 101, 93, 164, 251, 252, 14, 17, 8, 168, 253, 23, 180, 72, 166, 133, 84, 25, 156, 71, 208, 143, 251, 16, 212, 184 }; ECPoint.DeserializeFrom(new BinaryReader(new MemoryStream(input2)), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); action = () => ECPoint.DeserializeFrom(new BinaryReader(new MemoryStream(input2.Take(32).ToArray())), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); action.ShouldThrow <FormatException>(); byte[] input3 = { 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152 }; ECPoint.DeserializeFrom(new BinaryReader(new MemoryStream(input3)), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); byte[] input4 = { 3, 107, 23, 209, 242, 225, 44, 66, 71, 248, 188, 230, 229, 99, 164, 64, 242, 119, 3, 125, 129, 45, 235, 51, 160, 244, 161, 57, 69, 216, 152, 194, 150 }; ECPoint.DeserializeFrom(new BinaryReader(new MemoryStream(input4)), ECCurve.Secp256r1).Should().Be(ECCurve.Secp256r1.G); action = () => ECPoint.DeserializeFrom(new BinaryReader(new MemoryStream(input3.Take(input3.Length - 1).ToArray())), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G); action.ShouldThrow <FormatException>(); }
public void TestECPointConstructor() { ECPoint point = new ECPoint(); point.X.Should().BeNull(); point.Y.Should().BeNull(); point.Curve.Should().Be(ECCurve.Secp256r1); ECFieldElement X = new ECFieldElement(new BigInteger(100), ECCurve.Secp256k1); ECFieldElement Y = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1); point = new ECPoint(X, Y, ECCurve.Secp256k1); point.X.Should().Be(X); point.Y.Should().Be(Y); point.Curve.Should().Be(ECCurve.Secp256k1); Action action = () => new ECPoint(X, null, ECCurve.Secp256k1); action.ShouldThrow <ArgumentException>(); action = () => new ECPoint(null, Y, ECCurve.Secp256k1); action.ShouldThrow <ArgumentException>(); }
public void TestMultiply() { ECPoint p = ECCurve.Secp256k1.G; BigInteger k = BigInteger.Parse("100"); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("107303582290733097924842193972465022053148211775194373671539518313500194639752"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("103795966108782717446806684023742168462365449272639790795591544606836007446638"), ECCurve.Secp256k1), ECCurve.Secp256k1)); k = BigInteger.Parse("10000"); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("55279067612272658004429375184716238028207484982037227804583126224321918234542"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("93139664895507357192565643142424306097487832058389223752321585898830257071353"), ECCurve.Secp256k1), ECCurve.Secp256k1)); k = BigInteger.Parse("10000000000000"); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("115045167963494515061513744671884131783397561769819471159495798754884242293003"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("93759167105263077270762304290738437383691912799231615884447658154878797241853"), ECCurve.Secp256k1), ECCurve.Secp256k1)); k = BigInteger.Parse("1000000000000000000000000000000000000000"); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("114831276968810911840931876895388845736099852671055832194631099067239418074350"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("16721517996619732311261078486295444964227498319433363271180755596201863690708"), ECCurve.Secp256k1), ECCurve.Secp256k1)); k = new BigInteger(generatePrivateKey(100)); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("19222995016448259376216431079553428738726180595337971417371897285865264889977"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("6637081904924493791520919212064582313497884724460823966446023080706723904419"), ECCurve.Secp256k1), ECCurve.Secp256k1)); k = new BigInteger(generatePrivateKey(120)); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("79652345192111851576650978679091010173409410384772942769927955775006682639778"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("6460429961979335115790346961011058418773289452368186110818621539624566803831"), ECCurve.Secp256k1), ECCurve.Secp256k1)); k = new BigInteger(generatePrivateKey(300)); ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("105331914562708556186724786757483927866790351460145374033180496740107603569412"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("60523670886755698512704385951571322569877668383890769288780681319304421873758"), ECCurve.Secp256k1), ECCurve.Secp256k1)); }
private bool OnImportMultisigAddress(string[] args) { if (NoWallet()) { return(true); } if (args.Length < 4) { Console.WriteLine("Error. Invalid parameters."); return(true); } int m = int.Parse(args[2]); int n = args.Length - 3; if (m < 1 || m > n || n > 1024) { Console.WriteLine("Error. Invalid parameters."); return(true); } ECPoint[] publicKeys = args.Skip(3).Select(p => ECPoint.Parse(p, ECCurve.Secp256r1)).ToArray(); Contract multiSignContract = Contract.CreateMultiSigContract(m, publicKeys); KeyPair keyPair = Program.Wallet.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey(); WalletAccount account = Program.Wallet.CreateAccount(multiSignContract, keyPair); if (Program.Wallet is TLP6Wallet wallet) { wallet.Save(); } Console.WriteLine("Multisig. Addr.: " + multiSignContract.Address); return(true); }
public void TestEquals() { ECPoint point = ECCurve.Secp256k1.G; point.Equals(point).Should().BeTrue(); point.Equals(null).Should().BeFalse(); point = new ECPoint(null, null, ECCurve.Secp256k1); point.Equals(new ECPoint(null, null, ECCurve.Secp256r1)).Should().BeTrue(); point.Equals(ECCurve.Secp256r1.G).Should().BeFalse(); ECCurve.Secp256r1.G.Equals(point).Should().BeFalse(); ECFieldElement X1 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256k1); ECFieldElement Y1 = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1); ECFieldElement X2 = new ECFieldElement(new BigInteger(300), ECCurve.Secp256k1); ECFieldElement Y2 = new ECFieldElement(new BigInteger(400), ECCurve.Secp256k1); ECPoint point1 = new ECPoint(X1, Y1, ECCurve.Secp256k1); ECPoint point2 = new ECPoint(X2, Y1, ECCurve.Secp256k1); ECPoint point3 = new ECPoint(X1, Y2, ECCurve.Secp256k1); point1.Equals(point2).Should().BeFalse(); point1.Equals(point3).Should().BeFalse(); }
public void TestSerializeAndDeserializeConsensusContext() { var consensusContext = new ConsensusContext(null, null) { Block = new Block { PrevHash = Blockchain.GenesisBlock.Hash, Index = 1, Timestamp = 4244941711, NextConsensus = UInt160.Parse("5555AAAA5555AAAA5555AAAA5555AAAA5555AAAA"), ConsensusData = new ConsensusData { PrimaryIndex = 6 } }, ViewNumber = 2, Validators = new ECPoint[7] { ECPoint.Parse("02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70", ECCurve.Secp256r1), ECPoint.Parse("024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d", ECCurve.Secp256r1), ECPoint.Parse("02aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e", ECCurve.Secp256r1), ECPoint.Parse("02ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba554", ECCurve.Secp256r1), ECPoint.Parse("02df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e895093", ECCurve.Secp256r1), ECPoint.Parse("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", ECCurve.Secp256r1), ECPoint.Parse("03b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a", ECCurve.Secp256r1) }, MyIndex = -1 }; var testTx1 = TestUtils.CreateRandomHashTransaction(); var testTx2 = TestUtils.CreateRandomHashTransaction(); int txCountToInlcude = 256; consensusContext.TransactionHashes = new UInt256[txCountToInlcude]; Transaction[] txs = new Transaction[txCountToInlcude]; for (int i = 0; i < txCountToInlcude; i++) { txs[i] = TestUtils.CreateRandomHashTransaction(); consensusContext.TransactionHashes[i] = txs[i].Hash; } // consensusContext.TransactionHashes = new UInt256[2] {testTx1.Hash, testTx2.Hash}; consensusContext.Transactions = txs.ToDictionary(p => p.Hash); consensusContext.PreparationPayloads = new ConsensusPayload[consensusContext.Validators.Length]; var prepareRequestMessage = new PrepareRequest { TransactionHashes = consensusContext.TransactionHashes, Timestamp = 23 }; consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, prepareRequestMessage, 6, new[] { (byte)'3', (byte)'!' }); consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 0, new[] { (byte)'t', (byte)'e' }); consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 1, new[] { (byte)'s', (byte)'t' }); consensusContext.PreparationPayloads[2] = null; consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 3, new[] { (byte)'1', (byte)'2' }); consensusContext.PreparationPayloads[4] = null; consensusContext.PreparationPayloads[5] = null; consensusContext.CommitPayloads = new ConsensusPayload[consensusContext.Validators.Length]; using (SHA256 sha256 = SHA256.Create()) { consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()) }, 3, new[] { (byte)'3', (byte)'4' }); consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()) }, 3, new[] { (byte)'6', (byte)'7' }); } consensusContext.Block.Timestamp = TimeProvider.Current.UtcNow.ToTimestampMS(); consensusContext.ChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length]; consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 6 }, 0, new[] { (byte)'A' }); consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 5 }, 1, new[] { (byte)'B' }); consensusContext.ChangeViewPayloads[2] = null; consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' }); consensusContext.ChangeViewPayloads[4] = null; consensusContext.ChangeViewPayloads[5] = null; consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 1 }, 6, new[] { (byte)'D' }); consensusContext.LastChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length]; var copiedContext = TestUtils.CopyMsgBySerialization(consensusContext, new ConsensusContext(null, null)); copiedContext.Block.PrevHash.Should().Be(consensusContext.Block.PrevHash); copiedContext.Block.Index.Should().Be(consensusContext.Block.Index); copiedContext.ViewNumber.Should().Be(consensusContext.ViewNumber); copiedContext.Validators.ShouldAllBeEquivalentTo(consensusContext.Validators); copiedContext.MyIndex.Should().Be(consensusContext.MyIndex); copiedContext.Block.ConsensusData.PrimaryIndex.Should().Be(consensusContext.Block.ConsensusData.PrimaryIndex); copiedContext.Block.Timestamp.Should().Be(consensusContext.Block.Timestamp); copiedContext.Block.NextConsensus.Should().Be(consensusContext.Block.NextConsensus); copiedContext.TransactionHashes.ShouldAllBeEquivalentTo(consensusContext.TransactionHashes); copiedContext.Transactions.ShouldAllBeEquivalentTo(consensusContext.Transactions); copiedContext.Transactions.Values.ShouldAllBeEquivalentTo(consensusContext.Transactions.Values); copiedContext.PreparationPayloads.ShouldAllBeEquivalentTo(consensusContext.PreparationPayloads); copiedContext.CommitPayloads.ShouldAllBeEquivalentTo(consensusContext.CommitPayloads); copiedContext.ChangeViewPayloads.ShouldAllBeEquivalentTo(consensusContext.ChangeViewPayloads); }