-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from LedgerHQ/client-sdk
Created initial code for the `vanadium_client_sdk`; deleted the `host` crate.
- Loading branch information
Showing
27 changed files
with
1,448 additions
and
726 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,61 @@ | ||
This will be a test V-app. | ||
This is a test V-App, with a few simple computations and functionalities to test various aspects of the Vanadium VM. | ||
|
||
- [app](app) contains the Risc-V app, based on the V-app Sdk. | ||
- [client](client) folder contains the client of the app, based on the V-app Client Sdk. | ||
- [client](client) folder contains the client of the app, based on the V-app Client Sdk. | ||
|
||
The `client` is a library crate (see [lib.rs](client/src/lib.rs)), but it also has a test executable ([main.rs](client/src/main.rs)) to interact with the app from the command line. | ||
|
||
## Build the V-App | ||
|
||
### Risc-V | ||
|
||
In order to build the app for the Risc-V target, enter the `app` folder and run: | ||
|
||
```sh | ||
cargo build --release --target=riscv32i-unknown-none-elf | ||
``` | ||
|
||
### Native | ||
|
||
In order to build the app for the native target, enter the `app` folder and run: | ||
|
||
```sh | ||
cargo build --release --target=x86_64-unknown-linux-gnu | ||
``` | ||
|
||
## Run the V-App | ||
|
||
Make sure you built the V-App for the Risc-V target. | ||
|
||
Launch Vanadium on speculos. Then execute: | ||
|
||
From the `client` folder | ||
|
||
```sh | ||
cargo run | ||
``` | ||
|
||
If you want to run the V-app on a real device, execute instead: | ||
|
||
```sh | ||
cargo run -- --hid | ||
``` | ||
|
||
If you want to run the V-app natively, after building it for the native target, use: | ||
|
||
```sh | ||
cargo run -- --native | ||
``` | ||
|
||
|
||
### Client commands | ||
|
||
Once the client is running, these are the available commands: | ||
|
||
- `reverse <hex_buffer>` - Reverses the given buffer. | ||
- `sha256 <hex_buffer>` - Computes the sha256 hash of the given buffer. | ||
- `b58enc <hex_buffer>` - Computes the base58 encoding of the given buffer (the output is in hex as well). | ||
- `addnumbers <n>` - Computes the sum of the numbers between `1` and `n`. | ||
- `nprimes <n>` - Counts the number of primes up to `n` using the Sieve of Eratosthenes. | ||
- `panic <panic message>` - Cause the V-App to panic. Everything written after 'panic' is the panic message. | ||
- An empty command will exit the V-App. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Duplicated for simplicity with the corresponding module in the client crate. | ||
// Make sure to keep them in sync. | ||
|
||
#[derive(Debug)] | ||
pub enum Command { | ||
Reverse, | ||
AddNumbers, | ||
Base58Encode, | ||
Sha256, | ||
CountPrimes, | ||
Panic = 0xff, | ||
} | ||
|
||
impl TryFrom<u8> for Command { | ||
type Error = (); | ||
|
||
fn try_from(byte: u8) -> Result<Self, Self::Error> { | ||
match byte { | ||
0x00 => Ok(Command::Reverse), | ||
0x01 => Ok(Command::AddNumbers), | ||
0x02 => Ok(Command::Base58Encode), | ||
0x03 => Ok(Command::Sha256), | ||
0x04 => Ok(Command::CountPrimes), | ||
0xff => Ok(Command::Panic), | ||
_ => Err(()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use alloc::vec::Vec; | ||
|
||
pub fn handle_base58_encode(data: &[u8]) -> Vec<u8> { | ||
bs58::encode(data).into_vec() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use alloc::{vec, vec::Vec}; | ||
|
||
fn count_primes(n: u32) -> u32 { | ||
if n < 2 { | ||
return 0; | ||
} | ||
|
||
let mut is_prime = vec![true; (n + 1) as usize]; | ||
is_prime[0] = false; | ||
is_prime[1] = false; | ||
|
||
let mut i = 2; | ||
let mut square = 4; | ||
loop { | ||
if square > n { | ||
break; | ||
} | ||
if is_prime[i as usize] { | ||
let mut multiple = i * i; | ||
while multiple <= n { | ||
is_prime[multiple as usize] = false; | ||
multiple += i; | ||
} | ||
} | ||
square += 2 * i + 1; | ||
i += 1; | ||
} | ||
|
||
// Count the primes | ||
is_prime.iter().filter(|&&p| p).count() as u32 | ||
} | ||
|
||
pub fn handle_count_primes(data: &[u8]) -> Vec<u8> { | ||
if data.len() != 4 { | ||
return vec![]; | ||
} | ||
let n = u32::from_be_bytes(data[0..4].try_into().unwrap()); | ||
let count = count_primes(n); | ||
count.to_be_bytes().to_vec() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_count_primes() { | ||
assert_eq!(super::count_primes(0), 0); | ||
assert_eq!(super::count_primes(1), 0); | ||
assert_eq!(super::count_primes(2), 1); | ||
assert_eq!(super::count_primes(3), 2); | ||
assert_eq!(super::count_primes(4), 2); | ||
assert_eq!(super::count_primes(5), 3); | ||
assert_eq!(super::count_primes(10000), 1229); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod base58; | ||
mod count_primes; | ||
mod sha256; | ||
|
||
pub use base58::handle_base58_encode; | ||
pub use count_primes::handle_count_primes; | ||
pub use sha256::handle_sha256; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use alloc::vec::Vec; | ||
use sha2::Digest; | ||
|
||
pub fn handle_sha256(data: &[u8]) -> Vec<u8> { | ||
sha2::Sha256::digest(data).to_vec() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.