forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
77 lines (73 loc) · 2.53 KB
/
utils.rs
File metadata and controls
77 lines (73 loc) · 2.53 KB
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
use crate::bigint::BigIntImpl;
use crate::bigint::U256;
use crate::bn254::fq::bigint_to_u32_limbs;
use crate::bn254::fq::Fq;
use crate::bn254::fr::Fr;
use crate::treepp::*;
use ark_ff::BigInt;
#[derive(Debug, Clone)]
pub enum Hint {
Fq(ark_bn254::Fq),
Fr(ark_bn254::Fr),
Hash([u32; U256::N_LIMBS as usize]),
U256(num_bigint::BigInt),
BigIntegerTmulLC1(num_bigint::BigInt),
BigIntegerTmulLC2(num_bigint::BigInt),
BigIntegerTmulLC2W4(num_bigint::BigInt),
BigIntegerTmulLC4(num_bigint::BigInt),
}
impl Hint {
pub fn push(&self) -> Script {
const K1: (u32, u32, u32) = Fq::bigint_tmul_lc_1();
const K2: (u32, u32, u32) = Fq::bigint_tmul_lc_2();
const K3: (u32, u32, u32) = Fq::bigint_tmul_lc_2_w4();
const K4: (u32, u32, u32) = Fq::bigint_tmul_lc_4();
pub type T1 = BigIntImpl<{ K1.0 }, { K1.1 }>;
pub type T2 = BigIntImpl<{ K2.0 }, { K2.1 }>;
pub type T3 = BigIntImpl<{ K3.0 }, { K3.1 }>;
pub type T4 = BigIntImpl<{ K4.0 }, { K4.1 }>;
match self {
Hint::Fq(fq) => script! {
{ Fq::push(*fq) }
},
Hint::Fr(fr) => script! {
{ Fr::push(*fr) }
},
Hint::Hash(hash) => script! {
for h in hash {
{*h}
}
},
Hint::U256(num) => script! {
{ U256::push_u32_le(&bigint_to_u32_limbs(num.clone(), 256)) }
},
Hint::BigIntegerTmulLC1(a) => script! {
{ T1::push_u32_le(&bigint_to_u32_limbs(a.clone(), T1::N_BITS)) }
},
Hint::BigIntegerTmulLC2(a) => script! {
{ T2::push_u32_le(&bigint_to_u32_limbs(a.clone(), T2::N_BITS)) }
},
Hint::BigIntegerTmulLC2W4(a) => script! {
{ T3::push_u32_le(&bigint_to_u32_limbs(a.clone(), T3::N_BITS)) }
},
Hint::BigIntegerTmulLC4(a) => script! {
{ T4::push_u32_le(&bigint_to_u32_limbs(a.clone(), T4::N_BITS)) }
},
}
}
}
pub fn fq_to_bits(fq: BigInt<4>, limb_size: usize) -> Vec<u32> {
let mut bits: Vec<bool> = ark_ff::BitIteratorBE::new(fq.as_ref()).skip(2).collect();
bits.reverse();
bits.chunks(limb_size)
.map(|chunk| {
let mut factor = 1;
let res = chunk.iter().fold(0, |acc, &x| {
let r = acc + if x { factor } else { 0 };
factor *= 2;
r
});
res
})
.collect()
}