Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native BLS Signature Verification #120

Merged
merged 44 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
df02241
Fixing bls js calling
Gekctek Feb 20, 2024
a9f0cc4
Adding custom BLS implementation
Gekctek Feb 21, 2024
4687920
BVLSDKLFJKLJSDFKLJ
Gekctek Feb 21, 2024
f089050
Checkin
Feb 21, 2024
9f97749
Adding all the basic math
Feb 22, 2024
d29752b
Making changes
Feb 22, 2024
e60b892
Fixing tests
Gekctek Feb 22, 2024
ff1ca5d
Adding neg test
Gekctek Feb 22, 2024
c1226e0
Fixing tests
Gekctek Feb 22, 2024
079e068
Adding Fp tests
Gekctek Feb 22, 2024
39d28ea
Adding more tests
Gekctek Feb 22, 2024
9a7bac9
Fp2 Tests
Gekctek Feb 22, 2024
2bf6198
Fp6 Tests
Gekctek Feb 22, 2024
898a856
Misc fixes
Gekctek Feb 22, 2024
8b4fe26
Done
Gekctek Feb 23, 2024
08516f8
Misc
Gekctek Feb 23, 2024
21888bb
:)
Gekctek Feb 23, 2024
2114b0a
Adding hash curve functionality
Feb 23, 2024
2b3e51e
aaa
Feb 23, 2024
d500acd
Misc fixes
Gekctek Feb 23, 2024
7d805cf
Working
Gekctek Feb 24, 2024
36ceeb0
ICP changes
Gekctek Feb 23, 2024
ca3932d
Fix
Gekctek Feb 24, 2024
6a10beb
ICP
Gekctek Feb 24, 2024
c2a178d
G1
Feb 24, 2024
385e983
done
Gekctek Feb 24, 2024
7373a0d
Refactor
Gekctek Feb 24, 2024
ca099a9
Adding G2
Gekctek Feb 24, 2024
d0d9319
Working Non ICP
Gekctek Feb 24, 2024
15d427d
Fixing
Gekctek Feb 24, 2024
404cf20
Last
Gekctek Feb 24, 2024
b093f20
Ready to fix with TODO BLSICP
Gekctek Feb 24, 2024
3e66f48
G2 generator
Gekctek Feb 24, 2024
a348b42
Updating tests
Gekctek Feb 24, 2024
56f1e65
Structs
Gekctek Feb 25, 2024
e69ce29
AddWithCarry performance
Gekctek Feb 25, 2024
f1607ea
Array to tuples
Gekctek Feb 25, 2024
a5d4375
Optimizations
Gekctek Feb 25, 2024
1751d09
Fixing regression
Gekctek Feb 25, 2024
2d1b4a4
Adding comments
Gekctek Feb 25, 2024
0ba649e
ICP BLS Success and move tests dir
Gekctek Mar 21, 2024
dd6b34e
Removing .mono
Gekctek Mar 21, 2024
462ba44
Fixing tests
Gekctek Mar 21, 2024
074b0be
Fixing tests
Gekctek Mar 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ launchSettings.json
/dist

# Secret test files
test_SessionIdentity.json
test_SessionIdentity.json

