/* * BitMatrix.hpp * */ #ifndef OT_BITMATRIX_HPP_ #define OT_BITMATRIX_HPP_ #include "BitMatrix.h" #include "Tools/BitVector.h" template size_t Matrix::vertical_size() { return squares.size() * U::N_ROWS; } template bool Matrix::operator==(Matrix& other) { if (squares.size() != other.squares.size()) throw invalid_length("Matrix"); for (size_t i = 0; i < squares.size(); i++) if (not(squares[i] == other.squares[i])) return false; return true; } template bool Matrix::operator!=(Matrix& other) { return not (*this == other); } template void Matrix::randomize(PRNG& G) { for (size_t i = 0; i < squares.size(); i++) squares[i].randomize(G); } template void Matrix::randomize(int row, PRNG& G) { for (size_t i = 0; i < squares.size(); i++) squares[i].randomize(row, G); } template void Matrix::print_side_by_side(Matrix& other) { for (int i = 0; i < 32; i++) { for (int j = 0; j < 64; j++) cout << squares[0].get_bit(i,j); cout << " "; for (int j = 0; j < 64; j++) cout << other.squares[0].get_bit(i,j); cout << endl; } } template void Matrix::print_conditional(BitVector& conditions) { for (int i = 0; i < 32; i++) { if (conditions.get_bit(i)) for (int j = 0; j < 65; j++) cout << " "; for (int j = 0; j < 64; j++) cout << squares[0].get_bit(i,j); if (!conditions.get_bit(i)) for (int j = 0; j < 65; j++) cout << " "; cout << endl; } } template void Matrix::pack(octetStream& os) const { for (size_t i = 0; i < squares.size(); i++) squares[i].pack(os); } template void Matrix::unpack(octetStream& os) { for (size_t i = 0; i < squares.size(); i++) squares[i].unpack(os); } template Slice::Slice(U& bm, size_t start, size_t size) : bm(bm), start(start) { end = start + size; if (end > bm.squares.size()) { stringstream ss; ss << "Matrix slice (" << start << "," << end << ") larger than matrix (" << bm.squares.size() << ")"; throw invalid_argument(ss.str()); } } template Slice& Slice::rsub(Slice& other) { if (bm.squares.size() < other.end) throw invalid_length("rsub"); for (size_t i = other.start; i < other.end; i++) bm.squares[i].rsub(other.bm.squares[i]); return *this; } template Slice& Slice::sub(BitVector& other, int repeat) { if (end * U::PartType::n_columns() > other.size() * repeat) throw invalid_length(to_string(U::PartType::n_columns())); for (size_t i = start; i < end; i++) { if (repeat > 0) bm.squares[i].sub(other.get_ptr_to_byte(i / repeat, U::PartType::n_row_bytes())); else bm.squares[i].bit_sub(other, i * U::PartType::n_rows_allocated()); } return *this; } template void Slice::randomize(int row, PRNG& G) { for (size_t i = start; i < end; i++) bm.squares[i].randomize(row, G); } template void Slice::conditional_add(BitVector& conditions, U& other, bool useOffset) { for (size_t i = start; i < end; i++) bm.squares[i].conditional_add(conditions, other.squares[i], useOffset * i); } template template void Slice::print() { cout << "hex / value" << endl; for (int i = 0; i < 16; i++) { cout << T(bm.squares[0].rows[i]) << endl; } cout << endl; } template void Slice::pack(octetStream& os) const { os.reserve(U::PartType::size() * (end - start)); for (size_t i = start; i < end; i++) bm.squares[i].pack(os); } template void Slice::unpack(octetStream& os) { for (size_t i = start; i < end; i++) bm.squares[i].unpack(os); } #endif /* OT_BITMATRIX_HPP_ */