Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6087e0b

Browse files
committedJan 16, 2024
submit the code
1 parent 55b9b8f commit 6087e0b

File tree

9 files changed

+199
-2
lines changed

9 files changed

+199
-2
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Cargo.lock
1212

1313
# MSVC Windows builds of rustc generate these, which store debugging information
1414
*.pdb
15+
16+
.idea

‎Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "ecdsa0-example"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0"
8+
ecdsa0-methods = { path = "methods" }
9+
k256 = { version = "0.13", features = ["serde"] }
10+
rand_core = "0.6.4"
11+
risc0-zkvm = { git = "https://github.com/l2iterative/risc0/", branch="no-rust-runtime-for-host" }
12+
serde = "1.0"
13+
bytemuck = "1.14.0"
14+
15+
[features]
16+
cuda = ["risc0-zkvm/cuda"]
17+
default = []
18+
metal = ["risc0-zkvm/metal"]
19+
prove = ["risc0-zkvm/prove"]

‎README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
# ecdsa0-example
2-
Example ECDSA to test distributed proof generation on Ray for RISC Zero
1+
# ECDSA example
2+
3+
This is a copy-of-paste of the code in RISC0 example repo, for the pure purpose of changing the dependencies.
4+
5+
https://github.com/risc0/risc0/blob/main/examples/ecdsa/methods/guest/src/bin/ecdsa_verify.rs
6+
7+
We may make additional edits to test larger quantity of ECDSA signature verification.

‎methods/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "ecdsa0-methods"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
8+
9+
[build-dependencies]
10+
risc0-build = { git = "https://github.com/l2iterative/risc0/", branch="no-rust-runtime-for-host" }
11+
12+
[package.metadata.risc0]
13+
methods = ["guest"]

‎methods/build.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2024 RISC Zero, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {
16+
risc0_build::embed_methods();
17+
}

‎methods/guest/Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "ecdsa-verify"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[workspace]
7+
8+
[dependencies]
9+
risc0-zkvm = { git = "https://github.com/l2iterative/risc0/", branch="no-rust-runtime-for-host", default_features = false, features = ["std"] }
10+
hex-literal = "0.4"
11+
k256 = { version = "=0.13.1", features = ["arithmetic", "serde", "expose-field", "std", "ecdsa"], default_features = false }
12+
13+
[patch.crates-io]
14+
# Placing these patch statement in the workspace Cargo.toml will add RISC Zero SHA-256 and bigint
15+
# multiplication accelerator support for all downstream usages of the following crates.
16+
sha2 = { git = "https://github.com/risc0/RustCrypto-hashes", tag = "sha2-v0.10.6-risczero.0" }
17+
k256 = { git = "https://github.com/risc0/RustCrypto-elliptic-curves", tag = "k256/v0.13.1-risczero.1" }
18+
crypto-bigint = { git = "https://github.com/risc0/RustCrypto-crypto-bigint", tag = "v0.5.2-risczero.0" }
19+
20+
[profile.release]
21+
# Empirically observed to result in the best performance for this binary (in particular, better than
22+
# "fat"). It's often a good idea to experiment with different optimization levels and options.
23+
lto = "thin"

‎methods/guest/src/main.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 RISC Zero, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use k256::{
16+
ecdsa::{signature::Verifier, Signature, VerifyingKey},
17+
EncodedPoint,
18+
};
19+
use risc0_zkvm::guest::env;
20+
21+
fn main() {
22+
// Decode the verifying key, message, and signature from the inputs.
23+
let (encoded_verifying_key, message, signature): (EncodedPoint, Vec<u8>, Signature) =
24+
env::read();
25+
let verifying_key = VerifyingKey::from_encoded_point(&encoded_verifying_key).unwrap();
26+
27+
// Verify the signature, panicking if verification fails.
28+
verifying_key
29+
.verify(&message, &signature)
30+
.expect("ECDSA signature verification failed");
31+
32+
env::commit(&(encoded_verifying_key, message));
33+
}

‎methods/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2024 RISC Zero, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
include!(concat!(env!("OUT_DIR"), "/methods.rs"));

‎src/main.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2024 RISC Zero, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use ecdsa0_methods::{ECDSA_VERIFY_ELF, ECDSA_VERIFY_ID};
16+
use k256::{
17+
ecdsa::{signature::Signer, Signature, SigningKey, VerifyingKey},
18+
EncodedPoint,
19+
};
20+
use rand_core::OsRng;
21+
use risc0_zkvm::serde::to_vec;
22+
use risc0_zkvm::{default_prover, ExecutorEnv, Receipt};
23+
24+
/// Given an secp256k1 verifier key (i.e. public key), message and signature,
25+
/// runs the ECDSA verifier inside the zkVM and returns a receipt, including a
26+
/// journal and seal attesting to the fact that the prover knows a valid
27+
/// signature from the committed public key over the committed message.
28+
fn prove_ecdsa_verification(
29+
verifying_key: &VerifyingKey,
30+
message: &[u8],
31+
signature: &Signature,
32+
) -> Receipt {
33+
let input = (verifying_key.to_encoded_point(true), message, signature);
34+
let input_vec = to_vec(&input).unwrap();
35+
let input_bytes: &[u8] = bytemuck::cast_slice(&input_vec);
36+
37+
println!("input_bytes: {:?}", input_bytes);
38+
39+
let env = ExecutorEnv::builder()
40+
.write_slice(&input_bytes)
41+
.build()
42+
.unwrap();
43+
44+
// Obtain the default prover.
45+
let prover = default_prover();
46+
47+
// Produce a receipt by proving the specified ELF binary.
48+
prover.prove(env, ECDSA_VERIFY_ELF).unwrap()
49+
}
50+
51+
fn main() {
52+
// Generate a random secp256k1 keypair and sign the message.
53+
let signing_key = SigningKey::random(&mut OsRng); // Serialize with `::to_bytes()`
54+
let message = b"This is a message that will be signed, and verified within the zkVM";
55+
let signature: Signature = signing_key.sign(message);
56+
57+
// Run signature verified in the zkVM guest and get the resulting receipt.
58+
let receipt = prove_ecdsa_verification(signing_key.verifying_key(), message, &signature);
59+
60+
// Verify the receipt and then access the journal.
61+
receipt.verify(ECDSA_VERIFY_ID).unwrap();
62+
let (receipt_verifying_key, receipt_message): (EncodedPoint, Vec<u8>) =
63+
receipt.journal.decode().unwrap();
64+
65+
println!(
66+
"Verified the signature over message {:?} with key {}",
67+
std::str::from_utf8(&receipt_message[..]).unwrap(),
68+
receipt_verifying_key,
69+
);
70+
}

0 commit comments

Comments
 (0)