Skip to content

Commit

Permalink
Adjust implementation of ZK function
Browse files Browse the repository at this point in the history
  • Loading branch information
chopin-fan committed Dec 25, 2024
1 parent ff58f36 commit 4d02f27
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private void WhitelistAssemblies(Whitelist whitelist)
.Assembly(typeof(Address).Assembly, Trust.Full) // AElf.Types
.Assembly(typeof(IMethod).Assembly, Trust.Full) // AElf.CSharp.Core
.Assembly(typeof(SecretSharingHelper).Assembly, Trust.Partial) // AElf.Cryptography
.Assembly(typeof(PureFunctionHelper).Assembly, Trust.Full) // AElf.Cryptography
.Assembly(typeof(ISmartContractBridgeContext).Assembly, Trust.Full) // AElf.Kernel.SmartContract.Shared
.Assembly(typeof(Groth16.Net.Verifier).Assembly, Trust.Full) // AElf.Cryptography.ECDSA
;
Expand Down Expand Up @@ -201,11 +202,6 @@ private void WhitelistAElfTypes(Whitelist whitelist)
.Namespace("AElf.Cryptography.SecretSharing", Permission.Denied, type => type
.Type(typeof(SecretSharingHelper), Permission.Denied, member => member
.Member(nameof(SecretSharingHelper.DecodeSecret), Permission.Allowed)
.Member(nameof(SecretSharingHelper.Ed25519Verify), Permission.Allowed)
.Member(nameof(SecretSharingHelper.Keccak256), Permission.Allowed)
.Member(nameof(SecretSharingHelper.Bn254G1Mul), Permission.Allowed)
.Member(nameof(SecretSharingHelper.Bn254Pairing), Permission.Allowed)

));
}
}
57 changes: 57 additions & 0 deletions src/AElf.Cryptography/PureFunction/PureFunctionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using Bn254.Net;
using Nethereum.Util;
using Rebex.Security.Cryptography;

namespace AElf.Cryptography.SecretSharing
{
public static class PureFunctionHelper
{
public static bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey)
{
try
{
var instance = new Ed25519();
instance.FromPublicKey(publicKey);
return instance.VerifyMessage(message, signature);
}
catch (Exception e)

Check warning on line 18 in src/AElf.Cryptography/PureFunction/PureFunctionHelper.cs

View workflow job for this annotation

GitHub Actions / static-code-analysis

The variable 'e' is declared but never used
{
return false;
}
}

public static byte[] Keccak256(byte[] message)
{
return Sha3Keccack.Current.CalculateHash(message);
}

public static (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s)
{
var (xUInt256, yUInt256) = Bn254.Net.Bn254.Mul(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(s));
return (xUInt256.ToBigEndianBytes(), yUInt256.ToBigEndianBytes());
}

public static (byte[] x3, byte[] y3) Bn254G1Mul(byte[] x1, byte[] y1, byte[] x2, byte[] y2)
{
var (x3UInt256, y3UInt256) = Bn254.Net.Bn254.Add(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2));
return (x3UInt256.ToBigEndianBytes(), y3UInt256.ToBigEndianBytes());
}

public static bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[])[] input)
{
var elements = new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[input.Length];
for (var i = 0; i < input.Length; i++)
{
var (x1, y1, x2, y2, x3, y3) = input[i];
elements[i] = (UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2),
UInt256.FromBigEndianBytes(x3), UInt256.FromBigEndianBytes(y3));
}

return Bn254.Net.Bn254.Pairing(elements);
}
}
}
48 changes: 0 additions & 48 deletions src/AElf.Cryptography/SecretSharing/SecretSharingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Bn254.Net;
using Nethereum.Util;

namespace AElf.Cryptography.SecretSharing
{
Expand All @@ -13,52 +11,6 @@ namespace AElf.Cryptography.SecretSharing
/// </summary>
public static class SecretSharingHelper
{
public static bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey)
{
try
{
var instance = new Rebex.Security.Cryptography.Ed25519();
instance.FromPublicKey(publicKey);
return instance.VerifyMessage(message, signature);
}
catch (Exception e)
{
return false;
}
}

public static byte[] Keccak256(byte[] message)
{
return Sha3Keccack.Current.CalculateHash(message);
}

public static (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s)
{
var (xUInt256, yUInt256) = Bn254.Net.Bn254.Mul(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(s));
return (xUInt256.ToBigEndianBytes(), yUInt256.ToBigEndianBytes());
}

public static (byte[] x3, byte[] y3) Bn254G1Mul(byte[] x1, byte[] y1, byte[] x2, byte[] y2)
{
var (x3UInt256, y3UInt256) = Bn254.Net.Bn254.Add(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2));
return (x3UInt256.ToBigEndianBytes(), y3UInt256.ToBigEndianBytes());
}

public static bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[])[] input)
{
var elements = new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[input.Length];
for (var i = 0; i < input.Length; i++)
{
var (x1, y1, x2, y2, x3, y3) = input[i];
elements[i] = (UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1),
UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2),
UInt256.FromBigEndianBytes(x3), UInt256.FromBigEndianBytes(y3));
}

return Bn254.Net.Bn254.Pairing(elements);
}
public static List<byte[]> EncodeSecret(byte[] secretMessage, int threshold, int totalParts)
{
// Polynomial construction.
Expand Down

0 comments on commit 4d02f27

Please sign in to comment.