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 11 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
27 changes: 22 additions & 5 deletions src/Neo/SmartContract/Native/CryptoLib.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright (C) 2015-2022 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// Copyright (C) 2015-2023 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

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

namespace Neo.SmartContract.Native
{
Expand Down Expand Up @@ -63,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(CpuFee = 1 << 15)]
public static byte[] Keccak256(byte[] data)
{
KeccakDigest keccak = new KeccakDigest(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
17 changes: 16 additions & 1 deletion tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System.Security.Cryptography;
using Org.BouncyCastle.Utilities.Encoders;

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

[TestMethod]
public void TestKeccak256()
{
// Arrange
byte[] inputData = "Hello, Keccak!"u8.ToArray();
string expectedHashHex = "49222c3dd1a2b5cf30a8720050c31844dbc8eca466595b5ce9c11932b7aac985";

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

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