Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-Wye committed Feb 7, 2025
1 parent 9ecdf6f commit 37e4ebc
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 14 deletions.
34 changes: 20 additions & 14 deletions tests/eval/_ntt/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{ linkerScript
, makeBuilder
, python3
, t1main
}:

let
builder = makeBuilder { casePrefix = "eval"; };
build_ntt = caseName /* must be consistent with attr name */ : main_src: kernel_src:
build_ntt = caseName /* must be consistent with attr name */ : main_src: kernel_src: extra_flag:
builder {
caseName = caseName;

Expand All @@ -16,7 +17,11 @@ let
buildPhase = ''
runHook preBuild
${python3}/bin/python3 ./gen_data.py ${caseName}
$CC -T${linkerScript} \
-DCASE=${caseName} \
${extra_flag} \
${main_src} ${kernel_src} \
${t1main} \
-o $pname.elf
Expand All @@ -28,17 +33,18 @@ let
};

in {
ntt_64 = build_ntt "ntt_64" ./ntt.c ./ntt_64_main.c;
ntt_128 = build_ntt "ntt_128" ./ntt.c ./ntt_128_main.c;
ntt_256 = build_ntt "ntt_256" ./ntt.c ./ntt_256_main.c;
ntt_512 = build_ntt "ntt_512" ./ntt.c ./ntt_512_main.c;
ntt_1024 = build_ntt "ntt_1024" ./ntt.c ./ntt_1024_main.c;
ntt_4096 = build_ntt "ntt_4096" ./ntt.c ./ntt_4096_main.c;

ntt_mem_64 = build_ntt "ntt_mem_64" ./ntt_mem.c ./ntt_64_main.c;
ntt_mem_128 = build_ntt "ntt_mem_128" ./ntt_mem.c ./ntt_128_main.c;
ntt_mem_256 = build_ntt "ntt_mem_256" ./ntt_mem.c ./ntt_256_main.c;
ntt_mem_512 = build_ntt "ntt_mem_512" ./ntt_mem.c ./ntt_512_main.c;
ntt_mem_1024 = build_ntt "ntt_mem_1024" ./ntt_mem.c ./ntt_1024_main.c;
ntt_mem_4096 = build_ntt "ntt_mem_4096" ./ntt_mem.c ./ntt_4096_main.c;
ntt_test = build_ntt "ntt_64" ./ntt.c ./ntt_main.c "";
ntt_64 = build_ntt "ntt_64" ./ntt.c ./ntt_main.c "";
ntt_128 = build_ntt "ntt_128" ./ntt.c ./ntt_main.c "";
ntt_256 = build_ntt "ntt_256" ./ntt.c ./ntt_main.c "";
ntt_512 = build_ntt "ntt_512" ./ntt.c ./ntt_main.c "";
ntt_1024 = build_ntt "ntt_1024" ./ntt.c ./ntt_main.c "";
ntt_4096 = build_ntt "ntt_4096" ./ntt.c ./ntt_main.c "";

ntt_mem_64 = build_ntt "ntt_mem_64" ./ntt_mem.c ./ntt_main.c "-DUSE_SCALAR";
ntt_mem_128 = build_ntt "ntt_mem_128" ./ntt_mem.c ./ntt_main.c "-DUSE_SCALAR";
ntt_mem_256 = build_ntt "ntt_mem_256" ./ntt_mem.c ./ntt_main.c "-DUSE_SCALAR";
ntt_mem_512 = build_ntt "ntt_mem_512" ./ntt_mem.c ./ntt_main.c "-DUSE_SCALAR";
ntt_mem_1024 = build_ntt "ntt_mem_1024" ./ntt_mem.c ./ntt_main.c "-DUSE_SCALAR";
ntt_mem_4096 = build_ntt "ntt_mem_4096" ./ntt_mem.c ./ntt_main.c "-DUSE_SCALAR";
}
93 changes: 93 additions & 0 deletions tests/eval/_ntt/gen_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import json
import random

def genRandomPoly(l, p):
n = 1 << l
a = [random.randrange(p) for _ in range(n)]
return a

def genGoldPoly(l, p, g, poly):
n = 1 << l
poly_out = []
for i in range(n):
tmp = 0
for j in range(n):
tmp += poly[j] * pow(g, i * j, p)
tmp = tmp % p
poly_out.append(tmp)
return poly_out

def genScalarTW(l, p, g):
w = g

twiddle_list = []
for _ in range(l):
twiddle_list.append(w)
w = (w * w) % p

return twiddle_list

def genVectorTW(l, p, g):
n = 1 << l
m = 2
layerIndex = 0

outTW = []
while m <= n:
# print(f"// layer #{layerIndex}")
layerIndex+=1
wPower = 0

