@@ -10,7 +10,9 @@ use crate::miniscript::context;
10
10
use crate :: miniscript:: satisfy:: Placeholder ;
11
11
use crate :: plan:: Assets ;
12
12
use crate :: prelude:: * ;
13
- use crate :: { DescriptorPublicKey , MiniscriptKey , ScriptContext , ToPublicKey } ;
13
+ use crate :: {
14
+ DescriptorPublicKey , Error , MiniscriptKey , ScriptContext , ToPublicKey , MAX_ASSET_THRESHOLD ,
15
+ } ;
14
16
pub ( crate ) fn varint_len ( n : usize ) -> usize { bitcoin:: VarInt ( n as u64 ) . len ( ) }
15
17
16
18
pub ( crate ) trait ItemSize {
@@ -56,7 +58,7 @@ pub(crate) fn witness_to_scriptsig(witness: &[Vec<u8>]) -> ScriptBuf {
56
58
} else {
57
59
let push = <& PushBytes >:: try_from ( wit. as_slice ( ) )
58
60
. expect ( "All pushes in miniscript are <73 bytes" ) ;
59
- b = b. push_slice ( push)
61
+ b = b. push_slice ( push) ;
60
62
}
61
63
}
62
64
b. into_script ( )
@@ -129,12 +131,11 @@ pub fn combine_assets(
129
131
combine_assets ( k, dpk_v, index + 1 , current_assets. clone ( ) , all_assets) ;
130
132
let mut new_asset = current_assets;
131
133
new_asset = new_asset. add ( dpk_v[ index] . clone ( ) ) ;
132
- println ! ( "{:#?}" , new_asset) ;
133
134
combine_assets ( k - 1 , dpk_v, index + 1 , new_asset, all_assets)
134
135
}
135
136
136
137
// Do product of K combinations
137
- pub fn get_combinations_product ( values : & [ u64 ] , k : u64 ) -> Vec < u64 > {
138
+ pub fn get_combinations_product ( values : & [ u32 ] , k : u32 ) -> Vec < u32 > {
138
139
let mut products = Vec :: new ( ) ;
139
140
let n = values. len ( ) ;
140
141
@@ -145,10 +146,10 @@ pub fn get_combinations_product(values: &[u64], k: u64) -> Vec<u64> {
145
146
// Using bitwise operations to generate combinations
146
147
let max_combinations = 1u32 << n;
147
148
for combination_bits in 1 ..max_combinations {
148
- if combination_bits. count_ones ( ) as usize == k as usize {
149
+ if ( combination_bits. count_ones ( ) as usize ) == ( k as usize ) {
149
150
let mut product = 1 ;
150
151
for i in 0 ..n {
151
- if combination_bits & ( 1u32 << i) != 0 {
152
+ if ( combination_bits & ( 1u32 << i) ) != 0 {
152
153
product *= values[ i] ;
153
154
}
154
155
}
@@ -160,9 +161,20 @@ pub fn get_combinations_product(values: &[u64], k: u64) -> Vec<u64> {
160
161
}
161
162
162
163
// ways to select k things out of n
163
- pub fn k_of_n ( k : u64 , n : u64 ) -> u64 {
164
- if k == 0 || k == n {
165
- return 1 ;
164
+ pub fn k_of_n ( k : u32 , n : u32 ) -> Result < u32 , Error > {
165
+ let mut k = k;
166
+ if k > n - k {
167
+ k = n - k;
166
168
}
167
- k_of_n ( k - 1 , n - 1 ) + k_of_n ( k, n - 1 )
169
+
170
+ let mut result = 1 ;
171
+ for i in 0 ..k {
172
+ result *= n - i;
173
+ result /= i + 1 ;
174
+ if result > MAX_ASSET_THRESHOLD . into ( ) {
175
+ return Err ( Error :: MaxAssetThresholdExceeded ) ;
176
+ }
177
+ }
178
+
179
+ Ok ( result)
168
180
}
0 commit comments