Skip to content

Commit ea05bcf

Browse files
authored
Merge pull request #180 from sumiya11/slowly-refactoring-trace
(Slowly) Refactoring F4 trace
2 parents 0d94c8b + d2f481c commit ea05bcf

21 files changed

+403
-422
lines changed

benchmark/CI-scripts/.runtests.jl.swp

-16 KB
Binary file not shown.

ext/GroebnerDynamicPolynomialsExt.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ function Groebner.io_convert_polynomials_to_ir(
2727
reverse!(coeffs[i])
2828
end
2929
end
30-
ring =
31-
Groebner.PolyRing(ring.nvars, Groebner.ordering_transform(ring.ord, var_to_index), ring.ch)
30+
ring = Groebner.PolyRing(
31+
ring.nvars,
32+
Groebner.ordering_transform(ring.ord, var_to_index),
33+
ring.characteristic
34+
)
3235
options.ordering = Groebner.ordering_transform(options.ordering, var_to_index)
3336
ring, monoms, coeffs, options
3437
end

scripts/trace_matrices.jl

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Revise, Groebner, AbstractAlgebra
2+
3+
# 1. Create the system
4+
k = GF(2^30 + 3)
5+
sys = sort(Groebner.Examples.katsuran(4, k=k), by=leading_monomial)
6+
7+
# 2. Learn the trace
8+
trace, gb_truth = groebner_learn(sys)
9+
10+
# 3. Get the F4 matrices
11+
matrices = Groebner.trace_export_matrices(trace.recorded_traces[UInt32])
12+
13+
# 4. Inspect the data structure
14+
function make_matrix(sys, matrix)
15+
x = gens(parent(sys[1]))
16+
make_poly(idx, mult) = sys[idx] * prod(x .^ mult)
17+
reducers = map(make_poly, matrix.reducers.index, matrix.reducers.multiplier)
18+
to_be_reduced = map(make_poly, matrix.to_be_reduced.index, matrix.to_be_reduced.multiplier)
19+
(reducers=reducers, to_be_reduced=to_be_reduced)
20+
end
21+
22+
matrix_1 = make_matrix(sys, matrices[1])
23+
@info "The first matrix:" matrix_1.reducers matrix_1.to_be_reduced
24+
25+
# 5. Construct a basis by reducing the matrices
26+
function follow_the_trace(sys, matrices)
27+
gb = sys
28+
for matrix in matrices
29+
matrix = make_matrix(gb, matrix)
30+
# Can be computed via lin-alg
31+
reduced = broadcast(AbstractAlgebra.normal_form, matrix.to_be_reduced, Ref(matrix.reducers))
32+
gb = vcat(gb, reduced)
33+
end
34+
gb
35+
end
36+
37+
gb = follow_the_trace(sys, matrices)
38+
39+
@assert groebner(gb) == gb_truth # autoreduce by calling `groebner`

src/Groebner.jl

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ end
7878
# Includes
7979

8080
include("utils/invariants.jl")
81+
include("utils/tuples.jl")
8182
include("utils/packed.jl")
8283

8384
# Test systems, such as katsura, cyclic, etc
@@ -135,6 +136,7 @@ include("reconstruction/ratrec.jl")
135136

136137
#= more high level functions =#
137138
include("groebner/modular.jl")
139+
include("groebner/wrapped_trace.jl")
138140
include("groebner/groebner.jl")
139141
include("groebner/groebner_with_change_matrix.jl")
140142
include("groebner/learn_apply.jl")

src/f4/basis.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ function basis_well_formed(ring::PolyRing, basis::Basis, hashtable::MonomialHash
170170
basis.monoms[i],
171171
lt=(j, k) -> monom_isless(hashtable.monoms[k], hashtable.monoms[j], ring.ord)
172172
) && error("Bad polynomial")
173+
iszero(basis.coeffs[i][1]) && error("Leading coefficient is zero")
173174
for j in 1:length(basis.coeffs[i])
174-
iszero(basis.coeffs[i][j]) && error("Coefficient is zero")
175175
(ring.ground == :zp) &&
176-
!(basis.coeffs[i][j] < ring.ch) &&
176+
!(-1 < basis.coeffs[i][j] < ring.characteristic) &&
177177
error("Coefficients must be normalized")
178178
(basis.monoms[i][j] > hashtable.load) && error("Bad monomial")
179179
end

src/f4/learn_apply.jl

+14-20
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function f4_initialize_structs_with_trace(
2626
make_monic=make_monic,
2727
sort_input=sort_input
2828
)
29-
trace = trace_initialize(ring, basis_deepcopy(basis), basis, hashtable, permutation, params)
29+
trace = trace_initialize(ring, basis, hashtable, permutation, params)
3030
trace, basis, pairset, hashtable, permutation
3131
end
3232

@@ -153,21 +153,17 @@ function f4_reducegb_learn!(
153153
trace.output_nonredundant_indices = copy(basis.nonredundant_indices[1:k])
154154
end
155155

156-
function f4_learn!(
157-
trace::Trace,
158-
ring::PolyRing,
159-
basis::Basis{C},
160-
pairset::Pairset,
161-
hashtable::MonomialHashtable{M},
162-
params::AlgorithmParameters
163-
) where {M <: Monom, C <: Coeff}
156+
function f4_learn!(trace::Trace, pairset::Pairset, params::AlgorithmParameters)
157+
ring = trace.ring
158+
basis = trace.gb_basis
159+
hashtable = trace.hashtable
160+
164161
@invariant basis_well_formed(ring, basis, hashtable)
165-
@invariant basis == trace.gb_basis
166162
@invariant params.reduced
167163

168164
basis_make_monic!(basis, params.arithmetic, params.changematrix)
169165

170-
matrix = matrix_initialize(ring, C)
166+
matrix = matrix_initialize(ring, eltype(eltype(basis.coeffs)))
171167
update_ht = hashtable_initialize_secondary(hashtable)
172168
symbol_ht = hashtable_initialize_secondary(hashtable)
173169

@@ -480,23 +476,21 @@ function f4_standardize_basis_in_apply!(ring::PolyRing, trace::Trace, arithmetic
480476
basis_make_monic!(basis, arithmetic, false)
481477
end
482478

483-
function f4_apply!(
484-
trace::Trace,
485-
ring::PolyRing,
486-
basis::Basis{C},
487-
params::AlgorithmParameters
488-
) where {C <: Coeff}
489-
# @invariant basis_well_formed(ring, basis, trace.hashtable)
479+
function f4_apply!(trace::Trace, params::AlgorithmParameters)
480+
ring = trace.ring
481+
basis = trace.buf_basis
482+
hashtable = trace.hashtable
483+
484+
@invariant basis_well_formed(ring, basis, trace.hashtable)
490485
@invariant params.reduced
491486

492487
iters_total = length(trace.matrix_infos) - 1
493488
iters = 0
494-
hashtable = trace.hashtable
495489

496490
basis_make_monic!(basis, params.arithmetic, params.changematrix)
497491

498492
symbol_ht = hashtable_initialize_secondary(hashtable)
499-
matrix = matrix_initialize(ring, C)
493+
matrix = matrix_initialize(ring, eltype(eltype(basis.coeffs)))
500494

501495
basis_update!(basis, hashtable)
502496

0 commit comments

Comments
 (0)