Skip to content

Commit bfedd0f

Browse files
authored
Merge branch 'master' into fix/leveldb-deps
2 parents 18af18b + 82329e8 commit bfedd0f

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

src/Neo.VM/Instruction.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public int Size
5050
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5151
get
5252
{
53-
int prefixSize = OperandSizePrefixTable[(int)OpCode];
53+
var prefixSize = OperandSizePrefixTable[(byte)OpCode];
5454
return prefixSize > 0
5555
? 1 + prefixSize + Operand.Length
56-
: 1 + OperandSizeTable[(int)OpCode];
56+
: 1 + OperandSizeTable[(byte)OpCode];
5757
}
5858
}
5959

@@ -181,9 +181,9 @@ static Instruction()
181181
{
182182
foreach (FieldInfo field in typeof(OpCode).GetFields(BindingFlags.Public | BindingFlags.Static))
183183
{
184-
OperandSizeAttribute? attribute = field.GetCustomAttribute<OperandSizeAttribute>();
184+
var attribute = field.GetCustomAttribute<OperandSizeAttribute>();
185185
if (attribute == null) continue;
186-
int index = (int)(OpCode)field.GetValue(null)!;
186+
var index = (byte)(OpCode)field.GetValue(null)!;
187187
OperandSizePrefixTable[index] = attribute.SizePrefix;
188188
OperandSizeTable[index] = attribute.Size;
189189
}
@@ -198,12 +198,12 @@ private Instruction(OpCode opcode)
198198
internal Instruction(ReadOnlyMemory<byte> script, int ip) : this((OpCode)script.Span[ip++])
199199
{
200200
ReadOnlySpan<byte> span = script.Span;
201-
int operandSizePrefix = OperandSizePrefixTable[(int)OpCode];
201+
int operandSizePrefix = OperandSizePrefixTable[(byte)OpCode];
202202
int operandSize = 0;
203203
switch (operandSizePrefix)
204204
{
205205
case 0:
206-
operandSize = OperandSizeTable[(int)OpCode];
206+
operandSize = OperandSizeTable[(byte)OpCode];
207207
break;
208208
case 1:
209209
if (ip >= span.Length)

src/Neo/Cryptography/Murmur128.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System;
1313
using System.Buffers.Binary;
1414
using System.Runtime.CompilerServices;
15-
using System.Security.Cryptography;
1615

1716
namespace Neo.Cryptography
1817
{
@@ -32,7 +31,8 @@ public sealed class Murmur128 : System.Security.Cryptography.HashAlgorithm
3231
private readonly uint seed;
3332
private int length;
3433

35-
public override int HashSize => 128;
34+
public const int HashSizeInBits = 128;
35+
public override int HashSize => HashSizeInBits;
3636

3737
private ulong H1 { get; set; }
3838
private ulong H2 { get; set; }
@@ -44,6 +44,7 @@ public sealed class Murmur128 : System.Security.Cryptography.HashAlgorithm
4444
public Murmur128(uint seed)
4545
{
4646
this.seed = seed;
47+
HashSizeValue = HashSizeInBits;
4748
Initialize();
4849
}
4950

src/Neo/Cryptography/Murmur32.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
using System;
1313
using System.Buffers.Binary;
14-
using System.Security.Cryptography;
1514

1615
namespace Neo.Cryptography
1716
{
@@ -31,7 +30,8 @@ public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm
3130
private uint hash;
3231
private int length;
3332

34-
public override int HashSize => 32;
33+
public const int HashSizeInBits = 32;
34+
public override int HashSize => HashSizeInBits;
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="Murmur32"/> class with the specified seed.
@@ -40,6 +40,7 @@ public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm
4040
public Murmur32(uint seed)
4141
{
4242
this.seed = seed;
43+
HashSizeValue = HashSizeInBits;
4344
Initialize();
4445
}
4546

src/Neo/Cryptography/RIPEMD160Managed.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System;
1313
using System.Runtime.InteropServices;
1414
using System.Security;
15-
using System.Security.Cryptography;
1615

1716
namespace Neo.Cryptography
1817
{
@@ -27,7 +26,8 @@ public class RIPEMD160Managed : System.Security.Cryptography.HashAlgorithm
2726
private readonly uint[] _stateMD160;
2827
private readonly uint[] _blockDWords;
2928

30-
public override int HashSize => 160;
29+
public const int HashSizeInBits = 160;
30+
public override int HashSize => HashSizeInBits;
3131

3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="RIPEMD160Managed"/> class.
@@ -38,6 +38,7 @@ public RIPEMD160Managed()
3838
_blockDWords = new uint[16];
3939
_buffer = new byte[64];
4040

41+
HashSizeValue = HashSizeInBits;
4142
InitializeState();
4243
}
4344

tests/Neo.UnitTests/Cryptography/UT_Murmur128.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,15 @@ public void TestHashCore()
4242
array = "718f952132679baa9c5c2aa0d329fd2a".HexToBytes();
4343
array.Murmur128(123u).ToHexString().ToString().Should().Be("9b4aa747ff0cf4e41b3d96251551c8ae");
4444
}
45+
46+
[TestMethod]
47+
public void TestTryComputeHash()
48+
{
49+
var murmur128 = new Murmur128(123u);
50+
var buffer = new byte[murmur128.HashSize / 8];
51+
var ok = murmur128.TryComputeHash("hello world"u8.ToArray(), buffer, out _);
52+
ok.Should().BeTrue();
53+
buffer.ToHexString().Should().Be("e0a0632d4f51302c55e3b3e48d28795d");
54+
}
4555
}
4656
}

tests/Neo.UnitTests/Cryptography/UT_Murmur32.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using FluentAssertions;
1313
using Microsoft.VisualStudio.TestTools.UnitTesting;
1414
using Neo.Cryptography;
15+
using System.Buffers.Binary;
1516

1617
namespace Neo.UnitTests.Cryptography
1718
{
@@ -31,5 +32,19 @@ public void TestHashCore()
3132
byte[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 };
3233
array.Murmur32(10u).Should().Be(378574820u);
3334
}
35+
36+
[TestMethod]
37+
public void TestTryComputeHash()
38+
{
39+
var murmur3 = new Murmur32(10u);
40+
var buffer = new byte[murmur3.HashSize / 8];
41+
var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 };
42+
43+
var ok = murmur3.TryComputeHash(data, buffer, out _);
44+
ok.Should().BeTrue();
45+
46+
var hash = BinaryPrimitives.ReadUInt32LittleEndian(buffer);
47+
hash.Should().Be(378574820u);
48+
}
3449
}
3550
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2015-2024 The Neo Project.
2+
//
3+
// UT_RIPEMD160Managed.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using FluentAssertions;
13+
using Microsoft.VisualStudio.TestTools.UnitTesting;
14+
using Neo.Cryptography;
15+
using Neo.Extensions;
16+
17+
namespace Neo.UnitTests.Cryptography
18+
{
19+
[TestClass]
20+
public class UT_RIPEMD160Managed
21+
{
22+
[TestMethod]
23+
public void TestHashCore()
24+
{
25+
using var ripemd160 = new RIPEMD160Managed();
26+
var hash = ripemd160.ComputeHash("hello world"u8.ToArray());
27+
hash.ToHexString().Should().Be("98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f");
28+
}
29+
30+
[TestMethod]
31+
public void TestTryComputeHash()
32+
{
33+
using var ripemd160 = new RIPEMD160Managed();
34+
var buffer = new byte[ripemd160.HashSize / 8];
35+
var ok = ripemd160.TryComputeHash("hello world"u8.ToArray(), buffer, out _);
36+
ok.Should().BeTrue();
37+
buffer.ToHexString().Should().Be("98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f");
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)