forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinv.rs
291 lines (250 loc) · 7.27 KB
/
inv.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use crate::bigint::BigIntImpl;
use crate::pseudo::OP_NDUP;
use crate::treepp::*;
use core::ops::{Mul, Rem, Sub};
use num_bigint::BigUint;
use num_traits::Num;
impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
pub fn div2() -> Script {
script! {
{ Self::div2rem() }
OP_DROP
}
}
pub fn div2rem() -> Script {
script! {
{ Self::N_LIMBS - 1 } OP_ROLL
0
{ limb_shr1_carry(Self::HEAD) }
for _ in 1..Self::N_LIMBS {
{ Self::N_LIMBS } OP_ROLL
OP_SWAP
{ limb_shr1_carry(LIMB_SIZE) }
}
}
}
pub fn div3() -> Script {
script! {
{ Self::div3rem() }
OP_DROP
}
}
pub fn div3rem() -> Script {
script! {
{ Self::N_LIMBS - 1 } OP_ROLL
0
{ limb_div3_carry(Self::HEAD) }
for _ in 1..Self::N_LIMBS {
{ Self::N_LIMBS } OP_ROLL
OP_SWAP
{ limb_div3_carry(LIMB_SIZE) }
}
}
}
}
pub fn limb_shr1_carry(num_bits: u32) -> Script {
let powers_of_2_script = if num_bits < 7 {
script! {
for i in 0..num_bits - 1 {
{ 2_u32.pow(i) }
}
}
} else {
script! {
2 4 8 16 32 64 // 2^1 to 2^6
for _ in 0..num_bits - 7 {
OP_DUP OP_DUP OP_ADD
} // 2^7 to 2^{num_bits - 1}
}
};
script! {
{ powers_of_2_script }
{ num_bits - 1 } OP_ROLL
OP_IF
OP_DUP
OP_ELSE
0
OP_ENDIF
OP_TOALTSTACK
{ num_bits - 1 } OP_ROLL
for _ in 0..num_bits - 2 {
OP_2DUP OP_LESSTHANOREQUAL
OP_IF
OP_SWAP OP_SUB OP_SWAP OP_DUP OP_FROMALTSTACK OP_ADD OP_TOALTSTACK OP_SWAP
OP_ELSE
OP_NIP
OP_ENDIF
}
OP_2DUP OP_LESSTHANOREQUAL
OP_IF
OP_SWAP OP_SUB OP_FROMALTSTACK OP_1ADD
OP_ELSE
OP_NIP OP_FROMALTSTACK
OP_ENDIF
OP_SWAP
}
}
// divide limb by 3, also remainder
pub fn limb_div3_carry(limb_size: u32) -> Script {
let max_limb = (1 << limb_size) as i64;
let x_quotient = max_limb / 3;
let x_remainder = max_limb % 3;
let y_quotient = max_limb * 2 / 3;
let y_remainder = max_limb * 2 % 3;
let mut k = 0;
let mut cur = 1;
while cur < max_limb {
k += 1;
cur *= 3;
}
script! {
1 2 3 6 9 18 27 54
for _ in 0..k - 4 {
OP_2DUP OP_ADD
OP_DUP OP_DUP OP_ADD
}
{ 2 * k } OP_ROLL OP_DUP
0 OP_GREATERTHAN
OP_IF
OP_1SUB
OP_IF
{ y_remainder } { y_quotient }
OP_ELSE
{ x_remainder } { x_quotient }
OP_ENDIF
OP_ELSE
0
OP_ENDIF
OP_TOALTSTACK
{ 2 * k + 1 } OP_ROLL OP_ADD
for _ in 0..2 * k - 2 {
OP_2DUP OP_LESSTHANOREQUAL
OP_IF
OP_SWAP OP_SUB 2 OP_PICK OP_FROMALTSTACK OP_ADD OP_TOALTSTACK
OP_ELSE
OP_NIP
OP_ENDIF
}
OP_NIP OP_NIP OP_FROMALTSTACK OP_SWAP
}
}
#[cfg(test)]
mod test {
use crate::bigint::inv::{limb_div3_carry, limb_shr1_carry};
use crate::bigint::{U254, U64};
use crate::treepp::*;
use core::ops::{Div, Shr};
use num_bigint::{BigUint, RandomBits};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
#[test]
fn test_limb_shr1_carry() {
println!("limb_shr1_carry: {} bytes", limb_shr1_carry(29).len());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << 29;
let script = script! {
{ a }
{ 0 }
{ limb_shr1_carry(29) }
{ a & 1 } OP_EQUALVERIFY
{ a >> 1 } OP_EQUAL
};
run(script);
}
for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << 29;
let script = script! {
{ a }
{ 1 }
{ limb_shr1_carry(29) }
{ a & 1 } OP_EQUALVERIFY
{ (1 << 28) | (a >> 1) } OP_EQUAL
};
run(script);
}
}
#[test]
fn test_limb_div3_carry() {
println!("limb_div3_carry: {} bytes", limb_div3_carry(29).len());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..100 {
let mut a: u32 = prng.gen();
a %= 1 << 29;
let k = 2_u32.pow(29);
for r in 0..3 {
let a2 = a + r * k;
let b = a2 % 3;
let c = a2 / 3;
let script = script! {
{ a }
{ r }
{ limb_div3_carry(29) }
{ b } OP_EQUALVERIFY
{ c } OP_EQUAL
};
run(script);
}
}
}
#[test]
fn test_div2() {
println!("U254.div2: {} bytes", U254::div2().len());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..100 {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone().shr(1);
let script = script! {
{ U254::push_u32_le(&a.to_u32_digits()) }
{ U254::div2() }
{ 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 c: BigUint = a.clone().shr(1);
let script = script! {
{ U64::push_u32_le(&a.to_u32_digits()) }
{ U64::div2() }
{ U64::push_u32_le(&c.to_u32_digits()) }
{ U64::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}
#[test]
fn test_div3() {
println!("U254.div3: {} bytes", U254::div3().len());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..100 {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone().div(BigUint::from(3_u32));
let script = script! {
{ U254::push_u32_le(&a.to_u32_digits()) }
{ U254::div3() }
{ 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 c: BigUint = a.clone().div(BigUint::from(3_u32));
let script = script! {
{ U64::push_u32_le(&a.to_u32_digits()) }
{ U64::div3() }
{ U64::push_u32_le(&c.to_u32_digits()) }
{ U64::equalverify(1, 0) }
OP_TRUE
};
run(script);
}
}
}