Skip to content

Commit

Permalink
jets: add shax
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-levan committed Oct 3, 2023
1 parent 58af42e commit e2331d3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
65 changes: 64 additions & 1 deletion rust/ares/src/jets/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,49 @@ use crate::jets::util::{met, slot};
use crate::jets::{JetErr, Result};
use crate::mem::NockStack;
use crate::newt::Newt;
use crate::noun::{IndirectAtom, Noun};
use crate::noun::{DirectAtom, IndirectAtom, Noun};
use urcrypt_sys::*;

crate::gdb!();

pub fn jet_shas(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
let sam = slot(subject, 6)?;
let sal = slot(sam, 2)?.as_atom()?;
let ruz= slot(sam, 3)?.as_atom()?;

let mut sdc: DirectAtom;
let mut sic: IndirectAtom;
let salt = match sal.as_either() {
Left(direct) => {
sdc = direct.clone();
sdc.as_byteslice_mut()
}
Right(indirect) => {
sic = indirect.clone();
sic.as_mut_bytes()
}
};

let mdc: DirectAtom;
let mic: IndirectAtom;
let message = match ruz.as_either() {
Left(direct) => {
mdc = direct.clone();
mdc.as_byteslice()
}
Right(indirect) => {
mic = indirect.clone();
mic.as_bytes()
}
};

unsafe {
let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
urcrypt_shas(salt.as_mut_ptr(), salt.len(), message.as_ptr(), message.len(), out.as_mut_ptr());
Ok(ida.normalize_as_atom().as_noun())
}
}

pub fn jet_shax(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
let sam = slot(subject, 6)?;
let a = sam.as_atom()?;
Expand Down Expand Up @@ -124,5 +162,30 @@ mod tests {
D(123456789),
ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72)
);

// TODO test indirect atoms
}

#[test]
fn test_shas() {
let s = &mut init_stack();

let sam = T(s, &[D(1), D(1)]);
assert_jet_ubig(
s,
jet_shas,
sam,
ubig!(_0x547da92584bc986e5784edb746c29504bfd6b34572c83b7b96440ca77d35cdfc)
);

let sam = T(s, &[D(2), D(2)]);
assert_jet_ubig(
s,
jet_shas,
sam,
ubig!(_0x4cf01fe7cc56ef70d17735322488de0d31857afcfe451e199abe6295f78f5328)
);

// TODO test indirect atoms
}
}
4 changes: 4 additions & 0 deletions rust/ares/src/noun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ impl IndirectAtom {
unsafe { from_raw_parts(self.data_pointer() as *const u8, self.size() << 3) }
}

pub fn as_mut_bytes(&mut self) -> &mut [u8] {
unsafe { from_raw_parts_mut(self.data_pointer_mut() as *mut u8, self.size() << 3) }
}

/** BitSlice view on an indirect atom, with lifetime tied to reference to indirect atom. */
pub fn as_bitslice(&self) -> &BitSlice<u64, Lsb0> {
BitSlice::from_slice(self.as_slice())
Expand Down

0 comments on commit e2331d3

Please sign in to comment.