Skip to content

Commit

Permalink
feat(zktree): fix remaining issue and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenportkey committed Sep 10, 2024
1 parent cc20bed commit c2181e9
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 210 deletions.
67 changes: 36 additions & 31 deletions contract/ZkTreeVerifier/PairingLib.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using AElf.CSharp.Core;
using AElf.Sdk.CSharp;
using AElf.Types;
Expand All @@ -7,8 +9,6 @@

namespace AElf.Contracts.ZkTreeVerifier;

using PairingInput = (byte[], byte[], byte[], byte[], byte[], byte[]);

public class Proof
{
public G1Point A { get; set; }
Expand Down Expand Up @@ -94,10 +94,10 @@ internal static G1Point Negate(this CSharpSmartContractContext ctx, G1Point p)
internal static G1Point Addition(this CSharpSmartContractContext ctx, G1Point p1, G1Point p2)
{
var (x, y) = ctx.Bn254G1Add(
p1.X.ToBigEndianBytes(),
p1.Y.ToBigEndianBytes(),
p2.X.ToBigEndianBytes(),
p2.Y.ToBigEndianBytes()
p1.X.ToBytes32(),
p1.Y.ToBytes32(),
p2.X.ToBytes32(),
p2.Y.ToBytes32()
);
return new G1Point()
{
Expand All @@ -109,9 +109,9 @@ internal static G1Point Addition(this CSharpSmartContractContext ctx, G1Point p1
internal static G1Point ScalarMul(this CSharpSmartContractContext ctx, G1Point p, BigIntValue scalar)
{
var (x, y) = ctx.Bn254G1Mul(
p.X.ToBigEndianBytes(),
p.Y.ToBigEndianBytes(),
scalar.ToBigEndianBytes()
p.X.ToBytes32(),
p.Y.ToBytes32(),
scalar.ToBytes32()
);
return new G1Point()
{
Expand All @@ -120,28 +120,21 @@ internal static G1Point ScalarMul(this CSharpSmartContractContext ctx, G1Point p
};
}

internal static bool Pairing(this CSharpSmartContractContext ctx, G1Point[] p1, G2Point[] p2)
internal static bool Pairing(this CSharpSmartContractContext ctx, List<G1Point> p1, List<G2Point> p2)
{
if (p1.Length != p2.Length)
if (p1.Count != p2.Count)
{
throw new AssertionException("pairing-lengths-failed");
}

var elements = p1.Length;
var input = new PairingInput[elements];
for (var i = 0; i < elements; i++)
{
input[i] = (
p1[i].X.ToBigEndianBytes(),
p1[i].Y.ToBigEndianBytes(),
p2[i].X.A.ToBigEndianBytes(),
p2[i].X.B.ToBigEndianBytes(),
p2[i].Y.A.ToBigEndianBytes(),
p2[i].Y.B.ToBigEndianBytes()
);
}
var success = ctx.Bn254Pairing(p1.Zip(p2).Select(p =>
(
p.First.X.ToBytes32(), p.First.Y.ToBytes32(),
p.Second.X.A.ToBytes32(), p.Second.X.B.ToBytes32(),
p.Second.Y.A.ToBytes32(), p.Second.Y.B.ToBytes32()
)
).ToArray());

var success = ctx.Bn254Pairing(input);
if (!success)
{
throw new AssertionException("pairing-check-failed");
Expand All @@ -154,10 +147,10 @@ internal static bool PairingProd2(this CSharpSmartContractContext ctx,
G1Point a1, G2Point a2,
G1Point b1, G2Point b2)
{
return ctx.Pairing(new[]
return ctx.Pairing(new List<G1Point>()
{
a1, b1
}, new[]
}, new List<G2Point>()
{
a2, b2
});
Expand All @@ -168,10 +161,10 @@ internal static bool PairingProd3(this CSharpSmartContractContext ctx,
G1Point b1, G2Point b2,
G1Point c1, G2Point c2)
{
return ctx.Pairing(new[]
return ctx.Pairing(new List<G1Point>()
{
a1, b1, c1
}, new[]
}, new List<G2Point>()
{
a2, b2, c2
});
Expand All @@ -183,12 +176,24 @@ internal static bool PairingProd4(this CSharpSmartContractContext ctx,
G1Point c1, G2Point c2,
G1Point d1, G2Point d2)
{
return ctx.Pairing(new[]
return ctx.Pairing(new List<G1Point>()
{
a1, b1, c1, d1
}, new[]
}, new List<G2Point>()
{
a2, b2, c2, d2
});
}

public static byte[] ToBytes32(this 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;
}
}
7 changes: 4 additions & 3 deletions contract/ZkTreeVerifier/ZkWasmVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using AElf.Types;
using Google.Protobuf.WellKnownTypes;
Expand All @@ -10,7 +11,7 @@ class VerifyingKey
public G2Point beta2;
public G2Point gamma2;
public G2Point delta2;
public G1Point[] IC;
public List<G1Point> IC;
}

public class ZkTreeVerifier : ZkTreeVerifierContainer.ZkTreeVerifierBase
Expand Down Expand Up @@ -57,7 +58,7 @@ private bool Verify(BigIntValue[] input, Proof proof)
var snarkScalarField = "21888242871839275222246405745257275088548364400416034343698204186575808495617"
.ToBigIntValue();
var vk = VerifyingKey();
Assert(input.Length + 1 == vk.IC.Length, "verifier-bad-input");
Assert(input.Length + 1 == vk.IC.Count, "verifier-bad-input");
// Compute the linear combination vk_x
var vkX = new G1Point
{
Expand Down Expand Up @@ -104,7 +105,7 @@ G2Point MakeG2(string x1, string x2, string y1, string y2) =>
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"4082367875863433681332203403145435568316851327593401208105741076214120093531",
"8495653923123431417604973247489272438418190587263600148770280649306958101930"),
IC = new G1Point[3]
IC = new List<G1Point>()
{
MakeG1("907082046166848403662755682318758048763333219052759262226888852664247719678",
"13772868673976322661276556815121724196712611456125880819435532265591384929117"),
Expand Down
3 changes: 3 additions & 0 deletions src/AElf.Runtime.CSharp/AElf.Runtime.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

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

<ItemGroup>
Expand Down
Loading

0 comments on commit c2181e9

Please sign in to comment.