-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathraw_input.rs
96 lines (84 loc) · 2.22 KB
/
raw_input.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
use axiom_codec::{utils::native::encode_addr_to_field, HiLo};
use axiom_query::axiom_eth::Field;
use ethers::types::{Address, H256};
use super::flatten::FixLenVec;
pub trait RawInput<F: Field> {
type FEType<T: Copy>;
fn convert(&self) -> Self::FEType<F>;
}
impl<F: Field> RawInput<F> for usize {
type FEType<T: Copy> = T;
fn convert(&self) -> Self::FEType<F> {
F::from_u128(*self as u128)
}
}
impl<F: Field> RawInput<F> for u32 {
type FEType<T: Copy> = T;
fn convert(&self) -> Self::FEType<F> {
F::from(*self as u64)
}
}
impl<F: Field> RawInput<F> for u64 {
type FEType<T: Copy> = T;
fn convert(&self) -> Self::FEType<F> {
F::from(*self)
}
}
impl<F: Field> RawInput<F> for u128 {
type FEType<T: Copy> = T;
fn convert(&self) -> Self::FEType<F> {
F::from_u128(*self)
}
}
impl<F: Field> RawInput<F> for H256 {
type FEType<T: Copy> = HiLo<T>;
fn convert(&self) -> Self::FEType<F> {
HiLo::from(*self)
}
}
impl<F: Field> RawInput<F> for Address {
type FEType<T: Copy> = T;
fn convert(&self) -> Self::FEType<F> {
encode_addr_to_field(self)
}
}
impl<F: Field, const N: usize> RawInput<F> for [u8; N] {
type FEType<T: Copy> = [T; N];
fn convert(&self) -> Self::FEType<F> {
let mut res = [F::ZERO; N];
for i in 0..N {
res[i] = F::from(self[i] as u64);
}
res
}
}
impl<F: Field, const N: usize> RawInput<F> for [u64; N] {
type FEType<T: Copy> = [T; N];
fn convert(&self) -> Self::FEType<F> {
let mut res = [F::ZERO; N];
for i in 0..N {
res[i] = F::from(self[i]);
}
res
}
}
impl<F: Field, const N: usize> RawInput<F> for [usize; N] {
type FEType<T: Copy> = [T; N];
fn convert(&self) -> Self::FEType<F> {
let mut res = [F::ZERO; N];
for i in 0..N {
res[i] = F::from(self[i] as u64);
}
res
}
}
impl<F: Field, const N: usize> RawInput<F> for FixLenVec<usize, N> {
type FEType<T: Copy> = [T; N];
fn convert(&self) -> Self::FEType<F> {
let mut res = [F::ZERO; N];
for i in 0..N {
res[i] = F::from(self.0[i] as u64);
}
res
}
}