Skip to content

Commit bd59552

Browse files
committed
simd-0284: alt_bn128 little endian compatibility
1 parent fbe31ff commit bd59552

File tree

1 file changed

+152
-27
lines changed

1 file changed

+152
-27
lines changed

generators/syscalls_alt_bn128.py

Lines changed: 152 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import fd58
12
import base64
23
import hashlib
34
import test_suite.protos.vm_pb2 as vm_pb
5+
import test_suite.protos.context_pb2 as context_pb
46

57
OUTPUT_DIR = "./test-vectors/syscall/tests/alt_bn128"
68
HEAP_START = 0x300000000
79
CU_BASE = 542
810
CU_PER_ELEM = 61
911
CU_MEM_OP = 10
1012

13+
LITTLE_ENDIAN = 0x80
1114
ADD_OP = 0
1215
ADD_SZ = 64 + 64
1316
ADD_CU = 334
@@ -329,6 +332,15 @@ def _into_key_data(key_prefix, test_vectors):
329332
return [(key_prefix + str(j), data) for j, data in enumerate(test_vectors)]
330333

331334

335+
def to_little_endian_g1(input):
336+
chunk_size = 32
337+
result = bytearray()
338+
for i in range(0, len(input), chunk_size):
339+
chunk = input[i : i + chunk_size]
340+
result.extend(chunk[::-1])
341+
return result
342+
343+
332344
test_vectors_add = [
333345
# https://github.com/anza-xyz/agave/blob/v1.18.12/programs/bpf_loader/src/syscalls/mod.rs#L1547
334346
# invalid op
@@ -477,6 +489,28 @@ def _into_key_data(key_prefix, test_vectors):
477489
"cu_avail": ADD_CU,
478490
}
479491
)
492+
# little endian
493+
test_vectors_add.append(
494+
{
495+
"heap_prefix": bytes([0] * 64) + input,
496+
"op": ADD_OP | LITTLE_ENDIAN,
497+
"input_addr": HEAP_START + 64,
498+
"input_size": len(input),
499+
"result_addr": HEAP_START,
500+
"cu_avail": ADD_CU,
501+
}
502+
)
503+
test_vectors_add.append(
504+
{
505+
"heap_prefix": bytes([0] * 64) + to_little_endian_g1(input),
506+
"op": ADD_OP | LITTLE_ENDIAN,
507+
"input_addr": HEAP_START + 64,
508+
"input_size": len(input),
509+
"result_addr": HEAP_START,
510+
"cu_avail": ADD_CU,
511+
"little_endian": True,
512+
}
513+
)
480514

481515

