Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralize RISC0 constants in Rust implementation #276

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from pathlib import Path
from typing import Any, List

import garaga_rs

from garaga.definitions import CurveID, G1Point, G2Point
from garaga.hints import io
from garaga.hints.io import split_128
Expand All @@ -18,10 +20,7 @@
from garaga.precompiled_circuits.multi_miller_loop import MultiMillerLoopCircuit

# https://github.com/risc0/risc0-ethereum/blob/main/contracts/src/groth16/ControlID.sol
RISC0_CONTROL_ROOT = 0x8CDAD9242664BE3112ABA377C5425A4DF735EB1C6966472B561D2855932C0469
RISC0_BN254_CONTROL_ID = (
0x04446E66D300EB7FB45C9726BB53C793DDA407A62E9601618BB43C5C14657AC0
)
RISC0_CONTROL_ROOT, RISC0_BN254_CONTROL_ID = garaga_rs.get_risc0_constants()


class KeyPatternNotFound(Exception):
Expand Down
8 changes: 8 additions & 0 deletions tools/garaga_rs/src/python_bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@ fn garaga_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(msm::msm_calldata_builder, m)?)?;
m.add_function(wrap_pyfunction!(mpc_calldata::mpc_calldata_builder, m)?)?;
m.add_function(wrap_pyfunction!(groth16_calldata::get_groth16_calldata, m)?)?;
m.add_function(wrap_pyfunction!(get_risc0_constants, m)?)?;
Ok(())
}

#[pyfunction]
pub fn get_risc0_constants(py: Python) -> PyResult<PyObject> {
let (control_root, control_id) = crate::calldata::full_proof_with_hints::groth16::risc0_utils::get_risc0_constants();
let py_tuple = PyTuple::new(py, [control_root, control_id])?;
Ok(Py::from(py_tuple))
}
13 changes: 13 additions & 0 deletions tools/garaga_rs/src/wasm_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ pub fn get_groth16_calldata(
Ok(groth16_calldata_js)
}

#[wasm_bindgen]
pub fn get_risc0_constants() -> Result<Vec<JsValue>, JsValue> {
let (control_root, control_id) = crate::wasm_bindings::groth16::risc0_utils::get_risc0_constants();

// Convert BigUint values to JsValue
let result = vec![
biguint_to_jsvalue(control_root),
biguint_to_jsvalue(control_id),
];

Ok(result)
}

#[allow(dead_code)]
#[cfg(test)]
mod tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { CURVES, CurveId, G1Point, G2Point, findValueInStringToCurveId } from ".
import * as fs from 'fs';
import { bitLength, hexStringToBytes, modInverse, split128, toBigInt, toHexStr } from "../../hints/io";
import { createHash } from 'crypto';
import { get_risc0_constants } from "../../../wasm/pkg/garaga_rs";


//https://github.com/risc0/risc0-ethereum/blob/main/contracts/src/groth16/ControlID.sol
const RISC0_CONTROL_ROOT = BigInt("0x8CDAD9242664BE3112ABA377C5425A4DF735EB1C6966472B561D2855932C0469");
const RISC0_BN254_CONTROL_ID = BigInt("0x04446E66D300EB7FB45C9726BB53C793DDA407A62E9601618BB43C5C14657AC0");
const [RISC0_CONTROL_ROOT, RISC0_BN254_CONTROL_ID] = get_risc0_constants();
const SYSTEM_STATE_ZERO_DIGEST = Uint8Array.from(Buffer.from(
"A3ACC27117418996340B84E5A90F3EF4C49D22C79E44AAD822EC9C313E1EB8E2",
"hex"
Expand Down
5 changes: 5 additions & 0 deletions tools/npm/garaga_ts/src/wasm/pkg/garaga_rs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export function to_twistededwards(x_weirstrass: any, y_weirstrass: any): any[];
* @returns {any[]}
*/
export function get_groth16_calldata(proof_js: any, vk_js: any, curve_id_js: any): any[];
/**
* @returns {any[]}
*/
export function get_risc0_constants(): any[];

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

Expand All @@ -48,6 +52,7 @@ export interface InitOutput {
readonly to_weirstrass: (a: number, b: number, c: number) => void;
readonly to_twistededwards: (a: number, b: number, c: number) => void;
readonly get_groth16_calldata: (a: number, b: number, c: number, d: number) => void;
readonly get_risc0_constants: (a: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
Expand Down
22 changes: 22 additions & 0 deletions tools/npm/garaga_ts/src/wasm/pkg/garaga_rs.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,28 @@ export function get_groth16_calldata(proof_js, vk_js, curve_id_js) {
}
}

/**
* @returns {any[]}
*/
export function get_risc0_constants() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.get_risc0_constants(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
if (r3) {
throw takeObject(r2);
}
var v1 = getArrayJsValueFromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 4, 4);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

function handleError(f, args) {
try {
return f.apply(this, args);
Expand Down
2 changes: 1 addition & 1 deletion tools/npm/garaga_ts/src/wasm/pkg/garaga_rs_bg.wasm.js

Large diffs are not rendered by default.

Loading