From 809bd9035204bb33c1f6615cb57314f764b35222 Mon Sep 17 00:00:00 2001 From: Pavel Gross Date: Tue, 11 Mar 2025 22:56:28 +0300 Subject: [PATCH 1/4] [#40] Client: Add memory optimization for hash Signed-off-by: Pavel Gross --- .../FrostFS.SDK.Client.csproj | 2 +- .../Models/Session/FrostFsSessionToken.cs | 4 +- src/FrostFS.SDK.Client/ObjectWriter.cs | 2 +- .../Services/AccountingServiceProvider.cs | 2 +- .../Services/ApeManagerServiceProvider.cs | 6 +- .../Services/ContainerServiceProvider.cs | 12 ++-- .../Services/NetmapServiceProvider.cs | 6 +- .../Services/ObjectServiceProvider.cs | 27 +++++---- .../Services/SessionServiceProvider.cs | 2 +- src/FrostFS.SDK.Client/Tools/RequestSigner.cs | 57 ++++++++++++++---- src/FrostFS.SDK.Client/Tools/Verifier.cs | 5 +- src/FrostFS.SDK.Cryptography/Extentions.cs | 21 ++++++- src/FrostFS.SDK.Cryptography/HashStream.cs | 58 +++++++++++++++++++ .../Mocks/AsyncStreamReaderMock.cs | 4 +- .../ContainerServiceBase.cs | 14 ++--- src/FrostFS.SDK.Tests/Mocks/ObjectMock.cs | 4 +- .../Smoke/Client/ObjectTests/ObjectTests.cs | 8 +-- 17 files changed, 170 insertions(+), 64 deletions(-) create mode 100644 src/FrostFS.SDK.Cryptography/HashStream.cs diff --git a/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj b/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj index e75da94..7e1a0bf 100644 --- a/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj +++ b/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj @@ -4,7 +4,7 @@ netstandard2.0 12.0 enable - AllEnabledByDefault + AllEnabledByDefault diff --git a/src/FrostFS.SDK.Client/Models/Session/FrostFsSessionToken.cs b/src/FrostFS.SDK.Client/Models/Session/FrostFsSessionToken.cs index 708c9f6..808e91b 100644 --- a/src/FrostFS.SDK.Client/Models/Session/FrostFsSessionToken.cs +++ b/src/FrostFS.SDK.Client/Models/Session/FrostFsSessionToken.cs @@ -81,7 +81,7 @@ public class FrostFsSessionToken } sessionToken.Body.SessionKey = key.PublicKeyProto; - sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body); + sessionToken.Signature = key.SignMessagePart(sessionToken.Body); return sessionToken; } @@ -116,7 +116,7 @@ public class FrostFsSessionToken Verb = verb }; - sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body); + sessionToken.Signature = key.SignMessagePart(sessionToken.Body); return sessionToken; } diff --git a/src/FrostFS.SDK.Client/ObjectWriter.cs b/src/FrostFS.SDK.Client/ObjectWriter.cs index 5683740..a1859cc 100644 --- a/src/FrostFS.SDK.Client/ObjectWriter.cs +++ b/src/FrostFS.SDK.Client/ObjectWriter.cs @@ -34,7 +34,7 @@ namespace FrostFS.SDK.Client chunkRequest.AddMetaHeader(args.XHeaders); - chunkRequest.Sign(this.ctx.Key.ECDsaKey); + chunkRequest.Sign(this.ctx.Key); await streamer.Write(chunkRequest).ConfigureAwait(false); } diff --git a/src/FrostFS.SDK.Client/Services/AccountingServiceProvider.cs b/src/FrostFS.SDK.Client/Services/AccountingServiceProvider.cs index e87674f..ea45b37 100644 --- a/src/FrostFS.SDK.Client/Services/AccountingServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/AccountingServiceProvider.cs @@ -27,7 +27,7 @@ internal sealed class AccountingServiceProvider : ContextAccessor }; request.AddMetaHeader([]); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await _accountingServiceClient!.BalanceAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); diff --git a/src/FrostFS.SDK.Client/Services/ApeManagerServiceProvider.cs b/src/FrostFS.SDK.Client/Services/ApeManagerServiceProvider.cs index 5331b80..8ad35d5 100644 --- a/src/FrostFS.SDK.Client/Services/ApeManagerServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/ApeManagerServiceProvider.cs @@ -32,7 +32,7 @@ internal sealed class ApeManagerServiceProvider : ContextAccessor }; request.AddMetaHeader(args.XHeaders); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await _apeManagerServiceClient!.AddChainAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -53,7 +53,7 @@ internal sealed class ApeManagerServiceProvider : ContextAccessor }; request.AddMetaHeader(args.XHeaders); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await _apeManagerServiceClient!.RemoveChainAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -71,7 +71,7 @@ internal sealed class ApeManagerServiceProvider : ContextAccessor }; request.AddMetaHeader(args.XHeaders); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await _apeManagerServiceClient!.ListChainsAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); diff --git a/src/FrostFS.SDK.Client/Services/ContainerServiceProvider.cs b/src/FrostFS.SDK.Client/Services/ContainerServiceProvider.cs index d7ba34c..59783cf 100644 --- a/src/FrostFS.SDK.Client/Services/ContainerServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/ContainerServiceProvider.cs @@ -39,7 +39,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService internal async Task GetContainerAsync(PrmContainerGet args, CallContext ctx) { - GetRequest request = GetContainerRequest(args.Container.GetContainerID(), args.XHeaders, ClientContext.Key.ECDsaKey); + GetRequest request = GetContainerRequest(args.Container.GetContainerID(), args.XHeaders, ClientContext.Key); var response = await service.GetAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -59,7 +59,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService }; request.AddMetaHeader(args.XHeaders); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await service.ListAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -96,7 +96,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await service.PutAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -127,7 +127,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await service.DeleteAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -139,7 +139,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService Verifier.CheckResponse(response); } - private static GetRequest GetContainerRequest(ContainerID id, string[] xHeaders, ECDsa key) + private static GetRequest GetContainerRequest(ContainerID id, string[] xHeaders, ClientKey key) { var request = new GetRequest { @@ -163,7 +163,7 @@ internal sealed class ContainerServiceProvider(ContainerService.ContainerService private async Task WaitForContainer(WaitExpects expect, ContainerID id, PrmWait waitParams, CallContext ctx) { - var request = GetContainerRequest(id, [], ClientContext.Key.ECDsaKey); + var request = GetContainerRequest(id, [], ClientContext.Key); async Task action() { diff --git a/src/FrostFS.SDK.Client/Services/NetmapServiceProvider.cs b/src/FrostFS.SDK.Client/Services/NetmapServiceProvider.cs index a1bf17b..672c010 100644 --- a/src/FrostFS.SDK.Client/Services/NetmapServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/NetmapServiceProvider.cs @@ -50,7 +50,7 @@ internal sealed class NetmapServiceProvider : ContextAccessor }; request.AddMetaHeader([]); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await netmapServiceClient.LocalNodeInfoAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -64,7 +64,7 @@ internal sealed class NetmapServiceProvider : ContextAccessor var request = new NetworkInfoRequest(); request.AddMetaHeader([]); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await netmapServiceClient.NetworkInfoAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken) .ConfigureAwait(false); @@ -79,7 +79,7 @@ internal sealed class NetmapServiceProvider : ContextAccessor var request = new NetmapSnapshotRequest(); request.AddMetaHeader([]); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await netmapServiceClient.NetmapSnapshotAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); diff --git a/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs b/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs index 8992733..f220d00 100644 --- a/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs @@ -67,7 +67,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await client!.HeadAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken).ConfigureAwait(false); @@ -111,7 +111,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); return await GetObject(request, ctx).ConfigureAwait(false); } @@ -145,7 +145,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var call = client.GetRange(request, null, ctx.GetDeadline(), ctx.CancellationToken); return new RangeReader(call); @@ -185,7 +185,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await client.GetRangeHashAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -218,7 +218,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl ClientContext.Key); request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await client.DeleteAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken); @@ -247,7 +247,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); using var stream = GetSearchReader(request, ctx); @@ -283,7 +283,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders, protoToken); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); var response = await client.PutSingleAsync(request, null, ctx.GetDeadline(), ctx.CancellationToken).ConfigureAwait(false); @@ -363,7 +363,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl request.AddMetaHeader(args.XHeaders); } - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); await call.RequestStream.WriteAsync(request).ConfigureAwait(false); @@ -402,11 +402,11 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl throw new ArgumentException("The stream has zero length"); var networkSettings = await ClientContext.Client.GetNetworkSettingsAsync(ctx).ConfigureAwait(false); - args.PutObjectContext.MaxObjectSizeCache = (int)networkSettings.MaxObjectSize; + var partSize = (int)networkSettings.MaxObjectSize; var restBytes = args.PutObjectContext.FullLength; - var objectSize = (int)Math.Min((ulong)args.PutObjectContext.MaxObjectSizeCache, restBytes); + var objectSize = (int)Math.Min((ulong)partSize, restBytes); // define collection capacity var objectsCount = (int)(restBytes / (ulong)objectSize) + ((restBytes % (ulong)objectSize) > 0 ? 1 : 0); @@ -414,6 +414,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl // if the object fits one part, it can be loaded as non-complex object if (objectsCount == 1) { + args.PutObjectContext.MaxObjectSizeCache = partSize; var singlePartResult = await PutMultipartStreamObjectAsync(args, default).ConfigureAwait(false); return singlePartResult.ObjectId; } @@ -422,8 +423,6 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl SplitId splitId = new(); - var partSize = args.PutObjectContext.MaxObjectSizeCache; - // keep attributes for the large object var attributes = args.Header!.Attributes.ToArray(); header.Attributes = null; @@ -578,7 +577,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl }; chunkRequest.AddMetaHeader(args.XHeaders); - chunkRequest.Sign(ClientContext.Key.ECDsaKey); + chunkRequest.Sign(ClientContext.Key); await stream.Write(chunkRequest).ConfigureAwait(false); } @@ -640,7 +639,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl initRequest.AddMetaHeader(args.XHeaders, protoToken); - initRequest.Sign(ClientContext.Key.ECDsaKey); + initRequest.Sign(ClientContext.Key); return await PutObjectInit(initRequest, ctx).ConfigureAwait(false); } diff --git a/src/FrostFS.SDK.Client/Services/SessionServiceProvider.cs b/src/FrostFS.SDK.Client/Services/SessionServiceProvider.cs index e73a3d3..cde2b9e 100644 --- a/src/FrostFS.SDK.Client/Services/SessionServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/SessionServiceProvider.cs @@ -26,7 +26,7 @@ internal sealed class SessionServiceProvider : ContextAccessor }; request.AddMetaHeader(args.XHeaders); - request.Sign(ClientContext.Key.ECDsaKey); + request.Sign(ClientContext.Key); return await CreateSession(request, ctx).ConfigureAwait(false); } diff --git a/src/FrostFS.SDK.Client/Tools/RequestSigner.cs b/src/FrostFS.SDK.Client/Tools/RequestSigner.cs index f747b3f..3070edf 100644 --- a/src/FrostFS.SDK.Client/Tools/RequestSigner.cs +++ b/src/FrostFS.SDK.Client/Tools/RequestSigner.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Security.Cryptography; using FrostFS.Refs; @@ -13,7 +14,7 @@ using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Signers; using Org.BouncyCastle.Math; - +using Org.BouncyCastle.Utilities; using Signature = FrostFS.Refs.Signature; namespace FrostFS.SDK.Client; @@ -74,7 +75,7 @@ public static class RequestSigner }; } - public static ByteString SignData(this ECDsa key, byte[] data) + public static ByteString SignData(this ECDsa key, ReadOnlyMemory data) { if (key is null) { @@ -84,27 +85,61 @@ public static class RequestSigner Span result = stackalloc byte[65]; result[0] = 0x04; - //var hash = new byte[65]; - //hash[0] = 0x04; - key.SignHash(data.Sha512()).AsSpan().CopyTo(result[1..]); return ByteString.CopyFrom(result); } - - internal static Signature SignMessagePart(this ECDsa key, IMessage? data) + + public static ByteString SignDataByHash(this ECDsa key, byte[] hash) { - var data2Sign = data is null ? [] : data.ToByteArray(); + if (key is null) + { + throw new ArgumentNullException(nameof(key)); + } + + Span result = stackalloc byte[65]; + result[0] = 0x04; + + key.SignHash(hash).AsSpan().CopyTo(result[1..]); + + return ByteString.CopyFrom(result); + } + + internal static Signature SignMessagePart(this ClientKey key, IMessage? data) + { + if (data is null) + { + return new Signature + { + Key = key.PublicKeyProto, + Sign = key.ECDsaKey.SignData(ReadOnlyMemory.Empty), + }; + } + + var size = data.CalculateSize(); + + if (size == 0) + { + return new Signature + { + Key = key.PublicKeyProto, + Sign = key.ECDsaKey.SignData(ReadOnlyMemory.Empty), + }; + } + + using HashStream stream = new(); + data.WriteTo(stream); + var sig = new Signature { - Key = ByteString.CopyFrom(key.PublicKey()), - Sign = key.SignData(data2Sign), + Key = key.PublicKeyProto, + Sign = key.ECDsaKey.SignDataByHash(stream.Hash()) }; return sig; } - internal static void Sign(this IVerifiableMessage message, ECDsa key) + internal static void Sign(this IVerifiableMessage message, ClientKey key) { var meta = message.GetMetaHeader(); IVerificationHeader verify = message switch diff --git a/src/FrostFS.SDK.Client/Tools/Verifier.cs b/src/FrostFS.SDK.Client/Tools/Verifier.cs index 063fa21..ec70d20 100644 --- a/src/FrostFS.SDK.Client/Tools/Verifier.cs +++ b/src/FrostFS.SDK.Client/Tools/Verifier.cs @@ -63,14 +63,11 @@ public static class Verifier return signature.Key.ToByteArray().VerifyRFC6979(message.ToByteArray(), signature.Sign.ToByteArray()); } - public static bool VerifyData(this ECDsa key, byte[] data, byte[] sig) + public static bool VerifyData(this ECDsa key, ReadOnlyMemory data, byte[] sig) { if (key is null) throw new ArgumentNullException(nameof(key)); - if (data is null) - throw new ArgumentNullException(nameof(data)); - if (sig is null) throw new ArgumentNullException(nameof(sig)); diff --git a/src/FrostFS.SDK.Cryptography/Extentions.cs b/src/FrostFS.SDK.Cryptography/Extentions.cs index fc51c10..40ad6f0 100644 --- a/src/FrostFS.SDK.Cryptography/Extentions.cs +++ b/src/FrostFS.SDK.Cryptography/Extentions.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Security.Cryptography; using System.Threading; using CommunityToolkit.HighPerformance; @@ -60,14 +61,30 @@ public static class Extentions } } - public static byte[] Sha512(this byte[] value) + public static byte[] Sha512(this ReadOnlyMemory value) { bool lockTaken = false; try { _spinlockSha512.Enter(ref lockTaken); - return _sha512.ComputeHash(value); + return _sha512.ComputeHash(value.AsStream()); + } + finally + { + if (lockTaken) + _spinlockSha512.Exit(false); + } + } + + public static byte[] Sha512(this Stream stream) + { + bool lockTaken = false; + try + { + _spinlockSha512.Enter(ref lockTaken); + + return _sha512.ComputeHash(stream); } finally { diff --git a/src/FrostFS.SDK.Cryptography/HashStream.cs b/src/FrostFS.SDK.Cryptography/HashStream.cs new file mode 100644 index 0000000..d2ede1a --- /dev/null +++ b/src/FrostFS.SDK.Cryptography/HashStream.cs @@ -0,0 +1,58 @@ +using System.IO; +using System.Security.Cryptography; + +namespace FrostFS.SDK.Cryptography; + +public sealed class HashStream() : Stream +{ + private long position; + + private readonly SHA512 _hash = SHA512.Create(); + + public override bool CanRead => false; + + public override bool CanSeek => false; + + public override bool CanWrite => true; + + public override long Length => long.MaxValue; + + public override long Position + { + get { return position; } + set { position = value; } + } + + public override void Flush() + { } + + public override int Read(byte[] buffer, int offset, int count) + { + return 0; + } + + public override long Seek(long offset, SeekOrigin origin) + { + return 0; + } + + public override void SetLength(long value) + { } + + public override void Write(byte[] buffer, int offset, int count) + { + _hash.TransformBlock(buffer, offset, count, buffer, offset); + } + + public byte[] Hash() + { + _hash.TransformFinalBlock([], 0, 0); + return _hash.Hash; + } + + protected override void Dispose(bool disposing) + { + _hash?.Dispose(); + base.Dispose(disposing); + } +} \ No newline at end of file diff --git a/src/FrostFS.SDK.Tests/Mocks/AsyncStreamReaderMock.cs b/src/FrostFS.SDK.Tests/Mocks/AsyncStreamReaderMock.cs index 9e9a18d..0a3aec6 100644 --- a/src/FrostFS.SDK.Tests/Mocks/AsyncStreamReaderMock.cs +++ b/src/FrostFS.SDK.Tests/Mocks/AsyncStreamReaderMock.cs @@ -42,8 +42,8 @@ public class AsyncStreamReaderMock(string key, FrostFsObjectHeader objectHeader) ObjectId = new Refs.ObjectID { Value = ByteString.CopyFrom(SHA256.HashData(Array.Empty())) }, Signature = new Refs.Signature { - Key = ByteString.CopyFrom(Key.PublicKey()), - Sign = Key.SignData(header.ToByteArray()), + Key = Key.PublicKeyProto, + Sign = Key.ECDsaKey. SignData(header.ToByteArray()), } } }, diff --git a/src/FrostFS.SDK.Tests/Mocks/ContainerServiceMocks/ContainerServiceBase.cs b/src/FrostFS.SDK.Tests/Mocks/ContainerServiceMocks/ContainerServiceBase.cs index ab52c19..e6f25c2 100644 --- a/src/FrostFS.SDK.Tests/Mocks/ContainerServiceMocks/ContainerServiceBase.cs +++ b/src/FrostFS.SDK.Tests/Mocks/ContainerServiceMocks/ContainerServiceBase.cs @@ -18,7 +18,7 @@ namespace FrostFS.SDK.Tests; public abstract class ServiceBase(string key) { public string StringKey { get; private set; } = key; - public ECDsa Key { get; private set; } = key.LoadWif(); + public ClientKey Key { get; private set; } = new ClientKey(key.LoadWif()); public FrostFsVersion Version { get; set; } = DefaultVersion; public FrostFsPlacementPolicy PlacementPolicy { get; set; } = DefaultPlacementPolicy; @@ -44,21 +44,21 @@ public abstract class ServiceBase(string key) { MetaSignature = new Refs.Signature { - Key = ByteString.CopyFrom(Key.PublicKey()), + Key = Key.PublicKeyProto, Scheme = Refs.SignatureScheme.EcdsaRfc6979Sha256, - Sign = Key.SignData(response.MetaHeader.ToByteArray()) + Sign = Key.ECDsaKey.SignData(response.MetaHeader.ToByteArray()) }, BodySignature = new Refs.Signature { - Key = ByteString.CopyFrom(Key.PublicKey()), + Key = Key.PublicKeyProto, Scheme = Refs.SignatureScheme.EcdsaRfc6979Sha256, - Sign = Key.SignData(response.GetBody().ToByteArray()) + Sign = Key.ECDsaKey.SignData(response.GetBody().ToByteArray()) }, OriginSignature = new Refs.Signature { - Key = ByteString.CopyFrom(Key.PublicKey()), + Key = Key.PublicKeyProto, Scheme = Refs.SignatureScheme.EcdsaRfc6979Sha256, - Sign = Key.SignData([]) + Sign = Key.ECDsaKey.SignData(ReadOnlyMemory.Empty) } }; diff --git a/src/FrostFS.SDK.Tests/Mocks/ObjectMock.cs b/src/FrostFS.SDK.Tests/Mocks/ObjectMock.cs index 4f07e41..49a0f6d 100644 --- a/src/FrostFS.SDK.Tests/Mocks/ObjectMock.cs +++ b/src/FrostFS.SDK.Tests/Mocks/ObjectMock.cs @@ -92,8 +92,8 @@ public class ObjectMocker(string key) : ObjectServiceBase(key) headResponse.Body.Header.Signature = new Refs.Signature { - Key = ByteString.CopyFrom(Key.PublicKey()), - Sign = Key.SignData(headResponse.Body.Header.ToByteArray()), + Key = Key.PublicKeyProto, + Sign = Key.ECDsaKey.SignData(headResponse.Body.Header.ToByteArray()), }; headResponse.VerifyHeader = GetResponseVerificationHeader(headResponse); diff --git a/src/FrostFS.SDK.Tests/Smoke/Client/ObjectTests/ObjectTests.cs b/src/FrostFS.SDK.Tests/Smoke/Client/ObjectTests/ObjectTests.cs index 81f0cd3..03a9169 100644 --- a/src/FrostFS.SDK.Tests/Smoke/Client/ObjectTests/ObjectTests.cs +++ b/src/FrostFS.SDK.Tests/Smoke/Client/ObjectTests/ObjectTests.cs @@ -55,10 +55,10 @@ public class ObjectTests(ITestOutputHelper testOutputHelper) : SmokeTestsBase private async Task RunSuite(IFrostFSClient client, FrostFsContainerId containerId) { - int[] objectSizes = [1, 257, 6 * 1024, 20 * 1024]; + int[] objectSizes = [1, 257, 5 * 1024 * 1024, 20 * 1024 * 1024]; string[] objectTypes = [clientCut, serverCut, singleObject]; - + foreach (var objectSize in objectSizes) { _testOutputHelper.WriteLine($"test set for object size {objectSize}"); @@ -77,13 +77,13 @@ public class ObjectTests(ITestOutputHelper testOutputHelper) : SmokeTestsBase break; case clientCut: objectId = await CreateObjectClientCut(client, containerId, bytes); - _testOutputHelper.WriteLine($"\tclient side cut"); + _testOutputHelper.WriteLine($"\tclient side cut"); break; case singleObject: if (objectSize > 1 * 1024 * 1024) continue; objectId = await PutSingleObject(client, containerId, bytes); - _testOutputHelper.WriteLine($"\tput single object"); + _testOutputHelper.WriteLine($"\tput single object"); break; default: -- 2.45.3 From 6ae96c1d771924353f8658903bc35cf0f6c43fd0 Mon Sep 17 00:00:00 2001 From: Pavel Gross Date: Wed, 12 Mar 2025 00:11:50 +0300 Subject: [PATCH 2/4] [#41] Client: Remove ranges Signed-off-by: Pavel Gross --- .../Models/Netmap/FrostFsNetmapSnapshot.cs | 2 +- .../Models/Netmap/Placement/Context.cs | 4 +- .../Services/ObjectServiceProvider.cs | 2 +- src/FrostFS.SDK.Client/Tools/Range.cs | 248 ------------------ src/FrostFS.SDK.Client/Tools/RequestSigner.cs | 9 +- src/FrostFS.SDK.Client/Tools/Verifier.cs | 6 +- 6 files changed, 11 insertions(+), 260 deletions(-) delete mode 100644 src/FrostFS.SDK.Client/Tools/Range.cs diff --git a/src/FrostFS.SDK.Client/Models/Netmap/FrostFsNetmapSnapshot.cs b/src/FrostFS.SDK.Client/Models/Netmap/FrostFsNetmapSnapshot.cs index 1d90b04..e15c2a8 100644 --- a/src/FrostFS.SDK.Client/Models/Netmap/FrostFsNetmapSnapshot.cs +++ b/src/FrostFS.SDK.Client/Models/Netmap/FrostFsNetmapSnapshot.cs @@ -87,7 +87,7 @@ public class FrostFsNetmapSnapshot(ulong epoch, IReadOnlyList n if (expr.Selector == null) { - var lastFilter = expr.Filters[^1]; + var lastFilter = expr.Filters.Last(); var subCollestion = new List(); ret.Add(subCollestion); diff --git a/src/FrostFS.SDK.Client/Models/Netmap/Placement/Context.cs b/src/FrostFS.SDK.Client/Models/Netmap/Placement/Context.cs index cc5c20a..cd287d0 100644 --- a/src/FrostFS.SDK.Client/Models/Netmap/Placement/Context.cs +++ b/src/FrostFS.SDK.Client/Models/Netmap/Placement/Context.cs @@ -271,7 +271,7 @@ internal struct Context var ns = buckets[i].nodes; if (ns.Length >= maxNodesInBucket) { - res.Add(new List(ns[..maxNodesInBucket])); + res.Add(ns.Take(maxNodesInBucket).ToList()); } else if (ns.Length >= nodesInBucket) { @@ -353,7 +353,7 @@ internal struct Context var start = hasPrefix ? likeWildcard.Length : 0; var end = hasSuffix ? f.Value.Length - likeWildcard.Length : f.Value.Length; - var str = f.Value[start..end]; + var str = f.Value.Substring(start, end-start); if (hasPrefix && hasSuffix) return nodeInfo.Attributes[f.Key].Contains(str); diff --git a/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs b/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs index f220d00..3ce543d 100644 --- a/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs @@ -572,7 +572,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl { Body = new PutRequest.Types.Body { - Chunk = UnsafeByteOperations.UnsafeWrap(chunkBuffer.AsMemory()[..bytesCount]) + Chunk = UnsafeByteOperations.UnsafeWrap(chunkBuffer.AsMemory(0, bytesCount)) } }; diff --git a/src/FrostFS.SDK.Client/Tools/Range.cs b/src/FrostFS.SDK.Client/Tools/Range.cs deleted file mode 100644 index 8add54e..0000000 --- a/src/FrostFS.SDK.Client/Tools/Range.cs +++ /dev/null @@ -1,248 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace System -{ - /// Represent a type can be used to index a collection either from the start or the end. - /// - /// Index is used by the C# compiler to support the new index syntax - /// - /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ; - /// int lastElement = someArray[^1]; // lastElement = 5 - /// - /// - internal readonly struct Index : IEquatable - { - private readonly int _value; - - /// Construct an Index using a value and indicating if the index is from the start or from the end. - /// The index value. it has to be zero or positive number. - /// Indicating if the index is from the start or from the end. - /// - /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Index(int value, bool fromEnd = false) - { - if (value < 0) - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - - if (fromEnd) - _value = ~value; - else - _value = value; - } - - // The following private constructors mainly created for perf reason to avoid the checks - private Index(int value) - { - _value = value; - } - - /// Create an Index pointing at first element. - public static Index Start => new(0); - - /// Create an Index pointing at beyond last element. - public static Index End => new(~0); - - /// Create an Index from the start at the position indicated by the value. - /// The index value from the start. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Index FromStart(int value) - { - if (value < 0) - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - - return new Index(value); - } - - /// Create an Index from the end at the position indicated by the value. - /// The index value from the end. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Index FromEnd(int value) - { - if (value < 0) - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - - return new Index(~value); - } - - /// Returns the index value. - public int Value - { - get - { - return _value < 0 ? ~_value : _value; - } - } - - /// Indicates whether the index is from the start or the end. - public bool IsFromEnd => _value < 0; - - /// Calculate the offset from the start using the giving collection length. - /// The length of the collection that the Index will be used with. length has to be a positive value - /// - /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. - /// we don't validate either the returned offset is greater than the input length. - /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and - /// then used to index a collection will get out of range exception which will be same affect as the validation. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int GetOffset(int length) - { - var offset = _value; - if (IsFromEnd) - { - // offset = length - (~value) - // offset = length + (~(~value) + 1) - // offset = length + value + 1 - - offset += length + 1; - } - return offset; - } - - /// Indicates whether the current Index object is equal to another object of the same type. - /// An object to compare with this object - public override bool Equals(object? value) => value is Index index && _value == index._value; - - /// Indicates whether the current Index object is equal to another Index object. - /// An object to compare with this object - public bool Equals(Index other) => _value == other._value; - - /// Returns the hash code for this instance. - public override int GetHashCode() => _value; - - /// Converts integer number to an Index. - public static implicit operator Index(int value) => FromStart(value); - - /// Converts the value of the current Index object to its equivalent string representation. - public override string ToString() - { - if (IsFromEnd) - return $"^{(uint)Value}"; - - return $"{(uint)Value}"; - } - } - - /// Represent a range has start and end indexes. - /// - /// Range is used by the C# compiler to support the range syntax. - /// - /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 }; - /// int[] subArray1 = someArray[0..2]; // { 1, 2 } - /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } - /// - /// - /// Construct a Range object using the start and end indexes. - /// Represent the inclusive start index of the range. - /// Represent the exclusive end index of the range. - internal readonly struct Range(Index start, Index end) : IEquatable - { - /// Represent the inclusive start index of the Range. - public Index Start { get; } = start; - - /// Represent the exclusive end index of the Range. - public Index End { get; } = end; - - /// Indicates whether the current Range object is equal to another object of the same type. - /// An object to compare with this object - public override bool Equals(object? value) => - value is Range r && - r.Start.Equals(Start) && - r.End.Equals(End); - - /// Indicates whether the current Range object is equal to another Range object. - /// An object to compare with this object - public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); - - /// Returns the hash code for this instance. - public override int GetHashCode() - { - return Start.GetHashCode() * 31 + End.GetHashCode(); - } - - /// Converts the value of the current Range object to its equivalent string representation. - public override string ToString() - { - return Start + ".." + End; - } - - /// Create a Range object starting from start index to the end of the collection. - public static Range StartAt(Index start) => new(start, Index.End); - - /// Create a Range object starting from first element in the collection to the end Index. - public static Range EndAt(Index end) => new(Index.Start, end); - - /// Create a Range object starting from first element to the end. - public static Range All => new(Index.Start, Index.End); - - /// Calculate the start offset and length of range object using a collection length. - /// The length of the collection that the range will be used with. length has to be a positive value. - /// - /// For performance reason, we don't validate the input length parameter against negative values. - /// It is expected Range will be used with collections which always have non negative length/count. - /// We validate the range is inside the length scope though. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int Offset, int Length) GetOffsetAndLength(int length) - { - int start; - var startIndex = Start; - - if (startIndex.IsFromEnd) - start = length - startIndex.Value; - else - start = startIndex.Value; - - int end; - var endIndex = End; - - if (endIndex.IsFromEnd) - end = length - endIndex.Value; - else - end = endIndex.Value; - - if ((uint)end > (uint)length || (uint)start > (uint)end) - throw new ArgumentOutOfRangeException(nameof(length)); - - return (start, end - start); - } - } -} - -namespace System.Runtime.CompilerServices -{ - internal static class RuntimeHelpers - { - /// - /// Slices the specified array using the specified range. - /// - public static T[] GetSubArray(T[] array, Range range) - { - if (array == null) - throw new ArgumentNullException(nameof(array)); - - (int offset, int length) = range.GetOffsetAndLength(array.Length); - - if (default(T) != null || typeof(T[]) == array.GetType()) - { - // We know the type of the array to be exactly T[]. - - if (length == 0) - return []; - - var dest = new T[length]; - Array.Copy(array, offset, dest, 0, length); - return dest; - } - else - { - // The array is actually a U[] where U:T. - var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length); - Array.Copy(array, offset, dest, 0, length); - return dest; - } - } - } -} diff --git a/src/FrostFS.SDK.Client/Tools/RequestSigner.cs b/src/FrostFS.SDK.Client/Tools/RequestSigner.cs index 3070edf..80dfae7 100644 --- a/src/FrostFS.SDK.Client/Tools/RequestSigner.cs +++ b/src/FrostFS.SDK.Client/Tools/RequestSigner.cs @@ -49,14 +49,13 @@ public static class RequestSigner var sbytes = rs[1].ToByteArrayUnsigned(); var index = RFC6979SignatureSize / 2 - rbytes.Length; - rbytes.AsSpan().CopyTo(signature[index..]); + rbytes.AsSpan().CopyTo(signature.Slice(index)); index = RFC6979SignatureSize - sbytes.Length; - sbytes.AsSpan().CopyTo(signature[index..]); + sbytes.AsSpan().CopyTo(signature.Slice(index)); return ByteString.CopyFrom(signature); } - internal static SignatureRFC6979 SignRFC6979(this ECDsa key, IMessage message) { return new SignatureRFC6979 @@ -85,7 +84,7 @@ public static class RequestSigner Span result = stackalloc byte[65]; result[0] = 0x04; - key.SignHash(data.Sha512()).AsSpan().CopyTo(result[1..]); + key.SignHash(data.Sha512()).AsSpan().CopyTo(result.Slice(1)); return ByteString.CopyFrom(result); } @@ -100,7 +99,7 @@ public static class RequestSigner Span result = stackalloc byte[65]; result[0] = 0x04; - key.SignHash(hash).AsSpan().CopyTo(result[1..]); + key.SignHash(hash).AsSpan().CopyTo(result.Slice(1)); return ByteString.CopyFrom(result); } diff --git a/src/FrostFS.SDK.Client/Tools/Verifier.cs b/src/FrostFS.SDK.Client/Tools/Verifier.cs index ec70d20..c110431 100644 --- a/src/FrostFS.SDK.Client/Tools/Verifier.cs +++ b/src/FrostFS.SDK.Client/Tools/Verifier.cs @@ -27,8 +27,8 @@ public static class Verifier throw new FormatException($"Wrong signature size, expect={RFC6979SignatureSize}, actual={sig.Length}"); var rs = new BigInteger[2]; - rs[0] = new BigInteger(1, sig[..32]); - rs[1] = new BigInteger(1, sig[32..]); + rs[0] = new BigInteger(1, sig.AsSpan(0, 32).ToArray()); + rs[1] = new BigInteger(1, sig.AsSpan(32).ToArray()); return rs; } @@ -71,7 +71,7 @@ public static class Verifier if (sig is null) throw new ArgumentNullException(nameof(sig)); - return key.VerifyHash(data.Sha512(), sig[1..]); + return key.VerifyHash(data.Sha512(), sig.AsSpan(1).ToArray()); } public static bool VerifyMessagePart(this Signature sig, IMessage data) -- 2.45.3 From f93e33b49b7578ece6edf4e555fac4264269e2a4 Mon Sep 17 00:00:00 2001 From: Pavel Gross Date: Wed, 12 Mar 2025 10:37:12 +0300 Subject: [PATCH 3/4] [#40] Client: Add memory optimization for hash Signed-off-by: Pavel Gross --- src/FrostFS.SDK.Client/Tools/RequestSigner.cs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/FrostFS.SDK.Client/Tools/RequestSigner.cs b/src/FrostFS.SDK.Client/Tools/RequestSigner.cs index 80dfae7..e2aceef 100644 --- a/src/FrostFS.SDK.Client/Tools/RequestSigner.cs +++ b/src/FrostFS.SDK.Client/Tools/RequestSigner.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Security.Cryptography; using FrostFS.Refs; @@ -14,7 +13,6 @@ using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Signers; using Org.BouncyCastle.Math; -using Org.BouncyCastle.Utilities; using Signature = FrostFS.Refs.Signature; namespace FrostFS.SDK.Client; @@ -106,18 +104,7 @@ public static class RequestSigner internal static Signature SignMessagePart(this ClientKey key, IMessage? data) { - if (data is null) - { - return new Signature - { - Key = key.PublicKeyProto, - Sign = key.ECDsaKey.SignData(ReadOnlyMemory.Empty), - }; - } - - var size = data.CalculateSize(); - - if (size == 0) + if (data is null || data.CalculateSize() == 0) { return new Signature { -- 2.45.3 From 98cfd82313d3215b400a5a236b707bbbda8531d6 Mon Sep 17 00:00:00 2001 From: Pavel Gross Date: Wed, 12 Mar 2025 10:44:39 +0300 Subject: [PATCH 4/4] [#40] Client: Add memory optimization for hash: update version Signed-off-by: Pavel Gross --- README.md | 2 +- src/FrostFS.SDK.Client/AssemblyInfo.cs | 2 +- src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj | 6 +----- src/FrostFS.SDK.Cryptography/AssemblyInfo.cs | 2 +- .../FrostFS.SDK.Cryptography.csproj | 12 ++++-------- src/FrostFS.SDK.Protos/AssemblyInfo.cs | 2 +- src/FrostFS.SDK.Protos/FrostFS.SDK.Protos.csproj | 6 +----- 7 files changed, 10 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index add47ea..b7738b7 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ var createContainerParam = new PrmContainerCreate( var containerId = await client.PutContainerAsync(createContainerParam); -using var fileStream = File.OpenRead(@"C:\Users\Paul\Pictures\cat.jpeg"); +using var fileStream = File.OpenRead(@"cat.jpeg"); var param = new PrmObjectPut { diff --git a/src/FrostFS.SDK.Client/AssemblyInfo.cs b/src/FrostFS.SDK.Client/AssemblyInfo.cs index 61df422..e62079f 100644 --- a/src/FrostFS.SDK.Client/AssemblyInfo.cs +++ b/src/FrostFS.SDK.Client/AssemblyInfo.cs @@ -5,4 +5,4 @@ using System.Reflection; [assembly: AssemblyInformationalVersion("1.0.0+d6fe0344538a223303c9295452f0ad73681ca376")] [assembly: AssemblyProduct("FrostFS.SDK.Client")] [assembly: AssemblyTitle("FrostFS.SDK.Client")] -[assembly: AssemblyVersion("1.0.2")] +[assembly: AssemblyVersion("1.0.3")] diff --git a/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj b/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj index 7e1a0bf..5935922 100644 --- a/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj +++ b/src/FrostFS.SDK.Client/FrostFS.SDK.Client.csproj @@ -24,11 +24,7 @@ - true - - - - Assemblyinfo.cs + false diff --git a/src/FrostFS.SDK.Cryptography/AssemblyInfo.cs b/src/FrostFS.SDK.Cryptography/AssemblyInfo.cs index addd4ad..c03d604 100644 --- a/src/FrostFS.SDK.Cryptography/AssemblyInfo.cs +++ b/src/FrostFS.SDK.Cryptography/AssemblyInfo.cs @@ -5,4 +5,4 @@ using System.Reflection; [assembly: AssemblyInformationalVersion("1.0.0+d6fe0344538a223303c9295452f0ad73681ca376")] [assembly: AssemblyProduct("FrostFS.SDK.Cryptography")] [assembly: AssemblyTitle("FrostFS.SDK.Cryptography")] -[assembly: AssemblyVersion("1.0.2.0")] +[assembly: AssemblyVersion("1.0.3.0")] diff --git a/src/FrostFS.SDK.Cryptography/FrostFS.SDK.Cryptography.csproj b/src/FrostFS.SDK.Cryptography/FrostFS.SDK.Cryptography.csproj index c9a51d3..a4e819e 100644 --- a/src/FrostFS.SDK.Cryptography/FrostFS.SDK.Cryptography.csproj +++ b/src/FrostFS.SDK.Cryptography/FrostFS.SDK.Cryptography.csproj @@ -7,23 +7,19 @@ - true + true - <_SkipUpgradeNetAnalyzersNuGetWarning>true + <_SkipUpgradeNetAnalyzersNuGetWarning>true - true + true - true - - - - Assemblyinfo.cs + false diff --git a/src/FrostFS.SDK.Protos/AssemblyInfo.cs b/src/FrostFS.SDK.Protos/AssemblyInfo.cs index eb6f873..11ace79 100644 --- a/src/FrostFS.SDK.Protos/AssemblyInfo.cs +++ b/src/FrostFS.SDK.Protos/AssemblyInfo.cs @@ -5,4 +5,4 @@ using System.Reflection; [assembly: AssemblyInformationalVersion("1.0.0+d6fe0344538a223303c9295452f0ad73681ca376")] [assembly: AssemblyProduct("FrostFS.SDK.Protos")] [assembly: AssemblyTitle("FrostFS.SDK.Protos")] -[assembly: AssemblyVersion("1.0.2.0")] +[assembly: AssemblyVersion("1.0.3.0")] diff --git a/src/FrostFS.SDK.Protos/FrostFS.SDK.Protos.csproj b/src/FrostFS.SDK.Protos/FrostFS.SDK.Protos.csproj index cd6627e..d5d8a9e 100644 --- a/src/FrostFS.SDK.Protos/FrostFS.SDK.Protos.csproj +++ b/src/FrostFS.SDK.Protos/FrostFS.SDK.Protos.csproj @@ -19,11 +19,7 @@ - true - - - - Assemblyinfo.cs + false -- 2.45.3