482516
test_vectors_mul = [
@@ -1181,8 +1215,8 @@ def _into_key_data(key_prefix, test_vectors):
11811215

11821216
test_vectors_group = (
11831217
_into_key_data("a", test_vectors_add)
1184-
+ _into_key_data("m", test_vectors_mul)
1185-
+ _into_key_data("p", test_vectors_pairing)
1218+
# + _into_key_data("m", test_vectors_mul)
1219+
# + _into_key_data("p", test_vectors_pairing)
11861220
)
11871221

11881222
test_vectors_compression = (
@@ -1196,8 +1230,97 @@ def _into_key_data(key_prefix, test_vectors):
11961230
0xAAEF1EDEB6C5BF85, # enable_alt_bn128_syscall
11971231
0x9BB55B5DF1C396C5, # enable_alt_bn128_compression_syscall
11981232
0x8BA9E9038D9FDCFF, # simplify_alt_bn128_syscall_error_codes
1233+
0x54C5C5132EAAE808, # fix_alt_bn128_multiplication_input_length
1234+
]
1235+
extra_features = [
1236+
0xF08A42C3C040E908, # fix_alt_bn128_pairing_length_check
1237+
0x1B4ADDDC131EE908, # alt_bn128_little_endian
11991238
]
12001239

1240+
1241+
def add_solfuzz_agave_context(syscall_ctx):
1242+
syscall_ctx.instr_ctx.program_id = bytes([0] * 32)
1243+
syscall_ctx.instr_ctx.slot_context.slot = 10
1244+
1245+
account = context_pb.AcctState()
1246+
account.address = bytes([0] * 32)
1247+
account.lamports = 123456789
1248+
account.data = bytes([0] * 100)
1249+
account.executable = True
1250+
account.owner = fd58.dec32(
1251+
bytes("BPFLoaderUpgradeab1e11111111111111111111111", "utf-8")
1252+
)
1253+
syscall_ctx.instr_ctx.accounts.append(account)
1254+
1255+
account = context_pb.AcctState()
1256+
account.address = fd58.dec32(
1257+
bytes("SysvarC1ock11111111111111111111111111111111", "utf-8")
1258+
)
1259+
account.lamports = 1
1260+
account.data = bytes.fromhex("0a000000000000000000000000000000") + bytes([0] * 24)
1261+
account.owner = fd58.dec32(
1262+
bytes("Sysvar1111111111111111111111111111111111111", "utf-8")
1263+
)
1264+
syscall_ctx.instr_ctx.accounts.append(account)
1265+
1266+
account = context_pb.AcctState()
1267+
account.address = fd58.dec32(
1268+
bytes("SysvarEpochSchedu1e111111111111111111111111", "utf-8")
1269+
)
1270+
account.lamports = 1
1271+
account.data = bytes.fromhex(
1272+
"80970600000000008097060000000000010e00000000000000e0ff070000000000"
1273+
)
1274+
account.owner = fd58.dec32(
1275+
bytes("Sysvar1111111111111111111111111111111111111", "utf-8")
1276+
)
1277+
syscall_ctx.instr_ctx.accounts.append(account)
1278+
1279+
account = context_pb.AcctState()
1280+
account.address = fd58.dec32(
1281+
bytes("SysvarRent111111111111111111111111111111111", "utf-8")
1282+
)
1283+
account.lamports = 1
1284+
account.data = bytes.fromhex("980d000000000000000000000000004032")
1285+
account.owner = fd58.dec32(
1286+
bytes("Sysvar1111111111111111111111111111111111111", "utf-8")
1287+
)
1288+
syscall_ctx.instr_ctx.accounts.append(account)
1289+
1290+
account = context_pb.AcctState()
1291+
account.address = fd58.dec32(
1292+
bytes("SysvarLastRestartS1ot1111111111111111111111", "utf-8")
1293+
)
1294+
account.lamports = 1
1295+
account.data = bytes.fromhex("8813000000000000")
1296+
account.owner = fd58.dec32(
1297+
bytes("Sysvar1111111111111111111111111111111111111", "utf-8")
1298+
)
1299+
syscall_ctx.instr_ctx.accounts.append(account)
1300+
1301+
account = context_pb.AcctState()
1302+
account.address = fd58.dec32(
1303+
bytes("SysvarRecentB1ockHashes11111111111111111111", "utf-8")
1304+
)
1305+
account.lamports = 1
1306+
account.data = bytes.fromhex("96000000000000000000000000000000") + bytes([0] * 5992)
1307+
account.owner = fd58.dec32(
1308+
bytes("Sysvar1111111111111111111111111111111111111", "utf-8")
1309+
)
1310+
syscall_ctx.instr_ctx.accounts.append(account)
1311+
1312+
account = context_pb.AcctState()
1313+
account.address = fd58.dec32(
1314+
bytes("SysvarS1otHashes111111111111111111111111111", "utf-8")
1315+
)
1316+
account.lamports = 1
1317+
account.data = bytes([0] * 20488)
1318+
account.owner = fd58.dec32(
1319+
bytes("Sysvar1111111111111111111111111111111111111", "utf-8")
1320+
)
1321+
syscall_ctx.instr_ctx.accounts.append(account)
1322+
1323+
12011324
if __name__ == "__main__":
12021325
print("Generating syscall alt_bn128 tests...")
12031326

@@ -1211,41 +1334,43 @@ def _into_key_data(key_prefix, test_vectors):
12111334
syscall_ctx.vm_ctx.r2 = test.get("input_addr", 0)
12121335
syscall_ctx.vm_ctx.r3 = test.get("input_size", 0)
12131336
syscall_ctx.vm_ctx.r4 = test.get("result_addr", 0)
1214-
syscall_ctx.instr_ctx.cu_avail = test.get("cu_avail", 0)
1215-
syscall_ctx.instr_ctx.program_id = bytes(
1216-
[0] * 32
1217-
) # solfuzz-agave expectes a program_id
12181337

1338+
syscall_ctx.instr_ctx.cu_avail = test.get("cu_avail", 0)
12191339
syscall_ctx.instr_ctx.epoch_context.features.features.extend(features)
1340+
if test.get("little_endian"):
1341+
syscall_ctx.instr_ctx.epoch_context.features.features.extend(extra_features)
1342+
1343+
# required by solfuzz-agave
1344+
add_solfuzz_agave_context(syscall_ctx)
12201345

12211346
serialized_instr = syscall_ctx.SerializeToString(deterministic=True)
12221347
filename = str(key) + "_" + hashlib.sha3_256(serialized_instr).hexdigest()[:16]
1223-
with open(f"{OUTPUT_DIR}/{filename}.bin", "wb") as f:
1348+
with open(f"{OUTPUT_DIR}/{filename}.syscallctx", "wb") as f:
12241349
f.write(serialized_instr)
12251350

12261351
print("done!")
1227-
print("Generating syscall alt_bn128_compression tests...")
1352+
# print("Generating syscall alt_bn128_compression tests...")
12281353

1229-
for key, test in test_vectors_compression:
1230-
heap_prefix = test.get("heap_prefix", [])
1231-
syscall_ctx = vm_pb.SyscallContext()
1232-
syscall_ctx.syscall_invocation.function_name = b"sol_alt_bn128_compression"
1233-
syscall_ctx.syscall_invocation.heap_prefix = bytes(heap_prefix)
1234-
syscall_ctx.vm_ctx.heap_max = len(heap_prefix)
1235-
syscall_ctx.vm_ctx.r1 = test.get("op", 0)
1236-
syscall_ctx.vm_ctx.r2 = test.get("input_addr", 0)
1237-
syscall_ctx.vm_ctx.r3 = test.get("input_size", 0)
1238-
syscall_ctx.vm_ctx.r4 = test.get("result_addr", 0)
1239-
syscall_ctx.instr_ctx.cu_avail = test.get("cu_avail", 0)
1240-
syscall_ctx.instr_ctx.program_id = bytes(
1241-
[0] * 32
1242-
) # solfuzz-agave expectes a program_id
1354+
# for key, test in test_vectors_compression:
1355+
# heap_prefix = test.get("heap_prefix", [])
1356+
# syscall_ctx = vm_pb.SyscallContext()
1357+
# syscall_ctx.syscall_invocation.function_name = b"sol_alt_bn128_compression"
1358+
# syscall_ctx.syscall_invocation.heap_prefix = bytes(heap_prefix)
1359+
# syscall_ctx.vm_ctx.heap_max = len(heap_prefix)
1360+
# syscall_ctx.vm_ctx.r1 = test.get("op", 0)
1361+
# syscall_ctx.vm_ctx.r2 = test.get("input_addr", 0)
1362+
# syscall_ctx.vm_ctx.r3 = test.get("input_size", 0)
1363+
# syscall_ctx.vm_ctx.r4 = test.get("result_addr", 0)
12431364

1244-
syscall_ctx.instr_ctx.epoch_context.features.features.extend(features)
1365+
# syscall_ctx.instr_ctx.cu_avail = test.get("cu_avail", 0)
1366+
# syscall_ctx.instr_ctx.epoch_context.features.features.extend(features)
12451367

1246-
serialized_instr = syscall_ctx.SerializeToString(deterministic=True)
1247-
filename = str(key) + "_" + hashlib.sha3_256(serialized_instr).hexdigest()[:16]
1248-
with open(f"{OUTPUT_DIR}/{filename}.bin", "wb") as f:
1249-
f.write(serialized_instr)
1368+
# # required by solfuzz-agave
1369+
# add_solfuzz_agave_context(syscall_ctx)
1370+
1371+
# serialized_instr = syscall_ctx.SerializeToString(deterministic=True)
1372+
# filename = str(key) + "_" + hashlib.sha3_256(serialized_instr).hexdigest()[:16]
1373+
# with open(f"{OUTPUT_DIR}/{filename}.syscallctx", "wb") as f:
1374+
# f.write(serialized_instr)
12501375

12511376
print("done!")

0 commit comments

Comments
 (0)