Skip to content

Commit d0afb87

Browse files
committed
Test rand support in no std crate
1 parent c117114 commit d0afb87

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

no_std_test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = ["Elichai Turkel <[email protected]>"]
55

66
[dependencies]
7-
secp256k1 = { path = "../", default-features = false }
7+
secp256k1 = { path = "../", default-features = false, features = ["rand"] }
88
libc = { version = "0.2", default-features = false }
99

1010

no_std_test/src/main.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,48 @@
66
extern crate libc;
77
extern crate secp256k1;
88

9-
use core::fmt::*;
9+
use core::fmt::{self, write, Write};
1010
use core::intrinsics;
1111
use core::panic::PanicInfo;
1212

13+
use secp256k1::rand::{self, RngCore};
14+
use secp256k1::serde::Serialize;
1315
use secp256k1::*;
1416

17+
struct FakeRng;
18+
impl RngCore for FakeRng {
19+
fn next_u32(&mut self) -> u32 {
20+
57
21+
}
22+
fn next_u64(&mut self) -> u64 {
23+
57
24+
}
25+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
26+
for i in dest {
27+
*i = 57;
28+
}
29+
Ok(())
30+
}
31+
fn fill_bytes(&mut self, dest: &mut [u8]) {
32+
self.try_fill_bytes(dest).unwrap();
33+
}
34+
}
35+
1536
#[start]
1637
fn start(_argc: isize, _argv: *const *const u8) -> isize {
1738
let mut buf = [0u8; 600_000];
1839
let size = Secp256k1::preallocate_size();
1940
unsafe { libc::printf("needed size: %d\n\0".as_ptr() as _, size) };
2041

21-
let secp = Secp256k1::preallocated_new(&mut buf).unwrap();
22-
let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
42+
let mut secp = Secp256k1::preallocated_new(&mut buf).unwrap();
43+
secp.randomize(&mut FakeRng);
44+
let secret_key = SecretKey::new(&mut FakeRng);
2345
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
2446
let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
2547

2648
let sig = secp.sign(&message, &secret_key);
2749
assert!(secp.verify(&message, &sig, &public_key).is_ok());
50+
2851
unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) };
2952
0
3053
}
@@ -65,7 +88,7 @@ impl Print {
6588
}
6689

6790
impl Write for Print {
68-
fn write_str(&mut self, s: &str) -> Result {
91+
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
6992
let curr = self.loc;
7093
if curr + s.len() > MAX_PRINT {
7194
unsafe {

0 commit comments

Comments
 (0)