Test serde support in no-std crate

This commit is contained in:
Elichai Turkel 2019-10-24 21:05:04 +03:00
parent d0afb874a3
commit e5e22ee376
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
2 changed files with 15 additions and 1 deletions

View File

@ -4,8 +4,9 @@ version = "0.1.0"
authors = ["Elichai Turkel <elichai.turkel@gmail.com>"]
[dependencies]
secp256k1 = { path = "../", default-features = false, features = ["rand"] }
secp256k1 = { path = "../", default-features = false, features = ["serde", "rand"] }
libc = { version = "0.2", default-features = false }
serde_cbor = { version = "0.10", default-features = false } # A random serializer that supports no-std.
[profile.release]

View File

@ -5,6 +5,7 @@
#![no_std]
extern crate libc;
extern crate secp256k1;
extern crate serde_cbor;
use core::fmt::{self, write, Write};
use core::intrinsics;
@ -14,6 +15,10 @@ use secp256k1::rand::{self, RngCore};
use secp256k1::serde::Serialize;
use secp256k1::*;
use serde_cbor::de;
use serde_cbor::ser::SliceWrite;
use serde_cbor::Serializer;
struct FakeRng;
impl RngCore for FakeRng {
fn next_u32(&mut self) -> u32 {
@ -48,6 +53,14 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
let sig = secp.sign(&message, &secret_key);
assert!(secp.verify(&message, &sig, &public_key).is_ok());
let mut cbor_ser = [0u8; 100];
let writer = SliceWrite::new(&mut cbor_ser[..]);
let mut ser = Serializer::new(writer);
sig.serialize(&mut ser).unwrap();
let size = ser.into_inner().bytes_written();
let new_sig: Signature = de::from_mut_slice(&mut cbor_ser[..size]).unwrap();
assert_eq!(sig, new_sig);
unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) };
0
}