From c75210482e6b38e0cf0fe84f4fef12d8089f9bb2 Mon Sep 17 00:00:00 2001 From: chopin-fan Date: Sun, 29 Dec 2024 21:26:17 +0800 Subject: [PATCH] Adjust zk code --- AElf.All.sln | 14 - .../Patchers/Module/SafeMath/Patcher.cs | 1 - .../Whitelist/IWhitelistProvider.cs | 7 +- src/AElf.Cryptography/Bn254/Bn254Helper.cs | 7 +- .../AElf.Sdk.CSharp.Internal.csproj | 28 -- src/AElf.Sdk.CSharp.Internal/AssemblyInfo.cs | 3 - .../InternalBuiltIns.cs | 61 ---- .../AElf.Sdk.CSharp.Spec.csproj | 11 - src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs | 10 - src/AElf.Sdk.CSharp/AElf.Sdk.CSharp.csproj | 1 - .../CSharpSmartContractAbstract.cs | 5 +- .../CSharpSmartContractContext.cs | 2 - .../ContractsPreparation.cs | 1 - .../Action.cs | 3 +- .../CSharpSmartContractContextTests.cs | 308 +++++++++--------- 15 files changed, 162 insertions(+), 300 deletions(-) delete mode 100644 src/AElf.Sdk.CSharp.Internal/AElf.Sdk.CSharp.Internal.csproj delete mode 100644 src/AElf.Sdk.CSharp.Internal/AssemblyInfo.cs delete mode 100644 src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs delete mode 100644 src/AElf.Sdk.CSharp.Spec/AElf.Sdk.CSharp.Spec.csproj delete mode 100644 src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs diff --git a/AElf.All.sln b/AElf.All.sln index 782e186970..34ad27a9ac 100644 --- a/AElf.All.sln +++ b/AElf.All.sln @@ -385,10 +385,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Kernel.FeatureDisable. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Kernel.FeatureDisable.Core", "src\AElf.Kernel.FeatureDisable.Core\AElf.Kernel.FeatureDisable.Core.csproj", "{659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Sdk.CSharp.Spec", "src\AElf.Sdk.CSharp.Spec\AElf.Sdk.CSharp.Spec.csproj", "{45A77C41-142E-4296-A14A-D53067EC8B75}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AElf.Sdk.CSharp.Internal", "src\AElf.Sdk.CSharp.Internal\AElf.Sdk.CSharp.Internal.csproj", "{A77380A7-E425-45A3-BF3F-F865DA4F3F4E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1111,14 +1107,6 @@ Global {659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}.Debug|Any CPU.Build.0 = Debug|Any CPU {659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}.Release|Any CPU.ActiveCfg = Release|Any CPU {659A7C7A-44C9-424E-B4F6-D1D3656F7AD4}.Release|Any CPU.Build.0 = Release|Any CPU - {45A77C41-142E-4296-A14A-D53067EC8B75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {45A77C41-142E-4296-A14A-D53067EC8B75}.Debug|Any CPU.Build.0 = Debug|Any CPU - {45A77C41-142E-4296-A14A-D53067EC8B75}.Release|Any CPU.ActiveCfg = Release|Any CPU - {45A77C41-142E-4296-A14A-D53067EC8B75}.Release|Any CPU.Build.0 = Release|Any CPU - {A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A77380A7-E425-45A3-BF3F-F865DA4F3F4E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1313,7 +1301,5 @@ Global {A4ACE6D2-4CF8-4B52-93C9-BB8BEC0C098E} = {90B310B4-C2DB-419E-B5EE-97FA096B62CC} {8C0D86A4-D1A7-4B61-AC44-755F5AC75D67} = {4E54480A-D155-43ED-9736-1A5BE7957211} {659A7C7A-44C9-424E-B4F6-D1D3656F7AD4} = {90B310B4-C2DB-419E-B5EE-97FA096B62CC} - {45A77C41-142E-4296-A14A-D53067EC8B75} = {798BAA50-825B-4DD9-83E2-77A9EE6EBE09} - {A77380A7-E425-45A3-BF3F-F865DA4F3F4E} = {798BAA50-825B-4DD9-83E2-77A9EE6EBE09} EndGlobalSection EndGlobal diff --git a/src/AElf.CSharp.CodeOps/Patchers/Module/SafeMath/Patcher.cs b/src/AElf.CSharp.CodeOps/Patchers/Module/SafeMath/Patcher.cs index 2bc2e46232..2d8ec1ebe6 100644 --- a/src/AElf.CSharp.CodeOps/Patchers/Module/SafeMath/Patcher.cs +++ b/src/AElf.CSharp.CodeOps/Patchers/Module/SafeMath/Patcher.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using AElf.Sdk.CSharp; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Cecil.Rocks; diff --git a/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs b/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs index 69bef5c723..41e3ba704d 100644 --- a/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs +++ b/src/AElf.CSharp.CodeOps/Validators/Whitelist/IWhitelistProvider.cs @@ -176,10 +176,6 @@ private void WhitelistCryptographyHelpers(Whitelist whitelist) { whitelist // Selectively allowed types and members - .Namespace("AElf.Cryptography.SecretSharing", Permission.Denied, type => type - .Type(typeof(SecretSharingHelper), Permission.Denied, member => member - .Member(nameof(SecretSharingHelper.DecodeSecret), Permission.Allowed) - )) .Namespace("AElf.Cryptography.Bn254", Permission.Denied, type => type .Type(typeof(Bn254Helper), Permission.Denied, member => member .Member(nameof(Bn254Helper.Bn254Pairing), Permission.Allowed) @@ -229,7 +225,6 @@ private void WhitelistAElfTypes(Whitelist whitelist) // Selectively allowed types and members .Namespace("AElf.Cryptography.SecretSharing", Permission.Denied, type => type .Type(typeof(SecretSharingHelper), Permission.Denied, member => member - .Member(nameof(SecretSharingHelper.DecodeSecret), Permission.Allowed) - )); + .Member(nameof(SecretSharingHelper.DecodeSecret), Permission.Allowed))); } } \ No newline at end of file diff --git a/src/AElf.Cryptography/Bn254/Bn254Helper.cs b/src/AElf.Cryptography/Bn254/Bn254Helper.cs index 318a82b355..0cf74fc3bc 100644 --- a/src/AElf.Cryptography/Bn254/Bn254Helper.cs +++ b/src/AElf.Cryptography/Bn254/Bn254Helper.cs @@ -1,4 +1,5 @@ using Bn254.Net; +using NetBn254 = Bn254.Net; namespace AElf.Cryptography.Bn254 { @@ -6,7 +7,7 @@ public static class Bn254Helper { public static (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s) { - var (xUInt256, yUInt256) = global::Bn254.Net.Bn254.Mul(UInt256.FromBigEndianBytes(x1), + var (xUInt256, yUInt256) = NetBn254.Bn254.Mul(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1), UInt256.FromBigEndianBytes(s)); return (xUInt256.ToBigEndianBytes(), yUInt256.ToBigEndianBytes()); @@ -14,7 +15,7 @@ public static (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s) public static (byte[] x3, byte[] y3) Bn254G1Add(byte[] x1, byte[] y1, byte[] x2, byte[] y2) { - var (x3UInt256, y3UInt256) = global::Bn254.Net.Bn254.Add(UInt256.FromBigEndianBytes(x1), + var (x3UInt256, y3UInt256) = NetBn254.Bn254.Add(UInt256.FromBigEndianBytes(x1), UInt256.FromBigEndianBytes(y1), UInt256.FromBigEndianBytes(x2), UInt256.FromBigEndianBytes(y2)); return (x3UInt256.ToBigEndianBytes(), y3UInt256.ToBigEndianBytes()); @@ -31,7 +32,7 @@ public static bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[]) UInt256.FromBigEndianBytes(x3), UInt256.FromBigEndianBytes(y3)); } - return global::Bn254.Net.Bn254.Pairing(elements); + return NetBn254.Bn254.Pairing(elements); } } } \ No newline at end of file diff --git a/src/AElf.Sdk.CSharp.Internal/AElf.Sdk.CSharp.Internal.csproj b/src/AElf.Sdk.CSharp.Internal/AElf.Sdk.CSharp.Internal.csproj deleted file mode 100644 index ff42c2fa80..0000000000 --- a/src/AElf.Sdk.CSharp.Internal/AElf.Sdk.CSharp.Internal.csproj +++ /dev/null @@ -1,28 +0,0 @@ - - - - net8.0 - AElf.Sdk.CSharp.Internal - true - latest - Types for the C# smart contract SDK. - AElf.Sdk.CSharp.Internal - - - - - all - - - all - - - all - - - - - - - - diff --git a/src/AElf.Sdk.CSharp.Internal/AssemblyInfo.cs b/src/AElf.Sdk.CSharp.Internal/AssemblyInfo.cs deleted file mode 100644 index 1b80097c7f..0000000000 --- a/src/AElf.Sdk.CSharp.Internal/AssemblyInfo.cs +++ /dev/null @@ -1,3 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("AElf.Sdk.CSharp")] \ No newline at end of file diff --git a/src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs b/src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs deleted file mode 100644 index 0d61e255b8..0000000000 --- a/src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using AElf.Sdk.CSharp.Spec; -using Bn254.Net; -using Nethereum.Util; - -namespace AElf.Sdk.CSharp.Internal; - -internal class InternalBuiltIns : IBuiltIns -{ - public static void Initialize() - { - // call this method to ensure this assembly is loaded in the runtime. - } - - public 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 byte[] Keccak256(byte[] message) - { - return Sha3Keccack.Current.CalculateHash(message); - } - - public (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 (byte[] x3, byte[] y3) Bn254G1Add(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 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.Sdk.CSharp.Spec/AElf.Sdk.CSharp.Spec.csproj b/src/AElf.Sdk.CSharp.Spec/AElf.Sdk.CSharp.Spec.csproj deleted file mode 100644 index 81ab49bf3a..0000000000 --- a/src/AElf.Sdk.CSharp.Spec/AElf.Sdk.CSharp.Spec.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - net8.0 - enable - enable - Release - true - - - diff --git a/src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs b/src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs deleted file mode 100644 index f19de71ebe..0000000000 --- a/src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace AElf.Sdk.CSharp.Spec; - -public interface IBuiltIns -{ - bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey); - byte[] Keccak256(byte[] message); - (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s); - (byte[] x3, byte[] y3) Bn254G1Add(byte[] x1, byte[] y1, byte[] x2, byte[] y2); - bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[])[] input); -} \ No newline at end of file diff --git a/src/AElf.Sdk.CSharp/AElf.Sdk.CSharp.csproj b/src/AElf.Sdk.CSharp/AElf.Sdk.CSharp.csproj index 00cf548872..3d27d43369 100644 --- a/src/AElf.Sdk.CSharp/AElf.Sdk.CSharp.csproj +++ b/src/AElf.Sdk.CSharp/AElf.Sdk.CSharp.csproj @@ -13,7 +13,6 @@ - diff --git a/src/AElf.Sdk.CSharp/CSharpSmartContractAbstract.cs b/src/AElf.Sdk.CSharp/CSharpSmartContractAbstract.cs index 5553b6eb5a..f550dc596e 100644 --- a/src/AElf.Sdk.CSharp/CSharpSmartContractAbstract.cs +++ b/src/AElf.Sdk.CSharp/CSharpSmartContractAbstract.cs @@ -1,6 +1,5 @@ using AElf.Kernel.SmartContract; using AElf.Types; -using AElf.Sdk.CSharp.Spec; namespace AElf.Sdk.CSharp; @@ -21,6 +20,6 @@ protected void Assert(bool asserted, string message = "Assertion failed!") } internal abstract void InternalInitialize(ISmartContractBridgeContext bridgeContext); - - public IBuiltIns BuiltIns { get; internal set; } + + // public IBuiltIns BuiltIns { get; internal set; } } \ No newline at end of file diff --git a/src/AElf.Sdk.CSharp/CSharpSmartContractContext.cs b/src/AElf.Sdk.CSharp/CSharpSmartContractContext.cs index caf5cbde5e..911d71a2e6 100644 --- a/src/AElf.Sdk.CSharp/CSharpSmartContractContext.cs +++ b/src/AElf.Sdk.CSharp/CSharpSmartContractContext.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using AElf.Kernel.SmartContract; -using AElf.Sdk.CSharp.Internal; -using AElf.Sdk.CSharp.Spec; using AElf.Types; using Google.Protobuf; using Google.Protobuf.WellKnownTypes; diff --git a/test/AElf.Contracts.Economic.TestBase/ContractsPreparation.cs b/test/AElf.Contracts.Economic.TestBase/ContractsPreparation.cs index cde20e76c9..5aeca9457c 100644 --- a/test/AElf.Contracts.Economic.TestBase/ContractsPreparation.cs +++ b/test/AElf.Contracts.Economic.TestBase/ContractsPreparation.cs @@ -18,7 +18,6 @@ using AElf.Cryptography.ECDSA; using AElf.CSharp.Core.Extension; using AElf.Kernel; -using AElf.Sdk.CSharp; using AElf.Standards.ACS3; using AElf.Types; using Google.Protobuf; diff --git a/test/AElf.Contracts.TestContract.VirtualTransactionEvent/Action.cs b/test/AElf.Contracts.TestContract.VirtualTransactionEvent/Action.cs index 99fa3348e2..9703205edf 100644 --- a/test/AElf.Contracts.TestContract.VirtualTransactionEvent/Action.cs +++ b/test/AElf.Contracts.TestContract.VirtualTransactionEvent/Action.cs @@ -1,5 +1,4 @@ -using AElf.Sdk.CSharp; -using AElf.Types; +using AElf.Types; using Google.Protobuf.WellKnownTypes; namespace AElf.Contracts.TestContract.VirtualTransactionEvent; diff --git a/test/AElf.Sdk.CSharp.Tests/CSharpSmartContractContextTests.cs b/test/AElf.Sdk.CSharp.Tests/CSharpSmartContractContextTests.cs index 19e57e247b..24ac83db54 100644 --- a/test/AElf.Sdk.CSharp.Tests/CSharpSmartContractContextTests.cs +++ b/test/AElf.Sdk.CSharp.Tests/CSharpSmartContractContextTests.cs @@ -15,40 +15,40 @@ namespace AElf.Sdk.CSharp.Tests; public class CSharpSmartContractContextTests : SdkCSharpTestBase { - [Fact] - public void Verify_Ed25519Verify() - { - var bridgeContext = GetRequiredService().Create(); - var origin = SampleAddress.AddressList[0]; - bridgeContext.TransactionContext = new TransactionContext - { - Origin = origin, - Transaction = new Transaction - { - From = SampleAddress.AddressList[1], - To = SampleAddress.AddressList[2] - } - }; - var contractContext = new CSharpSmartContractContext(bridgeContext); - var publicKey = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; - var message = ""; - var signature = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"; - var ed25519VerifyResult = contractContext.Ed25519Verify( - ByteArrayHelper.HexStringToByteArray(signature), - ByteArrayHelper.HexStringToByteArray(message), - ByteArrayHelper.HexStringToByteArray(publicKey)); - ed25519VerifyResult.ShouldBe(true); - - var contractContext1 = new CSharpSmartContractContext(bridgeContext); - var publicKey1 = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; - var message1 = "1"; - var signature1 = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"; - Should.Throw(() => contractContext1.Ed25519Verify( - ByteArrayHelper.HexStringToByteArray(signature1), - ByteArrayHelper.HexStringToByteArray(message1), - ByteArrayHelper.HexStringToByteArray(publicKey1))); - - } + // [Fact] + // public void Verify_Ed25519Verify() + // { + // var bridgeContext = GetRequiredService().Create(); + // var origin = SampleAddress.AddressList[0]; + // bridgeContext.TransactionContext = new TransactionContext + // { + // Origin = origin, + // Transaction = new Transaction + // { + // From = SampleAddress.AddressList[1], + // To = SampleAddress.AddressList[2] + // } + // }; + // var contractContext = new CSharpSmartContractContext(bridgeContext); + // var publicKey = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; + // var message = ""; + // var signature = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"; + // var ed25519VerifyResult = contractContext.Ed25519Verify( + // ByteArrayHelper.HexStringToByteArray(signature), + // ByteArrayHelper.HexStringToByteArray(message), + // ByteArrayHelper.HexStringToByteArray(publicKey)); + // ed25519VerifyResult.ShouldBe(true); + // + // var contractContext1 = new CSharpSmartContractContext(bridgeContext); + // var publicKey1 = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; + // var message1 = "1"; + // var signature1 = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"; + // Should.Throw(() => contractContext1.Ed25519Verify( + // ByteArrayHelper.HexStringToByteArray(signature1), + // ByteArrayHelper.HexStringToByteArray(message1), + // ByteArrayHelper.HexStringToByteArray(publicKey1))); + // + // } [Fact] public void Verify_Transaction_Origin_SetValue() @@ -88,124 +88,124 @@ public void Transaction_VerifySignature_Test() result.ShouldBeTrue(); } - [Fact] - public void keccak256_test() - { - var bridgeContext = GetRequiredService().Create(); - var origin = SampleAddress.AddressList[0]; - var contractContext = new CSharpSmartContractContext(bridgeContext); - byte[] message = System.Text.Encoding.UTF8.GetBytes("Test message"); - var fact = contractContext.Keccak256(message); - var expected = Sha3Keccack.Current.CalculateHash(message); - fact.ShouldBe(expected); - } - public static byte[] ToBytes32(BigIntValue value) - { - var bytes = value.ToBigEndianBytes(); - var newArray = new byte[32]; - for (int i = 0; i < bytes.Length; i++) - { - newArray[31 - i] = bytes[bytes.Length - 1 - i]; - } - - return newArray; - } - - [Fact] - public void Bn254G1Mul_Test() - { - var bridgeContext = GetRequiredService().Create(); - var contractContext = new CSharpSmartContractContext(bridgeContext); - - // Arrange - byte[] x1 = ToBytes32(new BigIntValue(0)); - byte[] y1 = ToBytes32(new BigIntValue(0)); - byte[] scalar = ToBytes32(new BigIntValue(0)); - - // use raw api to compute result - var (expectedXUInt256, expectedYUInt256) = Bn254.Net.Bn254.Mul( - UInt256.FromBigEndianBytes(x1), - UInt256.FromBigEndianBytes(y1), - UInt256.FromBigEndianBytes(scalar) - ); - var expectedX = expectedXUInt256.ToBigEndianBytes(); - var expectedY = expectedYUInt256.ToBigEndianBytes(); - - // Act - var (xResult, yResult) = contractContext.Bn254G1Mul(x1, y1, scalar); - - // Assert - xResult.ShouldBe(expectedX); - yResult.ShouldBe(expectedY); - } - - [Fact] - public void Bn254G1Add_Test() - { - var bridgeContext = GetRequiredService().Create(); - var contractContext = new CSharpSmartContractContext(bridgeContext); - - // Arrange - byte[] x1 = ToBytes32(new BigIntValue(0)); - byte[] y1 = ToBytes32(new BigIntValue(0)); - byte[] x2 = ToBytes32(new BigIntValue(0)); - byte[] y2 = ToBytes32(new BigIntValue(0)); - - // Use raw API to compute expected results - var (expectedX3UInt256, expectedY3UInt256) = Bn254.Net.Bn254.Add( - UInt256.FromBigEndianBytes(x1), - UInt256.FromBigEndianBytes(y1), - UInt256.FromBigEndianBytes(x2), - UInt256.FromBigEndianBytes(y2) - ); - var expectedX3 = expectedX3UInt256.ToBigEndianBytes(); - var expectedY3 = expectedY3UInt256.ToBigEndianBytes(); - - // Act - var (x3Result, y3Result) = contractContext.Bn254G1Add(x1, y1, x2, y2); - - // Assert - x3Result.ShouldBe(expectedX3); - y3Result.ShouldBe(expectedY3); - } - - - [Fact] - public void Bn254Pairing_Test() - { - var bridgeContext = GetRequiredService().Create(); - var contractContext = new CSharpSmartContractContext(bridgeContext); - - // Arrange - var input = new (byte[], byte[], byte[], byte[], byte[], byte[])[] - { - ( - ToBytes32(new BigIntValue(0)), - ToBytes32(new BigIntValue(0)), - ToBytes32(new BigIntValue(0)), - ToBytes32(new BigIntValue(0)), - ToBytes32(new BigIntValue(0)), - ToBytes32(new BigIntValue(0)) - ) - }; - - // Use raw API to compute expected results - bool expected = Bn254.Net.Bn254.Pairing(new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[] - { - ( - UInt256.FromBigEndianBytes(input[0].Item1), - UInt256.FromBigEndianBytes(input[0].Item2), - UInt256.FromBigEndianBytes(input[0].Item3), - UInt256.FromBigEndianBytes(input[0].Item4), - UInt256.FromBigEndianBytes(input[0].Item5), - UInt256.FromBigEndianBytes(input[0].Item6) - ) - }); - - // Act - bool result = contractContext.Bn254Pairing(input); - - // Assert - result.ShouldBe(expected); - } + // [Fact] + // public void keccak256_test() + // { + // var bridgeContext = GetRequiredService().Create(); + // var origin = SampleAddress.AddressList[0]; + // var contractContext = new CSharpSmartContractContext(bridgeContext); + // byte[] message = System.Text.Encoding.UTF8.GetBytes("Test message"); + // var fact = contractContext.Keccak256(message); + // var expected = Sha3Keccack.Current.CalculateHash(message); + // fact.ShouldBe(expected); + // } + // public static byte[] ToBytes32(BigIntValue value) + // { + // var bytes = value.ToBigEndianBytes(); + // var newArray = new byte[32]; + // for (int i = 0; i < bytes.Length; i++) + // { + // newArray[31 - i] = bytes[bytes.Length - 1 - i]; + // } + // + // return newArray; + // } + // + // [Fact] + // public void Bn254G1Mul_Test() + // { + // var bridgeContext = GetRequiredService().Create(); + // var contractContext = new CSharpSmartContractContext(bridgeContext); + // + // // Arrange + // byte[] x1 = ToBytes32(new BigIntValue(0)); + // byte[] y1 = ToBytes32(new BigIntValue(0)); + // byte[] scalar = ToBytes32(new BigIntValue(0)); + // + // // use raw api to compute result + // var (expectedXUInt256, expectedYUInt256) = Bn254.Net.Bn254.Mul( + // UInt256.FromBigEndianBytes(x1), + // UInt256.FromBigEndianBytes(y1), + // UInt256.FromBigEndianBytes(scalar) + // ); + // var expectedX = expectedXUInt256.ToBigEndianBytes(); + // var expectedY = expectedYUInt256.ToBigEndianBytes(); + // + // // Act + // var (xResult, yResult) = contractContext.Bn254G1Mul(x1, y1, scalar); + // + // // Assert + // xResult.ShouldBe(expectedX); + // yResult.ShouldBe(expectedY); + // } + // + // [Fact] + // public void Bn254G1Add_Test() + // { + // var bridgeContext = GetRequiredService().Create(); + // var contractContext = new CSharpSmartContractContext(bridgeContext); + // + // // Arrange + // byte[] x1 = ToBytes32(new BigIntValue(0)); + // byte[] y1 = ToBytes32(new BigIntValue(0)); + // byte[] x2 = ToBytes32(new BigIntValue(0)); + // byte[] y2 = ToBytes32(new BigIntValue(0)); + // + // // Use raw API to compute expected results + // var (expectedX3UInt256, expectedY3UInt256) = Bn254.Net.Bn254.Add( + // UInt256.FromBigEndianBytes(x1), + // UInt256.FromBigEndianBytes(y1), + // UInt256.FromBigEndianBytes(x2), + // UInt256.FromBigEndianBytes(y2) + // ); + // var expectedX3 = expectedX3UInt256.ToBigEndianBytes(); + // var expectedY3 = expectedY3UInt256.ToBigEndianBytes(); + // + // // Act + // var (x3Result, y3Result) = contractContext.Bn254G1Add(x1, y1, x2, y2); + // + // // Assert + // x3Result.ShouldBe(expectedX3); + // y3Result.ShouldBe(expectedY3); + // } + // + // + // [Fact] + // public void Bn254Pairing_Test() + // { + // var bridgeContext = GetRequiredService().Create(); + // var contractContext = new CSharpSmartContractContext(bridgeContext); + // + // // Arrange + // var input = new (byte[], byte[], byte[], byte[], byte[], byte[])[] + // { + // ( + // ToBytes32(new BigIntValue(0)), + // ToBytes32(new BigIntValue(0)), + // ToBytes32(new BigIntValue(0)), + // ToBytes32(new BigIntValue(0)), + // ToBytes32(new BigIntValue(0)), + // ToBytes32(new BigIntValue(0)) + // ) + // }; + // + // // Use raw API to compute expected results + // bool expected = Bn254.Net.Bn254.Pairing(new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[] + // { + // ( + // UInt256.FromBigEndianBytes(input[0].Item1), + // UInt256.FromBigEndianBytes(input[0].Item2), + // UInt256.FromBigEndianBytes(input[0].Item3), + // UInt256.FromBigEndianBytes(input[0].Item4), + // UInt256.FromBigEndianBytes(input[0].Item5), + // UInt256.FromBigEndianBytes(input[0].Item6) + // ) + // }); + // + // // Act + // bool result = contractContext.Bn254Pairing(input); + // + // // Assert + // result.ShouldBe(expected); + // } } \ No newline at end of file