rue_clvm/
path.rs

1use clvmr::{reduction::EvalErr, Allocator, NodePtr};
2use num_bigint::BigInt;
3use num_traits::One;
4
5pub fn path_to_node(allocator: &mut Allocator, path: &BigInt) -> Result<NodePtr, EvalErr> {
6    let bytes = path.to_signed_bytes_be();
7    let mut slice = bytes.as_slice();
8    while (!slice.is_empty()) && (slice[0] == 0) {
9        slice = &slice[1..];
10    }
11    allocator.new_atom(slice)
12}
13
14pub fn compose_paths(a: BigInt, mut b: BigInt) -> BigInt {
15    let mut mask = BigInt::one();
16    let mut temp_path = a.clone();
17    while temp_path > BigInt::one() {
18        b <<= 1;
19        mask <<= 1;
20        temp_path >>= 1;
21    }
22
23    mask -= 1;
24    b | (a & mask)
25}
26
27pub fn first_path(path: BigInt) -> BigInt {
28    compose_paths(path, BigInt::from(2))
29}
30
31pub fn rest_path(path: BigInt) -> BigInt {
32    compose_paths(path, BigInt::from(3))
33}