-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.simf
More file actions
212 lines (183 loc) · 6.42 KB
/
utils.simf
File metadata and controls
212 lines (183 loc) · 6.42 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
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
#ifndef IMPORTED_UTILS_SIMF
#define IMPORTED_UTILS_SIMF
fn eq_128(val1: u128, val2: u128) -> bool {
jet::eq_256(<(u128, u128)>::into((0, val1)), <(u128, u128)>::into((0, val2)))
}
fn right_shift_256(value: u256, shift: u8) -> u256 {
let (a, b, c, d): (u64, u64, u64, u64) = <u256>::into(value);
let (a, b, c, d): (u64, u64, u64, u64) = match jet::lt_8(shift, 64) {
true => (a, b, c, d),
false => {
match jet::lt_8(shift, 128) {
true => (0, a, b, c),
false => {
match jet::lt_8(shift, 192) {
true => (0, 0, a, b),
false => (0, 0, 0, a),
}
}
}
}
};
let shift: u8 = jet::modulo_8(shift, 64);
let (_, left_shift): (bool, u8) = jet::subtract_8(64, shift);
let new_d: u64 = jet::or_64(jet::left_shift_64(left_shift, c), jet::right_shift_64(shift, d));
let new_c: u64 = jet::or_64(jet::left_shift_64(left_shift, b), jet::right_shift_64(shift, c));
let new_b: u64 = jet::or_64(jet::left_shift_64(left_shift, a), jet::right_shift_64(shift, b));
let new_a: u64 = jet::right_shift_64(shift, a);
<(u64, u64, u64, u64)>::into((new_a, new_b, new_c, new_d))
}
fn right_shift_128(value: u128, shift: u8) -> u128 {
let (a, b, c, d): (u32, u32, u32, u32) = <u128>::into(value);
let (a, b, c, d): (u32, u32, u32, u32) = match jet::lt_8(shift, 32) {
true => (a, b, c, d),
false => {
match jet::lt_8(shift, 64) {
true => (0, a, b, c),
false => {
match jet::lt_8(shift, 96) {
true => (0, 0, a, b),
false => (0, 0, 0, a),
}
}
}
}
};
let shift: u8 = jet::modulo_8(shift, 32);
let (_, left_shift): (bool, u8) = jet::subtract_8(32, shift);
let new_d: u32 = jet::or_32(jet::left_shift_32(left_shift, c), jet::right_shift_32(shift, d));
let new_c: u32 = jet::or_32(jet::left_shift_32(left_shift, b), jet::right_shift_32(shift, c));
let new_b: u32 = jet::or_32(jet::left_shift_32(left_shift, a), jet::right_shift_32(shift, b));
let new_a: u32 = jet::right_shift_32(shift, a);
<(u32, u32, u32, u32)>::into((new_a, new_b, new_c, new_d))
}
fn get_bit_at(value: u256, possition: u8) -> u1 {
let (a, b, c, d): (u64, u64, u64, u64) = <u256>::into(value);
let part: u64 = match jet::lt_8(possition, 64) {
true => d,
false => {
match jet::lt_8(possition, 128) {
true => c,
false => {
match jet::lt_8(possition, 192) {
true => b,
false => a,
}
},
}
},
};
let (_, in_part_possition): (u8, u8) = jet::div_mod_8(possition, 64);
<bool>::into(jet::lt_64(0, jet::and_64(part, jet::left_shift_64(in_part_possition, 1))))
}
fn get_byte_at(value: u256, possition: u8) -> u8 {
assert!(jet::lt_8(possition, 32));
let (a, b, c, d): (u64, u64, u64, u64) = <u256>::into(value);
let part: u64 = match jet::lt_8(possition, 8) {
true => d,
false => {
match jet::lt_8(possition, 16) {
true => c,
false => {
match jet::lt_8(possition, 24) {
true => b,
false => a,
}
},
}
},
};
let possition: u8 = jet::modulo_8(possition, 8);
let (a, b, c, d): (u16, u16, u16, u16) = <u64>::into(part);
let part: u16 = match jet::lt_8(possition, 2) {
true => d,
false => {
match jet::lt_8(possition, 4) {
true => c,
false => {
match jet::lt_8(possition, 6) {
true => b,
false => a,
}
},
}
},
};
let possition: u8 = jet::modulo_8(possition, 2);
let (a, b): (u8, u8) = <u16>::into(part);
match jet::eq_8(possition, 0) {
true => b,
false => a,
}
}
fn get_byte_at_128(value: u128, possition: u8) -> u8 {
assert!(jet::lt_8(possition, 16));
let (a, b, c, d): (u32, u32, u32, u32) = <u128>::into(value);
let part: u32 = match jet::lt_8(possition, 4) {
true => a,
false => {
match jet::lt_8(possition, 8) {
true => b,
false => {
match jet::lt_8(possition, 12) {
true => c,
false => d,
}
},
}
},
};
let possition: u8 = jet::modulo_8(possition, 4);
let (a, b, c, d): (u8, u8, u8, u8) = <u32>::into(part);
match jet::eq_8(possition, 0) {
true => a,
false => {
match jet::eq_8(possition, 1) {
true => b,
false => {
match jet::eq_8(possition, 2) {
true => c,
false => d,
}
},
}
},
}
}
fn get_at_array_iter(el: u256, ctx: (u8, u8, u256)) -> (u8, u8, u256) {
let (idx, pos, saved): (u8, u8, u256) = ctx;
match jet::eq_8(idx, pos) {
true => {
let (_, new_pos): (bool, u8) = jet::add_8(pos, 1);
(idx, new_pos, el)
},
false => {
let (_, new_pos): (bool, u8) = jet::add_8(pos, 1);
(idx, new_pos, saved)
}
}
}
#define DEFINE_GET_AT_ARR(N) \
fn get_at_array_##N(arr: [u256; N], index: u8) -> u256 { \
let (_, _, el): (u8, u8, u256) = array_fold::<get_at_array_iter, N>(arr, (index, 0, 0)); \
el \
}
fn get_at_array_128_iter(el: u128, ctx: (u32, u32, u128)) -> (u32, u32, u128) {
let (idx, pos, saved): (u32, u32, u128) = ctx;
match jet::eq_32(idx, pos) {
true => {
let (_, new_pos): (bool, u32) = jet::add_32(pos, 1);
(idx, new_pos, el)
},
false => {
let (_, new_pos): (bool, u32) = jet::add_32(pos, 1);
(idx, new_pos, saved)
}
}
}
#define DEFINE_GET_AT_ARR_128(N) \
fn get_at_array_128_##N(arr: [u128; N], index: u32) -> u128 { \
let (_, _, el): (u32, u32, u128) = array_fold::<get_at_array_128_iter, N>(arr, (index, 0, 0)); \
el \
}
#endif