-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints.sage
1233 lines (1101 loc) · 45.3 KB
/
constraints.sage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file computes the number of constraints, variables and NNZ for the
# constraint systems given in the paper.
#
# The structure is as follows: variables are defined in a dictionary, which
# also stores how many variables are necessary to express it. For Fp^2, this
# will always be one, for Fp we keep track of how efficient it is to express
# the real part, the imaginary part, and their sum. We then use this knowledge
# when asserting equalities.
#
# The functions `assert_product` and `assert_square` are used every time a
# constraint is added. Over Fp^2, this translates to a row of the R1CS, for Fp
# this is then lifted to 3 and 2 rows, respectively.
var('k X j0 j1')
NO_VARS_USED = 'no_vars_used'
VARS = 'variables'
def phi_coefs_for_ell(ell):
s = 12/gcd(12, ell - 1)
phi = DedekindEtaModularPolynomialDatabase()[ell]
var('x j')
# Compute the coefficients
coefs = []
for [coef, power] in (phi(x=x,j=j) + j*x).coefficients():
coefs.append(coef)
# The alternative coefficients
coefsp = []
for i in range(0, ell + 2):
coefsp.append(coefs[ell + 1 - i] * ell^(s * (ell - i)))
return phi, s, coefs, coefsp
# Returns
# - The number of constraints used to compute the product
# - The number of variables used to express the product
# - The number of non-zero entries in the constraint matrices
def assert_product(in_fp, left_input, right_input, output, expected_output = 0):
if in_fp:
constraints = 3
# We just have u2 as additional variable
no_variables = 1
# The number of non-zero entries in the matrix
[x1, x2, sum_x] = left_input['no_vars_used']
[y1, y2, sum_y] = right_input['no_vars_used']
[z1, z2, sum_z] = output['no_vars_used']
# u2 = x2 * y2
non_zero_entries = 1 + x2 + y2
# z1 - d*u2 = x1 * y1
non_zero_entries += z1 + 1 + x1 + y1
# zs + (1-d)u2 = xs + ys
non_zero_entries += sum_z + 1 + sum_x + sum_y
else:
constraints = 1
no_variables = 0
# The number of non-zero entries in the matrix
non_zero_entries = left_input['no_vars_used'] + right_input['no_vars_used'] + output['no_vars_used']
# Check that the constraint is valid
diff = left_input['vars'] * right_input['vars'] - output['vars'] - expected_output
if diff != 0:
print(expand(diff))
assert diff == 0
return [constraints, no_variables, non_zero_entries]
# Returns
# - The number of constraints used to compute the square
# - The number of variables used to express the square
# of the output
# - The number of non-zero entries in the constraint matrices
def assert_square(in_fp, inp, outp, expected_output = 0):
if in_fp:
constraints = 2
no_variables = 0
# The number of non-zero entries in the matrix
[x1, x2, sum_x] = inp['no_vars_used']
[z1, z2, sum_z] = outp['no_vars_used']
# z2 = 2 x1 * x2
non_zero_entries = z2 + x1 + x2
# z1 + (d+1)/2 z2 = sum_x * (x1 + d*x2)
non_zero_entries += z1 + min(z2, sum_z) + sum_x + x1 + min(x2, sum_x)
else:
constraints = 1
no_variables = 0
# The number of non-zero entries in the matrix
non_zero_entries = inp['no_vars_used'] + inp['no_vars_used'] + outp['no_vars_used']
# Check that the constraint is valid
diff = inp['vars'] * inp['vars'] - outp['vars'] - expected_output
if diff != 0:
print(expand(diff))
assert diff == 0
return [constraints, no_variables, non_zero_entries]
def scale_var(in_fp, coef, entry):
if in_fp:
return {
'vars': entry['vars'] * coef,
'no_vars_used': [x for x in entry['no_vars_used']]
}
else:
return {
'vars': entry['vars'] * coef,
'no_vars_used': entry['no_vars_used']
}
# Sum variable dictionaries
def sum_vars(in_fp, coefs_and_entries):
if in_fp:
result = {
'vars': 0,
'no_vars_used': [0, 0, 0]
}
else:
result = {
'vars': 0,
'no_vars_used': 0
}
for [coef, entry] in coefs_and_entries:
result['vars'] += coef * entry['vars']
if in_fp:
result['no_vars_used'] = [sum(x) for x in zip(result['no_vars_used'], entry['no_vars_used'])]
else:
result['no_vars_used'] += entry['no_vars_used']
return result
basis_regular = [1, 1, 2]
basis_sum = [1, 2, 1]
VARIABLE_ONE = { 'vars': 1, 'no_vars_used': [1, 0, 1] }
def original_paper(in_fp):
phi = ClassicalModularPolynomialDatabase()[2]
constraints = 0
non_zero_entries = 0
# We don't count the 1 in the variable count.
# We duplicate j0 and j1 in the dictionary, but this is merely for
# asserting correctness of the constraints.
if in_fp:
variables = 2 * (k + 1) \
+ 2 * (k + 1) \
+ 2 * (k + 1) \
+ 2 * k
var_dict = {
'1': VARIABLE_ONE,
'j0^1': { 'vars': j0, 'no_vars_used': basis_regular },
'j0^2': { 'vars': j0^2, 'no_vars_used': basis_regular },
'j0^3': { 'vars': j0^3, 'no_vars_used': basis_regular },
'j1^1': { 'vars': j1, 'no_vars_used': basis_regular },
'j1^2': { 'vars': j1^2, 'no_vars_used': basis_regular },
'j1^3': { 'vars': j1^3, 'no_vars_used': basis_regular },
'j0j1': { 'vars': j0*j1, 'no_vars_used': basis_regular }
}
else:
# We don't count the 1
variables = k + 1 \
+ k + 1 \
+ k + 1 \
+ k
var_dict = {
'1': {'vars': 1, 'no_vars_used': 1 },
'j0^1': { 'vars': j0, 'no_vars_used': 1 },
'j0^2': { 'vars': j0^2, 'no_vars_used': 1 },
'j0^3': { 'vars': j0^3, 'no_vars_used': 1 },
'j1^1': { 'vars': j1, 'no_vars_used': 1 },
'j1^2': { 'vars': j1^2, 'no_vars_used': 1 },
'j1^3': { 'vars': j1^3, 'no_vars_used': 1 },
'j0j1': { 'vars': j0*j1, 'no_vars_used': 1 }
}
[new_constr, new_vars, new_nnz] = assert_square(in_fp, var_dict['j0^1'], var_dict['j0^2'])
constraints += new_constr * (k + 1)
non_zero_entries += new_nnz * (k + 1)
variables += new_vars * (k + 1)
[new_constr, new_vars, new_nnz] = assert_product(in_fp, var_dict['j0^2'], var_dict['j0^1'], var_dict['j0^3'])
constraints += new_constr * (k + 1)
non_zero_entries += new_nnz * (k + 1)
variables += new_vars * (k + 1)
[new_constr, new_vars, new_nnz] = assert_product(in_fp, var_dict['j0^1'], var_dict['j1^1'], var_dict['j0j1'])
constraints += new_constr * k
non_zero_entries += new_nnz * k
variables += new_vars * k
# Compute polynomial
left_hand_side = scale_var(in_fp, -1488, var_dict['j0j1'])
right_hand_side = sum_vars(in_fp, [
[1, var_dict['j0^1']],
[1, var_dict['j1^1']],
[-1/1488, var_dict['j0j1']],
])
output = sum_vars(in_fp, [
[1, var_dict['j0^3']],
[1, var_dict['j1^3']],
[-162000, var_dict['j0^2']],
[-162000, var_dict['j1^2']],
[8748000000, var_dict['j0^1']],
[8748000000, var_dict['j1^1']],
[40773375, var_dict['j0j1']],
[-157464000000000, var_dict['1']],
])
[new_constr, new_vars, new_nnz] = assert_product(in_fp, left_hand_side, right_hand_side, output, -phi(j0=j0,j1=j1))
constraints += new_constr * k
non_zero_entries += new_nnz * k
variables += new_vars * k
return constraints, variables, non_zero_entries
# Compute the number of constraints, variables and NNZ
def simple_approach(ell, in_fp):
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
constraints = 0
non_zero_entries = 0
if in_fp:
# We don't count the 1
variables = 2 * k + 2 * (k + 1)
# Variables for real part, variables for imaginary part, variables for sum
var_dict = {
'1': VARIABLE_ONE,
'X^1': { 'vars': X, 'no_vars_used': basis_sum },
'y0': { 'vars': j0 - coefs[1], 'no_vars_used': basis_sum },
'y1': { 'vars': j1 - coefs[1], 'no_vars_used': basis_sum }
}
else:
# We don't count the 1
variables = k + k + 1
var_dict = {
'1': {'vars': 1, 'no_vars_used': 1 },
'X^1': { 'vars': X, 'no_vars_used': 1 },
'y0': { 'vars': j0 - coefs[1], 'no_vars_used': 1 },
'y1': { 'vars': j1 - coefs[1], 'no_vars_used': 1 }
}
# Compute the powers up to ell, inclusive.
# We first add the powers. We can always obtain these by squaring, except
# for the biggest power, if its odd. We only do this over Fp, since
# squaring is only cheaper here and the nnz even becomes more expensive
# over Fp^2.
#
# We have an additional trick to minimize the number of non zero entries.
# When using the squaring trick to obtain odd powers, we make sure to
# obtain a linear combination that is immediately useful. This saves a
# non-zero entry in the squaring, as well as in the computation of one of
# the polynomials. We need to make sure that the power is high enough that
# it is not needed in plain to compute another power.
# I.e. we replace the middle power by the linear combination
# X^{2i} + X^{2i + 1} + X^{2i + 2}
# if we do not need 2i + 2i + 1. Since we allways compute ell directly, it suffices that
# 4i + 1 is bigger than the next odd number, so
# 4i + 1 > 2 * floor(ell / 2) - 1
# or, we can replace power j = 2i + 1 by a linear combination with j + 1 if
# j > floor(ell / 2)
for i in range(2, ell + 1):
if in_fp:
variables += 2 * k
if i == 2 and i == ell: # Only in this specific case the regular basis is better
var_dict['X^' + str(i)] = {\
'vars': X^i,\
'no_vars_used': basis_regular,\
}
elif i != ell and i % 2 == 1 and i > floor(ell / 2):
# We use the linear combination directly here, where the second
# two terms are already correct for the first polynomial.
coef_a = coefs[i + 1]
coef_b = coefs[i + 2]
var_dict['xX^' + str(i - 1) + ' + cX^' + str(i) + ' + cX^' + str(i + 1)] = {\
'vars': coef_a^2/coef_b/4*X^(i-1) + coef_a*X^(i) + coef_b*X^(i + 1),\
'no_vars_used': basis_sum,\
}
else:
var_dict['X^' + str(i)] = {\
'vars': X^i,\
'no_vars_used': basis_sum,\
}
else:
variables += 1 * k
var_dict['X^' + str(i)] = {'vars': X^i, 'no_vars_used': 1 }
# Now actually compute these powers. Again, we use squaring whereever
# possible.
for i in range(2, ell + 1):
if i % 2 == 0:
# A simple squaring!
compute_from = var_dict['X^' + str(i / 2)]
output = var_dict['X^' + str(i)]
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(in_fp, compute_from, output)
elif i == ell or not in_fp:
# We do not have a higher even power to subtract, so multiply directly
left_hand_side = var_dict['X^1']
right_hand_side = var_dict['X^' + str(i - 1)]
output = var_dict['X^' + str(i)]
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(in_fp, left_hand_side, right_hand_side, output)
else:
# We use the following trick:
# (x^i + x^{i + 1})^2 = x^{2i} + 2x^{2i + 1} + x^{2i + 2}
#
# Now, if the power is high enough, we just use the result to
# express two powers correctly in the polynomial. Otherwise, we
# subtract of the wrong powers. In practice, there will be only one
# power, namely X^3 for ell=5 and X^5 for ell=7 or ell=13.
if i > floor(ell / 2):
coef_a = coefs[i + 1]
coef_b = coefs[i + 2]
inp = sum_vars(in_fp, [\
[coef_a / coef_b / 2, var_dict['X^' + str(floor(i / 2))]],\
[1, var_dict['X^' + str(ceil(i / 2))]],\
])
outputs = [\
[1/coef_b, var_dict['xX^' + str(i - 1) + ' + cX^' + str(i) + ' + cX^' + str(i + 1)]],\
]
else:
inp = sum_vars(in_fp, [\
[1, var_dict['X^' + str(floor(i / 2))]],\
[1, var_dict['X^' + str(ceil(i / 2))]],\
])
outputs = [\
[1, var_dict['X^' + str(i - 1)]],\
[2, var_dict['X^' + str(i)]],\
[1, var_dict['X^' + str(i + 1)]],\
]
output = sum_vars(in_fp, outputs)
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(in_fp, inp, output)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Build the last two constraints
# First constraint
left_input = var_dict['X^1']
right_input_list = [[-1, var_dict['y0']]]
# Compute the polynomial on the right hand side.
for i in range(1, ell + 1):
if in_fp and i != ell and i % 2 == 1 and i > floor(ell / 2):
# Here we can use this linear combination. We can omit the next
# power, and the first power can be corrected for by an earlier power.
right_input_list.append([1, var_dict['xX^' + str(i - 1) + ' + cX^' + str(i) + ' + cX^' + str(i + 1)]])
elif in_fp and (i + 1) < ell and (i + 1) % 2 == 1 and (i + 1) > floor(ell / 2):
# Compensate for the odd square trick
right_input_list.append([\
coefs[i + 1] - coefs[i + 2]^2 / coefs[i + 3] / 4,\
var_dict['X^' + str(i)],\
])
elif in_fp and (i - 1) < ell and (i - 1) % 2 == 1 and (i - 1) > floor(ell / 2):
# Do nothing, this power has already been included in the previous linear combination!
pass
else:
right_input_list.append([coefs[i + 1], var_dict['X^' + str(i)]])
right_input = sum_vars(in_fp, right_input_list)
output = scale_var(in_fp, -coefs[0], var_dict['1'])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(in_fp, left_input, right_input, output, phi(x=X,j=j0))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Second constraint
left_input = var_dict['X^' + str(ell)]
right_input = sum_vars(in_fp, [\
[coefsp[ell + 1], var_dict['X^1']],\
[-1, var_dict['y1']],\
])
output_list = [[-coefsp[0], var_dict['1']]]
for i in range(1, ell - 1 + 1):
if in_fp and i != ell and i % 2 == 1 and i > floor(ell / 2):
output_list.append([-coefsp[i]/coefs[i + 1], var_dict['xX^' + str(i - 1) + ' + cX^' + str(i) + ' + cX^' + str(i + 1)]])
# Compensate for the linear combination
elif in_fp and i + 1 != ell and (i + 1) % 2 == 1 and (i + 1) > floor(ell / 2):
output_list.append([-coefsp[i] + coefsp[i + 1] / coefs[i + 2]*coefs[i+2]^2/coefs[i + 3]/4, var_dict['X^' + str(i)]])
elif in_fp and i - 1 != ell and (i - 1) % 2 == 1 and (i - 1) > floor(ell / 2):
output_list.append([-coefsp[i] + coefsp[i - 1]/coefs[i]*coefs[i+1], var_dict['X^' + str(i)]])
else:
output_list.append([-coefsp[i], var_dict['X^' + str(i)]])
output = sum_vars(in_fp, output_list)
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(in_fp, left_input, right_input, output, X^(ell + 1)/ell^s * phi(x=ell^s/X,j=j1))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
return constraints, variables, non_zero_entries
# A specialized method for ell=2 over Fp
def special_2_over_fp():
ell = 2
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
y0 = coefs[1] - coefs[2]^2/4/coefs[3] - j0 - coefs[0]*coefs[2]/coefs[3]/2/ell^s
y1 = coefs[1] - coefs[2]^2/4/coefs[3] - j1 - coefs[0]*coefs[2]/coefs[3]/2/ell^s
constraints = 0
non_zero_entries = 0
variables = 2 * 2 * k + 2 * (k + 1)
var_dict = {
'1': VARIABLE_ONE,
'Z+': { 'vars': coefs[2]/2/coefs[3] + X, 'no_vars_used': basis_regular },
'Z-': { 'vars': coefs[2]/2/coefs[3]/ell^s + 1/X, 'no_vars_used': basis_regular },
'y0': { 'vars': y0, 'no_vars_used': basis_regular },
'y1': { 'vars': y1, 'no_vars_used': basis_regular },
}
# Compute product
# X*X^{-1} = 1
[n_constr, n_vars, n_nnz] = assert_product( \
True, \
sum_vars(True, [
[1, var_dict['Z+']],\
[-coefs[2]/2/coefs[3], var_dict['1']],\
]),
sum_vars(True, [
[1, var_dict['Z-']],\
[-coefs[2]/2/coefs[3]/ell^s, var_dict['1']],\
]),
var_dict['1']
)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += n_nnz * k
# Compute first square
[n_constr, n_vars, n_nnz] = assert_square(\
True,\
var_dict['Z+'],\
sum_vars(\
True,
[# Constant term is included in y
[-4096, var_dict['Z-']],\
[-1, var_dict['y0']],\
]
),\
phi(x=X,j=j0)/X
)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += n_nnz * k
# Compute second square
[n_constr, n_vars, n_nnz] = assert_square(\
True,\
var_dict['Z-'],\
sum_vars(\
True,
[# Constant term is included in y
[-1/16777216, var_dict['Z+']],\
[-1/16777216, var_dict['y1']],\
]
),\
X/coefs[3]/ell^(3*s)*phi(x=ell^s/X, j=j1)
)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += n_nnz * k
return constraints, variables, non_zero_entries
# Compute the number of constraints, variables and NNZ
def special_3_over_fp():
ell = 3
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
y0 = j0 - coefs[1]
y1 = j1 - coefs[1]
constraints = 0
non_zero_entries = 0
# We don't count the 1
variables = 3 * (2 * k) + 2 * (k + 1)
# Variables for real part, variables for imaginary part, variables for sum
var_dict = {
'1': VARIABLE_ONE,
'y0': { 'vars': y0, 'no_vars_used': basis_sum },
'y1': { 'vars': y1, 'no_vars_used': basis_sum },
'X^1': { 'vars': X, 'no_vars_used': basis_sum },
'X^2': { 'vars': X^2, 'no_vars_used': basis_sum },
'X^3': { 'vars': X^3, 'no_vars_used': basis_sum },
}
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(True,\
var_dict['X^1'],\
var_dict['X^2'],\
)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True,\
var_dict['X^1'],\
var_dict['X^2'],\
var_dict['X^3'],\
)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Build the last two constraints
# First constraint
left_input = var_dict['X^1']
right_input = sum_vars(True, [
[-1, var_dict['y0']],\
[1, var_dict['X^3']],\
])
# Compute the polynomial on the right hand side.
output = sum_vars(True, [\
[-36, var_dict['X^3']],\
[-270, var_dict['X^2']],\
[-729, var_dict['1']],\
])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True, left_input, right_input, output, phi(x=X,j=j0))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Second constraint
left_input = var_dict['X^3']
right_input = sum_vars(True, [\
[coefsp[ell + 1], var_dict['X^1']],\
[-1, var_dict['y1']],\
])
output = sum_vars(True, [\
[-coefsp[0], var_dict['1']],\
[-19131876, var_dict['X^1']],\
[-196830, var_dict['X^2']],\
])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True, left_input, right_input, output, X^(ell + 1)/ell^s * phi(x=ell^s/X,j=j1))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
return constraints, variables, non_zero_entries
# Compute the number of constraints, variables and NNZ
def special_new_3_over_fp():
ell = 3
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
y0 = j0 + 13946586480/729
y1 = j1 + 13946586480/729
constraints = 0
non_zero_entries = 0
# We don't count the 1
variables = 3 * (2 * k) + 2 * (k + 1)
# Variables for real part, variables for imaginary part, variables for sum
var_dict = {
'1': VARIABLE_ONE,
'y0': { 'vars': y0, 'no_vars_used': basis_sum },
'y1': { 'vars': y1, 'no_vars_used': basis_sum },
'X^1': { 'vars': X, 'no_vars_used': basis_sum },
'X^2': { 'vars': X^2, 'no_vars_used': basis_sum },
'square': { 'vars': (X^2 + 18*X - 27)^2, 'no_vars_used': basis_regular },
}
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(True,\
var_dict['X^1'],\
var_dict['X^2'],\
)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# First constraint, build the difference between polynomials to eliminate
# the constant term and divide by X.
constr1 = (phi(x=X,j=j0)*coefsp[0] - X^(ell+1)/ell^s*phi(x=ell^s/X, j=j1)*coefs[0])/X
left_input = var_dict['X^2']
right_input = sum_vars(True, [
[729, var_dict['y1']],\
[387419760, var_dict['X^1']],\
])
# Compute the polynomial on the right hand side.
output = sum_vars(True, [\
[-104460042960, var_dict['X^1']],\
[387420489, var_dict['y0']],\
[-7412066808269760, var_dict['1']],\
])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True, left_input, right_input, output, constr1)
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(True, sum_vars(True, [\
[1, var_dict['X^2']],\
[18, var_dict['X^1']],\
[-27, var_dict['1']],\
]),
var_dict['square'],
)
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True,
var_dict['X^1'],
sum_vars(True, [\
[19132848, var_dict['1']],\
[-1, var_dict['y0']],
]),
sum_vars(True, [\
[-1, var_dict['square']],
]),
phi(x=X,j=j0)
)
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
return constraints, variables, non_zero_entries
# Compute the number of constraints, variables and NNZ
def special_5_over_fp():
ell = 5
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
y0 = j0 - coefs[1]
y1 = j1 - coefs[1]
constraints = 0
non_zero_entries = 0
# We don't count the 1
variables = 5 * (2 * k) + 2 * (k + 1)
# Variables for real part, variables for imaginary part, variables for sum
var_dict = {
'1': VARIABLE_ONE,
'y0': { 'vars': y0, 'no_vars_used': basis_sum },
'y1': { 'vars': y1, 'no_vars_used': basis_sum },
'X^1': { 'vars': X, 'no_vars_used': basis_sum },
'X^2': { 'vars': X^2, 'no_vars_used': basis_sum },
'X^2 + X^3 + X^4': {
'vars': coefs[3]^2 / coefs[4] / 4 * X^2 + coefs[3]*X^3 + coefs[4]*X^4,
'no_vars_used': basis_sum
},
'X^4': { 'vars': X^4, 'no_vars_used': basis_sum },
'X^5': { 'vars': X^5, 'no_vars_used': basis_sum },
}
# Now actually compute these powers. Again, we use squaring whereever
# possible.
for i in range(2, ell + 1):
if i % 2 == 0:
# A simple squaring!
compute_from = var_dict['X^' + str(i / 2)]
output = var_dict['X^' + str(i)]
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(True, compute_from, output)
elif i == 3:
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(True,\
sum_vars(True, [\
[coefs[3]/coefs[4]/2, var_dict['X^1']],\
[1, var_dict['X^2']],\
]),
scale_var(True, 1/coefs[4], var_dict['X^2 + X^3 + X^4']),
)
else:
assert i == 5
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True,\
var_dict['X^1'],\
var_dict['X^4'],\
var_dict['X^5'],\
)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Build the last two constraints
# First constraint
left_input = var_dict['X^1']
right_input = sum_vars(True, [
[-1, var_dict['y0']],\
[1, var_dict['X^5']],\
])
# Compute the polynomial on the right hand side.
output = sum_vars(True, [\
[-30, var_dict['X^5']],\
[-1, var_dict['X^2 + X^3 + X^4']],\
[-14725/63, var_dict['X^2']],\
[-125, var_dict['1']],\
])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True, left_input, right_input, output, phi(x=X,j=j0))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Second constraint
left_input = var_dict['X^5']
right_input = sum_vars(True, [\
[coefsp[ell + 1], var_dict['X^1']],\
[-1, var_dict['y1']],\
])
output = sum_vars(True, [\
[4725000, var_dict['X^4']],\
[-coefsp[3]/coefs[3], var_dict['X^2 + X^3 + X^4']],\
[-37439453125/63, var_dict['X^2']],\
[-7324218750, var_dict['X^1']],\
[-30517578125, var_dict['1']],\
])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(True, left_input, right_input, output, X^(ell + 1)/ell^s * phi(x=ell^s/X,j=j1))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
return constraints, variables, non_zero_entries
# Compute the number of constraints, variables and NNZ, over Fp^2
def better_approach(ell):
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
constraints = 0
non_zero_entries = 0
t = (ell + 1) / 2
y0 = j0 - coefs[1]
y1 = j1 - coefs[1]
# We don't count the 1
variables = k + k + 1
var_dict = {
'1': {'vars': 1, 'no_vars_used': 1 },
'X^1': { 'vars': X, 'no_vars_used': 1 },
'y0': { 'vars': y0, 'no_vars_used': 1 },
'y1': { 'vars': y1, 'no_vars_used': 1 }
}
# Store the powers of X
# Unlike in the simple approach, we also need X^{t - 1} in plain.
for i in range(2, t + 1):
variables += 1 * k
var_dict['X^' + str(i)] = {
'vars': X^i,
'no_vars_used': 1
}
# Compute the powers of X, inclusive
for i in range(2, t + 1):
if i % 2 == 0:
# We can square
compute_from = var_dict['X^' + str(i / 2)]
output = var_dict['X^' + str(i)]
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(false, compute_from, output)
else:
# We do not have a higher even power to subtract, so multiply directly
left_hand_side = var_dict['X^1']
right_hand_side = var_dict['X^' + str(i - 1)]
output = var_dict['X^' + str(i)]
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(false, left_hand_side, right_hand_side, output)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Compute the crossterms with j
left_hand_side = var_dict['X^1']
right_hand_side = var_dict['y0']
variables += 1 * k
var_dict['X^1y0'] = { 'vars': X*y0, 'no_vars_used': 1 }
output = var_dict['X^1y0']
[n_constr, n_vars, nnnz] = assert_product(false, left_hand_side, right_hand_side, output)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += nnnz * k
left_hand_side = var_dict['X^' + str(t - 1)]
right_hand_side = var_dict['y1']
variables += 1 * k
var_dict['X^' + str(t - 1) + 'y1'] = { 'vars': X^(t-1)*y1, 'no_vars_used': 1 }
output = var_dict['X^' + str(t - 1) + 'y1']
[n_constr, n_vars, nnnz] = assert_product(false, left_hand_side, right_hand_side, output)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += nnnz * k
# Build the last two constraints
# First constraint
left_input = var_dict['X^' + str(t)]
right_input_list = [[coefs[t], var_dict['1']]]
for i in range(1, t + 1):
assert i - 1 != t
right_input_list.append([coefs[i + t], var_dict['X^' + str(i)]])
right_input = sum_vars(false, right_input_list)
output_list = [\
[-coefs[0], var_dict['1']],\
[1, var_dict['X^1y0']],\
]
for i in range(2, t - 1 + 1):
output_list.append([-coefs[i], var_dict['X^' + str(i)]])
output = sum_vars(false, output_list)
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(false, left_input, right_input, output, phi(x=X,j=j0))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Second constraint
left_input = var_dict['X^' + str(t)]
right_input_list = [\
[coefsp[t], var_dict['1']],\
[-1, var_dict['X^' + str(t - 1) + 'y1']],\
]
for i in range(1, t + 1):
if i == t - 1:
# This is included in y'.
pass
else:
right_input_list.append([coefsp[t + i], var_dict['X^' + str(i)]])
right_input = sum_vars(false, right_input_list)
output_list = [[-coefsp[0], var_dict['1']]]
for i in range(1, t - 1 + 1):
output_list.append([-coefsp[i], var_dict['X^' + str(i)]])
output = sum_vars(false, output_list)
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(false, left_input, right_input, output, X^(ell+1)/ell^s*phi(x=ell^s/X,j=j1))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
return constraints, variables, non_zero_entries
def special_7_over_fp():
ell = 7
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
constraints = 0
non_zero_entries = 0
t = (ell + 1) / 2
y0 = 1728 - j0
y1 = 1728 - j1
# We don't count the 1
variables = 2 * k + 2 * (k + 1)
# Variables for real part, variables for imaginary part, variables for sum
var_dict = {
'1': VARIABLE_ONE,
'X^1': { 'vars': X, 'no_vars_used': basis_sum },
'y0': { 'vars': y0, 'no_vars_used': basis_sum },
'y1': { 'vars': y1, 'no_vars_used': basis_sum }
}
# Store the powers of X
# Unlike in the simple approach, we also need X^{t - 1} in plain.
for i in range(2, t + 1):
variables += 2 * k
if i == 3:
var_dict['xX^2 + cX^3 + cX^4'] = {
'vars': X^4 + 14*X^3 + (14*14/4 + 63 - 14*14/4)*X^2,
'no_vars_used': basis_sum
}
else:
var_dict['X^' + str(i)] = {
'vars': X^i,
'no_vars_used': basis_sum
}
# Compute the powers of X, inclusive
for i in range(2, t + 1):
if i % 2 == 0:
# We can square
compute_from = var_dict['X^' + str(i / 2)]
output = var_dict['X^' + str(i)]
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(true, compute_from, output)
else:
assert i == 3
# Use the squaring trick for odd powers!
inputs = [\
[7, var_dict['X^1']],\
[1, var_dict['X^2']],\
]
inp = sum_vars(true, inputs)
output = sum_vars(true, [\
[1, var_dict['xX^2 + cX^3 + cX^4']],\
[-14, var_dict['X^2']],\
])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(true, inp, output)
# Update the variables
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Compute the crossterms with j
variables += 2 * k
var_dict['X^1y0'] = { 'vars': X*y0, 'no_vars_used': basis_sum }
left_hand_side = var_dict['X^1']
right_hand_side = var_dict['y0']
output = var_dict['X^1y0']
[n_constr, n_vars, nnnz] = assert_product(true, left_hand_side, right_hand_side, output)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += nnnz * k
variables += 2 * k
var_dict['1 + X^1 + X^2 + X^3j + X^3 + X^4'] = {
'vars': coefsp[t] - coefsp[3]/14 + coefsp[t + 1] * X^1 + coefsp[t + 2] * X^2 + coefsp[t + 3] * X^3 + coefsp[t + 4] * X^4 - j1*X^3,
'no_vars_used': basis_sum
}
left_hand_side = sum_vars(true, [\
[-9/2, var_dict['X^2']],\
[1/14, var_dict['xX^2 + cX^3 + cX^4']],\
[-1/14, var_dict['X^4']],
])
# Here we subtract the factor for X^7 away
right_hand_side = sum_vars(true, [\
[1, var_dict['y1']],\
[coefsp[t + 3] - 1728, var_dict['1']],\
])
output = sum_vars(true, [\
[-coefsp[t] + coefsp[3]/14, var_dict['1']],\
[-coefsp[t + 1], var_dict['X^1']],\
[-coefsp[t + 2], var_dict['X^2']],\
[-coefsp[t + 4], var_dict['X^4']],\
[1, var_dict['1 + X^1 + X^2 + X^3j + X^3 + X^4']],\
])
[n_constr, n_vars, nnnz] = assert_product(true, left_hand_side, right_hand_side, output)
constraints += n_constr * k
variables += n_vars * k
non_zero_entries += nnnz * k
# Build the last two constraints
# First constraint
inpt = sum_vars(true, [\
[-7, var_dict['1']],\
[70, var_dict['X^1']],\
[1, var_dict['xX^2 + cX^3 + cX^4']],\
])
output = scale_var(true, -1, var_dict['X^1y0'])
[new_constraints, new_no_variables, new_non_zero_entries] = assert_square(true, inpt, output, phi(x=X, j=j0))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
# Second constraint
left_input = var_dict['X^4']
right_input = var_dict['1 + X^1 + X^2 + X^3j + X^3 + X^4']
output_list = [
[-coefsp[0], var_dict['1']],\
[-coefsp[1], var_dict['X^1']],\
[-41564215210, var_dict['X^2']],\
[-coefsp[3]/14, var_dict['xX^2 + cX^3 + cX^4']],\
]
output = sum_vars(true, output_list)
[new_constraints, new_no_variables, new_non_zero_entries] = assert_product(true, left_input, right_input, output, X^(ell + 1)/ell^2 * phi(x=ell^s/X, j=j1))
constraints += new_constraints * k
variables += new_no_variables * k
non_zero_entries += new_non_zero_entries * k
return constraints, variables, non_zero_entries
def special_13_over_fp():
ell = 13
phi, s, coefs, coefsp = phi_coefs_for_ell(ell)
constraints = 0
non_zero_entries = 0
t = (ell + 1) / 2
y0 = j0
y1 = j1
# We don't count the 1
variables = 2 * k + 2 * (k + 1)
# Variables for real part, variables for imaginary part, variables for sum
var_dict = {
'1': VARIABLE_ONE,
'X^1': { 'vars': X, 'no_vars_used': basis_sum },
'y0': { 'vars': y0 - 4464, 'no_vars_used': basis_sum },
'y1': { 'vars': y1 - 4464, 'no_vars_used': basis_sum }
}
# Store the powers of X
# Unlike in the simple approach, we also need X^{t - 1} in plain.
for i in range(2, t + 1):
variables += 2 * k
if i == 5:
# 260*x^4 + 78*x^5 + 13*x^6
var_dict['xX^4 + cX^5 + cX^6'] = {
'vars': -13 + 143*X + 468*X^2 + 494*X^3 + 260*X^4 + 78*X^5 + 13*X^6 + X^7,
'no_vars_used': basis_sum
}