From 4d02f2733fc4cd1f10198637c3bd9ca021c93440 Mon Sep 17 00:00:00 2001 From: chopin-fan Date: Wed, 25 Dec 2024 14:13:17 +0800 Subject: [PATCH] Adjust implementation of ZK function --- .../Whitelist/IWhitelistProvider.cs | 6 +- .../PureFunction/PureFunctionHelper.cs | 57 +++++++++++++++++++ .../SecretSharing/SecretSharingHelper.cs | 48 ---------------- 3 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 src/AElf.Cryptography/PureFunction/PureFunctionHelper.cs diff --git a/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs b/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs index 4d525eac88..3b9dc807b9 100644 --- a/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs +++ b/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs @@ -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 ; @@ -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) - )); } } \ No newline at end of file diff --git a/src/AElf.Cryptography/PureFunction/PureFunctionHelper.cs b/src/AElf.Cryptography/PureFunction/PureFunctionHelper.cs new file mode 100644 index 0000000000..01cc9e51c1 --- /dev/null +++ b/src/AElf.Cryptography/PureFunction/PureFunctionHelper.cs @@ -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) + { + 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); + } + } +} \ No newline at end of file diff --git a/src/AElf.Cryptography/SecretSharing/SecretSharingHelper.cs b/src/AElf.Cryptography/SecretSharing/SecretSharingHelper.cs index fb7d79a58e..919db05d9b 100644 --- a/src/AElf.Cryptography/SecretSharing/SecretSharingHelper.cs +++ b/src/AElf.Cryptography/SecretSharing/SecretSharingHelper.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; -using Bn254.Net; -using Nethereum.Util; namespace AElf.Cryptography.SecretSharing { @@ -13,52 +11,6 @@ namespace AElf.Cryptography.SecretSharing /// 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 EncodeSecret(byte[] secretMessage, int threshold, int totalParts) { // Polynomial construction.