|
2 | 2 | // SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Security.Cryptography; |
5 | 6 | using NUnit.Framework; |
6 | 7 |
|
7 | 8 | namespace Nethermind.Crypto.Secp256k1.Test; |
@@ -122,4 +123,35 @@ public void Can_recover_uncompressed() |
122 | 123 | SecP256k1.RecoverKeyFromCompact(recovered, messageHash, signature, recoveryId, false); |
123 | 124 | Assert.That(recovered, Has.Length.EqualTo(65)); |
124 | 125 | } |
| 126 | + |
| 127 | + [Test] |
| 128 | + public void Recover_returns_false_for_out_of_range_recovery_id([Values(-1, 4, 255)] int recoveryId) |
| 129 | + { |
| 130 | + byte[] privateKey = new byte[32]; |
| 131 | + privateKey[0] = 1; |
| 132 | + byte[] messageHash = new byte[32]; |
| 133 | + messageHash[0] = 1; |
| 134 | + byte[]? signature = SecP256k1.SignCompact(messageHash, privateKey, out _); |
| 135 | + byte[] recovered = new byte[33]; |
| 136 | + Assert.That(SecP256k1.RecoverKeyFromCompact(recovered, messageHash, signature, recoveryId, true), Is.False); |
| 137 | + } |
| 138 | + |
| 139 | + [Test] |
| 140 | + public void Can_decompress() |
| 141 | + { |
| 142 | + byte[] privateKey = new byte[32]; |
| 143 | + privateKey[0] = 1; |
| 144 | + byte[] compressed = SecP256k1.GetPublicKey(privateKey, true)!; |
| 145 | + byte[] uncompressed = SecP256k1.GetPublicKey(privateKey, false)!; |
| 146 | + Assert.That(SecP256k1.Decompress(compressed), Is.EqualTo(uncompressed)); |
| 147 | + } |
| 148 | + |
| 149 | + [TestCase("000000000000000000000000000000000000000000000000000000000000000000")] |
| 150 | + [TestCase("020000000000000000000000000000000000000000000000000000000000000000")] |
| 151 | + [TestCase("02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")] |
| 152 | + public void Decompress_throws_for_invalid_key(string compressedHex) |
| 153 | + { |
| 154 | + byte[] compressed = Convert.FromHexString(compressedHex); |
| 155 | + Assert.That(() => SecP256k1.Decompress(compressed), Throws.TypeOf<CryptographicException>()); |
| 156 | + } |
125 | 157 | } |
0 commit comments