-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating the methods that compute the project public and private keys
(some of the code is borrowed from wasabi but it will get deleted)
- Loading branch information
1 parent
3c04be3
commit b075103
Showing
4 changed files
with
467 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.