From d9379bed702045c73c391c85d189e0093963b86b Mon Sep 17 00:00:00 2001 From: Temnij Date: Thu, 28 Mar 2024 20:21:52 +0600 Subject: [PATCH 01/12] Base58 perfomance improvments --- src/Neo/Cryptography/Base58.cs | 55 +++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index adecc8b631..9c208fdab9 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -9,7 +9,9 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. +using Akka.IO; using System; +using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; @@ -27,6 +29,26 @@ public static class Base58 /// public const string Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + private static readonly char s_zeroChar = Alphabet[0]; + private static readonly int[] s_mapBase58 = new int[]{ + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, + -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1, + 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, + -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, + 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, + }; + /// /// Converts the specified , which encodes binary data as base-58 digits, to an equivalent byte array. The encoded contains the checksum of the binary data. /// @@ -74,19 +96,28 @@ public static byte[] Decode(string input) var bi = BigInteger.Zero; for (int i = 0; i < input.Length; i++) { - int digit = Alphabet.IndexOf(input[i]); - if (digit < 0) + var digit = s_mapBase58[(byte)input[i]]; + if (digit == -1) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); bi = bi * Alphabet.Length + digit; } // Encode BigInteger to byte[] // Leading zero bytes get encoded as leading `1` characters - int leadingZeroCount = input.TakeWhile(c => c == Alphabet[0]).Count(); - var leadingZeros = new byte[leadingZeroCount]; - if (bi.IsZero) return leadingZeros; - var bytesWithoutLeadingZeros = bi.ToByteArray(isUnsigned: true, isBigEndian: true); - return Concat(leadingZeros, bytesWithoutLeadingZeros); + int leadingZeroCount = LeadingBase58Zeros(input); + if (bi.IsZero) + { + return new byte[leadingZeroCount]; + } + + int decodedSize = bi.GetByteCount(true) + leadingZeroCount; + + Span result = decodedSize <= 128 + ? stackalloc byte[decodedSize] + : new byte[decodedSize]; + + _ = bi.TryWriteBytes(result[leadingZeroCount..], out _, true, true); + return result.ToArray(); } /// @@ -111,9 +142,17 @@ public static string Encode(ReadOnlySpan input) // Append `1` for each leading 0 byte for (int i = 0; i < input.Length && input[i] == 0; i++) { - sb.Insert(0, Alphabet[0]); + sb.Insert(0, s_zeroChar); } return sb.ToString(); } + + private static int LeadingBase58Zeros(string collection) + { + int i = 0; + for (; i < collection.Length && collection[i] == s_zeroChar; i++) { } + + return i; + } } } From 66bbd57060b2140da0f1f0c664c08d0294b91a29 Mon Sep 17 00:00:00 2001 From: Temnij Date: Thu, 28 Mar 2024 20:22:56 +0600 Subject: [PATCH 02/12] Fix usings --- src/Neo/Cryptography/Base58.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 9c208fdab9..51b11874be 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -9,13 +9,9 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using Akka.IO; using System; -using System.Collections.Generic; -using System.Linq; using System.Numerics; using System.Text; -using static Neo.Helper; namespace Neo.Cryptography { From 87691119befca3ca9d3ddaa2539f75d7f5655feb Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 29 Mar 2024 01:17:19 +0800 Subject: [PATCH 03/12] Update src/Neo/Cryptography/Base58.cs --- src/Neo/Cryptography/Base58.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 51b11874be..24f141d792 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -35,14 +35,6 @@ public static class Base58 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, }; /// From fb882ca012dca22f3e4ed4769d04b691feabd133 Mon Sep 17 00:00:00 2001 From: Temnij Date: Fri, 29 Mar 2024 14:31:30 +0600 Subject: [PATCH 04/12] Base58 improvments #2 --- src/Neo/Cryptography/Base58.cs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 24f141d792..9eea4ba4d2 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -10,7 +10,10 @@ // modifications are permitted. using System; +using System.Collections.Generic; +using System.Linq; using System.Numerics; +using System.Runtime.CompilerServices; using System.Text; namespace Neo.Cryptography @@ -26,16 +29,7 @@ public static class Base58 public const string Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; private static readonly char s_zeroChar = Alphabet[0]; - private static readonly int[] s_mapBase58 = new int[]{ - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, - -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1, - 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, - -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, - 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, - }; + private static readonly Lazy> s_alphabetDic = new(() => Enumerable.Range(0, Alphabet.Length).ToDictionary(t => Alphabet[t], t => t)); /// /// Converts the specified , which encodes binary data as base-58 digits, to an equivalent byte array. The encoded contains the checksum of the binary data. @@ -84,8 +78,7 @@ public static byte[] Decode(string input) var bi = BigInteger.Zero; for (int i = 0; i < input.Length; i++) { - var digit = s_mapBase58[(byte)input[i]]; - if (digit == -1) + if (!s_alphabetDic.Value.TryGetValue(input[i], out var digit)) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); bi = bi * Alphabet.Length + digit; } @@ -135,10 +128,13 @@ public static string Encode(ReadOnlySpan input) return sb.ToString(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int LeadingBase58Zeros(string collection) { int i = 0; - for (; i < collection.Length && collection[i] == s_zeroChar; i++) { } + + var len = collection.Length; + for (; i < len && collection[i] == s_zeroChar; i++) { } return i; } From 194c456f80548b7f87c4bddebdc4fdf69bbd5603 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 29 Mar 2024 01:36:23 -0700 Subject: [PATCH 05/12] Update src/Neo/Cryptography/Base58.cs --- src/Neo/Cryptography/Base58.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 9eea4ba4d2..907d041226 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -131,8 +131,7 @@ public static string Encode(ReadOnlySpan input) [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int LeadingBase58Zeros(string collection) { - int i = 0; - + var i = 0; var len = collection.Length; for (; i < len && collection[i] == s_zeroChar; i++) { } From faeb909fcb9f1ff527a6ec645885ab291213992c Mon Sep 17 00:00:00 2001 From: Temnij Date: Mon, 14 Oct 2024 23:56:55 +0600 Subject: [PATCH 06/12] fix perfomance --- src/Neo/Cryptography/Base58.cs | 61 ++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 907d041226..92ed0282ab 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -14,6 +14,7 @@ using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; +using System.Security.Cryptography; using System.Text; namespace Neo.Cryptography @@ -28,8 +29,8 @@ public static class Base58 /// public const string Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - private static readonly char s_zeroChar = Alphabet[0]; - private static readonly Lazy> s_alphabetDic = new(() => Enumerable.Range(0, Alphabet.Length).ToDictionary(t => Alphabet[t], t => t)); + private const char ZeroChar = '1'; + private static readonly IReadOnlyDictionary s_alphabetDic = Enumerable.Range(0, Alphabet.Length).ToDictionary(t => Alphabet[t], t => t); /// /// Converts the specified , which encodes binary data as base-58 digits, to an equivalent byte array. The encoded contains the checksum of the binary data. @@ -78,7 +79,7 @@ public static byte[] Decode(string input) var bi = BigInteger.Zero; for (int i = 0; i < input.Length; i++) { - if (!s_alphabetDic.Value.TryGetValue(input[i], out var digit)) + if (!s_alphabetDic.TryGetValue(input[i], out var digit)) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); bi = bi * Alphabet.Length + digit; } @@ -91,14 +92,11 @@ public static byte[] Decode(string input) return new byte[leadingZeroCount]; } - int decodedSize = bi.GetByteCount(true) + leadingZeroCount; + int decodedSize = leadingZeroCount + bi.GetByteCount(true); + byte[] result = new byte[decodedSize]; - Span result = decodedSize <= 128 - ? stackalloc byte[decodedSize] - : new byte[decodedSize]; - - _ = bi.TryWriteBytes(result[leadingZeroCount..], out _, true, true); - return result.ToArray(); + _ = bi.TryWriteBytes(result.AsSpan(leadingZeroCount), out _, true, true); + return result; } /// @@ -112,7 +110,7 @@ public static string Encode(ReadOnlySpan input) BigInteger value = new(input, isUnsigned: true, isBigEndian: true); // Encode BigInteger to Base58 string - var sb = new StringBuilder(); + var sb = new StringBuilder(input.Length * 138 / 100 + 5); while (value > 0) { @@ -123,7 +121,7 @@ public static string Encode(ReadOnlySpan input) // Append `1` for each leading 0 byte for (int i = 0; i < input.Length && input[i] == 0; i++) { - sb.Insert(0, s_zeroChar); + sb.Insert(0, ZeroChar); } return sb.ToString(); } @@ -133,9 +131,46 @@ private static int LeadingBase58Zeros(string collection) { var i = 0; var len = collection.Length; - for (; i < len && collection[i] == s_zeroChar; i++) { } + for (; i < len && collection[i] == ZeroChar; i++) { } return i; } } } + +public static class Ext +{ + public static byte[] Sha256(this ReadOnlySpan value) + { + byte[] buffer = new byte[32]; + using var sha256 = SHA256.Create(); + sha256.TryComputeHash(value, buffer, out _); + return buffer; + } + + + + /// + /// Computes the hash value for the specified byte array using the sha256 algorithm. + /// + /// The input to compute the hash code for. + /// The computed hash code. + public static byte[] Sha256(this byte[] value) + { + using var sha256 = SHA256.Create(); + return sha256.ComputeHash(value); + } + + /// + /// Computes the hash value for the specified region of the specified byte array using the sha256 algorithm. + /// + /// The input to compute the hash code for. + /// The offset into the byte array from which to begin using data. + /// The number of bytes in the array to use as data. + /// The computed hash code. + public static byte[] Sha256(this byte[] value, int offset, int count) + { + using var sha256 = SHA256.Create(); + return sha256.ComputeHash(value, offset, count); + } +} From 1b641d8beb9cce4ba74193121d76fca150762e2c Mon Sep 17 00:00:00 2001 From: Temnij Date: Tue, 15 Oct 2024 01:10:19 +0600 Subject: [PATCH 07/12] remove Ext, further optimisations --- src/Neo/Cryptography/Base58.cs | 71 ++++++++++++---------------------- 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 92ed0282ab..65e826fbf5 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -28,9 +28,19 @@ public static class Base58 /// Represents the alphabet of the base-58 encoder. /// public const string Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - private const char ZeroChar = '1'; - private static readonly IReadOnlyDictionary s_alphabetDic = Enumerable.Range(0, Alphabet.Length).ToDictionary(t => Alphabet[t], t => t); + private static readonly BigInteger s_alphabetLength = Alphabet.Length; + + private static readonly sbyte[] s_decodeMap = + [ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, + 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, + 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, + -1, -1, -1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, + 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, + ]; /// /// Converts the specified , which encodes binary data as base-58 digits, to an equivalent byte array. The encoded contains the checksum of the binary data. @@ -79,9 +89,9 @@ public static byte[] Decode(string input) var bi = BigInteger.Zero; for (int i = 0; i < input.Length; i++) { - if (!s_alphabetDic.TryGetValue(input[i], out var digit)) + if (input[i] > 127 || s_decodeMap[input[i]] == -1) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); - bi = bi * Alphabet.Length + digit; + bi = bi * s_alphabetLength + s_decodeMap[input[i]]; } // Encode BigInteger to byte[] @@ -92,8 +102,7 @@ public static byte[] Decode(string input) return new byte[leadingZeroCount]; } - int decodedSize = leadingZeroCount + bi.GetByteCount(true); - byte[] result = new byte[decodedSize]; + byte[] result = new byte[leadingZeroCount + bi.GetByteCount(true)]; _ = bi.TryWriteBytes(result.AsSpan(leadingZeroCount), out _, true, true); return result; @@ -114,16 +123,21 @@ public static string Encode(ReadOnlySpan input) while (value > 0) { - value = BigInteger.DivRem(value, Alphabet.Length, out var remainder); - sb.Insert(0, Alphabet[(int)remainder]); + value = BigInteger.DivRem(value, s_alphabetLength, out var remainder); + sb.Append(Alphabet[(int)remainder]); } // Append `1` for each leading 0 byte for (int i = 0; i < input.Length && input[i] == 0; i++) { - sb.Insert(0, ZeroChar); + sb.Append(ZeroChar); } - return sb.ToString(); + + Span copy = stackalloc char[sb.Length]; + sb.CopyTo(0, copy, sb.Length); + copy.Reverse(); + + return copy.ToString(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -137,40 +151,3 @@ private static int LeadingBase58Zeros(string collection) } } } - -public static class Ext -{ - public static byte[] Sha256(this ReadOnlySpan value) - { - byte[] buffer = new byte[32]; - using var sha256 = SHA256.Create(); - sha256.TryComputeHash(value, buffer, out _); - return buffer; - } - - - - /// - /// Computes the hash value for the specified byte array using the sha256 algorithm. - /// - /// The input to compute the hash code for. - /// The computed hash code. - public static byte[] Sha256(this byte[] value) - { - using var sha256 = SHA256.Create(); - return sha256.ComputeHash(value); - } - - /// - /// Computes the hash value for the specified region of the specified byte array using the sha256 algorithm. - /// - /// The input to compute the hash code for. - /// The offset into the byte array from which to begin using data. - /// The number of bytes in the array to use as data. - /// The computed hash code. - public static byte[] Sha256(this byte[] value, int offset, int count) - { - using var sha256 = SHA256.Create(); - return sha256.ComputeHash(value, offset, count); - } -} From bd3a205a6a23d0bdb44092230a27e4eb80769cde Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 17 Oct 2024 09:55:26 -0700 Subject: [PATCH 08/12] Clean --- src/Neo/Cryptography/Base58.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index d1a8e0d70b..1f6eed7296 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -10,11 +10,9 @@ // modifications are permitted. using System; -using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; -using System.Security.Cryptography; using System.Text; namespace Neo.Cryptography From 2682e8b26cc4b86232a3de28f96d3f14d035e3ca Mon Sep 17 00:00:00 2001 From: Temnij Date: Fri, 18 Oct 2024 13:53:49 +0600 Subject: [PATCH 09/12] format --- src/Neo/Cryptography/Base58.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 1f6eed7296..cd3fcdd515 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -29,6 +29,7 @@ public static class Base58 private const char ZeroChar = '1'; private static readonly BigInteger s_alphabetLength = Alphabet.Length; + #pragma warning disable format private static readonly sbyte[] s_decodeMap = [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -39,6 +40,7 @@ public static class Base58 -1, -1, -1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, ]; + #pragma warning restore format /// /// Converts the specified , which encodes binary data as base-58 digits, to an equivalent byte array. The encoded contains the checksum of the binary data. From 8c2c57729e88aaea67bcd69fb1bcf25bc7516cb6 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 6 Nov 2024 13:39:14 +0100 Subject: [PATCH 10/12] Reduce array --- src/Neo/Cryptography/Base58.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index cd3fcdd515..cde6ec090b 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -38,7 +38,7 @@ public static class Base58 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, - 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, + 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 ]; #pragma warning restore format @@ -89,7 +89,7 @@ public static byte[] Decode(string input) var bi = BigInteger.Zero; for (int i = 0; i < input.Length; i++) { - if (input[i] > 127 || s_decodeMap[input[i]] == -1) + if (input[i] >= 123 || s_decodeMap[input[i]] == -1) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); bi = bi * s_alphabetLength + s_decodeMap[input[i]]; } From ea3f6d8ae83cf5cbe25ee24431967c3b1fbe2b62 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 6 Nov 2024 13:42:40 +0100 Subject: [PATCH 11/12] reuse var --- src/Neo/Cryptography/Base58.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index cde6ec090b..5b6b33b4bf 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -87,23 +87,27 @@ public static byte[] Decode(string input) { // Decode Base58 string to BigInteger var bi = BigInteger.Zero; - for (int i = 0; i < input.Length; i++) + sbyte value; + for (var i = 0; i < input.Length; i++) { - if (input[i] >= 123 || s_decodeMap[input[i]] == -1) + if (input[i] >= 123) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); - bi = bi * s_alphabetLength + s_decodeMap[input[i]]; + value = s_decodeMap[input[i]]; + if (value == -1) + throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); + bi = bi * s_alphabetLength + value; } // Encode BigInteger to byte[] // Leading zero bytes get encoded as leading `1` characters - int leadingZeroCount = LeadingBase58Zeros(input); + var leadingZeroCount = LeadingBase58Zeros(input); if (bi.IsZero) { return new byte[leadingZeroCount]; } - byte[] result = new byte[leadingZeroCount + bi.GetByteCount(true)]; + var result = new byte[leadingZeroCount + bi.GetByteCount(true)]; _ = bi.TryWriteBytes(result.AsSpan(leadingZeroCount), out _, true, true); return result; From 8840fb34d9b8ee2909a9071e737ececfa31069bf Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 6 Nov 2024 13:43:19 +0100 Subject: [PATCH 12/12] same name as before --- src/Neo/Cryptography/Base58.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Neo/Cryptography/Base58.cs b/src/Neo/Cryptography/Base58.cs index 5b6b33b4bf..5f2fa3cc34 100644 --- a/src/Neo/Cryptography/Base58.cs +++ b/src/Neo/Cryptography/Base58.cs @@ -87,15 +87,15 @@ public static byte[] Decode(string input) { // Decode Base58 string to BigInteger var bi = BigInteger.Zero; - sbyte value; + sbyte digit; for (var i = 0; i < input.Length; i++) { if (input[i] >= 123) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); - value = s_decodeMap[input[i]]; - if (value == -1) + digit = s_decodeMap[input[i]]; + if (digit == -1) throw new FormatException($"Invalid Base58 character '{input[i]}' at position {i}"); - bi = bi * s_alphabetLength + value; + bi = bi * s_alphabetLength + digit; } // Encode BigInteger to byte[]