1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "bn.js")]
extern "C" {
    pub type BN;

    #[wasm_bindgen(constructor)]
    pub fn new(number: String, base: u32) -> BN;

    #[wasm_bindgen(constructor)]
    pub fn new_from_array(number: &[u8], base: u32) -> BN;

    #[wasm_bindgen(method, getter)]
    pub fn negative(this: &BN) -> u8; // positive = 0, negative = 1

    #[wasm_bindgen(method, js_name = byteLength)]
    pub fn byte_length(this: &BN) -> u32;

    #[wasm_bindgen(catch, method, js_name = toArray)]
    pub fn to_array(this: &BN, endian: String, length: u32) -> Result<Box<[u8]>, JsValue>;

    #[wasm_bindgen(method)]
    pub fn toString(this: &BN, base: u32) -> String;
}

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

    use super::BN;

    #[wasm_bindgen_test]
    fn new() {
        assert_eq!(BN::new("10".into(), 10).toString(10), "10".to_string());
        assert_eq!(BN::new("dead".into(), 16).toString(16), "dead".to_string());
    }

    #[wasm_bindgen_test]
    fn new_from_array() {
        assert_eq!(
            BN::new_from_array(&[0, 10, 0], 10).toString(10),
            "2560".to_string()
        );
        assert_eq!(
            BN::new_from_array(&[30, 0], 16).toString(16),
            "1e00".to_string()
        );
    }

    #[wasm_bindgen_test]
    fn negative() {
        assert_eq!(BN::new("10".into(), 10).negative(), 0);
        assert_eq!(BN::new("-10".into(), 10).negative(), 1);
    }

    #[wasm_bindgen_test]
    fn byte_length() {
        assert_eq!(BN::new("10".into(), 10).byte_length(), 1);
        assert_eq!(BN::new("10000".into(), 10).byte_length(), 2);
    }

    #[wasm_bindgen_test]
    fn to_array() {
        assert_eq!(
            *BN::new("10000".into(), 10)
                .to_array("le".into(), 5)
                .unwrap(),
            [16, 39, 0, 0, 0]
        );
        assert_eq!(
            *BN::new("10000".into(), 10)
                .to_array("be".into(), 5)
                .unwrap(),
            [0, 0, 0, 39, 16]
        );
    }

    #[wasm_bindgen_test]
    fn to_string() {
        assert_eq!(*BN::new("10".into(), 10).toString(10), "10".to_string());
        assert_eq!(*BN::new("dead".into(), 16).toString(16), "dead".to_string());
    }
}