/
dubey_holenstein.sage
2346 lines (2188 loc) · 75.9 KB
/
dubey_holenstein.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
# -*- mode: sage-shell:sage -*-
# Implementation of Dubey Holenstein method
# Note that they use the modern integral definition, not classically integral
# This makes this largely incompatible with Sage's Quadratic form definition?
# However Sage's Genus class is compatible.
# Did I mention how much I hate this subject?
#The normal form functions are copied from Brandon's code. Oh wait, those use Sage's definitions.. Adaptation time
import numpy
import sage.arith.misc
# This should not be necessary
def sqrt_mod_q(a, q):
p = prime_factors(q)[0]
if p==2:
return sqrt_mod_even(a,q)
else:
return sqrt_mod_primepow(a,p, q)
def sqrt_mod_primepow(a, p, q):
olda = a
a = a.lift()
if a==0:
return 0
k = valuation(q, p)
val = valuation(a,p)
if val %2 == 1:
assert False
retval = val/2
t = a/(p**val)
k = k-val
s = Mod(t, p).sqrt().lift()
for i in range(2, k+1):
corr = (s*s-t) %(p**i)
s -= (corr/(2*s)) % (p**i)
ret = Mod(s*p**(retval), q)
assert ret*ret == olda
return ret
def sqrt_mod_even(a, q):
olda = a
a = a.lift()
if a==0:
return 0
k = valuation(q, 2)
val = valuation(a, 2)
if val %2 == 1:
assert False
retval = val/2
t = a/(2**val)
if q<=8:
for i in range(0, 7):
if i*i % q ==a:
return Mod(i, q)
s = Mod(t, 8).sqrt().lift()
for i in range(4, k+1):
corr = (s*s-t) %(2**i)
s -= (corr/(2*s)) % (2**i)
ret = Mod(s*2**(retval), q)
assert ret*ret == olda
return ret
def twonf_with_change_vars_correct(Q):
#basically copied from the built-in function local_normal_form(), specialized to p=2, but it also computes a change-of-variables to the local normal form. it skips the "Cassels' proof" step. this is accounted for later.
I = list(range(Q.dim()))
P = identity_matrix(QQ,Q.dim())
Q_Jordan = DiagonalQuadraticForm(ZZ,[])
while Q.dim() > 0:
n = Q.dim()
(min_i,min_j) = Q.find_entry_with_minimal_scale_at_prime(2)
if min_i == min_j:
min_val = valuation(2 * Q[min_i,min_j],2)
else:
min_val = valuation(Q[min_i, min_j],2)
if (min_i == min_j):
block_size = 1
Q.swap_variables(0,min_i,in_place = True)
P.swap_rows(I[0],I[min_i])
else:
Q.swap_variables(0, min_i, in_place = True)
Q.swap_variables(1, min_j, in_place = True)
P.swap_rows(I[0],I[min_i])
P.swap_rows(I[1],I[min_j])
block_size = 2
min_scale = 2^(min_val)
if (block_size == 1):
a = 2 * Q[0,0]
for j in range(block_size, n):
b = Q[0, j]
g = GCD(a, b)
Q.multiply_variable(ZZ(a/g), j, in_place = True)
Q.add_symmetric(ZZ(-b/g), j, 0, in_place = True)
P.rescale_row(I[j],a/g)
P.add_multiple_of_row(I[j],I[0],-b/g)
NewQ = QuadraticForm(matrix([a]))
else:
a1 = 2*Q[0,0]
a2 = Q[0,1]
b2 = 2*Q[1,1]
big_det = (a1*b2 - a2^2)
small_det = big_det / (min_scale * min_scale)
for j in range(block_size,n):
a = Q[0,j]
b = Q[1,j]
Q.multiply_variable(big_det,j,in_place = True)
Q.add_symmetric(ZZ(-(a*b2 - b*a2)),j,0,in_place = True)
Q.add_symmetric(ZZ(-(-a*a2 + b*a1)),j,1,in_place = True)
Q.divide_variable(ZZ(min_scale^2),j,in_place = True)
P.rescale_row(I[j],big_det)
P.add_multiple_of_row(I[j],I[0],-a*b2+b*a2)
P.add_multiple_of_row(I[j],I[1],a*a2-b*a1)
P.rescale_row(I[j],min_scale^(-2))
Qgcd = gcd([Q[0,0],Q[0,1],Q[1,1]])
Qgcdsqf = Qgcd.squarefree_part()
fsq = ZZ(Qgcd / Qgcdsqf)
fsq = fsq / 2^(fsq.valuation(2))
f = ZZ(sqrt(Qgcd / Qgcdsqf))
f = f / 2^(f.valuation(2))
NewQ = QuadraticForm(matrix([[ZZ(2*Q[0,0]/fsq),ZZ(Q[0,1]/fsq)],[ZZ(Q[0,1]/fsq),ZZ(2*Q[1,1]/fsq)]]))
P.rescale_row(I[0],f^(-1))
P.rescale_row(I[0],f^(-1))
Q_Jordan = Q_Jordan + NewQ
for j in range(block_size):
I.remove(I[0])
Q = Q.extract_variables(range(block_size, n))
return [Q_Jordan,P]
def local_normal_form_with_change_vars(Q,p):
#basically copied from the built-in function local_normal_form(), but it also computes a change-of-variables to the local normal form
I = list(range(Q.dim()))
assert p != 2
M = identity_matrix(QQ,Q.dim())
Q_Jordan = DiagonalQuadraticForm(ZZ,[])
while Q.dim() > 0:
n = Q.dim()
(min_i, min_j) = Q.find_entry_with_minimal_scale_at_prime(p)
if min_i == min_j:
min_val = valuation(2 * Q[min_i, min_j], p)
else:
min_val = valuation(Q[min_i, min_j], p)
if (min_i == min_j):
Q.swap_variables(0, min_i, in_place = True)
M.swap_rows(I[0],I[min_i])
else:
Q.swap_variables(0, min_i, in_place = True)
Q.swap_variables(1, min_j, in_place = True)
M.swap_rows(I[0],I[min_i])
M.swap_rows(I[1],I[min_j])
Q.add_symmetric(1, 0, 1, in_place = True)
M.add_multiple_of_row(I[0],I[1],1)
min_scale = p ** min_val
a = 2 * Q[0,0]
for j in range(1, n):
b = Q[0, j]
g = GCD(a, b)
Q.multiply_variable(ZZ(a/g), j, in_place = True)
Q.add_symmetric(ZZ(-b/g), j, 0, in_place = True)
M.rescale_row(I[j],a/g)
M.add_multiple_of_row(I[j],I[0],-b/g)
Q_Jordan = Q_Jordan + Q.extract_variables(range(1))
I.remove(I[0])
Q = Q.extract_variables(range(1, n))
return [Q_Jordan,M]
def make_similar_quadratic_form(A):
M=Matrix(ZZ, A.dimensions()[0], A.dimensions()[1], 0)
for i in range(0, A.dimensions()[0]):
for j in range(0, i):
M[i,j]=A[i,j]+A[j,i]
M[j,i]=M[i,j]
for i in range(0, A.dimensions()[0]):
M[i,i] = 2*A[i,i]
return QuadraticForm(ZZ, M)
def locally_block_split(A, q):
r"""
Given a symmetric matrix $A$ with entries in $\mathbb{Z}$, return $U, D$ st $U^{T}AU=D \pmod{q}$ and $D$ is in blocks.
q must be a prime power.
"""
if q%2==0:
return locally_block_split_2adic(A, q)
else:
return locally_block_split_padic(A, q)
def locally_block_split_padic(A, q):
Q = make_similar_quadratic_form(A);
assert len(prime_factors(q))==1
p = prime_factors(q)[0]
[_,U]=local_normal_form_with_change_vars(Q, p)
return [U.transpose(), U*A*U.transpose()]
def locally_block_split_2adic(A, q):
Q = make_similar_quadratic_form(A);
assert len(prime_factors(q))==1
p = prime_factors(q)[0]
assert p == 2
[_,U]=twonf_with_change_vars_correct(Q)
return [U.transpose(), U*A*U.transpose()]
# Block splitting is all we need. Next we identify pieces, find a solution, apply U and then CRT to solve completely. I don't quite get how this works exactly.
# We just need one, so try every single splitting.
# Understand paper more: we want a primitive splitting, and I'd like to know if one exists or not
# The key is to look at the p-adic symbols. We store as pairs of (ord, symb) where ord is an integer representing the power of p^k
# symb is 1,3,5,7 for p=2, and 1 or -1 if p odd
def power_mod_form(q):
p = prime_factors(q)[0]
return (p, valuation(q, p))
def symbol_of(t, q):
t = Mod(t, q).lift()
p = prime_factors(q)[0]
if p==2:
ordert=valuation(t, p)
if t == 0:
symbolt = 0
else:
symbolt = (t//(p**ordert)) % 8
return (ordert, symbolt)
else:
ordert=valuation(t, p)
if t == 0:
symbolt = 0
else:
symbolt = kronecker_symbol(t//(p**ordert), p)
return ( ordert, symbolt)
def splitting_works(s1, s2, s3, q):
''' Returns True if there is are t, a, b in s1, s2, s3 st t=a+b, false if not '''
# Woops, what about 0?
# NB that s1 is \gamma, s2 is \gamma_1, s3 is \gamma_2 in paper notation
(val1, sym1)=s1
(val2, sym2)=s2
(val3, sym3)=s3
p = prime_factors(q)[0]
# Several special cases with infinity need to be handled
if val1 == Infinity:
if val2 != val3:
return False
else:
if val2 == Infinity:
return val3==Infinity
else:
## Mistake in paper: this requires paying attention to the symbol and paper doesn't.
if p==2:
return s2 == symbol_of((-(2**val3)*sym3)%q,q)
else:
return sym2 == kronecker_symbol(-1, p)*sym3
if val2 == Infinity:
if s1 == s3:
return True
else:
return False
if val3 == Infinity:
return s1 == s2
# Now we know that infinity isn't present
if val1==val2 and val1==val3:
if p == 2:
return False
else:
sols=p - (p %4) - (( sym2+sym3)*(kronecker_symbol(-1, p)*sym2+sym1))
return sols != 0
if val1 != val3:
#swap to simplify next case
(val3, val2) = (val2, val3)
(sym3, sym2) = (sym2, sym3)
# Casework from Lemma 21
gamma_3 = 0
if p != 2:
if val1<val2:
gamma_3 = s1
else:
gamma_3 = (val2, kronecker_symbol(-1, p)*sym2)
else:
if val1<val2:
magic_s_1 = (sym1 - (2 ** (val2-val1))*sym2)% 8
gamma_3 = (val1, magic_s_1)
else:
magic_s_2 = (sym1*(2**(val1-val2)) - sym2)% 8
gamma_3 = (val2, magic_s_2)
return gamma_3 == (val3, sym3)
def sample_from_squareclass(s1, q):
p = prime_factors(q)[0]
ordq = valuation(q, p)
if s1[0] == Infinity:
return 0
if p == 2:
return (p**s1[0]*s1[1])%q
else:
t = 0
while (kronecker_symbol(t, p)!=s1[1]):
t = randrange(1, p)
return (p**s1[0]*t)%q
def find_split(t, s1, s2, q):
''' Returns (a,b) with a inhabiting symbol s1, b inhabiting symbol s2 and t=a+b mod q'''
#Insert test assertions
if not splitting_works(symbol_of(t,q), s1, s2, q):
assert False
# Now what to do? Paper is somewhat unclear, but when valuations different any work
p = prime_factors(q)[0]
valt = valuation(t, p)
if valt != s1[0]:
a = sample_from_squareclass(s1, q)
return (a, (t-a)%q)
elif valt != s2[0]:
a = sample_from_squareclass(s2, q)
return ((t-a)%q, a)
# Valuations same we apply lemma 26
if valt == Infinity:
return (0, 0)
valc = s1[0]
valq = valuation(q, p)
crpt = t/p**valt
done = False
while not done:
tau_1 = 0
while gcd(tau_1, p) != 1:
tau_1 = randrange(1, p**(valq-valc))
tau_2 = (crpt - tau_1) %q
if symbol_of(tau_1*p**valc, q) == s1 and symbol_of(tau_2*p**valc, q) == s2:
done = True
return (tau_1*p**valc, tau_2*p**valc)
# How to think about blocks? Starting point+type? Or by coeffs?
# Here by coeffs. s is a type, t is an actual number
# Possible refactoring
# For type I the condition is simple. Solving also easy. So why break up types?
def solveable_type_I(s, block, q):
p = prime_factors(q)[0]
ordb = valuation(block, p)
ords = s[0]
if ordb == Infinity:
if ords == Infinity:
return True
else:
return False
if ords == Infinity:
return True #Check
if ords - ordb >= 0 and (ordb-ords) %2 ==0:
if p != 2:
return s[1] ==symbol_of(block, q)[1]
else:
nmod = min(8, 2**(valuation(q, 2)-s[0]))
return s[1]% nmod == symbol_of(block, q)[1]% nmod
else:
return False
def solveable_type_I_prim(s, block, q):
return s == symbol_of(block, q)
def solution_type_I(t, block, q):
if not solveable_type_I(symbol_of(t,q), block, q):
assert False
p = prime_factors(q)[0]
ordb = valuation(block, p)
ordt = valuation(t, p)
if ordt == Infinity:
return 0
tprime = t/(p**(ordt))
blockprime = block/(p**ordb)
if p == 2:
k = valuation(q, 2)
littlemod = 2**(k-ordt)
y=(Mod(tprime, littlemod)/Mod(blockprime, littlemod))
y = sqrt_mod_q(y, littlemod).lift()
sqrp = y
else:
sqrp=(Mod(tprime,q)/Mod(blockprime,q))
sqrp = sqrt_mod_q(sqrp, q).lift()
ans = sqrp * p**((ordt-ordb)/2)
if Mod(ans, q)**2*Mod(block, q) != Mod(t, q):
assert False
return ans
# The paper has a complicated counting algorithm that determines both of these together recusively
# important: we write our b as the coefficient of the quadratic form, not matrix element i.e. 2x matrix element
# Need to make lots of tests
def count_prim_nonprim_type_II(s, a, b, c, q):
lp1 = valuation(b, 2) #This line is confusing to me also. Results from choice above
k = valuation(q, 2)
if lp1 >= k:
if s[0] >= k:
nonprim = 4**(k-1)
prim = 4**k - nonprim
return (prim, nonprim)
else:
return (0, 0)
elif s[0]<lp1:
return (0,0)
else:
prim = 0
nonprim = 0
k = k -lp1
s = (s[0]-lp1, s[1])
a = a/(2**lp1)
b = b/(2**lp1)
c = c/(2**lp1)
# Now need to reconstruct s mod 2 for this next step
if s[0] == Infinity:
sm = 0
else:
sm = (s[1]*2**s[0])
for pair in [(0,1), (1, 0), (1, 1)]:
(a1, a2) = pair
if Mod(a*a1**2+b*a1*a2+c*a2**2, 2) == Mod(sm, 2):
prim += 2**(k-1)
if sm % 4 == 0:
if k == 1:
nonprim += 1
else:
(n1,n2) = count_prim_nonprim_type_II((s[0]-2, s[1]), a,b,c, 2**(k-2))
nonprim += nonprim +4*(n1+n2)
prim = 4**(lp1)*prim
nonprim = 4**(lp1)*nonprim
return (prim, nonprim)
def solveable_type_II(s, a, b, c, q):
(n1, n2)=count_prim_nonprim_type_II(s, a, b, c, q)
return n1+n2>0
def solveable_type_II_prim(s, a, b, c, q):
(n1, n2)=count_prim_nonprim_type_II(s, a, b, c, q)
return n1>0
def solution_type_II(t, a, b, c, q):
solution=(0,0)
if q == 1:
return (0, 0)
sym = symbol_of(t, q)
assert solveable_type_II(symbol_of(t, q), a, b, c, q)
if solveable_type_II_prim(symbol_of(t, q), a,b, c,q):
return solution_type_II_prim(t, a, b, c, q)
else:
# It isn't primitive solveable because we need to scale the solution
assert Mod(q, 4) == 0
assert Mod(t, 4) == 0
(a1, a2)= solution_type_II(t/4, a, b, c, q/4)
solution = (a1*2, a2*2)
z = solution[0]
y = solution[1]
assert Mod(a*z*z+b*z*y+c*y*y, q)==Mod(t, q)
return solution
def solution_type_II_prim(t, a, b, c, q):
assert solveable_type_II_prim(symbol_of(t, q), a, b, c,q)
solution=(0,0)
lp1 = valuation(b, 2)
if lp1 >= valuation(q,2):
if valuation(t, 2) >=lp1:
solution = (0,1) #Will solve equation, is primative
else:
assert False #Unsolveable case
else:
#rescale
solution=(0,0)
tp = t/2**lp1
ap = a/2**lp1
bp = b/2**lp1
cp = c/2**lp1
exp = valuation(q,2)-lp1
#Find starting solution
for tmp in [(0,1), (1, 1), (1, 0)]:
(a1, a2) = tmp
if Mod(ap*a1**2+bp*a1*a2+cp*a2**2, 2) == Mod(tp,2):
solution = tmp
if solution==(0,0):
assert False
#Henselize
for i in range(1, exp+1):
(y1, y2) = solution
b1 = 0
b2 = 0
if Mod(y1, 2) == 1:
b1 = 0
b2 = Mod(((tp - (ap*y1*y1+bp*y1*y2+cp*y2*y2))/(bp*2**i)),2).lift()
else:
b2 = 0
b1 = Mod(((tp - (ap*y1*y1+bp*y1*y2+cp*y2*y2))/(bp*2**i)),2).lift()
solution=(y1+b1*(2**i), y2+b2*(2**i))
z1 = solution[0]
z2 = solution[1]
z = solution[0]
y = solution[1]
assert Mod(a*z*z+b*z*y+c*y*y, q) == Mod(t, q)
return solution
def random_test_type_II(q,lbound, coeffsize):
# Create random type II block
# Random x, y
# Then ask for solution
l=randrange(0, lbound)
a=randrange(0, coeffsize)
b=randrange(0, coeffsize)
c=randrange(0, coeffsize)
b=2*b+1
a=a*(2**l)
b=b*(2**l)
c=c*(2**l)
x=randrange(0, coeffsize*2**l)
y=randrange(0, coeffsize*2**l)
t=a*x*x+b*x*y+c*y*y
t=Mod(t, q).lift()
assert solveable_type_II(symbol_of(t, q), a,b,c, q)
solution=solution_type_II(t, a, b, c, q)
z = solution[0]
w = solution[1]
assert Mod(a*z*z+b*w*z+c*w*w,q)==Mod(t, q)
def random_test_find_split(q, coeffsize):
a = randrange(0, coeffsize)
b = randrange(0, coeffsize)
t = (a+b)% q
s = symbol_of(t, q)
s1 = symbol_of(a, q)
s2 = symbol_of(b, q)
assert splitting_works(s, s1, s2, q)
(c, d)=find_split(t, s1, s2, q)
assert symbol_of(c,q)==s1
assert symbol_of(d,q)==s2
assert Mod(c+d, q) == t
def random_test_block_I(q, coeffsize):
block = randrange(0, coeffsize)
x = randrange(0, coeffsize)
val = Mod(block*x*x, q).lift()
y = solution_type_I(val, block, q)
assert Mod(block*y*y, q)==Mod(val, q)
# The algorithm is now clear: search for a splitting that works, then go through and carry out with actual t, accumulate results
# We take each block, do it mod q, lift, and then represent the form as a list of blocks
# Then we apply dynamic programing to find a possible set of splits
# Then we find splits and representations, and put together to get a vector
# Lastly we apply U in reverse and take mod q
# And then do a final check
def find_modq_representation(A, t,q):
tmp=locally_block_split(A, q)
U=tmp[0]
D=tmp[1]
summands = form_into_subblocks(D,q)
typelist=find_modq_rep_types_recurse(summands, symbol_of(t,q), q)
if typelist == False:
assert False
vec = find_modq_rep_sum(summands,typelist,t,q)
assert Mod(vec.inner_product(D*vec),q) ==Mod(t, q)
ans=U*vec
assert Mod(ans.inner_product(A*ans), q)==Mod(t, q)
return ans
def is_modq_representable(A, t, q):
return is_modq_rep_type(A, symbol_of(t, q), q)
def is_modq_rep_type(A, sym, q):
tmp=locally_block_split(A, q)
U=tmp[0]
D=tmp[1]
summands = form_into_subblocks(D,q)
typelist=find_modq_rep_types_recurse(summands, sym, q)
if typelist == False:
return False
else:
return True
def form_into_subblocks(A, q):
n = A.dimensions()[0]
blocklist = list() # Blocklist has list either of tuples of 1 number or 3
i = 0
while i<n:
a=Mod(A[i,i],q).lift()
if i<n-1:
b=Mod(2*A[i,i+1],q).lift()
if b != 0:
c=Mod(A[i+1, i+1],q).lift()
blocklist.append([a, b,c])
i+=2
else:
blocklist.append([a])
i+=1
else:
blocklist.append([a])
i+=1
return blocklist
def solveable_block_any(block, symb, q):
if len(block)==1:
return solveable_type_I(symb, block[0], q)
else:
return solveable_type_II(symb, block[0], block[1], block[2], q)
def solveable_block_prim(block, symb, q):
if len(block)==1:
return solveable_type_I_prim(symb, block[0], q)
else:
return solveable_type_II_prim(symb, block[0], block[1], block[2], q)
#Todo: make this greedy and solve with as few blocks as possible
# Figure out speed?
def solution_symbol_prim(Q, s, q):
p=prime_factors(q)[0]
exp = valuation(q, p)
allowed_symbols=list()
allowed_symbols.append((Infinity, 0))
for i in range(0, exp):
if p == 2:
allowed_symbols.append((i, 1))
allowed_symbols.append((i, 3))
allowed_symbols.append((i, 5))
allowed_symbols.append((i, 7))
else:
allowed_symbols.append((i, 1))
allowed_symbols.append((i, -1))
if len(Q)==1:
if solveable_block_prim(Q[0], s, q):
return [(s, "end")]
else:
return False
else:
for ext in allowed_symbols:
if solveable_block_prim(Q[0], ext, q):
for res in allowed_symbols:
if splitting_works(s, ext, res, q):
recurse = solution_symbol_any(Q[1:], res, q)
if recurse != False:
recurse.insert(0, (ext, res))
return recurse
for ext in allowed_symbols:
if solveable_block_any(Q[0], ext, q):
for res in allowed_symbols:
if splitting_works(s, ext, res, q):
recurse = solution_symbol_prim(Q[1:], res, q)
if recurse != False:
recurse.insert(0, (ext, res))
return recurse
return False
def solution_symbol_any(Q, s, q):
p=prime_factors(q)[0]
exp = valuation(q, p)
allowed_symbols=list()
allowed_symbols.append((Infinity, 0))
for i in range(0, exp):
if p == 2:
allowed_symbols.append((i, 1))
allowed_symbols.append((i, 3))
allowed_symbols.append((i, 5))
allowed_symbols.append((i, 7))
else:
allowed_symbols.append((i, 1))
allowed_symbols.append((i, -1))
if len(Q)==1:
if solveable_block_any(Q[0], s, q):
return [(s, "end")]
else:
return False
else:
for ext in allowed_symbols:
if solveable_block_any(Q[0], ext, q):
for res in allowed_symbols:
if splitting_works(s, ext, res, q):
recurse = solution_symbol_any(Q[1:], res, q)
if recurse != False:
recurse.insert(0, (ext, res))
return recurse
return False
def find_modq_rep_types_recurse(Q, symb, q):
prim = solution_symbol_prim(Q, symb, q)
if prim == False:
return prim
prim.insert(0, ("empty symbol", symb))
return prim
#Test these, adapt rest of program
def find_modq_rep_types(Q, t,q):
p=prime_factors(q)[0]
exp = valuation(q, p)
allowed_symbols=list()
allowed_symbols.append((Infinity, 0))
for i in range(0, exp):
if p == 2:
allowed_symbols.append((i, 1))
allowed_symbols.append((i, 3))
allowed_symbols.append((i, 5))
allowed_symbols.append((i, 7))
else:
allowed_symbols.append((i, 1))
allowed_symbols.append((i, -1))
summand_count = len(Q)
primitive_solutions = list()
imprimitive_solutions = [[("empty symbol",symbol_of(t, q))]]
# Redo this as DFS
# Just use a helper function for it that extends solutions either making them primative
# or not
# two helper functions
# Simple recusion, just write it: recursively solving the problem for a split or not
for i in range(0, summand_count):
# Update the current solution lists by seeing if they can extend
# (Think through definition and edge and invariants)
# Each solution is a list of pairs. The first pair is special
# The i+1 pair (ext, res) is something so that res[i] splits into ext and res
# and ext satisfies the Q[i] block
# final res is special
new_prim_sol = list()
new_imprim_sol = list()
for solution in primitive_solutions:
if i<summand_count-1:
tail=solution[i][1]
for ext in allowed_symbols:
for res in allowed_symbols:
if splitting_works(tail, ext, res, q) and solveable_block_any(Q[i], ext, q):
new_prim_sol.append(solution+[(ext, res)])
else:
tail = solution[i][1]
if solveable_block_any(Q[i],tail, q):
new_prim_sol.append(solution+[(tail, "end")])
for solution in imprimitive_solutions:
if i< summand_count-1:
tail=solution[i][1]
for ext in allowed_symbols:
for res in allowed_symbols:
if splitting_works(tail, ext, res, q) and solveable_block_prim(Q[i], ext, q):
new_prim_sol.append(solution+[(ext, res)])
elif splitting_works(tail, ext, res, q) and solveable_block_any(Q[i], ext, q):
new_imprim_sol.append(solution+[(ext, res)])
else:
tail = solution[i][1]
if solveable_block_prim(Q[i], tail, q):
new_prim_sol.append(solution+[(ext, res)])
primitive_solutions = new_prim_sol
imprimitive_solutions = new_imprim_sol
return primitive_solutions[0]
def solve_block(block, t, q):
if len(block)==1:
return [solution_type_I(t, block[0], q)]
else:
return solution_type_II(t, block[0], block[1], block[2], q)
def find_modq_rep_sum(Q, typelist, t, q):
n = len(Q)
ans = list()
curr = t
assert symbol_of(curr, q) == typelist[0][1]
for i in range(1, n+1):
if i<n:
here,curr = find_split(curr, typelist[i][0], typelist[i][1], q)
else:
here = curr
ans+=solve_block(Q[i-1], here, q)
return vector(ZZ, ans)
# This tests above through random generation again
def test_find_modq_representation(q, n,coeffsize):
p = prime_factors(q)[0]
# Form random A
A = Matrix(ZZ, n, n,0)
for i in range(0, n):
A[i,i]=randrange(0, coeffsize)
for j in range(i+1, n):
A[i,j]=randrange(0, coeffsize)
A[j, i]=A[i,j]
# Random vector
v = vector(ZZ, n)
for i in range(0, n):
v[i] = randrange(0, coeffsize)
if v[0] % p == 0:
v[0] = 1
# Compute result
t = v.inner_product(A*v)
t = t %q
ans = find_modq_representation(A, t, q)
assert Mod(ans.inner_product(A*ans),q)==Mod(t, q)
# Now some CRT to deal with non prime power q
def p_part(m, p):
return p**(valuation(m, p))
def find_modular_representation(A, t, m): #THis needs testing
primes = prime_factors(m)
residues = list()
moduli = list()
n=A.dimensions()[0]
retval = vector(ZZ, n)
for i in range(0,n):
residues.insert(i, list())
moduli.insert(i,list())
for p in primes:
vec = find_modq_representation(A, t, p_part(m, p))
for i in range(0, n):
residues[i].append(vec[i]%(p_part(m,p)))
moduli[i].append(p_part(m,p))
for i in range(0, n):
retval[i]=crt(residues[i], moduli[i])
assert retval.inner_product(A*retval)%m==t%m
return retval
def test_modular_representation(n, modulus, coeffsize):
# Form random A
A = Matrix(ZZ, n, n,0)
for i in range(0, n):
A[i,i]=randrange(0, coeffsize)
for j in range(i+1, n):
A[i,j]=randrange(0, coeffsize)
A[j, i]=A[i,j]
# Random vector
v = vector(ZZ, n)
for i in range(0, n):
v[i] = randrange(0, coeffsize)
v[0]=1 #Give right gcd
# Compute result
t = v.inner_product(A*v)
t = t %modulus
ans = find_modular_representation(A, t, modulus)
assert Mod(ans.inner_product(A*ans),modulus)==Mod(t, modulus)
# Normalization of quadratic forms
# Think about strategy here
# P-adic normalization is mostly diagonalization followed by sorting by p-power (might be done?)
# and then combining the nonresidue bits in each scale compartment (bit tricky)
# We need some helpers for local transformation
def distinguished_nonsquare(p):
P = Primes()
i=2
while kronecker_symbol(i, p) == 1:
i = P.next(i)
return i
def odd_p_normalization_from_diagonal(D,q):
''' Returns U, T st. U.transpose()*D*U=T, and T is in the p-adic normal form'''
#We can just sweep down and write in the U?
# Or are they best as compositions
# The sorting by scales is done
p = prime_factors(q)[0]
L = Matrix(IntegerModRing(q), D) #local copy to check at end
n = D.dimensions()[0]
U = Matrix(IntegerModRing(q), n,n, 1)
#First normalize the diagonal
Ut = Matrix(IntegerModRing(q), n, n, 0)
for i in range(0, n):
num=L[i,i].lift()
scale=valuation(num, p)
red = num/(p**scale)
if kronecker_symbol(red,p) == -1:
red = Mod(red,q)/Mod(distinguished_nonsquare(p), q)
Ut[i,i]=sqrt_mod_q(Mod(red, q)**(-1),q)
L=Ut.transpose()*L*Ut
U=U*Ut
#Not done yet: need to walk things down the scale
for i in range(0, n-1):
num = L[i,i].lift()
scale = valuation(num, p)
nnum = L[i+1, i+1].lift()
nscale = valuation(nnum, p)
Ut = Matrix(IntegerModRing(q), n, n, 1)
if scale == nscale:
rednum = num/(p**scale)
rednnum = nnum/(p**scale)
if rednum == distinguished_nonsquare(p):
if rednnum == 1:
Ut[i, i] = 0
Ut[i+1, i+1] = 0
Ut[i, i+1] = 1
Ut[i+1, i] = 1
else:
A = odd_q_normalization_helper(q)
Ut[i,i] = A[0,0]
Ut[i, i+1] = A[0, 1]
Ut[i+1, i] = A[1,0]
Ut[i+1, i+1] = A[1,1]
U=U*Ut
L = Ut.transpose()*L*Ut
assert gcd(Ut.determinant().lift(),q)==1
return U, L
def odd_q_normalization_helper(q):
# solution: (a^2+c^2)*r=1
# b = c
# d = -a
#standard 2x2 matrix equation
p = prime_factors(q)[0]
A = Matrix(ZZ, 2, 2, [[distinguished_nonsquare(p), 0], [0, distinguished_nonsquare(p)]])
(a, c) = find_modq_representation(A, 1, q)
return Matrix(IntegerModRing(q), 2, 2, [a, c, c, -a])
# 2-adic case is much more complex
# Helper functions:
def twoadic_adjacent_blocks(A, ind1, ind2):
pass
def twoadic_blocktype(A, ind):
pass
def twoadic_same_scale(A, ind1, ind2):
pass
# Normalize Type I and Type II blocks
def normalization_factor_type_I(block,q):
val = valuation(block, 2)
res = block/2**val
typus = res % 8
fixer = typus/res
return sqrt_mod_q(Mod(fixer,q),q)
#Nota bena, uses matrix entries not the form coefficients
def normalization_matrix_type_II(a,b,c,q):
scale = valuation(b, 2)
a = a/(2**scale)
b = b/(2**scale)
c = c/(2**scale)
s = q
q = 2*q
ourmat = Matrix(IntegerModRing(q), 2, 2, [a, b, b, c])
lamb = ourmat.determinant().lift()%8
vec = find_modq_representation(ourmat.lift(), 2, q)
if vec[0] %2 == 0:
T = Matrix(IntegerModRing(q), 2, 2, [[0, 1], [1, 0]])
x1 = vec[1]
x2 = vec[0]
else:
T = Matrix(IntegerModRing(q), 2, 2, 1)
x1 = vec[0]
x2 = vec[1]
U = Matrix(IntegerModRing(q), 2, 2, [x1, 0, x2, Mod(1/x1,q)])
G=T*U
ndet=(G.transpose()*ourmat*G).determinant()
xfix = sqrt_mod_q(lamb/ndet,q)
V = Matrix(IntegerModRing(q), 2, 2, [1, 0, 0, xfix])
G = G*V
S = G.transpose()*ourmat*G
W = Matrix(IntegerModRing(q), 2, 2, [1, Mod((1-S[0,1].lift())/2, q),0, 1])
G = G*W
checkmat=G.transpose()*ourmat*G
checkmat=Matrix(IntegerModRing(s), matlift(checkmat))
assert checkmat.determinant().lift() %8 == ourmat.determinant().lift()%8
assert isplusminus(checkmat)
return G
def isplusminus(mat):
if mat[0,0]==2 and mat[1,0]==1 and mat[0,1]==1:
return (mat[1,1] == 4 or mat[1,1]==2)
else:
return False
def matlift(mat):
M=Matrix(ZZ, mat.dimensions()[0], mat.dimensions()[1],0)
for i in range(0,mat.dimensions()[0]):
for j in range(0,mat.dimensions()[1]):
M[i,j]=mat[i,j].lift()
return M
#TODO: write the global part that normalizes all blocks
# Break scales to either by all type I or all type II
# First the local part where we break a type I+type II sum
def find_local_scalebreak(tau, a, b, c, q):
scale = valuation(tau, 2)
tau = tau/(2**scale)
a = a/(2**scale)
b = b/(2**scale)
c = c/(2**scale)
# we should check block formedness. Assume normalized
ourmat = Matrix(IntegerModRing(q), 3,3, [tau, 0, 0, 0, a, b, 0, b, c])
oldmat = ourmat
U=Matrix(IntegerModRing(q), 3, 3, 0)
if c==4:
V=normalization_matrix_type_II(8,1,2, q)
V=V.inverse()
U=Matrix(IntegerModRing(q), 3, 3, [1,0,0, 0, V[0,0], V[0,1], 0, V[1,0], V[1,1]])
else:
U=Matrix(IntegerModRing(q), 3, 3, 1)
ourmat = U.transpose()*ourmat*U
r = -ourmat[0,0]/(ourmat[1,1]+1)
V = Matrix(IntegerModRing(q), 3, 3, [[1,1,1], [r, 1, 0], [0,1,1]])
G = U*V
ourmat = V.transpose()*ourmat*V
W = locally_block_split_2adic(matlift(ourmat),q)[0]
W = Matrix(IntegerModRing(q), W)
G = G*W
ourmat = W.transpose()*ourmat*W
# We don't need to normalize split pieces as compartment will take care of it
return G
# And now the global part that applies above repeatedly to a scale
# Sign walk
# We only need to put sign into or out of a compartment at the ends as canonicalization step