Skip to content

Commit

Permalink
feat(zkp): Add methods required for supporting zkp verification over …
Browse files Browse the repository at this point in the history
…Bn254
  • Loading branch information
gldeng committed Sep 4, 2024
1 parent 53f389c commit a57f39e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/AElf.CSharp.CodeOps/Validators/Method/ArrayValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using AElf.CSharp.Core;
using AElf.Types;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Volo.Abp.DependencyInjection;
Expand All @@ -24,7 +25,7 @@ public class ArrayValidator : IValidator<MethodDefinition>, ITransientDependency
.LimitByTotalSize(typeof(decimal), sizeof(decimal))
.LimitByTotalSize(typeof(char), sizeof(char))
.LimitByTotalSize(typeof(String), 128) // Need to limit the size of strings by disallowing String.Concat
.LimitByTotalSize(typeof(BigIntValue), 128)
// It isn't possible to estimate runtime sizes for below, so limit by count
.LimitByCount(typeof(Type), 5)
.LimitByCount(typeof(Object), 5) // Support object in Linq queries
Expand Down
10 changes: 8 additions & 2 deletions src/AElf.Sdk.CSharp.Internal/AElf.Sdk.CSharp.Internal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Rebex.Elliptic.Ed25519" Version="1.2.1" >
<PackageReference Include="Bn254.Net" Version="0.1.0-preview.3" >
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Rebex.Elliptic.Ed25519" Version="1.2.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Nethereum.Web3" Version="4.21.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AElf.Sdk.CSharp.Spec\AElf.Sdk.CSharp.Spec.csproj" />
<ProjectReference Include="..\AElf.Sdk.CSharp.Spec\AElf.Sdk.CSharp.Spec.csproj"/>
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions src/AElf.Sdk.CSharp.Internal/InternalBuiltIns.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using AElf.Sdk.CSharp.Spec;
using Bn254.Net;
using Nethereum.Util;

namespace AElf.Sdk.CSharp.Internal;

