Skip to content

Commit 8c9e0c6

Browse files
Jim8yshargon
andauthored
Adding keccak256 (#2925)
* Create codeql.yml * Keccak256 * Delete .github/workflows/codeql.yml * Update src/Neo/SmartContract/Native/CryptoLib.cs * add more keccak256 test cases as required * Create codeql.yml * Keccak256 * Delete .github/workflows/codeql.yml * Update src/Neo/SmartContract/Native/CryptoLib.cs * add more keccak256 test cases as required * HF_Manticore * Fix copyright * Create codeql.yml * Keccak256 * Delete .github/workflows/codeql.yml * Update src/Neo/SmartContract/Native/CryptoLib.cs * add more keccak256 test cases as required * Create codeql.yml * Keccak256 * Delete .github/workflows/codeql.yml * Update src/Neo/SmartContract/Native/CryptoLib.cs * add more keccak256 test cases as required * HF_Manticore * Fix copyright * HF_Manticore * Update CryptoLib.cs * Update CryptoLib.cs * Update UT_CryptoLib.cs * Apply suggestions from code review * clean usings --------- Co-authored-by: Shargon <shargon@gmail.com>
1 parent 963c557 commit 8c9e0c6

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

src/Neo/Hardfork.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Neo
1414
public enum Hardfork : byte
1515
{
1616
HF_Aspidochelone,
17-
HF_Basilisk
17+
HF_Basilisk,
18+
HF_Cockatrice
1819
}
1920
}

src/Neo/SmartContract/Native/CryptoLib.cs

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using Neo.Cryptography;
1313
using Neo.Cryptography.ECC;
14+
using Org.BouncyCastle.Crypto.Digests;
1415
using System;
1516
using System.Collections.Generic;
1617

@@ -64,6 +65,21 @@ public static byte[] Murmur32(byte[] data, uint seed)
6465
return murmur.ComputeHash(data);
6566
}
6667

68+
/// <summary>
69+
/// Computes the hash value for the specified byte array using the keccak256 algorithm.
70+
/// </summary>
71+
/// <param name="data">The input to compute the hash code for.</param>
72+
/// <returns>Computed hash</returns>
73+
[ContractMethod(Hardfork.HF_Cockatrice, CpuFee = 1 << 15)]
74+
public static byte[] Keccak256(byte[] data)
75+
{
76+
KeccakDigest keccak = new(256);
77+
keccak.BlockUpdate(data, 0, data.Length);
78+
byte[] result = new byte[keccak.GetDigestSize()];
79+
keccak.DoFinal(result, 0);
80+
return result;
81+
}
82+
6783
/// <summary>
6884
/// Verifies that a digital signature is appropriate for the provided key and message using the ECDSA algorithm.
6985
/// </summary>

tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs

+93
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Neo.SmartContract;
1616
using Neo.SmartContract.Native;
1717
using Neo.VM;
18+
using Org.BouncyCastle.Utilities.Encoders;
1819

1920
namespace Neo.UnitTests.SmartContract.Native
2021
{
@@ -334,5 +335,97 @@ public void TestBls12381ScalarMul_Compat()
334335
BLS12381PointType.G2Proj
335336
);
336337
}
338+
339+
/// <summary>
340+
/// Keccak256 cases are verified in https://emn178.github.io/online-tools/keccak_256.html
341+
/// </summary>
342+
[TestMethod]
343+
public void TestKeccak256_HelloWorld()
344+
{
345+
// Arrange
346+
byte[] inputData = "Hello, World!"u8.ToArray();
347+
string expectedHashHex = "acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f";
348+
349+
// Act
350+
byte[] outputData = CryptoLib.Keccak256(inputData);
351+
string outputHashHex = Hex.ToHexString(outputData);
352+
353+
// Assert
354+
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Hello, World!'.");
355+
}
356+
[TestMethod]
357+
public void TestKeccak256_Keccak()
358+
{
359+
// Arrange
360+
byte[] inputData = "Keccak"u8.ToArray();
361+
string expectedHashHex = "868c016b666c7d3698636ee1bd023f3f065621514ab61bf26f062c175fdbe7f2";
362+
363+
// Act
364+
byte[] outputData = CryptoLib.Keccak256(inputData);
365+
string outputHashHex = Hex.ToHexString(outputData);
366+
367+
// Assert
368+
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Keccak'.");
369+
}
370+
371+
[TestMethod]
372+
public void TestKeccak256_Cryptography()
373+
{
374+
// Arrange
375+
byte[] inputData = "Cryptography"u8.ToArray();
376+
string expectedHashHex = "53d49d225dd2cfe77d8c5e2112bcc9efe77bea1c7aa5e5ede5798a36e99e2d29";
377+
378+
// Act
379+
byte[] outputData = CryptoLib.Keccak256(inputData);
380+
string outputHashHex = Hex.ToHexString(outputData);
381+
382+
// Assert
383+
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Cryptography'.");
384+
}
385+
386+
[TestMethod]
387+
public void TestKeccak256_Testing123()
388+
{
389+
// Arrange
390+
byte[] inputData = "Testing123"u8.ToArray();
391+
string expectedHashHex = "3f82db7b16b0818a1c6b2c6152e265f682d5ebcf497c9aad776ad38bc39cb6ca";
392+
393+
// Act
394+
byte[] outputData = CryptoLib.Keccak256(inputData);
395+
string outputHashHex = Hex.ToHexString(outputData);
396+
397+
// Assert
398+
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Testing123'.");
399+
}
400+
401+
[TestMethod]
402+
public void TestKeccak256_LongString()
403+
{
404+
// Arrange
405+
byte[] inputData = "This is a longer string for Keccak256 testing purposes."u8.ToArray();
406+
string expectedHashHex = "24115e5c2359f85f6840b42acd2f7ea47bc239583e576d766fa173bf711bdd2f";
407+
408+
// Act
409+
byte[] outputData = CryptoLib.Keccak256(inputData);
410+
string outputHashHex = Hex.ToHexString(outputData);
411+
412+
// Assert
413+
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for the longer string.");
414+
}
415+
416+
[TestMethod]
417+
public void TestKeccak256_BlankString()
418+
{
419+
// Arrange
420+
byte[] inputData = ""u8.ToArray();
421+
string expectedHashHex = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";
422+
423+
// Act
424+
byte[] outputData = CryptoLib.Keccak256(inputData);
425+
string outputHashHex = Hex.ToHexString(outputData);
426+
427+
// Assert
428+
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for blank string.");
429+
}
337430
}
338431
}

0 commit comments

Comments
 (0)