.mono/
9 changes: 8 additions & 1 deletion ICP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EdjCase.ICP.WebSockets", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.WebSockets", "samples\Sample.WebSockets\Sample.WebSockets.csproj", "{B4F9E828-BC3D-4EFF-9E21-53DD82928AB0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSockets.Tests", "test\WebSockets.Tests\WebSockets.Tests.csproj", "{3015AFBC-B866-459F-B25C-4BEA00C2A91E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSockets.Tests", "test\WebSockets.Tests\WebSockets.Tests.csproj", "{3015AFBC-B866-459F-B25C-4BEA00C2A91E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BLS.Tests", "test\BLS.Tests\BLS.Tests.csproj", "{213F30BA-D147-4291-93A3-13A8A006126D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -111,6 +113,10 @@ Global
{3015AFBC-B866-459F-B25C-4BEA00C2A91E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3015AFBC-B866-459F-B25C-4BEA00C2A91E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3015AFBC-B866-459F-B25C-4BEA00C2A91E}.Release|Any CPU.Build.0 = Release|Any CPU
{213F30BA-D147-4291-93A3-13A8A006126D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{213F30BA-D147-4291-93A3-13A8A006126D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{213F30BA-D147-4291-93A3-13A8A006126D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{213F30BA-D147-4291-93A3-13A8A006126D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -126,6 +132,7 @@ Global
{94023B5A-0650-4E67-A685-D1C979DA717F} = {9A64FA9E-AC6C-4C1F-B648-E9E64F172EA8}
{B4F9E828-BC3D-4EFF-9E21-53DD82928AB0} = {7FADA9D9-5FDA-4CFB-8A53-A578A61FBBA9}
{3015AFBC-B866-459F-B25C-4BEA00C2A91E} = {F71B8320-C279-4A79-A8D4-4039DB39D522}
{213F30BA-D147-4291-93A3-13A8A006126D} = {F71B8320-C279-4A79-A8D4-4039DB39D522}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3103E11A-E792-49EE-98C5-B2F3709DB088}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ public enum MyVariantTag
- [IC Http Interface Spec](https://smartcontracts.org/docs/current/references/ic-interface-spec)
- [Candid Spec](https://github.com/dfinity/candid/blob/master/spec/Candid.md)
- [Candid Decoder](https://fxa77-fiaaa-aaaae-aaana-cai.raw.ic0.app/explain)
- [Candid UI Tester](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.ic0.app)
- [Candid UI Tester](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.ic0.app)
33 changes: 30 additions & 3 deletions UnityAssets/WebGlBlsCryptography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,46 @@
using UnityEngine;
using UnityEngine.Networking;

#if UNITY_WEBGL
#if UNITY_WEBGL && !UNITY_EDITOR
public class WebGlBlsCryptography : IBlsCryptography
{
public bool VerifySignature(byte[] publicKey, byte[] messageHash, byte[] signature)
{
return BrowserBlsLib.VerifySignature(publicKey, messageHash, signature);
string publicKeyHex = ToHexString(publicKey);
string messageHashHex = ToHexString(messageHash);
string signatureHex = ToHexString(signature);
return BrowserBlsLib.VerifySignature(publicKeyHex, messageHashHex, signatureHex);
}

public static string ToHexString(byte[] bytes)
{
char[] stringValue = new char[bytes.Length * 2];
int i = 0;
foreach (byte b in bytes)
{
int charIndex = i++ * 2;
int quotient = Math.DivRem(b, 16, out int remainder);
stringValue[charIndex] = GetChar(quotient);
stringValue[charIndex + 1] = GetChar(remainder);
}

return new string(stringValue); // returns: "48656C6C6F20776F726C64" for "Hello world"

}
private static char GetChar(int value)
{
if (value < 10)
{
return (char)(value + 48); // 0->9
}
return (char)(value + 65 - 10); // A->F ASCII
}
}

internal static class BrowserBlsLib
{
[DllImport("__Internal")]
public static extern bool VerifySignature(byte[] publicKey, byte[] messageHash, byte[] signature);
public static extern bool VerifySignature(string publicKeyHex, string messageHashHex, string signatureHex);
}
#endif

6 changes: 5 additions & 1 deletion samples/Sample.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
using EdjCase.ICP.Agent.Standards.AssetCanister;
using System.Text;
using EdjCase.ICP.Agent.Standards.AssetCanister.Models;
using EdjCase.ICP.BLS;
using EdjCase.ICP.Candid.Utilities;
using EdjCase.ICP.BLS.Models;
using System.Diagnostics;

public class Program
{
Expand Down Expand Up @@ -182,7 +186,7 @@ string filePath
try
{
contentStream = File.OpenRead(filePath);

await client.UploadAssetChunkedAsync(
key: key,
contentType: contentType,
Expand Down
78 changes: 39 additions & 39 deletions src/Agent/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Agent/Agents/HttpAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public HttpAgent(
{
this.Identity = identity;
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
this.bls = bls ?? new WasmBlsCryptography();
this.bls = bls ?? new DefaultBlsCryptograhy();
}

/// <param name="identity">Optional. Identity to use for each request. If unspecified, will use anonymous identity</param>
Expand All @@ -65,7 +65,7 @@ public HttpAgent(
{
BaseAddress = httpBoundryNodeUrl ?? new Uri("https://ic0.app/")
});
this.bls = bls ?? new WasmBlsCryptography();
this.bls = bls ?? new DefaultBlsCryptograhy();
}


Expand Down
35 changes: 12 additions & 23 deletions src/Agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ IAgent agent = new HttpAgent();
// Create Candid arg to send in request
ulong proposalId = 1234;
CandidArg arg = CandidArg.FromCandid(
CandidTypedValue.FromObject(proposalId) // Conversion can be C# or custom types
CandidTypedValue.FromObject(proposalId) // Conversion can be C# or custom types
);

// Make request to IC
Expand Down Expand Up @@ -89,19 +89,19 @@ string name = await client.Name();
// Get the balance of a specific account
Account account = new Account
{
Id = Principal.FromText("<account_id>")
Id = Principal.FromText("<account_id>")
};
UnboundedUInt balance = await client.BalanceOf(account);

// Transfer tokens from one account to another
TransferArgs transferArgs = new TransferArgs
{
To = new Account
{
Id = Principal.FromText("<to_account_id>")
},
Amount = 1,
Memo = "<memo>"
To = new Account
{
Id = Principal.FromText("<to_account_id>")
},
Amount = 1,
Memo = "<memo>"
};
TransferResult transferResult = await client.Transfer(transferArgs);
```
Expand Down Expand Up @@ -172,18 +172,7 @@ var delegatedIdentity = new DelegationIdentity(innerIdentity, chain);
Due to how WebGL works by converting C# to JS/WASM using IL2CPP there are a few additional steps to avoid
incompatibilities.
- UnityHttpClient - The .NET `HttpClient` does not work in many cases, so `UnityHttpClient` is added via Unity C# script.
```cs
var client = new UnityHttpClient();
var agent = new HttpAgent(client);
```
- WebGlBlsCryptography - The BLS signature verification relies on a 3rd party library and due to that library not being directly compatible with the WebGL builds, `WebGlBlsCryptography` needs to be used instead of the default `WasmBlsCryptography` class.

```cs
var bls = new WebGlBlsCrytography();
var agent = new HttpAgent(client, bls: bls);
```
In addition, the `noble-curves.js` file located [HERE](`https://github.com/paulmillr/noble-curves/releases/tag/1.2.0`) needs to be included in the Unity project and referenced by the HTML page
```html
<script src="{path_to_dir}/noble-curves.js"/>
```
That will load in the JS bls library that will be referenced by the `Bls.jslib` that is included in the ICP.NET unity package
```cs
var client = new UnityHttpClient();
var agent = new HttpAgent(client);
```
16 changes: 8 additions & 8 deletions src/BLS/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading