jets: rip out as_mut_bytes

This commit is contained in:
Matthew LeVan 2024-01-08 17:01:37 -05:00
parent 0f9175ada6
commit 5b0b389775
4 changed files with 37 additions and 33 deletions

View File

@ -170,7 +170,7 @@ mod util {
stack: &mut NockStack,
key: &mut [u8; N],
ads: Noun,
mut txt: Atom,
txt: Atom,
) -> Result {
unsafe {
let ac_siv_data = _allocate_ads(stack, ads)?;
@ -180,16 +180,17 @@ mod util {
);
let txt_len = met(3, txt);
let txt_bytes = &mut (txt.as_mut_bytes()[0..txt_len]);
let (mut iv, iv_bytes) = IndirectAtom::new_raw_mut_bytearray::<16, NockStack>(stack);
match txt_len {
0 => {
ac_aes_siv_en::<N>(key, txt_bytes, siv_data, iv_bytes, &mut [0u8; 0]).unwrap();
ac_aes_siv_en::<N>(key, &mut [], siv_data, iv_bytes, &mut [0u8; 0]).unwrap();
Ok(T(stack, &[iv.normalize_as_atom().as_noun(), D(0), D(0)]))
}
_ => {
let (_txt_ida, txt_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len);
txt_bytes.copy_from_slice(&txt.as_bytes()[0..txt_len]);
let (mut out_atom, out_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len);
ac_aes_siv_en::<N>(key, txt_bytes, siv_data, iv_bytes, out_bytes).unwrap();
Ok(T(
@ -209,19 +210,20 @@ mod util {
stack: &mut NockStack,
key: &mut [u8; N],
ads: Noun,
mut iv: Atom,
iv: Atom,
len: Atom,
mut txt: Atom,
txt: Atom,
) -> Result {
unsafe {
let txt_len = match len.as_direct() {
Ok(direct) => direct.data() as usize,
Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))),
};
let txt_bytes = &mut (txt.as_mut_bytes()[0..txt_len]);
let (_txt_ida, txt_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len);
txt_bytes.copy_from_slice(&txt.as_bytes()[0..txt_len]);
let iv_bytes = &mut [0u8; 16];
iv_bytes.copy_from_slice(&iv.as_mut_bytes()[0..16]);
iv_bytes.copy_from_slice(&iv.as_bytes()[0..16]);
let ac_siv_data = _allocate_ads(stack, ads)?;
let siv_data: &mut [&mut [u8]] = std::slice::from_raw_parts_mut(

View File

@ -61,7 +61,7 @@ pub fn jet_shar(context: &mut Context, subject: Noun) -> Result {
pub fn jet_sign(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
let mut msg = slot(subject, 12)?.as_atom()?;
let msg = slot(subject, 12)?.as_atom()?;
let sed = slot(subject, 13)?.as_atom()?;
unsafe {
@ -73,7 +73,8 @@ pub fn jet_sign(context: &mut Context, subject: Noun) -> Result {
seed[0..sed_bytes.len()].copy_from_slice(sed_bytes);
let msg_len = met(3, msg);
let message = &mut (msg.as_mut_bytes())[0..msg_len]; // drop trailing zeros
let (_msg_ida, message) = IndirectAtom::new_raw_mut_bytes(stack, msg_len);
message.copy_from_slice(&msg.as_bytes()[0..msg_len]);
let (mut sig_ida, sig) = IndirectAtom::new_raw_mut_bytearray::<64, NockStack>(stack);
ac_ed_sign(message, seed, sig);

View File

@ -11,14 +11,15 @@ pub fn jet_shas(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
let sam = slot(subject, 6)?;
let sal = slot(sam, 2)?.as_atom()?;
let mut ruz = slot(sam, 3)?.as_atom()?;
let ruz = slot(sam, 3)?.as_atom()?;
let sal_bytes = &(sal.as_bytes())[0..met(3, sal)]; // drop trailing zeros
let (mut _salt_ida, salt) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) };
salt.copy_from_slice(sal_bytes);
let msg_len = met(3, ruz);
let message = &mut (ruz.as_mut_bytes())[0..msg_len]; // drop trailing zeros
let (mut _msg_ida, message) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, msg_len) };
message.copy_from_slice(&ruz.as_bytes()[0..msg_len]);
unsafe {
let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
@ -30,12 +31,14 @@ pub fn jet_shas(context: &mut Context, subject: Noun) -> Result {
pub fn jet_shax(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
let sam = slot(subject, 6)?;
let mut msg = sam.as_atom()?;
let msg = sam.as_atom()?;
let len = met(3, msg);
unsafe {
let (mut _msg_ida, msg_copy) = IndirectAtom::new_raw_mut_bytes(stack, len);
msg_copy.copy_from_slice(&msg.as_bytes()[0..len]);
let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
ac_shay(&mut (msg.as_mut_bytes())[0..len], out);
ac_shay(&mut (msg_copy)[0..len], out);
Ok(ida.normalize_as_atom().as_noun())
}
}
@ -44,18 +47,23 @@ pub fn jet_shay(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
let sam = slot(subject, 6)?;
let wid = slot(sam, 2)?.as_atom()?;
let mut dat = slot(sam, 3)?.as_atom()?;
let dat = slot(sam, 3)?.as_atom()?;
let width = match wid.as_direct() {
Ok(direct) => direct.data() as usize,
Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))),
};
let message = &mut (dat.as_mut_bytes())[0..width];
unsafe {
let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
ac_shay(message, out);
if width > 0 {
let (mut _msg_ida, msg) = IndirectAtom::new_raw_mut_bytes(stack, width);
msg.copy_from_slice(&dat.as_bytes()[0..width]);
ac_shay(msg, out);
}
else {
ac_shay(&mut [], out);
}
Ok(out_ida.normalize_as_atom().as_noun())
}
}
@ -64,7 +72,7 @@ pub fn jet_shal(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
let sam = slot(subject, 6)?;
let wid = slot(sam, 2)?.as_atom()?;
let mut dat = slot(sam, 3)?.as_atom()?;
let dat = slot(sam, 3)?.as_atom()?;
let _width = match wid.as_direct() {
Ok(direct) => direct.data() as usize,
@ -72,11 +80,17 @@ pub fn jet_shal(context: &mut Context, subject: Noun) -> Result {
};
let msg_len = met(3, dat);
let message = &mut (dat.as_mut_bytes())[0..msg_len]; // drop trailing zeros
unsafe {
let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 64);
ac_shal(message, out);
if msg_len > 0 {
let (mut _msg_ida, msg) = IndirectAtom::new_raw_mut_bytes(stack, msg_len);
msg.copy_from_slice(&dat.as_bytes()[0..msg_len]);
ac_shal(msg, out);
}
else {
ac_shal(&mut [], out);
}
Ok(ida.normalize_as_atom().as_noun())
}
}

View File

@ -249,11 +249,6 @@ impl DirectAtom {
let bytes: &[u8; 8] = unsafe { std::mem::transmute(&self.0) };
&bytes[..]
}
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
let bytes: &mut [u8; 8] = unsafe { std::mem::transmute(&mut self.0) };
&mut bytes[..]
}
}
impl fmt::Display for DirectAtom {
@ -824,14 +819,6 @@ impl Atom {
}
}
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
if self.is_direct() {
unsafe { self.direct.as_mut_bytes() }
} else {
unsafe { self.indirect.as_mut_bytes() }
}
}
pub fn as_u64(self) -> Result<u64> {
if self.is_direct() {
Ok(unsafe { self.direct.data() })