Skip to content

Commit

Permalink
Debug/proof verification (AbdelStark#18)
Browse files Browse the repository at this point in the history
* debug

* debug
  • Loading branch information
AbdelStark authored Jul 31, 2024
1 parent f329d7e commit eb33346
Show file tree
Hide file tree
Showing 21 changed files with 383 additions and 162 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/cli", "crates/core", "crates/program_wasm"]
members = ["crates/cli", "crates/core", "crates/stwo_wasm"]
resolver = "2"

[workspace.package]
Expand All @@ -12,9 +12,7 @@ license = "MIT"


[workspace.dependencies]
#stwo-prover = { git = "https://github.com/starkware-libs/stwo.git" }
# Temporary fork for serde compatibility of StarkProof
stwo-prover = { git = "https://github.com/MSghais/stwo.git", rev = "8401c93" }
stwo-prover = { git = "https://github.com/starkware-libs/stwo.git" }
tokio = { version = "1", default-features = false }
tracing = { version = "0.1", default-features = false }
tracing-subscriber = "0.3"
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ cargo run --bin dvm_customer

The user CLI binary will submit a proving request to the Nostr network. The prover agent will generate a proof for the request and publish it to the Nostr network. The user CLI binary will be able to verify the proof.

## Askeladd DVM Marketplace

See the [Askeladd DVM Marketplace](https://github.com/AbdelStark/askeladd-dvm-marketplace) for more information.

This requires to build the STWO WASM bindings and to copy the generated artifacts.

Go to [crates/stwo_wasm](crates/stwo_wasm) and run `wasm-pack build --target web` to build the STWO WASM bindings.

Then copy the generated `pkg` directory to [askeladd-dvm-marketplace/src/](askeladd-dvm-marketplace/src/).

## 🤝 Contributing

We love contributions! If you have ideas for improvements or find any issues, please open an issue or submit a pull request.
Expand Down
40 changes: 27 additions & 13 deletions askeladd-dvm-marketplace/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions askeladd-dvm-marketplace/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use client";

import { useState, useEffect, useMemo } from "react";
import { NDKEvent, NDKKind, NostrEvent } from '@nostr-dev-kit/ndk';
import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk';
import { useNostrContext } from "@/context/NostrContext";
import { useSendNote } from "@/hooks/useSendNote";
import { JobResultProver, KIND_JOB_REQUEST, KIND_JOB_RESULT, StarkProof } from "@/types";
import init, { run_fibonacci_example, run_fibonacci_verify_exemple } from "../pkg/program_wasm";
import { JobResultProver, KIND_JOB_REQUEST, KIND_JOB_RESULT } from "@/types";
import init, { verify_stark_proof, prove_and_verify } from "../pkg/stwo_wasm";
import { useFetchEvents } from "@/hooks/useFetchEvents";
import { ASKELADD_RELAY } from "@/constants/relay";
import { Relay } from 'nostr-tools/relay'
import { Relay } from 'nostr-tools/relay';
import { Event as EventNostr, SimplePool } from "nostr-tools";
export default function Home() {
const [logSize, setLogSize] = useState<number>(5);
Expand Down Expand Up @@ -277,9 +277,11 @@ export default function Home() {
try {
if (proof) {
setIsLoading(true);
const prove_result = run_fibonacci_example(logSize, claim);
const prove_result = prove_and_verify(logSize, claim);
console.log("prove_result", prove_result);
const verify_result = run_fibonacci_verify_exemple(logSize, claim, JSON.stringify(starkProof));
const serialised_proof_from_nostr_event = JSON.stringify(starkProof);
console.log("serialised_proof_from_nostr_event", serialised_proof_from_nostr_event);
const verify_result = verify_stark_proof(logSize, claim, serialised_proof_from_nostr_event);
console.log("verify result", verify_result);
console.log("verify message", verify_result.message);
console.log("verify success", verify_result.success);
Expand All @@ -294,7 +296,7 @@ export default function Home() {
for (let event of events) {
const jobProofSerialize: JobResultProver = JSON.parse(event?.content)
const proofSerialize = jobProofSerialize?.response?.proof;
const verify_result = run_fibonacci_verify_exemple(logSize, claim, JSON.stringify(proofSerialize));
const verify_result = verify_stark_proof(logSize, claim, JSON.stringify(proofSerialize));
if (verify_result?.success) {
console.log("loop verify result", verify_result.message);
console.log("loop verify success", verify_result.success);
Expand Down
8 changes: 0 additions & 8 deletions askeladd-dvm-marketplace/src/lib/stwo.ts

This file was deleted.

21 changes: 21 additions & 0 deletions askeladd-dvm-marketplace/src/pkg/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 A₿del ∞/21M 🐺 - 🐱

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 12 additions & 6 deletions askeladd-dvm-marketplace/src/pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "program_wasm",
"name": "stwo_wasm",
"type": "module",
"description": "Stwo wasm bindings",
"version": "0.1.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/AbdelStark/askeladd"
},
"files": [
"program_wasm_bg.wasm",
"program_wasm.js",
"program_wasm.d.ts"
"stwo_wasm_bg.wasm",
"stwo_wasm.js",
"stwo_wasm.d.ts"
],
"main": "program_wasm.js",
"types": "program_wasm.d.ts",
"main": "stwo_wasm.js",
"types": "stwo_wasm.d.ts",
"sideEffects": [
"./snippets/*"
]
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
/**
* @param {number} log_size
* @param {number} claim
* @returns {FibonacciResult}
* @returns {StwoResult}
*/
export function run_fibonacci_example(log_size: number, claim: number): FibonacciResult;
export function prove_and_verify(log_size: number, claim: number): StwoResult;
/**
* @param {number} log_size
* @param {number} claim
* @param {string} stark_proof_str
* @returns {FibonacciResult}
* @returns {StwoResult}
*/
export function run_fibonacci_verify_exemple(log_size: number, claim: number, stark_proof_str: string): FibonacciResult;
export function verify_stark_proof(log_size: number, claim: number, stark_proof_str: string): StwoResult;
/**
*/
export class FibonacciResult {
export class StwoResult {
free(): void;
/**
*/
Expand All @@ -29,11 +29,11 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly __wbg_fibonacciresult_free: (a: number) => void;
readonly fibonacciresult_success: (a: number) => number;
readonly fibonacciresult_message: (a: number, b: number) => void;
readonly run_fibonacci_example: (a: number, b: number) => number;
readonly run_fibonacci_verify_exemple: (a: number, b: number, c: number, d: number) => number;
readonly __wbg_stworesult_free: (a: number) => void;
readonly stworesult_success: (a: number) => number;
readonly stworesult_message: (a: number, b: number) => void;
readonly prove_and_verify: (a: number, b: number) => number;
readonly verify_stark_proof: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ function getInt32Memory0() {
/**
* @param {number} log_size
* @param {number} claim
* @returns {FibonacciResult}
* @returns {StwoResult}
*/
export function run_fibonacci_example(log_size, claim) {
const ret = wasm.run_fibonacci_example(log_size, claim);
return FibonacciResult.__wrap(ret);
export function prove_and_verify(log_size, claim) {
const ret = wasm.prove_and_verify(log_size, claim);
return StwoResult.__wrap(ret);
}

let WASM_VECTOR_LEN = 0;
Expand Down Expand Up @@ -95,46 +95,46 @@ function passStringToWasm0(arg, malloc, realloc) {
* @param {number} log_size
* @param {number} claim
* @param {string} stark_proof_str
* @returns {FibonacciResult}
* @returns {StwoResult}
*/
export function run_fibonacci_verify_exemple(log_size, claim, stark_proof_str) {
export function verify_stark_proof(log_size, claim, stark_proof_str) {
const ptr0 = passStringToWasm0(stark_proof_str, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.run_fibonacci_verify_exemple(log_size, claim, ptr0, len0);
return FibonacciResult.__wrap(ret);
const ret = wasm.verify_stark_proof(log_size, claim, ptr0, len0);
return StwoResult.__wrap(ret);
}

const FibonacciResultFinalization = (typeof FinalizationRegistry === 'undefined')
const StwoResultFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_fibonacciresult_free(ptr >>> 0));
: new FinalizationRegistry(ptr => wasm.__wbg_stworesult_free(ptr >>> 0));
/**
*/
export class FibonacciResult {
export class StwoResult {

static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(FibonacciResult.prototype);
const obj = Object.create(StwoResult.prototype);
obj.__wbg_ptr = ptr;
FibonacciResultFinalization.register(obj, obj.__wbg_ptr, obj);
StwoResultFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}

__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
FibonacciResultFinalization.unregister(this);
StwoResultFinalization.unregister(this);
return ptr;
}

free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_fibonacciresult_free(ptr);
wasm.__wbg_stworesult_free(ptr);
}
/**
* @returns {boolean}
*/
get success() {
const ret = wasm.fibonacciresult_success(this.__wbg_ptr);
const ret = wasm.stworesult_success(this.__wbg_ptr);
return ret !== 0;
}
/**
Expand All @@ -145,7 +145,7 @@ export class FibonacciResult {
let deferred1_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.fibonacciresult_message(retptr, this.__wbg_ptr);
wasm.stworesult_message(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred1_0 = r0;
Expand Down Expand Up @@ -192,6 +192,9 @@ async function __wbg_load(module, imports) {
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_log_b6f0d1d33b5b64be = function(arg0, arg1) {
console.log(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
Expand Down Expand Up @@ -233,7 +236,7 @@ async function __wbg_init(input) {
if (wasm !== undefined) return wasm;

if (typeof input === 'undefined') {
input = new URL('program_wasm_bg.wasm', import.meta.url);
input = new URL('stwo_wasm_bg.wasm', import.meta.url);
}
const imports = __wbg_get_imports();

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function __wbg_fibonacciresult_free(a: number): void;
export function fibonacciresult_success(a: number): number;
export function fibonacciresult_message(a: number, b: number): void;
export function run_fibonacci_example(a: number, b: number): number;
export function run_fibonacci_verify_exemple(a: number, b: number, c: number, d: number): number;
export function __wbg_stworesult_free(a: number): void;
export function stworesult_success(a: number): number;
export function stworesult_message(a: number, b: number): void;
export function prove_and_verify(a: number, b: number): number;
export function verify_stark_proof(a: number, b: number, c: number, d: number): number;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
Expand Down
5 changes: 5 additions & 0 deletions crates/core/src/dvm/service_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,16 @@ impl ServiceProvider {

match self.proving_service.generate_proof(request) {
Ok(response) => {
let serialized_proof = serde_json::to_string(&response.proof)?;
println!("Generated proof: {:?}", serialized_proof);

let job_result = GenerateZKPJobResult {
job_id: job_id.clone(),
response,
};

let response_json = serde_json::to_string(&job_result)?;
println!("Response JSON: {:?}", response_json);

let job_result_event: Event =
EventBuilder::job_result(*event, response_json, 0, None)
Expand Down
Loading

0 comments on commit eb33346

Please sign in to comment.