-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
184 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <stdio.h> | ||
#include <assert.h> | ||
|
||
// #define USE_SCALAR | ||
#define DEBUG | ||
// #define WITHMAIN | ||
|
||
#if(CASE==ntt_64) | ||
#include "ntt_64.h" | ||
#elif(CASE==ntt_128) | ||
#include "ntt_128.h" | ||
#elif(CASE==ntt_256) | ||
#include "ntt_256.h" | ||
#elif(CASE==ntt_512) | ||
#include "ntt_512.h" | ||
#elif(CASE==ntt_1024) | ||
#include "ntt_1024.h" | ||
#elif(CASE==ntt_4096) | ||
#include "ntt_4096.h" | ||
#else | ||
#error "undefined ntt case" | ||
#endif | ||
|
||
|
||
void ntt(const int *array, int l, const int *twiddle, int p, int *dst); | ||
|
||
void test() { | ||
const int l = macroL; | ||
const int n = macroN; | ||
static const int arr[macroN] = { | ||
macroIn | ||
}; | ||
#ifdef USE_SCALAR | ||
static const int twiddle[] = { | ||
macroScalarTW | ||
}; | ||
#else | ||
static const int twiddle[] = { | ||
macroVectorTW | ||
}; | ||
#endif | ||
const int p = macroP; | ||
int dst[macroN]; | ||
ntt(arr, l, twiddle, p, dst); | ||
|
||
#ifdef DEBUG | ||
const int gold[macroN] = { | ||
macroOut | ||
}; | ||
printf("n = %d\n", n); | ||
for (int i = 0; i < n; i++) { | ||
// dst[i] = dst[i] % p; | ||
if(dst[i] < 0) dst[i] += p; | ||
if(dst[i] != gold[i]) { | ||
printf("(%d %d, i)", dst[i], gold[i], i); | ||
if ((i + 1) % 8 == 0) { | ||
printf("\n"); | ||
} else { | ||
printf(" "); | ||
} | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
#ifdef WITHMAIN | ||
int main(void) { | ||
test(); | ||
} | ||
#endif | ||
|