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

fix: inputs for RSA example #271

Merged
merged 4 commits into from
Apr 18, 2024
Merged
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
443 changes: 430 additions & 13 deletions examples/rsa/program/Cargo.lock

Large diffs are not rendered by default.

Binary file modified examples/rsa/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
55 changes: 24 additions & 31 deletions examples/rsa/program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,40 @@ sp1_zkvm::entrypoint!(main);

use rsa::PaddingScheme;
use rsa::PublicKey;
use rsa::{
pkcs8::{DecodePrivateKey, DecodePublicKey},
RsaPrivateKey, RsaPublicKey,
};
use rsa::{pkcs8::DecodePublicKey, RsaPublicKey};
use sha2::Digest;
use sha2::Sha256;
use std::vec;

const RSA_2048_PRIV_DER: &[u8] = include_bytes!("rsa2048-priv.der");
const RSA_2048_PUB_DER: &[u8] = include_bytes!("rsa2048-pub.der");

pub fn main() {
let private_key = RsaPrivateKey::from_pkcs8_der(RSA_2048_PRIV_DER).unwrap();
let public_key = RsaPublicKey::from_public_key_der(RSA_2048_PUB_DER).unwrap();
println!("{:?}{:?}", private_key, public_key);
// Read an input to the program.
//
// Behind the scenes, this compiles down to a custom system call which handles reading inputs
let pk_der = sp1_zkvm::io::read::<Vec<u8>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this program actually work? I don't think this should work because the io reader doesn't know how many bytes to actually read, if I'm not mistaken. You can either use read_slice and pass in a mutable slice with a particular length or you can just read a RsaPublicKey if that implements Serde, Deserde

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it does when running in dev mode with SP1Prover::execute(REGEX_IO_ELF, stdin) but when trying with SP1Prover::prove(REGEX_IO_ELF, stdin) it prints the message from the program and gets killed:
image

I thought it was something related to my machine (Ubuntu 22) because it gets killed too when running the initial RSA example.

because the io reader doesn't know how many bytes to actually read

This was my understanding as well but then it works when String is passed along and String boils down to Vec<u8> 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it on a macOS, and I confirm the program works.

let message = sp1_zkvm::io::read::<Vec<u8>>();
let signature = sp1_zkvm::io::read::<Vec<u8>>();

let public_key = RsaPublicKey::from_public_key_der(&pk_der).unwrap();

let message = b"Hello world!";
let mut hasher = Sha256::new();
hasher.update(message);
let hashed_msg = hasher.finalize();

let signature = vec![
32, 121, 247, 109, 107, 249, 210, 178, 234, 149, 136, 242, 34, 135, 250, 127, 150, 225, 43,
137, 241, 39, 139, 78, 179, 49, 169, 111, 200, 96, 183, 227, 70, 15, 46, 227, 114, 103,
169, 170, 57, 107, 214, 102, 222, 13, 19, 216, 241, 134, 26, 124, 96, 202, 29, 185, 69, 4,
204, 78, 223, 61, 124, 41, 179, 255, 84, 58, 47, 137, 242, 102, 161, 37, 45, 20, 39, 129,
67, 55, 210, 164, 105, 82, 214, 223, 194, 201, 143, 114, 99, 237, 157, 42, 73, 50, 175,
160, 145, 95, 138, 242, 157, 90, 100, 170, 206, 39, 80, 49, 65, 55, 202, 214, 17, 19, 183,
244, 184, 17, 108, 171, 54, 178, 242, 137, 215, 67, 185, 198, 122, 234, 132, 240, 73, 42,
123, 46, 201, 19, 197, 248, 9, 122, 16, 86, 67, 250, 237, 245, 43, 199, 65, 62, 153, 160,
44, 108, 21, 125, 197, 154, 231, 115, 225, 38, 238, 229, 143, 203, 159, 65, 147, 18, 9,
224, 14, 43, 58, 16, 7, 148, 2, 187, 97, 95, 70, 174, 68, 149, 7, 79, 223, 124, 207, 57,
214, 242, 126, 2, 7, 3, 198, 202, 26, 136, 237, 106, 205, 11, 227, 120, 162, 104, 22, 167,
192, 124, 239, 39, 201, 157, 45, 85, 147, 247, 1, 240, 217, 220, 218, 79, 238, 135, 100,
22, 44, 88, 95, 9, 64, 224, 101, 57, 54, 171, 218, 6, 160, 137, 97, 114, 90, 32, 47, 184,
];
let padding = PaddingScheme::new_pkcs1v15_sign(Some(rsa::hash::Hash::SHA2_256));
let verification = public_key.verify(padding, &hashed_msg, &signature);

match verification {
Ok(_) => println!("Signature verified successfully."),
Err(e) => println!("Failed to verify signature: {:?}", e),
}
let verified = match verification {
Ok(_) => {
println!("Signature verified successfully.");
true
}
Err(e) => {
println!("Failed to verify signature: {:?}", e);
false
}
};

// Write the output of the program.
//
// Behind the scenes, this also compiles down to a custom system call which handles writing
sp1_zkvm::io::commit(&verified);
}
Loading