Skip to content

Commit

Permalink
Bignum syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC01 committed May 23, 2021
1 parent 8664b2c commit 504b55c
Show file tree
Hide file tree
Showing 15 changed files with 4,659 additions and 821 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/bpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ members = [
"rust/128bit",
"rust/128bit_dep",
"rust/alloc",
"rust/bignum",
"rust/call_depth",
"rust/caller_access",
"rust/custom_heap",
Expand Down
1 change: 1 addition & 0 deletions programs/bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn main() {
let rust_programs = [
"128bit",
"alloc",
"bignum",
"call_depth",
"caller_access",
"custom_heap",
Expand Down
19 changes: 19 additions & 0 deletions programs/bpf/rust/bignum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "solana-bpf-rust-bignum"
version = "1.7.0"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-bpf-rust-bignum"
edition = "2018"

[dependencies]
solana-program = { path = "../../../../sdk/program", version = "=1.7.0" }

[lib]
crate-type = ["cdylib"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
130 changes: 130 additions & 0 deletions programs/bpf/rust/bignum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//! @brief BigNumber Syscall test

// #17082
extern crate solana_program;
use solana_program::{bignum::BigNumber, custom_panic_default, msg};

const LONG_DEC_STRING: &str = "1470463693494555670176851280755142329532258274256991544781479988\
712408107190720087233560906792937436573943189716784305633216335039\
300236370809933808677983409391545753391897467230180786617074456716\
591448871466263060696957107957862111484694673874424855359234132302\
162208163361387727626078022804936564470716886986414133429438273232\
416190048073715996321578752244853524209178212395809614878549824744\
227969245726015222693764433413133633359171080169137831743765672068\
374040331773668233371864426354886263106537340208256187214278963052\
996538599452325797319977413534714912781503130883692806087195354368\
8304190675878204079994222";
const NEG_LONG_DEC_STRING: &str =
"-1470463693494555670176851280755142329532258274256991544781479988\
712408107190720087233560906792937436573943189716784305633216335039\
300236370809933808677983409391545753391897467230180786617074456716\
591448871466263060696957107957862111484694673874424855359234132302\
162208163361387727626078022804936564470716886986414133429438273232\
416190048073715996321578752244853524209178212395809614878549824744\
227969245726015222693764433413133633359171080169137831743765672068\
374040331773668233371864426354886263106537340208256187214278963052\
996538599452325797319977413534714912781503130883692806087195354368\
8304190675878204079994222";

/// BigNumber construction
fn test_constructors() {
msg!("BigNumber constructors");
let base_bn_0 = BigNumber::new();
assert_eq!(base_bn_0.to_bytes(), vec![0]);
let default_0 = BigNumber::default();
assert!(base_bn_0 == default_0);
let new_bn_0 = BigNumber::from_u32(0);
assert!(new_bn_0 == default_0);
let max_bn_u32 = BigNumber::from_u32(u32::MAX);
assert_eq!(max_bn_u32.to_bytes(), vec![255, 255, 255, 255]);
let bn_from_dec = BigNumber::from_dec_str(LONG_DEC_STRING);
assert!(!bn_from_dec.is_negative());
let bn_from_dec = BigNumber::from_dec_str(NEG_LONG_DEC_STRING);
assert!(bn_from_dec.is_negative());
}

/// BigNumber simple number and simple maths
fn test_basic_maths() {
msg!("BigNumber Basic Maths");
let bn_5 = BigNumber::from_u32(5);
let bn_258 = BigNumber::from_u32(258);
let added = bn_5.add(&bn_258);
assert_eq!(added.to_bytes(), [1, 7]);
let subed = bn_5.sub(&bn_258);
assert_eq!(subed.to_bytes(), vec![253]);
let muled = bn_5.mul(&bn_5);
assert_eq!(muled.to_bytes(), vec![25]);
let bn_300 = BigNumber::from_u32(300);
let bn_10 = BigNumber::from_u32(10);
let dived = bn_300.div(&bn_10);
assert_eq!(dived.to_bytes(), vec![30]);
}

/// BigNumber bigger numbers and complex maths
fn test_complex_maths() {
msg!("BigNumber Complex Maths");
let bn_arg1 = BigNumber::from_u32(300);
let sqr_res = bn_arg1.sqr();
assert_eq!(sqr_res.to_bytes(), vec![1, 95, 144]);
let bn_arg2 = BigNumber::from_u32(8);
let bn_arg3 = BigNumber::from_u32(2);
let exp_res = bn_arg2.exp(&bn_arg3);
assert_eq!(exp_res.to_bytes(), vec![64]);
let bn_arg1 = BigNumber::from_u32(300);
let bn_arg2 = BigNumber::from_u32(11);
let mod_sqr = bn_arg1.mod_sqr(&bn_arg2);
assert_eq!(mod_sqr.to_bytes(), vec![9]);
let bn_arg1 = BigNumber::from_u32(300);
let bn_arg2 = BigNumber::from_u32(11);
let bn_arg3 = BigNumber::from_u32(7);
let mod_exp = bn_arg1.mod_exp(&bn_arg2, &bn_arg3);
assert_eq!(mod_exp.to_bytes(), vec![6]);
let mod_mul = bn_arg1.mod_mul(&bn_arg2, &bn_arg3);
assert_eq!(mod_mul.to_bytes(), vec![3]);
let bn_arg1 = BigNumber::from_u32(415);
let bn_arg2 = BigNumber::from_u32(7);
let mod_inv = bn_arg1.mod_inv(&bn_arg2);
assert_eq!(mod_inv.to_bytes(), vec![4]);
}

fn test_output_logging() {
let bn_from_dec = BigNumber::from_dec_str(LONG_DEC_STRING);
bn_from_dec.log();
let bn_from_dec = BigNumber::from_dec_str(NEG_LONG_DEC_STRING);
bn_from_dec.log();
}

#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
msg!("bignum");
test_constructors();
test_basic_maths();
test_complex_maths();
test_output_logging();
0u64
}

custom_panic_default!();

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_basic_constructors_pass() {
test_constructors();
}
#[test]
fn test_simple_maths_pass() {
test_basic_maths();
}
#[test]
fn test_complex_maths_pass() {
test_complex_maths();
}

#[test]
fn test_logging() {
test_output_logging();
}
}
1 change: 1 addition & 0 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ fn test_program_bpf_sanity() {
programs.extend_from_slice(&[
("solana_bpf_rust_128bit", true),
("solana_bpf_rust_alloc", true),
("solana_bpf_rust_bignum", true),
("solana_bpf_rust_custom_heap", true),
("solana_bpf_rust_dep_crate", true),
("solana_bpf_rust_external_spend", false),
Expand Down
1 change: 1 addition & 0 deletions programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ byteorder = "1.3.4"
log = "0.4.11"
num-derive = "0.3"
num-traits = "0.2"
openssl = "0.10.32"
rand_core = "0.6.2"
sha3 = "0.9.1"
solana-measure = { path = "../../measure", version = "=1.7.0" }
Expand Down
Loading

0 comments on commit 504b55c

Please sign in to comment.