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
/*
TODO: impl the code below and optimize it
Example code for calculating the modular inverse of a matrix in Rust.
use nalgebra::*;
// maybe implement EEA here and use nalgebras gcd function
fn modular_inverse(x: f32, modulus: i32) -> Option<f32> {
for i in 1..modulus {
if (i as f32 * x % modulus as f32 + modulus as f32) % modulus as f32 == 1.0 {
return Some(i as f32);
}
}
None
}
fn minor(matrix: &DMatrix<f32>, row: usize, col: usize) -> f32 {
let submatrix = matrix.clone().remove_row(row).remove_column(col);
submatrix.determinant()
}
fn adjugate(matrix: &DMatrix<f32>) -> DMatrix<f32> {
let cofactor_matrix = matrix.map_with_location(|(i, j), _| {
let sign = if (i + j) % 2 == 0 { 1.0 } else { -1.0 };
sign * minor(matrix, i, j)
});
cofactor_matrix.transpose()
}
fn main() {
let m = Matrix3::new(
5.0, 17.0, 6.0,
2.0, 21.0, 14.0,
19.0, 3.0, 11.0,
);
let determinant = m.determinant() as f32; // Calculate the determinant
// Adjust the determinant for modulo 28
let det_mod: f32 = (determinant % 28.0 + 28.0) % 28.0;
dbg!(&determinant);
// Find the modular inverse of the determinant
match modular_inverse(det_mod, 28) {
Some(det_mod_inverse) => {
// Manually compute the adjugate matrix
let adjugate = Matrix3::new(
(m[(1, 1)] * m[(2, 2)] - m[(1, 2)] * m[(2, 1)]), - (m[(1, 0)] * m[(2, 2)] - m[(1, 2)] * m[(2, 0)]), (m[(1, 0)] * m[(2, 1)] - m[(1, 1)] * m[(2, 0)]),
- (m[(0, 1)] * m[(2, 2)] - m[(0, 2)] * m[(2, 1)]), (m[(0, 0)] * m[(2, 2)] - m[(0, 2)] * m[(2, 0)]), - (m[(0, 0)] * m[(2, 1)] - m[(0, 1)] * m[(2, 0)]),
(m[(0, 1)] * m[(1, 2)] - m[(0, 2)] * m[(1, 1)]), - (m[(0, 0)] * m[(1, 2)] - m[(0, 2)] * m[(1, 0)]), (m[(0, 0)] * m[(1, 1)] - m[(0, 1)] * m[(1, 0)]),
).transpose();
// Apply the modular inverse to the adjugate and then apply modulo 28
let modular_inverse = adjugate.map(|x| ((x * det_mod_inverse) % 28.0 + 28.0) % 28.0);
println!("Modular Inverse:\n{}", modular_inverse);
}
None => println!("Matrix is not invertible in mod 28"),
}
let m = Matrix3::new(
5.0, 17.0, 6.0,
2.0, 21.0, 14.0,
19.0, 3.0, 11.0,
);
let m = DMatrix::from_row_slice(3, 3, m.as_slice());
let adj = adjugate(&m);
println!("Adjugate Matrix:\n{}", &adj);
let adj_trans = adj.transpose();
println!("Transposed Adjugate Matrix:\n{}", &adj_trans);
let determinant = m.determinant() as f32; // Calculate the determinant
// Adjust the determinant for modulo 28
let det_mod: f32 = (determinant % 28.0 + 28.0) % 28.0;
dbg!(&determinant);
// Find the modular inverse of the determinant
match modular_inverse(det_mod, 28) {
Some(det_mod_inverse) => {
// Apply the modular inverse to the adjugate and then apply modulo 28
let modular_inverse = adj_trans.map(|x| ((x * det_mod_inverse) % 28.0 + 28.0) % 28.0);
println!("Modular Inverse:\n{}", modular_inverse);
}
None => println!("Matrix is not invertible in mod 28"),
}
}
*/
use crate;
use crate*;
use crateDecrypt;