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
use crate::halo2_proofs::halo2curves::group::ff::PrimeField;
use halo2_base::utils::BigPrimeField;
use ethers_core::types;
pub use ethers_core::types::{
transaction::{eip2930::AccessList, response::Transaction},
Address, Block, Bytes, Signature, H160, H256, H64, U256, U64,
};
pub trait Field: BigPrimeField + PrimeField<Repr = [u8; 32]> {}
impl<F> Field for F where F: BigPrimeField + PrimeField<Repr = [u8; 32]> {}
pub trait ToScalar<F> {
fn to_scalar(&self) -> Option<F>;
}
pub trait ToWord {
fn to_word(&self) -> Word;
}
pub trait ToAddress {
fn to_address(&self) -> Address;
}
pub trait ToBigEndian {
fn to_be_bytes(&self) -> [u8; 32];
}
pub trait ToLittleEndian {
fn to_le_bytes(&self) -> [u8; 32];
}
pub type Word = U256;
impl ToBigEndian for U256 {
fn to_be_bytes(&self) -> [u8; 32] {
let mut bytes = [0u8; 32];
self.to_big_endian(&mut bytes);
bytes
}
}
impl ToLittleEndian for U256 {
fn to_le_bytes(&self) -> [u8; 32] {
let mut bytes = [0u8; 32];
self.to_little_endian(&mut bytes);
bytes
}
}
impl<F: Field> ToScalar<F> for U256 {
fn to_scalar(&self) -> Option<F> {
let mut bytes = [0u8; 32];
self.to_little_endian(&mut bytes);
Some(F::from_bytes_le(&bytes))
}
}
impl ToAddress for U256 {
fn to_address(&self) -> Address {
Address::from_slice(&self.to_be_bytes()[12..])
}
}
pub type Hash = types::H256;
impl ToWord for Hash {
fn to_word(&self) -> Word {
Word::from(self.as_bytes())
}
}
impl ToWord for Address {
fn to_word(&self) -> Word {
let mut bytes = [0u8; 32];
bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes());
Word::from(bytes)
}
}
impl ToWord for bool {
fn to_word(&self) -> Word {
if *self {
Word::one()
} else {
Word::zero()
}
}
}
impl<F: Field> ToScalar<F> for Address {
fn to_scalar(&self) -> Option<F> {
let mut bytes = [0u8; 32];
bytes[32 - Self::len_bytes()..].copy_from_slice(self.as_bytes());
bytes.reverse();
Some(F::from_bytes_le(&bytes))
}
}
impl<F: Field> ToScalar<F> for bool {
fn to_scalar(&self) -> Option<F> {
self.to_word().to_scalar()
}
}
#[macro_export]
macro_rules! address {
($addr_hex:expr) => {{
use std::str::FromStr;
$crate::Address::from_str(&$addr_hex).expect("invalid hex Address")
}};
}
#[macro_export]
macro_rules! word {
($word_hex:expr) => {
$crate::Word::from_str_radix(&$word_hex, 16).expect("invalid hex Word")
};
}
#[macro_export]
macro_rules! word_map {
() => {
std::collections::HashMap::new()
};
($($key_hex:expr => $value_hex:expr),*) => {
{
std::collections::HashMap::from_iter([(
$(word!($key_hex), word!($value_hex)),*
)])
}
}
}