forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsub.rs
137 lines (121 loc) · 3.75 KB
/
sub.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::bigint::BigIntImpl;
use crate::treepp::*;
impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
/// Compute the difference of two BigInts
pub fn sub(a: u32, b: u32) -> Script {
script! {
{Self::zip(a, b)}
{ 1 << LIMB_SIZE }
// A0 - B0
limb_sub_borrow OP_TOALTSTACK
// from A1 - (B1 + borrow_0)
// to A{N-2} - (B{N-2} + borrow_{N-3})
for _ in 0..Self::N_LIMBS - 2 {
OP_ROT
OP_ADD
OP_SWAP
limb_sub_borrow OP_TOALTSTACK
}
// A{N-1} - (B{N-1} + borrow_{N-2})
OP_NIP
OP_ADD
{ limb_sub_noborrow(Self::HEAD_OFFSET) }
for _ in 0..Self::N_LIMBS - 1 {
OP_FROMALTSTACK
}
}
}
}
/// Compute the difference of two limbs, including the carry bit
///
/// Author: @stillsaiko
pub fn limb_sub_borrow() -> Script {
script! {
OP_ROT OP_ROT
OP_SUB
OP_DUP
0
OP_LESSTHAN
OP_TUCK
OP_IF
2 OP_PICK OP_ADD
OP_ENDIF
}
}
/// Compute the sum of two limbs, dropping the carry bit
///
/// Author: @weikengchen
pub fn limb_sub_noborrow(head_offset: u32) -> Script {
script! {
OP_SUB
OP_DUP
0
OP_LESSTHAN
OP_IF
{ head_offset }
OP_ADD
OP_ENDIF
}
}
#[cfg(test)]
mod test {
use crate::bigint::{U254, U64};
use crate::treepp::*;
use core::ops::{Rem, Shl};
use num_bigint::{BigUint, RandomBits};
use num_traits::One;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
#[test]
fn test_sub() {
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..100 {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let mut c: BigUint = BigUint::one().shl(254) + &a - &b;
c = c.rem(BigUint::one().shl(254));
let script = script! {
{ U254::push_u32_le(&a.to_u32_digits()) }
{ U254::push_u32_le(&b.to_u32_digits()) }
{ U254::sub(1, 0) }
{ U254::push_u32_le(&c.to_u32_digits()) }
{ U254::equalverify(1, 0) }
OP_TRUE
};
run(script);
let script = script! {
{ U254::push_u32_le(&b.to_u32_digits()) }
{ U254::push_u32_le(&a.to_u32_digits()) }
{ U254::sub(0, 1) }
{ U254::push_u32_le(&c.to_u32_digits()) }
{ U254::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
for _ in 0..100 {
let a: BigUint = prng.sample(RandomBits::new(64));
let b: BigUint = prng.sample(RandomBits::new(64));
let mut c: BigUint = BigUint::one().shl(64) + &a - &b;
c = c.rem(BigUint::one().shl(64));
let script = script! {
{ U64::push_u32_le(&a.to_u32_digits()) }
{ U64::push_u32_le(&b.to_u32_digits()) }
{ U64::sub(1, 0) }
{ U64::push_u32_le(&c.to_u32_digits()) }
{ U64::equalverify(1, 0) }
OP_TRUE
};
run(script);
let script = script! {
{ U64::push_u32_le(&b.to_u32_digits()) }
{ U64::push_u32_le(&a.to_u32_digits()) }
{ U64::sub(0, 1) }
{ U64::push_u32_le(&c.to_u32_digits()) }
{ U64::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}
}