Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Dev - ready for v1.0 #54

Merged
merged 144 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
144 commits
Select commit Hold shift + click to select a range
fe36f1b
Prettified Code!
nom4dv3 Jul 11, 2023
d75f565
add compile-full command
fewwwww Jul 13, 2023
018b74b
fix typo
fewwwww Jul 13, 2023
144f124
add inner func
nom4dv3 Jul 13, 2023
1778654
Merge branch 'dev' of v3.github.com:hyperoracle/zkgraph into norman
nom4dv3 Jul 13, 2023
0def5e0
add bundle env func to compatible with c-wasm
nom4dv3 Jul 13, 2023
b46602a
Merge pull request #44 from hyperoracle/norman
nom4dv3 Jul 13, 2023
7c46cc8
fix response to consist with the server
nom4dv3 Jul 13, 2023
ced575f
add: full exec.js
nom4dv3 Jul 13, 2023
a2633b5
add: full prove.js
nom4dv3 Jul 13, 2023
40ec427
init setup, solve conflicts
nom4dv3 Jul 13, 2023
d2fff8b
try post to zkwasm setup
nom4dv3 Jul 13, 2023
3586f71
update package-lock.json
fewwwww Jul 14, 2023
444050f
add urls to config
fewwwww Jul 14, 2023
788df22
add log content
fewwwww Jul 14, 2023
feff76a
fix imports
fewwwww Jul 14, 2023
6dc177c
add config for user
fewwwww Jul 14, 2023
1ad7329
finish set up with zkwasm provider
fewwwww Jul 14, 2023
388b841
add content for set up image
fewwwww Jul 14, 2023
bbc8cc2
update start guide
fewwwww Jul 14, 2023
fb5762a
make lib npm package
fewwwww Jul 14, 2023
2c9f779
correct output log
fewwwww Jul 14, 2023
6659875
only export require in lib/zkwasm
fewwwww Jul 14, 2023
bf753fd
remove public key as config and compute for it
fewwwww Jul 14, 2023
2b52f86
fix log: image ID -> task ID
nom4dv3 Jul 14, 2023
70826db
rename bundle-js/ -> api/
nom4dv3 Jul 14, 2023
571798f
change full circuit size to 22
nom4dv3 Jul 14, 2023
ece78d8
move setup request logic to zkwasm_setup.js
nom4dv3 Jul 14, 2023
a48c91e
use privkey instead of address
nom4dv3 Jul 14, 2023
0d87a20
fix: import loadZKGraphConfig
nom4dv3 Jul 14, 2023
dadd4f5
update: pretest->test for local prove
nom4dv3 Jul 14, 2023
8105990
add zkwasm full prove
nom4dv3 Jul 14, 2023
b28d9ac
fix config import
nom4dv3 Jul 14, 2023
c509851
finish prove!
nom4dv3 Jul 14, 2023
56f3b9d
add task status in setup
nom4dv3 Jul 14, 2023
00290b6
update output log
fewwwww Jul 14, 2023
57d4fa9
add command and fix folder structure description
fewwwww Jul 14, 2023
7fe0aff
refactor apis
fewwwww Jul 14, 2023
24f54db
try merge 2 bundle_xxx.js
nom4dv3 Jul 15, 2023
b9a9921
merge to 1 bundle.js
nom4dv3 Jul 15, 2023
f7127ff
update prove with merged bundle
nom4dv3 Jul 15, 2023
8ab20cd
merge log in prove
nom4dv3 Jul 15, 2023
e9d81f2
fix filepath
nom4dv3 Jul 15, 2023
da0dcf4
update config.WasmBinaryPath
nom4dv3 Jul 15, 2023
7bf73e1
improve wasm import in exec
nom4dv3 Jul 15, 2023
b3e3c89
rename wasmPath
nom4dv3 Jul 15, 2023
0c06e5c
rename WasmBinPath -> WasmPath
nom4dv3 Jul 15, 2023
f5934c5
rename WasmBinPath
nom4dv3 Jul 15, 2023
ddc4575
merge duplicated code & use config wasm path
nom4dv3 Jul 15, 2023
27d67b7
clean
nom4dv3 Jul 15, 2023
d16377e
fix test.sh
nom4dv3 Jul 15, 2023
42b603a
add full test to test.sh
nom4dv3 Jul 15, 2023
eadf03f
fix compile err: add --exportRuntime
nom4dv3 Jul 15, 2023
aedde7b
improve loc log
nom4dv3 Jul 15, 2023
14b89ba
rm deploy-local in package.json
nom4dv3 Jul 15, 2023
9dd90b1
update config-example.js
nom4dv3 Jul 15, 2023
d105cc3
Merge pull request #45 from hyperoracle/norman
nom4dv3 Jul 15, 2023
463bbf2
update config guide
fewwwww Jul 15, 2023
fa9abde
log proof detail (aux data) after proof generated
fewwwww Jul 15, 2023
2597fc1
add toHexStringBytes32Reverse and loading animation
fewwwww Jul 15, 2023
815aa77
add loading animation when waiting for progress
fewwwww Jul 15, 2023
2b14665
write proof data as txt
fewwwww Jul 17, 2023
8dc113f
update compile server endpoint
nom4dv3 Jul 17, 2023
ebb2453
fix: update CompilerServerEndpoint
nom4dv3 Jul 17, 2023
970f230
fix: Bytes.fromHexString support odd length hexstring
nom4dv3 Jul 17, 2023
8bd3f60
add Bytes.padding
nom4dv3 Jul 17, 2023
a554159
update mapping.ts
nom4dv3 Jul 17, 2023
2c03f4f
remove extra new line
fewwwww Jul 17, 2023
c4ed124
update inputs for prove
fewwwww Jul 17, 2023
f8589f5
experiment with fix mapping.ts
fewwwww Jul 17, 2023
3215301
rename&optimize paddingTo
nom4dv3 Jul 17, 2023
370eda4
Update README.md
kartinW Jul 17, 2023
676ff18
add comment
fewwwww Jul 17, 2023
77b5c26
pre-compile in remote compile mode
nom4dv3 Jul 18, 2023
e7a6138
Merge branch 'zkwasm_integrate' of v3.github.com:hyperoracle/zkgraph …
nom4dv3 Jul 18, 2023
1733f5a
implement BigInt class
fewwwww Jul 19, 2023
4ac8c57
add test for uint8ArrayToUint32Array
fewwwww Jul 19, 2023
48bdb8d
update callable interfaces with tests
fewwwww Jul 19, 2023
6a592a6
update mapping based on latest BigInt
fewwwww Jul 19, 2023
51dafd9
rename folder name
fewwwww Jul 19, 2023
ef9f86a
add suggested provider
fewwwww Jul 20, 2023
e56aae6
add test for very large int
fewwwww Jul 20, 2023
33b5c3e
fix big endian
fewwwww Jul 20, 2023
22fed49
refactor lib types
fewwwww Jul 20, 2023
5d7e0d9
update comment on exported class
fewwwww Jul 20, 2023
5a9f52b
add test logs for BigInt.fromBytes
fewwwww Jul 20, 2023
c903dfc
make compile error pretty
fewwwww Jul 20, 2023
8e025b5
add remote compile err msg
nom4dv3 Jul 22, 2023
93e6cba
fix: exec on correct heap offset (call __as_start)
nom4dv3 Jul 22, 2023
9c53b63
Merge branch 'norman' of v3.github.com:hyperoracle/zkgraph into bignum
nom4dv3 Jul 23, 2023
5b459f1
Merge branch 'bignum' of v3.github.com:hyperoracle/zkgraph into bignum
nom4dv3 Jul 24, 2023
228f2ce
remove log for input
fewwwww Jul 24, 2023
783c11e
apply digit mask for BigInt.fromBytes
fewwwww Jul 24, 2023
f5e48be
trim leading zeroes for fromBytesBigEndian
fewwwww Jul 24, 2023
04dec94
fix overflow when applying digit mask
fewwwww Jul 24, 2023
4744c60
mask last element
fewwwww Jul 25, 2023
d15e4d6
failed testcase
nom4dv3 Jul 25, 2023
60294bd
fix #49
nom4dv3 Jul 25, 2023
72d7225
fix array conversion
fewwwww Jul 25, 2023
372900d
Merge branch 'bignum' of https://github.com/hyperoracle/zkgraph into …
fewwwww Jul 25, 2023
a0e88c9
fix edge case
nom4dv3 Jul 25, 2023
1b8a8e5
fix test
fewwwww Jul 25, 2023
e65e2d4
update conversion implementation
fewwwww Jul 25, 2023
d774fdb
update function name
fewwwww Jul 25, 2023
3acec5d
reverse on copy
fewwwww Jul 25, 2023
66263c4
fix endian
fewwwww Jul 25, 2023
9f96b8e
refactor function implementation
fewwwww Jul 26, 2023
2ce1dfe
separate pad functions
fewwwww Jul 26, 2023
77947f5
update default zkgraph
fewwwww Jul 26, 2023
32f835b
remove test logs
fewwwww Jul 26, 2023
0ddf74d
add syntax sugars
fewwwww Jul 26, 2023
9fc4dbf
Merge pull request #50 from hyperoracle/bignum
fewwwww Jul 26, 2023
5052e3d
add setup-local
nom4dv3 Jul 26, 2023
00e27c9
add prove-local -- prove
nom4dv3 Jul 26, 2023
fedd2b5
add axios err handle
nom4dv3 Jul 26, 2023
ff14f60
update err message
nom4dv3 Jul 26, 2023
84eaf57
fix set up errors by removing unknown wasm opcodes
fewwwww Jul 26, 2023
273a8cc
Merge branch 'dev' into bigint-doc
fewwwww Jul 26, 2023
58856c2
update error handling for await
fewwwww Jul 26, 2023
8c5f31b
fix loading bar width
fewwwww Jul 26, 2023
4acd0be
add wat cost and disable overriding method
fewwwww Jul 26, 2023
c78e72e
add wat cost and add syntax sugar
fewwwww Jul 26, 2023
10ea9de
fix wording
fewwwww Jul 26, 2023
ff39528
add wat cost to all documentable methods
fewwwww Jul 26, 2023
9bdfb2d
add test for BigInt square
fewwwww Jul 26, 2023
85b361e
handle edge cases
fewwwww Jul 26, 2023
5348f4c
add test for added functions
fewwwww Jul 26, 2023
a10e876
revert changes
fewwwww Jul 27, 2023
54af717
fix: setup return fail while actually success
nom4dv3 Jul 27, 2023
e801641
set local circuit size to 19
nom4dv3 Jul 27, 2023
a7a432f
add prove script options err handle
nom4dv3 Jul 27, 2023
79f6b08
refactor require(i32) to require(bool)
nom4dv3 Jul 27, 2023
24684f9
set local cir size to 20
nom4dv3 Jul 27, 2023
2da6d62
log setup response error for further debug
nom4dv3 Jul 27, 2023
fd3714b
print proof output file path
nom4dv3 Jul 27, 2023
b6aa583
add the 1st zkgraph example in src/ #46
nom4dv3 Jul 27, 2023
55ef2a9
rm example/mvp_test, add example/hello
nom4dv3 Jul 27, 2023
77e6fd6
rm js_log
nom4dv3 Jul 27, 2023
a2b20b9
update readme
fewwwww Jul 27, 2023
01e4aa9
add syntax sugar
fewwwww Jul 27, 2023
d7295c9
Prettified Code!
nom4dv3 Jul 27, 2023
1afd89a
omit errmsg if Image already exists
nom4dv3 Jul 27, 2023
e648bd1
Prettified Code!
nom4dv3 Jul 27, 2023
2fe0a62
update error message
fewwwww Jul 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 55 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

