FK20 CUDA
test_fft.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 #for data standards, see ../doc/test_fft.md
4 
5 import FK20Py
6 
7 import random
8 import pickle
9 from tqdm import tqdm
10 
11 def pointToInt(i : FK20Py.blst.P1) -> int:
12  b = i.serialize()
13  x=b[:48]
14  y=b[48:]
15  return int.from_bytes(x, byteorder='big'), int.from_bytes(y, byteorder='big')
16 
17 def pointToHexString(i : FK20Py.blst.P1) -> str:
18  x,y = pointToInt(i)
19  return '{:096x}{:096x}'.format(x,y)
20 
21 MAX_DEGREE_POLY = FK20Py.MODULUS-1
22 N_POINTS = 512 #Number of points in the Poly
23 N_TESTS = 2 #Number of tests to generate
24 
25 def stringfyFFT_Trace(fft) -> str:
26  return ' ' .join(pointToHexString(point) for point in fft)
27 
29  return [random.randint(1, MAX_DEGREE_POLY) for _ in range(N_POINTS)]
30 
31 def generateTest(polynomial:list, setup:int):
32  '''
33  generates a test case for the fft part of the algorithm
34  '''
35  setup = FK20Py.generate_setup(setup, len(polynomial))
36  _ = FK20Py.commit_to_poly(polynomial, setup)
37  # Computing the proofs on the double
38  fftin, fftout = FK20Py.fftTrace(polynomial, setup)
39  return fftin, fftout
40 
41 def generateAllTest(nTest=1):
42  polys=[]
43  inputs=[]
44  outputs = []
45  setup = random.getrandbits(256)
46  print(f'setup {setup:0{256//4}x}')
47 
48  for testN in tqdm(range(nTest)):
49  poly = genRandonPoly()
50  print('polynomial', *[f'{i:0{256//4}x}' for i in poly], sep=' ')
51  polys.append(poly)
52 
53  fftin, fftout = generateTest(poly, setup)
54  print(f"fftTestInput_{testN}", stringfyFFT_Trace(fftin))
55  print(f"fftTestOutput_{testN}", stringfyFFT_Trace(fftout))
56  inputs.append ([pointToInt(i) for i in fftin])
57  outputs.append([pointToInt(i) for i in fftout])
58  return {'polys':polys,
59  'inputs':inputs,
60  'outputs':outputs,
61  'setup':setup}
62 
63 def printExpectedOutput(test, skip=True):
64  if skip: return
65  print("#")
66  for idx, output in enumerate(test['outputs']):
67  print(f"fftTestOutput_{idx}", *[f'{i:0{764//4}x}' for i in output])
68 
69 import sys
70 def eprint(*args, **kwargs):
71  print(*args, file=sys.stderr, **kwargs)
72 
73 if __name__ == '__main__':
74  random.seed(0) #remove after debug
75  print(f"NTESTS {N_TESTS}")
76  test = generateAllTest(N_TESTS)
77  with open("testfft.pickle", 'wb') as f:
78  pickle.dump(test, f)
79 
80  eprint(len(test['inputs'][0]))
81 
def generateTest(list polynomial, int setup)
Definition: test_fft.py:31
int pointToInt(FK20Py.blst.P1 i)
Definition: test_fft.py:11
def genRandonPoly()
Definition: test_fft.py:28
def generateAllTest(nTest=1)
Definition: test_fft.py:41
def eprint(*args, **kwargs)
Definition: test_fft.py:70
def printExpectedOutput(test, skip=True)
Definition: test_fft.py:63
str pointToHexString(FK20Py.blst.P1 i)
Definition: test_fft.py:17
str stringfyFFT_Trace(fft)
Definition: test_fft.py:25