-rw-r--r-- 2417 cryptattacktester-20231020/column_swaps.h raw
#ifndef COLUMN_SWAPS_H
#define COLUMN_SWAPS_H
#include "bigint.h"
#include "permutation.h"
#include "bit_matrix.h"
static bit column_swaps(vector<bit> &s_prime, vector<vector<bit>> &H_prime, vector<vector<bit>> &column_map,
bigint N,
bigint K,
bigint L,
bigint X,
bigint Y
)
{
const bigint R = N - K;
const bigint KK = K + L;
const bigint RR = N - KK;
bit success;
// swapping X columns while maintaining "systematic form"
for (bigint t = 0; t < 2; t++)
{
// permuting entries in column_map
permutation pi_KK(KK);
permutation pi_RR(RR);
pi_KK.permute(column_map);
pi_RR.permute(column_map,KK);
// permuting rows and columns of H_prime
pi_KK.permute(H_prime);
for (bigint i = 0; i < KK; i++)
pi_RR.permute(H_prime.at(i),L);
// permuting rows of s_prime
pi_RR.permute(s_prime,L);
if (t == 1)
break;
// gaussian elimination on x rows
vector<vector<bit>> m;
for (bigint i = 0; i < KK + X; i++)
m.push_back(bit_vector_extract(H_prime.at(i), L, L + X));
m.push_back(bit_vector_extract(s_prime, L, L + X));
vector<vector<bit>> pivots(0);
success = bit_matrix_reduced_echelon(pivots, m, Y);
for (bigint i = 0; i < X; i++)
s_prime.at(i + L) = m.back().at(i);
m.pop_back();
for (bigint i = 0; i < KK + X; i++)
for (bigint j = 0; j < X; j++)
H_prime.at(i).at(j + L) = m.at(i).at(j);
// swapping columns
for (bigint i = 0; i < X; i++)
{
H_prime.at(KK + i) = ram_read_write(H_prime, i, Y, pivots.at(i), H_prime.at(KK + i));
column_map.at(KK + i) = ram_read_write(column_map, i, Y, pivots.at(i), column_map.at(KK + i));
}
// maintaining systematic form
for (bigint x = 0; x < X; x++)
for (bigint i = 0; i < KK; i++)
{
for (bigint j = 0; j < L; j++) H_prime.at(i).at(j) ^= H_prime.at(i).at(x + L) & H_prime.at(x + KK).at(j);
for (bigint j = L+X; j < R; j++) H_prime.at(i).at(j) ^= H_prime.at(i).at(x + L) & H_prime.at(x + KK).at(j);
}
for (bigint x = 0; x < X; x++)
{
for (bigint j = 0; j < L; j++) s_prime.at(j) ^= s_prime.at(x + L) & H_prime.at(x + KK).at(j);
for (bigint j = L+X; j < R; j++) s_prime.at(j) ^= s_prime.at(x + L) & H_prime.at(x + KK).at(j);
}
for (bigint x = KK; x < KK + X; x++)
{
for (bigint j = 0; j < L; j++) H_prime.at(x).at(j) = bit(0);
for (bigint j = L+X; j < R; j++) H_prime.at(x).at(j) = bit(0);
}
}
return success;
}
#endif