Expand All @@ -9,6 +11,7 @@ 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
Expand All @@ -22,4 +25,37 @@ public bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey)
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);
}
}
4 changes: 4 additions & 0 deletions src/AElf.Sdk.CSharp.Spec/IBuiltIns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
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);
}
55 changes: 40 additions & 15 deletions src/AElf.Sdk.CSharp/CSharpSmartContractContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace AElf.Sdk.CSharp;
public class CSharpSmartContractContext : ISmartContractBridgeContext, IBuiltIns
{
private IBuiltIns BuiltInsImplementation { get; }

public CSharpSmartContractContext(ISmartContractBridgeContext smartContractBridgeContextImplementation)
{
SmartContractBridgeContextImplementation = smartContractBridgeContextImplementation;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void FireLogEvent(LogEvent logEvent)
/// The height of the block that contains the transaction before charging.
/// </summary>
public Transaction Transaction => SmartContractBridgeContextImplementation.Transaction;

/// <summary>
/// The time included in the current blocks header.
/// </summary>
Expand Down Expand Up @@ -153,7 +153,7 @@ public void DeployContract(Address address, SmartContractRegistration registrati
{
SmartContractBridgeContextImplementation.DeployContract(address, registration, name);
}

/// <summary>
/// Update a smart contract (only the genesis contract can call it).
/// </summary>
Expand All @@ -164,17 +164,21 @@ public void UpdateContract(Address address, SmartContractRegistration registrati
{
SmartContractBridgeContextImplementation.UpdateContract(address, registration, name);
}

public ContractInfoDto DeploySmartContract(Address address, SmartContractRegistration registration, Hash name)
{
return SmartContractBridgeContextImplementation.DeploySmartContract(address,registration,name);
return SmartContractBridgeContextImplementation.DeploySmartContract(address, registration, name);
}
public ContractInfoDto UpdateSmartContract(Address address, SmartContractRegistration registration, Hash name,string previousContractVersion)

public ContractInfoDto UpdateSmartContract(Address address, SmartContractRegistration registration, Hash name,
string previousContractVersion)
{
return SmartContractBridgeContextImplementation.UpdateSmartContract(address,registration,name,previousContractVersion);
return SmartContractBridgeContextImplementation.UpdateSmartContract(address, registration, name,
previousContractVersion);
}

public ContractVersionCheckDto CheckContractVersion(string previousContractVersion, SmartContractRegistration registration)
public ContractVersionCheckDto CheckContractVersion(string previousContractVersion,
SmartContractRegistration registration)
{
return SmartContractBridgeContextImplementation.CheckContractVersion(previousContractVersion, registration);
}
Expand Down Expand Up @@ -226,13 +230,14 @@ public void SendVirtualInline(Hash fromVirtualAddress, Address toAddress, string
SmartContractBridgeContextImplementation.SendVirtualInline(fromVirtualAddress, toAddress, methodName,
args);
}

public void SendVirtualInline(Hash fromVirtualAddress, Address toAddress, string methodName, ByteString args,bool logTransaction)

public void SendVirtualInline(Hash fromVirtualAddress, Address toAddress, string methodName, ByteString args,
bool logTransaction)
{
SmartContractBridgeContextImplementation.SendVirtualInline(fromVirtualAddress, toAddress, methodName,
args,logTransaction);
args, logTransaction);
}


/// <summary>
/// Sends a virtual inline transaction to another contract. This method is only available to system smart contract.
Expand All @@ -250,7 +255,7 @@ public void SendVirtualInlineBySystemContract(Hash fromVirtualAddress, Address t
SmartContractBridgeContextImplementation.SendVirtualInlineBySystemContract(fromVirtualAddress, toAddress,
methodName, args);
}

public void SendVirtualInlineBySystemContract(Hash fromVirtualAddress, Address toAddress, string methodName,
ByteString args, bool logTransaction)
{
Expand Down Expand Up @@ -391,14 +396,34 @@ public Address ConvertVirtualAddressToContractAddressWithContractHashName(Hash v
return SmartContractBridgeContextImplementation.ConvertVirtualAddressToContractAddressWithContractHashName(
virtualAddress);
}

public bool ECVrfVerify(byte[] pubKey, byte[] alpha, byte[] pi, out byte[] beta)
{
return SmartContractBridgeContextImplementation.ECVrfVerify(pubKey, alpha, pi, out beta);
}

public bool Ed25519Verify(byte[] signature, byte[] message, byte[] publicKey)
{
return BuiltInsImplementation.Ed25519Verify(signature, message, publicKey);
}

public byte[] Keccak256(byte[] message)
{
return BuiltInsImplementation.Keccak256(message);
}

public (byte[] x, byte[] y) Bn254G1Mul(byte[] x1, byte[] y1, byte[] s)
{
return BuiltInsImplementation.Bn254G1Mul(x1, y1, s);
}

public (byte[] x3, byte[] y3) Bn254G1Add(byte[] x1, byte[] y1, byte[] x2, byte[] y2)
{
return BuiltInsImplementation.Bn254G1Add(x1, y1, x2, y2);
}

public bool Bn254Pairing((byte[], byte[], byte[], byte[], byte[], byte[])[] input)
{
return BuiltInsImplementation.Bn254Pairing(input);
}
}
61 changes: 60 additions & 1 deletion src/AElf.Types/Types/BigIntValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,30 @@

namespace AElf.Types
{

public partial class BigIntValue : IComparable, IComparable<BigIntValue>
{
#region Frequent Values

public static BigIntValue Zero => new BigIntValue { Value = "0" };
public static BigIntValue One => new BigIntValue { Value = "1" };

#endregion

public static BigIntValue FromBigEndianBytes(byte[] bigEndianBytes)
{
var bigInteger = new BigInteger(bigEndianBytes, true, true);
return new BigIntValue
{
Value = bigInteger.ToString()
};
}

public byte[] ToBigEndianBytes()
{
var bigInteger = ConvertStringToBigInteger(Value);
return bigInteger.ToByteArray(true, true);
}

public int CompareTo(object obj)
{
if (!(obj is BigIntValue bigInt)) throw new InvalidOperationException();
Expand Down Expand Up @@ -126,6 +147,43 @@ private static bool LessThan(in BigIntValue a, in BigIntValue b)
return aBigInt < bBigInt;
}

#region Operators

public static BigIntValue operator %(BigIntValue a, BigIntValue b)
{
return BigInteger.Remainder(ConvertStringToBigInteger(a.Value), ConvertStringToBigInteger(b.Value))
.ToString();
}

public static BigIntValue operator +(BigIntValue a, BigIntValue b)
{
return BigInteger.Add(ConvertStringToBigInteger(a.Value), ConvertStringToBigInteger(b.Value)).ToString();
}

public static BigIntValue operator -(BigIntValue a, BigIntValue b)
{
return BigInteger.Subtract(ConvertStringToBigInteger(a.Value), ConvertStringToBigInteger(b.Value))
.ToString();
}

public static BigIntValue operator *(BigIntValue a, BigIntValue b)
{
return BigInteger.Multiply(ConvertStringToBigInteger(a.Value), ConvertStringToBigInteger(b.Value))
.ToString();
}

public static bool operator ==(BigIntValue a, BigIntValue b)
{
return ConvertStringToBigInteger(a?.Value ?? "0") == ConvertStringToBigInteger(b?.Value ?? "0");
}

public static bool operator !=(BigIntValue a, BigIntValue b)
{
return !(a == b);
}

#endregion

#region < <= > >=

public static bool operator <(in BigIntValue a, in BigIntValue b)
Expand All @@ -149,5 +207,6 @@ private static bool LessThan(in BigIntValue a, in BigIntValue b)
}

#endregion

}
}

0 comments on commit a57f39e

Please sign in to comment.