-rw-r--r-- 4087 cryptattacktester-20231020/ramtest.cpp raw
#include <cassert>
#include <iostream>
#include "random.h"
#include "ram.h"
#include "ram_cost.h"
using namespace std;
int main()
{
for (bigint addrs = 1;addrs < 20;++addrs) {
for (bigint wordsize = 0;wordsize < 20;++wordsize) {
cout << "ramtest read " << addrs << ' ' << wordsize << '\n' << flush;
for (bigint ibits = 0;ibits < 10;++ibits) {
vector<vector<bit>> x;
for (bigint j = 0;j < addrs;++j) {
vector<bit> xj;
for (bigint k = 0;k < wordsize;++k)
xj.push_back(bit(random_bool()));
x.push_back(xj);
}
for (bigint I = 0;I < (1<<ibits);++I) {
vector<bit> i;
for (bigint j = 0;j < ibits;++j)
i.push_back(bit(I.bit(j)));
bigint ops = -bit::ops();
vector<bit> xi = ram_read(x,0,addrs,i,ibits);
assert(ops+bit::ops() == ram_read_cost(addrs,ibits,wordsize));
assert(xi.size() == wordsize);
if (I < addrs)
for (bigint k = 0;k < wordsize;++k)
assert(xi.at(k).value() == x[I].at(k).value());
}
}
}
}
for (bigint addrs = 1;addrs < 20;++addrs) {
for (bigint wordsize = 0;wordsize < 20;++wordsize) {
cout << "ramtest write " << addrs << ' ' << wordsize << '\n' << flush;
for (bigint ibits = 0;ibits < 10;++ibits) {
vector<vector<bit>> x;
for (bigint j = 0;j < addrs;++j) {
vector<bit> xj;
for (bigint k = 0;k < wordsize;++k)
xj.push_back(bit(random_bool()));
x.push_back(xj);
}
for (bigint I = 0;I < (1<<ibits);++I) {
for (bigint top = 0;top < 2;++top) {
vector<bit> i;
for (bigint j = 0;j < ibits;++j)
i.push_back(bit(I.bit(j)));
vector<bit> d;
for (bigint j = 0;j < wordsize;++j)
d.push_back(bit(random_bool()));
vector<vector<bit>> x_cmp = x;
bigint ops = -bit::ops();
ram_write(x,0,addrs,i,ibits,d,bit(0),top);
assert(ops+bit::ops() == ram_write_cost(addrs,ibits,wordsize,top));
if (I < addrs) {
for (bigint k = 0;k < wordsize;++k)
assert(d.at(k).value() == x[I].at(k).value());
for (bigint j = 0;j < addrs;++j)
if (j != I)
for (bigint k = 0;k < wordsize;++k)
assert(x[j].at(k).value() == x_cmp[j].at(k).value());
}
}
}
}
}
}
for (bigint addrs = 1;addrs < 20;++addrs) {
for (bigint wordsize = 0;wordsize < 20;++wordsize) {
cout << "ramtest readwrite " << addrs << ' ' << wordsize << '\n' << flush;
for (bigint ibits = 0;ibits < 10;++ibits) {
vector<vector<bit>> x;
for (bigint j = 0;j < addrs;++j) {
vector<bit> xj;
for (bigint k = 0;k < wordsize;++k)
xj.push_back(bit(random_bool()));
x.push_back(xj);
}
for (bigint I = 0;I < (1<<ibits);++I) {
for (bigint top = 0;top < 2;++top) {
vector<bit> i;
for (bigint j = 0;j < ibits;++j)
i.push_back(bit(I.bit(j)));
vector<bit> d;
for (bigint j = 0;j < wordsize;++j)
d.push_back(bit(random_bool()));
vector<vector<bit>> x_cmp = x;
bigint ops = -bit::ops();
auto y = ram_read_write(x,0,addrs,i,ibits,d,bit(0),top);
assert(ops+bit::ops() == ram_read_write_cost(addrs,ibits,wordsize,top));
if (I < addrs) {
for (bigint k = 0;k < wordsize;++k)
assert(y.at(k).value() == x_cmp[I].at(k).value());
for (bigint k = 0;k < wordsize;++k)
assert(d.at(k).value() == x[I].at(k).value());
for (bigint j = 0;j < addrs;++j)
if (j != I)
for (bigint k = 0;k < wordsize;++k)
assert(x[j].at(k).value() == x_cmp[j].at(k).value());
}
}
}
}
}
}
return 0;
}