Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding keccak256 #2925

Merged
merged 40 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7570cec
Create codeql.yml
Jim8y Jan 3, 2023
0aceab8
Merge branch 'neo-project:master' into master
Jim8y Jan 29, 2023
503a7cd
Merge branch 'neo-project:master' into master
Jim8y Mar 27, 2023
b594c71
Merge branch 'neo-project:master' into master
Jim8y Aug 20, 2023
1419c72
Merge branch 'neo-project:master' into master
Jim8y Sep 5, 2023
05812ee
Merge branch 'master' of github.com:neo-project/neo
Jim8y Sep 6, 2023
c7f72ec
Merge branch 'master' of github.com:neo-project/neo
Jim8y Sep 30, 2023
12bafdb
Keccak256
Jim8y Sep 30, 2023
b4f6ed8
Delete .github/workflows/codeql.yml
Jim8y Sep 30, 2023
896a790
Update src/Neo/SmartContract/Native/CryptoLib.cs
shargon Oct 2, 2023
f92cc9f
Merge branch 'master' into adding-Keccak256
Jim8y Oct 3, 2023
c1ee50a
add more keccak256 test cases as required
Jim8y Oct 9, 2023
bf57c50
Create codeql.yml
Jim8y Jan 3, 2023
c95a6c5
Keccak256
Jim8y Sep 30, 2023
3057cc7
Delete .github/workflows/codeql.yml
Jim8y Sep 30, 2023
a9308c8
Update src/Neo/SmartContract/Native/CryptoLib.cs
shargon Oct 2, 2023
6ed7e06
add more keccak256 test cases as required
Jim8y Oct 9, 2023
a020675
HF_Manticore
shargon Nov 6, 2023
e152638
HF Manticore
shargon Nov 6, 2023
1e7f745
Fix copyright
shargon Nov 6, 2023
e5f5378
Create codeql.yml
Jim8y Jan 3, 2023
edd693f
Keccak256
Jim8y Sep 30, 2023
cc388bd
Delete .github/workflows/codeql.yml
Jim8y Sep 30, 2023
f7d8ada
Update src/Neo/SmartContract/Native/CryptoLib.cs
shargon Oct 2, 2023
b2aa41c
add more keccak256 test cases as required
Jim8y Oct 9, 2023
d9ccc45
Create codeql.yml
Jim8y Jan 3, 2023
d1363ad
Keccak256
Jim8y Sep 30, 2023
a23192a
Delete .github/workflows/codeql.yml
Jim8y Sep 30, 2023
200e91f
Update src/Neo/SmartContract/Native/CryptoLib.cs
shargon Oct 2, 2023
25cdb28
add more keccak256 test cases as required
Jim8y Oct 9, 2023
3983a32
HF_Manticore
shargon Nov 6, 2023
d6d6f85
Fix copyright
shargon Nov 6, 2023
7cac796
HF_Manticore
shargon Nov 6, 2023
e56e68b
rebase
shargon Nov 6, 2023
f8f328c
Merge branch 'update-native-contracts' into adding-Keccak256
shargon Jan 2, 2024
16676ad
Update CryptoLib.cs
shargon Jan 2, 2024
4171bc0
Update CryptoLib.cs
shargon Jan 2, 2024
60a1de1
Update UT_CryptoLib.cs
shargon Jan 2, 2024
f9c3259
Apply suggestions from code review
shargon Jan 2, 2024
560d357
clean usings
shargon Jan 2, 2024
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
3 changes: 2 additions & 1 deletion src/Neo/Hardfork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Neo
public enum Hardfork : byte
{
HF_Aspidochelone,
HF_Basilisk
HF_Basilisk,
HF_Cockatrice
}
}
16 changes: 16 additions & 0 deletions src/Neo/SmartContract/Native/CryptoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Collections.Generic;

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

/// <summary>
/// Computes the hash value for the specified byte array using the keccak256 algorithm.
/// </summary>
/// <param name="data">The input to compute the hash code for.</param>
/// <returns>Computed hash</returns>
[ContractMethod(Hardfork.HF_Cockatrice, CpuFee = 1 << 15)]
public static byte[] Keccak256(byte[] data)
{
KeccakDigest keccak = new(256);
keccak.BlockUpdate(data, 0, data.Length);
byte[] result = new byte[keccak.GetDigestSize()];
keccak.DoFinal(result, 0);
return result;
}

/// <summary>
/// Verifies that a digital signature is appropriate for the provided key and message using the ECDSA algorithm.
/// </summary>
Expand Down
93 changes: 93 additions & 0 deletions tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Org.BouncyCastle.Utilities.Encoders;

namespace Neo.UnitTests.SmartContract.Native
{
Expand Down Expand Up @@ -334,5 +335,97 @@ public void TestBls12381ScalarMul_Compat()
BLS12381PointType.G2Proj
);
}

/// <summary>
/// Keccak256 cases are verified in https://emn178.github.io/online-tools/keccak_256.html
/// </summary>
[TestMethod]
public void TestKeccak256_HelloWorld()
{
// Arrange
byte[] inputData = "Hello, World!"u8.ToArray();
string expectedHashHex = "acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Hello, World!'.");
}
[TestMethod]
public void TestKeccak256_Keccak()
{
// Arrange
byte[] inputData = "Keccak"u8.ToArray();
string expectedHashHex = "868c016b666c7d3698636ee1bd023f3f065621514ab61bf26f062c175fdbe7f2";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Keccak'.");
}

[TestMethod]
public void TestKeccak256_Cryptography()
{
// Arrange
byte[] inputData = "Cryptography"u8.ToArray();
string expectedHashHex = "53d49d225dd2cfe77d8c5e2112bcc9efe77bea1c7aa5e5ede5798a36e99e2d29";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Cryptography'.");
}

[TestMethod]
public void TestKeccak256_Testing123()
{
// Arrange
byte[] inputData = "Testing123"u8.ToArray();
string expectedHashHex = "3f82db7b16b0818a1c6b2c6152e265f682d5ebcf497c9aad776ad38bc39cb6ca";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for 'Testing123'.");
}

[TestMethod]
public void TestKeccak256_LongString()
{
// Arrange
byte[] inputData = "This is a longer string for Keccak256 testing purposes."u8.ToArray();
string expectedHashHex = "24115e5c2359f85f6840b42acd2f7ea47bc239583e576d766fa173bf711bdd2f";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for the longer string.");
}

[TestMethod]
public void TestKeccak256_BlankString()
{
// Arrange
byte[] inputData = ""u8.ToArray();
string expectedHashHex = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";

// Act
byte[] outputData = CryptoLib.Keccak256(inputData);
string outputHashHex = Hex.ToHexString(outputData);

// Assert
Assert.AreEqual(expectedHashHex, outputHashHex, "Keccak256 hash did not match expected value for blank string.");
}
}
}