-rw-r--r-- 1247 cryptattacktester-20231020/parity.cpp raw
#include "parity.h"
#include "ram.h"
#include "bit_vector.h"
using namespace std;
bit parity_known(vector<bit> &s, vector<vector<bit>> &H, vector<vector<bit>> &map, const bit known)
{
s.insert(s.begin(), known);
for (bigint i = 0; i < H.size(); i++)
H.at(i).insert(H.at(i).begin(), bit(1));
// add other rows to the first row
const bigint R = s.size();
const bigint N = H.size();
const bigint K = N-R;
for (bigint j = 1; j < R; j++)
s.at(0) ^= s.at(j);
for (bigint i = 0; i < K+1; i++)
for (bigint j = 1; j < R; j++)
H.at(i).at(0) ^= H.at(i).at(j);
for (bigint i = K+1; i < N; i++)
H.at(i).at(0) = bit(0);
// find first 1 of the first row, swap columns
vector<bit> row(0);
for (bigint i = 0; i < K; i++)
row.push_back(H.at(i).at(0));
vector<bit> idx = bit_vector_first_one(row);
H.at(K) = ram_read_write(H, 0, K, idx, H.at(K));
map.at(K) = ram_read_write(map, 0, K, idx, map.at(K));
// conditionally add the first row to other rows
for (bigint j = 1; j < R; j++)
s.at(j) ^= s.at(0) & H.at(K).at(j);
for (bigint i = 0; i < K; i++)
for (bigint j = 1; j < R; j++)
H.at(i).at(j) ^= H.at(i).at(0) & H.at(K).at(j);
for (bigint j = 1; j < R; j++)
H.at(K).at(j) = bit(0);
return H.at(K).at(0);
}