Skip to content

Commit

Permalink
include BigInt::DefOp public flag into bibc file format
Browse files Browse the repository at this point in the history
  • Loading branch information
mars-risc0 committed Oct 23, 2024
1 parent 9fb7a2e commit 6ae253c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions zirgen/Dialect/BigInt/Bytecode/bibc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ struct Type {
// Input wire
struct Input {
uint64_t label;
// TODO: add isPublic flag
uint32_t bitWidth;
uint32_t minBits;
uint16_t minBits;
bool isPublic;
};

// Code section is a sequence of ops
Expand Down
25 changes: 22 additions & 3 deletions zirgen/Dialect/BigInt/Bytecode/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ HEADER: 24 bytes
INPUT TABLE: 16 bytes each
8 | label
4 | bitWidth
4 | minBits
2 | minBits
2 | isPublic
TYPE TABLE: 32 bytes each
8 | coeffs
Expand Down Expand Up @@ -77,6 +78,14 @@ IOException::IOException(const char* file, const char* func, int line, const cha

namespace {

void writeU16(uint16_t value, FILE* stream) {
std::array<uint8_t, 2> buf;
buf[0] = (value >> 0x00) & 0xFFU;
buf[1] = (value >> 0x08) & 0xFFU;
size_t writ = fwrite(buf.data(), buf.size(), 1, stream);
check(ferror(stream) || !writ);
}

void writeU32(uint32_t value, FILE* stream) {
std::array<uint8_t, 4> buf;
buf[0] = (value >> 0x00) & 0xFFU;
Expand Down Expand Up @@ -113,7 +122,8 @@ void writeHeader(const Program& p, FILE* stream) {
void writeInput(const Input& i, FILE* stream) {
writeU64(i.label, stream);
writeU32(i.bitWidth, stream);
writeU32(i.minBits, stream);
writeU16(i.minBits, stream);
writeU16(i.isPublic ? 1 : 0, stream);
}

void writeType(const Type& t, FILE* stream) {
Expand Down Expand Up @@ -165,6 +175,14 @@ void write(const Program& p, FILE* stream) {

namespace {

uint32_t readU16(FILE* stream) {
check(feof(stream));
std::array<uint8_t, 2> buf;
size_t got = fread(buf.data(), buf.size(), 1, stream);
check(ferror(stream) || !got);
return (static_cast<uint16_t>(buf[0]) << 0x00) | (static_cast<uint16_t>(buf[1]) << 0x08);
}

uint32_t readU32(FILE* stream) {
check(feof(stream));
std::array<uint8_t, 4> buf;
Expand Down Expand Up @@ -197,7 +215,8 @@ void readHeader(Program& p, FILE* stream) {
void readInput(Input& wire, FILE* stream) {
wire.label = readU64(stream);
wire.bitWidth = readU32(stream);
wire.minBits = readU32(stream);
wire.minBits = readU16(stream);
wire.isPublic = readU16(stream) != 0;
}

void readType(Type& t, FILE* stream) {
Expand Down
7 changes: 7 additions & 0 deletions zirgen/Dialect/BigInt/Bytecode/mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ std::unique_ptr<Program> encode(mlir::func::FuncOp func) {
Input input;
input.label = op.getLabel();
input.bitWidth = op.getBitWidth();
if (op.getBitWidth() >= 0xFFFFFFFFU) {
throw std::runtime_error("unexpectedly large bitWidth");
}
input.minBits = op.getMinBits();
if (op.getMinBits() >= 0xFFFFU) {
throw std::runtime_error("unexpectedly large minBits");
}
input.isPublic = op.getIsPublic();
newOp.operandA = builder.def(input);
builder.emit(newOp);
})
Expand Down

0 comments on commit 6ae253c

Please sign in to comment.