## Getting Started

To create your zkGraph project based on this template, click `Use this template`, and `Creating a new repository`.
To create your zkGraph project based on this template:

Option 1:

Click `Use this template`, and `Creating a new repository`.

Option 2:

Use `gh` cli

```bash
gh repo create zkgraph-new --public --template="https://github.com/hyperoracle/zkgraph.git"
```

### Configuration

Expand All @@ -11,9 +23,12 @@ After clone your project, you need to create `config.js` file at root folder bas
```js
// ./config.js
export const config = {
// Etherum JSON RPC provider URL:
// (Please note the provider must support debug_getRawReceipts RPC method.)
// Update your Etherum JSON RPC provider URL here.
// Please note that the provider must support debug_getRawReceipts RPC method.
// Recommended provider: ANKR.
JsonRpcProviderUrl: "https://{URL}",
UserPrivateKey: "0x{PRIVATE_KEY}",
// ...and other configs.
};
```

Expand Down Expand Up @@ -49,11 +64,44 @@ npm run compile-local
npm run exec-local -- {block_id}
```

### Prove Local (input generation / pre-test)
### Set Up Local Image

```bash
npm run setup-local
```

### Prove Local Image (input generation / pre-test / prove)

```bash
npm run prove-local -- --inputgen {block_id} {expected_state}
npm run prove-local -- --pretest {block_id} {expected_state}
npm run prove-local -- --prove {block_id} {expected_state}
```

### Compile (with Compile Server)

```bash
npm run compile
```

### Set Up Image (Link Compiled with Compiler Server)

```bash
npm run setup
```

### Prove Full Image (Link Compiled with Compiler Server)

```bash
npm run prove -- --inputgen {block_id} {expected_state}
npm run prove -- --pretest {block_id} {expected_state}
npm run prove -- --prove {block_id} {expected_state}
```

### Verifier Contract Interface

```AggregatorVerifier
https://github.com/DelphinusLab/halo2aggregator-s/blob/main/sol/contracts/AggregatorVerifier.sol#L40
```

## Develop
Expand Down Expand Up @@ -114,16 +162,16 @@ More info and API reference can be found in [Hyper Oracle zkGraph docs](https://
## Lib Dev Tips

1. Don't use `I8.parseInt` because it will be compiled to `i32.extend8_s (aka. Unknown opcode 192 (0xC0) in WASM)`.
2. Don't use template literals (`${}`), for example when throwing errors, because it will be compiled to too many WASM instructions (~1000 diff).
3. Don't use `FC extensions` opcodes, because it will be compiled to `Unknown opcode 252 (0xFC) in WASM`.
2. Try not to use template literals (`${}`), for example when throwing errors, because it will be compiled to too many WASM instructions (~1000 diff).
3. Try not to use `FC extensions` opcodes (`<u32>parseInt(...)`, `f64`, or `Math`), because it will be compiled to `Unknown opcode 252 (0xFC) in WASM`, and generates too many instructions.

References: [WebAssembly Opcodes](https://pengowray.github.io/wasm-ops/).

## Structure

This repo has the following folders relevant to zkGraph development:

- `bundle-js`: APIs (the scripts in `package.json`) for compile, execute, prove, and deploy zkGraph for testing locally.
- `api`: APIs (the scripts in `package.json`) for compile, execute, prove, and deploy zkGraph for testing locally, and fully with zkWASM node.
- `example`: Example zkGraphs.
- `lib`: AssemblyScript library for zkGraph development, with data structure such as Bytes, ByteArray and BigInt.
- `src`: Where your actual zkGraph should be in. Contains `mapping.ts` and `zkgraph.yaml`.
Expand Down
28 changes: 21 additions & 7 deletions bundle-js/common/api_helper.js → api/common/api_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,26 @@ export function genStreamAndMatchedEventOffsets(rawreceiptList, eventList) {
}

// Format inputs with length and input value
export function formatVarLenInput(inputs) {
var formatted = "";
inputs.map((input) => {
formatted += `0x${Math.ceil(input.length / 2).toString(
16,
)}:i64 0x${input}:bytes-packed `;
});
export function formatIntInput(input) {
return `0x${input.toString(16)}:i64 `;
}

// Format bytes input
export function formatHexStringInput(input) {
return `0x${trimPrefix(input, "0x")}:bytes-packed `;
}

// Format inputs with length and input value
export function formatVarLenInput(input) {
// var formatted = "";
// inputs.map((input) => {
// var inp = trimPrefix(input, '0x')
// formatted += `${formatIntInput(Math.ceil(inp.length / 2))}${formatHexStringInput(inp)}`;
// });

var inp = trimPrefix(input, "0x");
var formatted = `${formatIntInput(
Math.ceil(inp.length / 2),
)}${formatHexStringInput(inp)}`;
return formatted;
}
35 changes: 32 additions & 3 deletions bundle-js/common/bundle_local.js → api/common/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ async function instantiate(module, imports = {}) {
// lib/common/zkwasm/wasm_input(i32) => i64
return zkwasmmock.wasm_input(x) || 0n;
},
js_log(arg) {
// to compatible with c-wasm
console.log(arg);
},
js_log_u64(arg) {
// to compatible with c-wasm
console.log(arg);
},
}),
};
const { exports } = await WebAssembly.instantiate(module, adaptedImports);
Expand Down Expand Up @@ -123,8 +131,28 @@ async function instantiate(module, imports = {}) {
}
return adaptedExports;
}
export const { memory, asmain, zkmain } = await (async (url) =>
instantiate(
// export const { memory, asmain, zkmain } = await (async (url) =>
// instantiate(
// await (async () => {
// try {
// return await globalThis.WebAssembly.compileStreaming(
// globalThis.fetch(url),
// );
// } catch {
// return globalThis.WebAssembly.compile(
// await (await import("node:fs/promises")).readFile(url),
// );
// }
// })(),
// {},
// ))(new URL("../../build/zkgraph_full.wasm", import.meta.url));

export const instantiateWasm = async (wasmpath) => {
// update this when move bundle.js
let curPathToRoot = "../../";

let url = new URL(curPathToRoot + wasmpath, import.meta.url);
return instantiate(
await (async () => {
try {
return await globalThis.WebAssembly.compileStreaming(
Expand All @@ -137,4 +165,5 @@ export const { memory, asmain, zkmain } = await (async (url) =>
}
})(),
{},
))(new URL("../../build/zkgraph_local.wasm", import.meta.url));
);
};
4 changes: 2 additions & 2 deletions bundle-js/common/config.js → api/common/config_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function loadYaml(fname) {
}
}

export function loadConfig(fname) {
export function loadZKGraphConfig(fname) {
const config = loadYaml(fname);
const source_address = config.dataSources[0].source.address;

Expand All @@ -36,4 +36,4 @@ export function loadConfig(fname) {
return [source_address, source_esigs];
}

export function applyConfig(configObj) {} //placeholder
export function applyZKGraphConfig(configObj) {} //placeholder
16 changes: 16 additions & 0 deletions api/common/ethers_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ethers } from "ethers";

export async function getRawReceipts(ethersProvider, blockid) {
if (Number.isFinite(blockid)) {
blockid = "0x" + blockid.toString(16);
}
return ethersProvider.send("debug_getRawReceipts", [blockid]);
}

export async function getBlockByNumber(ethersProvider, blockNumber) {
const fullBlock = await ethersProvider.send("eth_getBlockByNumber", [
ethers.utils.hexValue(blockNumber),
false,
]);
return fullBlock;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
118 changes: 118 additions & 0 deletions api/common/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
export function fromHexString(hexString) {
hexString = hexString.startsWith("0x") ? hexString.slice(2) : hexString;
hexString = hexString.length % 2 ? "0" + hexString : hexString;
return Uint8Array.from(Buffer.from(hexString, "hex"));
}

export function toHexString(bytes) {
return Buffer.from(bytes).toString("hex");
}

export function toHexStringBytes32Reverse(arr) {
let result = "";
for (let i = 0; i < arr.length / 32; i++) {
result +=
"0x" + toHexString(arr.slice(i * 32, (i + 1) * 32).reverse()) + "\n";
}
return result;
}

export function areEqualArrays(first, second) {
return (
first.length === second.length &&
first.every((value, index) => value === second[index])
);
}

export function trimPrefix(str, prefix) {
if (str.startsWith(prefix)) {
str = str.substring(prefix.length);
}
return str;
}

export function logDivider() {
const line = "=".repeat(process.stdout.columns);
console.log(line);
}

export function logLoadingAnimation() {
// If width is equal to process.stdout.columns, the bar will overflow into the next line.
// 4 is the length of the prefix "[*] ".
// 55 is about the same length as the longest message in this script.
const width = Math.min(process.stdout.columns - 4, 55);
let frame = 0;
let stop = false;

const frames = ["▓"];
let position = 0;
const intervalId = setInterval(() => {
if (stop) {
clearInterval(intervalId);
process.stdout.clearLine();
process.stdout.cursorTo(0);
return;
}

const currentFrame = frames[frame % frames.length];
const loadingBar = `[*] ${currentFrame.repeat(
position,
)}▒${currentFrame.repeat(width - position - 1)}`;

process.stdout.cursorTo(0);
process.stdout.write(loadingBar);

position = (position + 1) % width;

frame++;
}, 400);

return {
stopAndClear: () => {
stop = true;
process.stdout.clearLine();
process.stdout.cursorTo(0);
},
};
}

export function concatHexStrings(hexStrings) {
let result = "";
for (let hexString of hexStrings) {
result += hexString.startsWith("0x") ? hexString.slice(2) : hexString;
}
return "0x" + result;
}

export function currentNpmScriptName() {
return process.env.npm_lifecycle_event;
}

export function logReceiptAndEvents(
rawreceiptList,
blockid,
matchedEventOffsets,
filteredEventList,
) {
console.log(
"[*]",
rawreceiptList.length,
rawreceiptList.length > 1
? "receipts fetched from block"
: "receipt fetched from block",
blockid,
);
console.log(
"[*]",
matchedEventOffsets.length / 7,
matchedEventOffsets.length / 7 > 1 ? "events matched" : "event matched",
);
for (let i in filteredEventList) {
for (let j in filteredEventList[i]) {
filteredEventList[i][j].prettyPrint(
"\tTx[" + i + "]Event[" + j + "]",
false,
);
}
}
}
Loading