|
| 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