for j in range(m//2):
k = 0
while k < n:
currentW = pow(g, wPower, p)
k += m
outTW.append(currentW)
# print(currentW, end =", ")
wPower += n // m
m *= 2
# print("\n")
return outTW

def main(l, p, g):
poly_in = genRandomPoly(l, p)
poly_out = genGoldPoly(l, p, g, poly_in)
scalar_tw = genScalarTW(l, p, g)
vector_tw = genVectorTW(l, p, g)
n = 1 << l
data = {
"l": l,
"n": n,
"p": p,
"input": poly_in,
"output": poly_out,
"vector_tw": vector_tw,
"scalar_tw": scalar_tw,
}
# json_name = "ntt_" + str(n) + ".json"
# with open(json_name, "w") as out:
# json.dump(data, out)
header_file = "ntt_" + str(n) + ".h"

# with open(json_name, "r") as json_in:
# data = json.load(json_in)
header_str = "#define macroL " + str(data["l"]) + "\n"
header_str += "#define macroN " + str(data["n"]) + "\n"
header_str += "#define macroP " + str(data["p"]) + "\n"
header_str += "#define macroIn " + ','.join(str(e) for e in data["input"]) + "\n"
header_str += "#define macroOut " + ','.join(str(e) for e in data["output"]) + "\n"
header_str += "#define macroScalarTW " + ','.join(str(e) for e in data["scalar_tw"]) + "\n"
header_str += "#define macroVectorTW " + ','.join(str(e) for e in data["vector_tw"]) + "\n"
with open(header_file, "w") as header_out:
header_out.write(header_str)

if __name__ == '__main__':
p = 12289
main(6, p, 7311)
main(7, p, 12149)
main(8, p, 8340)
main(9, p, 3400)
main(10, p, 10302)
main(12, p, 1331)

7 changes: 7 additions & 0 deletions tests/eval/_ntt/ntt_1024.h

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tests/eval/_ntt/ntt_128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define macroL 7
#define macroN 128
#define macroP 12289
#define macroIn 8014,1298,6558,3396,6743,6899,540,9709,231,3225,7584,1384,5689,1631,10189,2381,10495,11780,2040,9081,9910,10447,723,10473,3940,11381,7318,1640,9276,683,6104,3618,847,7066,5675,4928,5550,7217,10486,5115,2017,11584,10870,817,2799,8146,2386,6453,10113,4105,3525,8923,593,7229,9921,3149,8662,7216,1695,465,11336,8720,7868,7071,5582,2814,3661,2193,3034,10616,56,9996,5147,244,5337,7681,6791,5779,8630,9357,7157,5935,4586,6379,90,8719,3604,3729,6621,12130,10255,10723,9274,8370,8710,11436,5495,9453,9085,8271,11534,10983,10139,4814,2236,2865,7714,12113,375,10602,802,6220,1021,8523,4299,7104,2564,4733,6382,1673,413,11389,12126,4181,6488,10920,6870,8012
#define macroOut 4466,1715,9976,88,10984,4675,3453,10254,7614,3105,953,3980,7396,10102,5923,4310,10628,7774,7115,5168,3935,9748,681,7424,4176,4554,9661,1576,6650,2451,10298,5192,1060,5714,6547,5781,2904,2043,9435,9308,9755,697,9176,2658,693,2065,2081,10032,8680,7249,7178,8812,2052,7343,8539,5624,3475,3025,1981,2910,9034,585,5997,6078,2033,10264,4635,5471,9969,3243,10703,4052,2868,352,1187,607,9063,9435,7713,7171,5379,1133,3627,10742,4284,902,9860,9163,4341,4261,8966,2551,11140,4430,5153,11011,8983,7292,578,8167,9021,2874,4905,8384,12273,2071,8367,5023,3475,4525,9415,9807,5116,7160,3034,1080,9734,9967,11098,11923,553,9354,11218,4925,3878,6295,8097,6597
#define macroScalarTW 12149,7311,5860,4134,8246,1479,12288
#define macroVectorTW 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,1,1,1,1,1,1,1,1,4134,4134,4134,4134,4134,4134,4134,4134,8246,8246,8246,8246,8246,8246,8246,8246,11567,11567,11567,11567,11567,11567,11567,11567,1479,1479,1479,1479,1479,1479,1479,1479,6553,6553,6553,6553,6553,6553,6553,6553,5146,5146,5146,5146,5146,5146,5146,5146,1305,1305,1305,1305,1305,1305,1305,1305,1,1,1,1,5860,5860,5860,5860,4134,4134,4134,4134,3621,3621,3621,3621,8246,8246,8246,8246,1212,1212,1212,1212,11567,11567,11567,11567,8785,8785,8785,8785,1479,1479,1479,1479,3195,3195,3195,3195,6553,6553,6553,6553,9744,9744,9744,9744,5146,5146,5146,5146,10643,10643,10643,10643,1305,1305,1305,1305,3542,3542,3542,3542,1,1,7311,7311,5860,5860,3006,3006,4134,4134,5023,5023,3621,3621,2625,2625,8246,8246,8961,8961,1212,1212,563,563,11567,11567,5728,5728,8785,8785,4821,4821,1479,1479,10938,10938,3195,3195,9545,9545,6553,6553,6461,6461,9744,9744,11340,11340,5146,5146,5777,5777,10643,10643,9314,9314,1305,1305,4591,4591,3542,3542,2639,2639,1,12149,7311,8736,5860,2963,3006,9275,4134,11112,5023,9542,3621,9198,2625,1170,8246,726,8961,11227,1212,2366,563,7203,11567,2768,5728,9154,8785,11289,4821,955,1479,1853,10938,4805,3195,7393,9545,3201,6553,4255,6461,4846,9744,12208,11340,9970,5146,4611,5777,2294,10643,9238,9314,10963,1305,1635,4591,8577,3542,7969,2639,11499
7 changes: 7 additions & 0 deletions tests/eval/_ntt/ntt_256.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define macroL 8
#define macroN 256
#define macroP 12289
#define macroIn 2763,7263,11077,7596,2436,1687,369,9360,3224,1632,3075,4634,9805,8411,10238,4936,6361,4734,7757,723,9592,7782,7171,4241,4037,6893,5821,8054,3106,5388,2273,6675,833,8733,11581,9165,11547,1463,3022,5947,9279,7197,934,7420,10088,8113,6668,3112,9683,10356,5454,7873,6272,1639,6188,2946,3009,7105,4081,4477,2434,5034,3086,5135,12006,5650,6090,5316,2190,3495,2614,6975,8261,2333,8242,10168,9160,12245,10689,2492,9968,2613,6724,7566,1046,10013,4161,11502,7412,2675,11340,8803,6132,8102,10677,3003,5056,9598,3254,9868,2489,217,7948,4757,4650,3353,5132,9647,10451,8498,9471,1778,10934,5119,5241,8052,904,5465,6047,81,5929,4081,6603,11431,3750,3227,3741,11769,8212,487,7554,11574,217,9450,2874,6510,9521,4907,7317,9154,152,2634,1446,10624,9181,6580,6739,11208,11757,3410,2913,10364,2114,11399,7233,5795,8336,11071,8018,6561,3573,6333,848,5370,3110,3574,5792,8393,8262,6064,7225,4161,4221,1336,5391,7177,8409,37,11747,7605,4601,11282,8955,3174,6037,5324,1226,1382,11140,1068,1301,11525,263,4601,9367,2371,4524,10013,9704,1204,6662,4307,1842,10995,667,11806,8954,9073,8520,2196,4898,4487,8034,9051,4723,6535,12176,475,5715,4269,3251,3923,6669,662,2914,6802,4483,7324,9211,395,2485,3083,988,1305,4038,4511,4659,5888,74,4504,9796,4734,7786,714,738,6412,6019,1647,2422,10442,2801,1095,9853,10987,10408,7045
#define macroOut 6389,10441,11823,2649,9301,458,3270,10925,1306,3821,9704,1637,4573,11885,5947,10157,10448,686,11643,6689,1934,10601,8991,5991,6428,7088,1566,6320,1011,2078,9286,2214,12149,2452,5647,2415,11953,3908,4421,2847,9355,6772,11605,11364,8073,4335,5142,2798,5776,554,8306,7642,15,3730,6917,4404,3967,11875,7269,12010,2079,1250,6563,9596,5089,5325,9822,6311,5845,5966,196,1989,4808,12257,6517,10250,4180,2139,10296,1897,4879,6246,9798,2527,2678,1885,9748,2203,7700,3033,6152,7292,5035,3202,6096,6669,9982,8943,9156,272,183,6905,1598,2954,3130,2854,3013,5259,1018,7384,1835,7947,5745,9370,4268,2631,9079,959,7924,8919,9704,204,12256,11194,793,5677,9866,5589,9926,8154,3523,6394,4999,2615,8527,10515,8918,9792,10708,6420,6055,11575,11027,6138,9913,6021,9455,10548,10641,6949,9013,10234,4894,9009,6444,2673,5105,3668,1351,8874,2618,6752,10539,11150,4428,6252,4483,11480,10424,9420,161,7688,921,10119,10444,452,10336,8498,10268,294,147,9976,641,11822,5263,9894,1154,3912,635,11967,8241,9983,4939,9564,7947,6260,10257,10231,9759,2623,9561,9062,3785,4116,814,7146,4556,10568,10282,10408,1467,11164,4900,8515,4106,3836,3322,11494,11718,11251,3090,6974,264,973,7845,5598,4162,8447,2994,6761,11367,6329,1750,11979,9360,1000,2169,11262,10025,9640,9377,8217,8594,10705,1226,2753,10630,9274,1942,6068,2942,630,443,9853,20,8870
#define macroScalarTW 8340,12149,7311,5860,4134,8246,1479,12288
#define macroVectorTW 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,4134,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,8246,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,11567,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,1479,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,6553,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,5146,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1305,1,1,1,1,1,1,1,1,5860,5860,5860,5860,5860,5860,5860,5860,4134,4134,4134,4134,4134,4134,4134,4134,3621,3621,3621,3621,3621,3621,3621,3621,8246,8246,8246,8246,8246,8246,8246,8246,1212,1212,1212,1212,1212,1212,1212,1212,11567,11567,11567,11567,11567,11567,11567,11567,8785,8785,8785,8785,8785,8785,8785,8785,1479,1479,1479,1479,1479,1479,1479,1479,3195,3195,3195,3195,3195,3195,3195,3195,6553,6553,6553,6553,6553,6553,6553,6553,9744,9744,9744,9744,9744,9744,9744,9744,5146,5146,5146,5146,5146,5146,5146,5146,10643,10643,10643,10643,10643,10643,10643,10643,1305,1305,1305,1305,1305,1305,1305,1305,3542,3542,3542,3542,3542,3542,3542,3542,1,1,1,1,7311,7311,7311,7311,5860,5860,5860,5860,3006,3006,3006,3006,4134,4134,4134,4134,5023,5023,5023,5023,3621,3621,3621,3621,2625,2625,2625,2625,8246,8246,8246,8246,8961,8961,8961,8961,1212,1212,1212,1212,563,563,563,563,11567,11567,11567,11567,5728,5728,5728,5728,8785,8785,8785,8785,4821,4821,4821,4821,1479,1479,1479,1479,10938,10938,10938,10938,3195,3195,3195,3195,9545,9545,9545,9545,6553,6553,6553,6553,6461,6461,6461,6461,9744,9744,9744,9744,11340,11340,11340,11340,5146,5146,5146,5146,5777,5777,5777,5777,10643,10643,10643,10643,9314,9314,9314,9314,1305,1305,1305,1305,4591,4591,4591,4591,3542,3542,3542,3542,2639,2639,2639,2639,1,1,12149,12149,7311,7311,8736,8736,5860,5860,2963,2963,3006,3006,9275,9275,4134,4134,11112,11112,5023,5023,9542,9542,3621,3621,9198,9198,2625,2625,1170,1170,8246,8246,726,726,8961,8961,11227,11227,1212,1212,2366,2366,563,563,7203,7203,11567,11567,2768,2768,5728,5728,9154,9154,8785,8785,11289,11289,4821,4821,955,955,1479,1479,1853,1853,10938,10938,4805,4805,3195,3195,7393,7393,9545,9545,3201,3201,6553,6553,4255,4255,6461,6461,4846,4846,9744,9744,12208,12208,11340,11340,9970,9970,5146,5146,4611,4611,5777,5777,2294,2294,10643,10643,9238,9238,9314,9314,10963,10963,1305,1305,1635,1635,4591,4591,8577,8577,3542,3542,7969,7969,2639,2639,11499,11499,1,8340,12149,12144,7311,8011,8736,9048,5860,11336,2963,10530,3006,480,9275,6534,4134,6915,11112,2731,5023,10908,9542,9005,3621,5067,9198,3382,2625,5791,1170,334,8246,2396,726,8652,8961,5331,11227,3289,1212,6522,2366,8595,563,1022,7203,4388,11567,130,2768,6378,5728,4177,9154,5092,8785,12171,11289,4231,4821,9821,955,1428,1479,8993,1853,6747,10938,1673,4805,11560,3195,3748,7393,3707,9545,9447,3201,4632,6553,2837,4255,8357,6461,9764,4846,9408,9744,10092,12208,355,11340,11745,9970,2426,5146,4452,4611,3459,5777,7300,2294,10276,10643,11462,9238,5179,9314,12280,10963,1260,1305,7935,1635,7399,4591,8705,8577,10200,3542,9813,7969,2548,2639,11950,11499,10593
7 changes: 7 additions & 0 deletions tests/eval/_ntt/ntt_4096.h

Large diffs are not rendered by default.

Loading

0 comments on commit 37e4ebc

Please sign in to comment.