Skip to content

Commit

Permalink
Update bigint2 precompile instructions + suggestions (#146)
Browse files Browse the repository at this point in the history
Co-authored-by: Mars Saxman <[email protected]>
  • Loading branch information
justinfrevert and mars-risc0 authored Jan 8, 2025
1 parent 0c07684 commit 324dd65
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion zirgen/Dialect/BigInt/Precompiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,49 @@ To keep the demo simple, let's imagine that we want to create a trivial precompi
## Write a bigint program
First, create files under [`zirgen/circuit/bigint/`] which will hold the accelerator builder function. We might call them `add128.h` and `add128.cpp`, declaring and then defining a function like this:

In add128.cpp:
```
#include "zirgen/circuit/bigint/add128.h"
namespace zirgen::BigInt {
void genAdd128(mlir::OpBuilder& builder, mlir::Location loc) {
auto lhs = builder.create<BigInt::LoadOp>(loc, 128, 11, 0);
auto rhs = builder.create<BigInt::LoadOp>(loc, 128, 12, 0);
auto sum = builder.create<BigInt::AddOp>(loc, lhs, rhs);
builder.create<BigInt::StoreOp>(loc, sum, 13, 0);
}
}
```

In add128.h
```
#include "zirgen/Dialect/BigInt/IR/BigInt.h"
namespace zirgen::BigInt {
void genAdd128(mlir::OpBuilder& builder, mlir::Location loc);
}
```

We might also like to create a test function here, but we'll gloss over that for now.

## Compile your bigint program to a bigint blob using bigint2c
To make use of the new function, we must add it as an option of the `bigint2c` compiler program, which generates the precompiled blobs we'll need to load into the zkVM. Inside `zirgen/circuit/bigint/bigint2c.cpp`, we'll add a new command line option:
To make use of the new function, we must add it as an option of the `bigint2c` compiler program, which generates the precompiled blobs we'll need to load into the zkVM. Ensure you include the new circuit header file:

```
#include "zirgen/circuit/bigint/add128.h"
```

Inside `zirgen/circuit/bigint/bigint2c.cpp`, we'll add a new command line option:

```
enum class Program {
ModPow65537,
EC_Double,
EC_Add,
ModAdd,
ModInv,
ModMul,
ModSub,
Add128
};
```
Expand Down Expand Up @@ -65,10 +89,36 @@ BLOBS = [
"modpow65537_4096",
"ec_double_256",
"ec_add_256",
"modadd_256",
"modinv_256",
"modmul_256",
"modsub_256",
"add128",
]
```

Add "add128.cpp" to `cc_library.srcs`. Add "add128.h" to `cc_library.hdrs`.

```
srcs = [
"elliptic_curve.cpp",
"field.cpp",
"rsa.cpp",
"add128.cpp",
],
```

```
hdrs = [
"elliptic_curve.h",
"field.h",
"rsa.h",
"add128.h",
"//zirgen/circuit/recursion",
"//zirgen/circuit/rv32im/v1/edsl:rv32im",
],
```

We can now `bazelisk build //zirgen/circuit/bigint/...` to produce our new blob along with the others. In order to make this available to the `risc0` world, we must add the new blob to the bootstrap process. In `zirgen/bootstrap/src/main.rs`, in the `bigint2` function, after a series of similar lines, we must add these lines to copy the new blob over:


Expand Down

0 comments on commit 324dd65

Please sign in to comment.