|
| 1 | +#![feature(lang_items)] |
| 2 | +#![feature(start)] |
| 3 | +#![feature(core_intrinsics)] |
| 4 | +#![feature(panic_info_message)] |
| 5 | +#![no_std] |
| 6 | +extern crate libc; |
| 7 | +extern crate secp256k1; |
| 8 | + |
| 9 | +use core::fmt::*; |
| 10 | +use core::intrinsics; |
| 11 | +use core::panic::PanicInfo; |
| 12 | + |
| 13 | +use secp256k1::*; |
| 14 | + |
| 15 | +#[start] |
| 16 | +fn start(_argc: isize, _argv: *const *const u8) -> isize { |
| 17 | + let mut buf = [0u8; 600_000]; |
| 18 | + let size = Secp256k1::preallocate_size(); |
| 19 | + unsafe { libc::printf("needed size: %d\n\0".as_ptr() as _, size) }; |
| 20 | + |
| 21 | + let secp = Secp256k1::preallocated_new(&mut buf).unwrap(); |
| 22 | + let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order"); |
| 23 | + let public_key = PublicKey::from_secret_key(&secp, &secret_key); |
| 24 | + let message = Message::from_slice(&[0xab; 32]).expect("32 bytes"); |
| 25 | + |
| 26 | + let sig = secp.sign(&message, &secret_key); |
| 27 | + assert!(secp.verify(&message, &sig, &public_key).is_ok()); |
| 28 | + unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) }; |
| 29 | + 0 |
| 30 | +} |
| 31 | + |
| 32 | +// These functions are used by the compiler, but not |
| 33 | +// for a bare-bones hello world. These are normally |
| 34 | +// provided by libstd. |
| 35 | +#[lang = "eh_personality"] |
| 36 | +#[no_mangle] |
| 37 | +pub extern "C" fn rust_eh_personality() {} |
| 38 | + |
| 39 | +// This function may be needed based on the compilation target. |
| 40 | +#[lang = "eh_unwind_resume"] |
| 41 | +#[no_mangle] |
| 42 | +pub extern "C" fn rust_eh_unwind_resume() {} |
| 43 | + |
| 44 | +const MAX_PRINT: usize = 511; |
| 45 | +struct Print { |
| 46 | + loc: usize, |
| 47 | + buf: [u8; 512], |
| 48 | +} |
| 49 | + |
| 50 | +impl Print { |
| 51 | + pub fn new() -> Self { |
| 52 | + Self { |
| 53 | + loc: 0, |
| 54 | + buf: [0u8; 512], |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pub fn print(&self) { |
| 59 | + unsafe { |
| 60 | + let newline = "\n"; |
| 61 | + libc::printf(self.buf.as_ptr() as _); |
| 62 | + libc::printf(newline.as_ptr() as _); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Write for Print { |
| 68 | + fn write_str(&mut self, s: &str) -> Result { |
| 69 | + let curr = self.loc; |
| 70 | + if curr + s.len() > MAX_PRINT { |
| 71 | + unsafe { |
| 72 | + libc::printf("overflow\n\0".as_ptr() as _); |
| 73 | + intrinsics::abort(); |
| 74 | + } |
| 75 | + } |
| 76 | + self.loc += s.len(); |
| 77 | + self.buf[curr..self.loc].copy_from_slice(s.as_bytes()); |
| 78 | + Ok(()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[panic_handler] |
| 83 | +fn panic(info: &PanicInfo) -> ! { |
| 84 | + unsafe { libc::printf("shi1\n\0".as_ptr() as _) }; |
| 85 | + let msg = info.message().unwrap(); |
| 86 | + let mut buf = Print::new(); |
| 87 | + write(&mut buf, *msg).unwrap(); |
| 88 | + buf.print(); |
| 89 | + unsafe { intrinsics::abort() } |
| 90 | +} |
0 commit comments