用dplyr+绘制分支运动;ggplot2
我试图说明在四步旋转散列中,输入位如何影响输出位。哈希函数如下所示:用dplyr+绘制分支运动;ggplot2,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我试图说明在四步旋转散列中,输入位如何影响输出位。哈希函数如下所示: #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) uint32_t rotating_hash(uint32_t state, uint32_t input) { uint32_t hash = state; uint8_t *p = (uint8_t*)&input; // mix ; combi
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
uint32_t rotating_hash(uint32_t state, uint32_t input)
{
uint32_t hash = state;
uint8_t *p = (uint8_t*)&input;
// mix ; combine
hash ^= *(p++);
hash += rot(hash, 4) ^ *(p++);
hash += rot(hash, 4) ^ *(p++);
hash += rot(hash, 4) ^ *p;
return hash;
}
library(tibble)
library(dplyr)
library(magrittr)
rot_hash_positions <- function(bit) {
operation_1 <- bit
operation_2 <- outer(operation_1, c(4, 0, -28), FUN = '+') %>% as.vector() %>% { . %% 32 }
operation_3 <- outer(operation_2, c(4, 0, -28), FUN = '+') %>% as.vector() %>% { . %% 32 }
operation_4 <- outer(operation_3, c(4, 0, -28), FUN = '+') %>% as.vector() %>% { . %% 32 }
rbind(tibble(bit = bit, operation = 1, positions = operation_1),
tibble(bit = bit, operation = 2, positions = operation_2),
tibble(bit = bit, operation = 3, positions = operation_3),
tibble(bit = bit, operation = 4, positions = operation_4))
}
bit_movement <- do.call(rbind, lapply(0:31, rot_hash_positions))
> bit_movement %>% filter(bit == 0)
# A tibble: 10 x 3
bit operation positions
<int> <dbl> <dbl>
1 0 1 0
2 0 2 4
3 0 2 0
4 0 3 8
5 0 3 4
6 0 3 0
7 0 4 12
8 0 4 8
9 0 4 4
10 0 4 0
library(ggplot2)
plot_bitmovement <- function(bit_movement, highlight_bits) {
ggplot(bit_movement, aes(
y = positions,
x = operation,
group = factor(bit, levels = 1:32)
)) +
geom_line(colour = "gray") +
geom_point(colour = "gray") +
geom_line(data = highlight_bits, colour = "black") +
geom_point(data = highlight_bits, colour = "black") +
coord_flip() +
scale_y_reverse(breaks = 0:31, labels = 1:32) +
scale_x_reverse() +
theme_minimal() +
theme(
legend.position = "none"
) + ylab("Bit-position") + xlab("Operation")
}
其中我突出显示了绘图中的第一个或最后一个字节:
bit_movement <- do.call(rbind, lapply(0:31, rot_hash_positions))
plot_bitmovement(bit_movement, bit_movement %>% filter(bit < 8))
位\u移动百分比过滤器(位<8))
last_byte我想出了这个主意。我不知道它有多优雅,所以我还是喜欢评论它
library(purrr)
get_steps <- function(bm, step) {
op1 <- bm %>%
filter(operation == !!(step-1)) %>%
mutate(x = operation, y = positions) %>%
select(bit, x, y)
op2 <- bm %>%
filter(operation == !!step) %>%
mutate(xend = operation, yend = positions) %>%
select(bit, xend, yend)
inner_join(op1, op2, by = "bit")
}
plot_bitmovement <- function(bit_movement, highlight_bits) {
bm_segs <- map(2:4, ~ get_steps(bit_movement, .x)) %>% bind_rows()
hl_segs <- map(2:4, ~ get_steps(highlight_bits, .x)) %>% bind_rows()
ggplot(bm_segs, aes(
x = x,
y = y,
xend = xend,
yend = yend)
) +
geom_segment(colour = "grey") +
geom_point(colour = "grey") +
geom_point(aes(x = xend, y = yend), colour = "grey") +
geom_segment(data = hl_segs, colour = "black") +
geom_point(data = hl_segs, colour = "black") +
geom_point(aes(x = xend, y = yend), data = hl_segs, colour = "black") +
coord_flip() +
scale_y_reverse(breaks = 0:31, labels = 1:32) +
scale_x_reverse() +
theme_minimal() +
theme(
legend.position = "none"
) + ylab("Bit-position") + xlab("Operation")
}
bit_movement <- do.call(rbind, lapply(0:31, rot_hash_positions))
plot_bitmovement(bit_movement, bit_movement %>% filter(bit < 8))
库(purrr)
获得%
突变(x=操作,y=位置)%>%
选择(位、x、y)
op2%
过滤器(操作==!!步骤)%>%
变异(xend=操作,yend=位置)%>%
选择(位、xend、yend)
内部连接(op1,op2,by=“bit”)
}
不幸的是,要理解你在寻找什么有点困难。是否有可能制作一个较小的示例,并提供所需输出的相应“图纸”?干杯。下面我自己尝试画的这幅画,显示了我的目标。我想绘制显示散列函数中每个combine+mix操作中各个位如何移动的段。我在下面自己的尝试中做到了。。。现在,我想,我只是在寻找一种优雅的方式来做到这一点。我现在所拥有的将起作用,除了我必须确定在计算哈希函数的函数(我总是需要它)和对get\u steps
函数的调用中执行了多少操作。我想我可以通过max over手术得到它,不过。。。
library(purrr)
get_steps <- function(bm, step) {
op1 <- bm %>%
filter(operation == !!(step-1)) %>%
mutate(x = operation, y = positions) %>%
select(bit, x, y)
op2 <- bm %>%
filter(operation == !!step) %>%
mutate(xend = operation, yend = positions) %>%
select(bit, xend, yend)
inner_join(op1, op2, by = "bit")
}
plot_bitmovement <- function(bit_movement, highlight_bits) {
bm_segs <- map(2:4, ~ get_steps(bit_movement, .x)) %>% bind_rows()
hl_segs <- map(2:4, ~ get_steps(highlight_bits, .x)) %>% bind_rows()
ggplot(bm_segs, aes(
x = x,
y = y,
xend = xend,
yend = yend)
) +
geom_segment(colour = "grey") +
geom_point(colour = "grey") +
geom_point(aes(x = xend, y = yend), colour = "grey") +
geom_segment(data = hl_segs, colour = "black") +
geom_point(data = hl_segs, colour = "black") +
geom_point(aes(x = xend, y = yend), data = hl_segs, colour = "black") +
coord_flip() +
scale_y_reverse(breaks = 0:31, labels = 1:32) +
scale_x_reverse() +
theme_minimal() +
theme(
legend.position = "none"
) + ylab("Bit-position") + xlab("Operation")
}
bit_movement <- do.call(rbind, lapply(0:31, rot_hash_positions))
plot_bitmovement(bit_movement, bit_movement %>% filter(bit < 8))