Skip to content

Commit

Permalink
Creating the methods that compute the project public and private keys
Browse files Browse the repository at this point in the history
(some of the code is borrowed from wasabi but it will get deleted)
  • Loading branch information
dangershony committed Dec 3, 2024
1 parent 3c04be3 commit b075103
Show file tree
Hide file tree
Showing 4 changed files with 467 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Angor.Test/ProjectIdentifierDerivationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using NBitcoin;
using NBitcoin.Secp256k1;
using Xunit;

namespace Angor.Shared.Tests
{
public class ProjectIdentifierDerivationTests
{
[Fact]
public void TestComputeSharedSecretMethods()
{
// Arrange
var privKey1 = new Key();
var privKey2 = new Key();
var ecPrivKey1 = ECPrivKey.Create(privKey1.ToBytes());
var ecPrivKey2 = ECPrivKey.Create(privKey2.ToBytes());
var ecPubKey1 = ecPrivKey1.CreatePubKey();
var ecPubKey2 = ecPrivKey2.CreatePubKey();

// Act
var sharedSecretFounder = ProjectIdentifierDerivation.ComputeSharedSecretFounder(ecPrivKey1, ecPubKey2);
var sharedSecretAngor = ProjectIdentifierDerivation.ComputeSharedSecretAngor(ecPubKey1, ecPrivKey2);

// Assert
Assert.Equal(sharedSecretFounder.ToBytes(), sharedSecretAngor.CreatePubKey().ToBytes());
}
}
}
1 change: 1 addition & 0 deletions src/Angor/Shared/Angor.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<PropertyGroup>
<DefineConstants>HAS_SPAN</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
104 changes: 104 additions & 0 deletions src/Angor/Shared/ByteHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
namespace Angor.Shared;

public static unsafe class ByteHelpers
{
// Taken from wasabi wallet
// https://stackoverflow.com/questions/415291/best-way-to-combine-two-or-more-byte-arrays-in-c-sharp
/// <summary>
/// Fastest byte array concatenation in C#
/// </summary>
public static byte[] Combine(params byte[][] arrays)
{
return Combine(arrays.AsEnumerable());
}

public static byte[] Combine(IEnumerable<byte[]> arrays)
{
byte[] ret = new byte[arrays.Sum(x => x.Length)];
int offset = 0;
foreach (byte[] data in arrays)
{
Buffer.BlockCopy(data, 0, ret, offset, data.Length);
offset += data.Length;
}
return ret;
}

// https://stackoverflow.com/a/8808245/2061103
// Copyright (c) 2008-2013 Hafthor Stefansson
// Distributed under the MIT/X11 software license
// Ref: http://www.opensource.org/licenses/mit-license.php.
/// <summary>
/// Fastest byte array comparison in C#
/// </summary>
public static unsafe bool CompareFastUnsafe(byte[]? array1, byte[]? array2)
{
if (array1 == array2)
{
return true;
}

if (array1 is null || array2 is null || array1.Length != array2.Length)
{
return false;
}

fixed (byte* p1 = array1, p2 = array2)
{
byte* x1 = p1, x2 = p2;
int l = array1.Length;
for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
{
if (*((long*)x1) != *((long*)x2))
{
return false;
}
}

if ((l & 4) != 0)
{
if (*((int*)x1) != *((int*)x2))
{
return false;
}
x1 += 4;
x2 += 4;
}
if ((l & 2) != 0)
{
if (*((short*)x1) != *((short*)x2))
{
return false;
}
x1 += 2;
x2 += 2;
}
if ((l & 1) != 0)
{
if (*x1 != *x2)
{
return false;
}
}

return true;
}
}

/// <seealso cref="Convert.ToHexString(byte[])"/>
public static string ToHex(params byte[] bytes)
{
return Convert.ToHexString(bytes);
}

/// <seealso cref="Convert.FromHexString(string)"/>
public static byte[] FromHex(string hex)
{
if (string.IsNullOrWhiteSpace(hex))
{
return Array.Empty<byte>();
}

return Convert.FromHexString(hex);
}
}
Loading

0 comments on commit b075103

Please sign in to comment.