-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.rs
79 lines (69 loc) · 2.69 KB
/
test.rs
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
use std::str::FromStr;
use crate::bigint::{U255Cmpeq, U510};
use crate::script_loader::AsmScriptLoader;
use crate::traits::comparable::Comparable;
use crate::traits::integer::{NonNativeInteger, NonNativeLimbInteger};
use crate::{debug::print_script_size, treepp::*};
use num_bigint::{BigUint, RandomBits};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
/// Tests the validity of asm files
#[test]
fn test_asm_files() {
let asm_test_files = [
include_str!("asm_tests/test_1.asm"),
include_str!("asm_tests/test_2.asm"),
include_str!("asm_tests/test_3.asm"),
];
for (i, test_file) in asm_test_files.iter().enumerate() {
println!("testing asm file {:?}...", i);
let test_script = AsmScriptLoader::from_raw_str(test_file);
let exec_result = execute_script(test_script);
assert!(exec_result.success, "test asm file {:?} is wrong!", i);
println!("asm file {:?} is correct!", i);
}
}
/// Tests pushing a 255-bit number to the stack. Example taken from the original bitcointalk topic:
/// https://bitcointalk.org/index.php?topic=5477449.0
#[test]
fn test_push_le_slice_255_bits() {
let example_number = BigUint::from_str(
"44347314585423944296568073680235476145090606693409235654433373536726375170836",
);
let expected_limbs: Vec<u32> = vec![
25099, 22628, 4378, 17693, 627, 25528, 24377, 28384, 14745, 26534, 13152, 27940, 18633,
23354, 13719, 31767, 788,
];
let script = script! {
{ U255Cmpeq::OP_PUSH_U32LESLICE(&example_number.unwrap().to_u32_digits()) }
for limb in expected_limbs.iter().rev() {
{ *limb }
OP_EQUALVERIFY
}
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
// Tests the multiplication of two 254-bit numbers and two 64-bit numbers.
#[test]
fn test_255_bit_cmpeq_widening_mul() {
const TESTS_NUMBER: usize = 10;
print_script_size("255-bit widening mul", U255Cmpeq::OP_WIDENINGMUL::<U510>());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUMBER {
let a: BigUint = prng.sample(RandomBits::new(255));
let b: BigUint = prng.sample(RandomBits::new(255));
let c: BigUint = a.clone() * b.clone();
let script = script! {
{ U255Cmpeq::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U255Cmpeq::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U255Cmpeq::OP_WIDENINGMUL::<U510>() }
{ U510::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U510::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}