Skip to content

Commit a157ce3

Browse files
committed
Add Ethereum-compatible aliases for BLS12-381
1 parent 5cc2cfd commit a157ce3

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

src/Neo/SmartContract/Native/CryptoLib.BLS12_381.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public static InteropInterface Bls12381Add(InteropInterface x, InteropInterface
9797
};
9898
}
9999

100+
[ContractMethod(CpuFee = 1 << 19, Name = "bls12_g1add")]
101+
public static InteropInterface Bls12G1Add(InteropInterface x, InteropInterface y)
102+
=> Bls12381Add(x, y);
103+
104+
[ContractMethod(CpuFee = 1 << 19, Name = "bls12_g2add")]
105+
public static InteropInterface Bls12G2Add(InteropInterface x, InteropInterface y)
106+
=> Bls12381Add(x, y);
107+
100108
/// <summary>
101109
/// Mul operation of gt point and multiplier
102110
/// </summary>
@@ -119,6 +127,14 @@ public static InteropInterface Bls12381Mul(InteropInterface x, byte[] mul, bool
119127
};
120128
}
121129

130+
[ContractMethod(CpuFee = 1 << 21, Name = "bls12_g1mul")]
131+
public static InteropInterface Bls12G1Mul(InteropInterface x, byte[] mul, bool neg)
132+
=> Bls12381Mul(x, mul, neg);
133+
134+
[ContractMethod(CpuFee = 1 << 21, Name = "bls12_g2mul")]
135+
public static InteropInterface Bls12G2Mul(InteropInterface x, byte[] mul, bool neg)
136+
=> Bls12381Mul(x, mul, neg);
137+
122138
/// <summary>
123139
/// Pairing operation of g1 and g2
124140
/// </summary>
@@ -142,5 +158,9 @@ public static InteropInterface Bls12381Pairing(InteropInterface g1, InteropInter
142158
};
143159
return new(Bls12.Pairing(in g1a, in g2a));
144160
}
161+
162+
[ContractMethod(CpuFee = 1 << 23, Name = "bls12_pairing")]
163+
public static InteropInterface Bls12Pairing(InteropInterface g1, InteropInterface g2)
164+
=> Bls12381Pairing(g1, g2);
145165
}
146166
}

tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Neo.Network.P2P;
1919
using Neo.Network.P2P.Payloads;
2020
using Neo.SmartContract;
21+
using Neo.Persistence;
2122
using Neo.SmartContract.Native;
2223
using Neo.VM;
2324
using Org.BouncyCastle.Utilities.Encoders;
@@ -263,6 +264,33 @@ public void TestBls12381Pairing()
263264
Assert.AreEqual(expected.ToLower(), result.GetInterface<Gt>().ToArray().ToHexString());
264265
}
265266

267+
[TestMethod]
268+
public void TestBls12AddAliases()
269+
{
270+
var expected = InvokeBlsAddMethod("bls12381Add");
271+
foreach (var alias in new[] { "bls12_g1add", "bls12_g2add" })
272+
{
273+
CollectionAssert.AreEqual(expected, InvokeBlsAddMethod(alias));
274+
}
275+
}
276+
277+
[TestMethod]
278+
public void TestBls12MulAliases()
279+
{
280+
var expected = InvokeBlsMulMethod("bls12381Mul", false);
281+
foreach (var alias in new[] { "bls12_g1mul", "bls12_g2mul" })
282+
{
283+
CollectionAssert.AreEqual(expected, InvokeBlsMulMethod(alias, false));
284+
}
285+
}
286+
287+
[TestMethod]
288+
public void TestBls12PairingAlias()
289+
{
290+
var expected = InvokeBlsPairingMethod("bls12381Pairing");
291+
CollectionAssert.AreEqual(expected, InvokeBlsPairingMethod("bls12_pairing"));
292+
}
293+
266294
[TestMethod]
267295
public void Bls12381Equal()
268296
{
@@ -1174,5 +1202,62 @@ private bool CallVerifyWithEd25519(byte[] message, byte[] publicKey, byte[] sign
11741202
return engine.ResultStack.Pop().GetBoolean();
11751203
}
11761204
}
1205+
1206+
private byte[] InvokeBlsAddMethod(string methodName)
1207+
{
1208+
var snapshotCache = TestBlockchain.GetTestSnapshotCache();
1209+
using ScriptBuilder script = new();
1210+
script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", gt);
1211+
script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", gt);
1212+
script.EmitPush(2);
1213+
script.Emit(OpCode.PACK);
1214+
script.EmitPush(CallFlags.All);
1215+
script.EmitPush(methodName);
1216+
script.EmitPush(NativeContract.CryptoLib.Hash);
1217+
script.EmitSysCall(ApplicationEngine.System_Contract_Call);
1218+
return ExecuteBlsScript(script, snapshotCache);
1219+
}
1220+
1221+
private byte[] InvokeBlsMulMethod(string methodName, bool neg)
1222+
{
1223+
var snapshotCache = TestBlockchain.GetTestSnapshotCache();
1224+
using ScriptBuilder script = new();
1225+
byte[] data = new byte[32];
1226+
data[0] = 0x03;
1227+
script.EmitPush(neg);
1228+
script.EmitPush(data);
1229+
script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", gt);
1230+
script.EmitPush(3);
1231+
script.Emit(OpCode.PACK);
1232+
script.EmitPush(CallFlags.All);
1233+
script.EmitPush(methodName);
1234+
script.EmitPush(NativeContract.CryptoLib.Hash);
1235+
script.EmitSysCall(ApplicationEngine.System_Contract_Call);
1236+
return ExecuteBlsScript(script, snapshotCache);
1237+
}
1238+
1239+
private byte[] InvokeBlsPairingMethod(string methodName)
1240+
{
1241+
var snapshotCache = TestBlockchain.GetTestSnapshotCache();
1242+
using ScriptBuilder script = new();
1243+
script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g2);
1244+
script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g1);
1245+
script.EmitPush(2);
1246+
script.Emit(OpCode.PACK);
1247+
script.EmitPush(CallFlags.All);
1248+
script.EmitPush(methodName);
1249+
script.EmitPush(NativeContract.CryptoLib.Hash);
1250+
script.EmitSysCall(ApplicationEngine.System_Contract_Call);
1251+
return ExecuteBlsScript(script, snapshotCache);
1252+
}
1253+
1254+
private byte[] ExecuteBlsScript(ScriptBuilder script, StoreCache snapshotCache)
1255+
{
1256+
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache,
1257+
settings: TestProtocolSettings.Default);
1258+
engine.LoadScript(script.ToArray());
1259+
Assert.AreEqual(VMState.HALT, engine.Execute());
1260+
return engine.ResultStack.Pop().GetInterface<Gt>().ToArray();
1261+
}
11771262
}
11781263
}

0 commit comments

Comments
 (0)