Skip to content

Commit

Permalink
add Rust/Py binding and update wasm files through docker compose build
Browse files Browse the repository at this point in the history
  • Loading branch information
augustin-v committed Dec 17, 2024
1 parent 67dc686 commit 7d703ea
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
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))
}
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.

0 comments on commit 7d703ea

Please sign in to comment.