Skip to content

Commit

Permalink
jets: add puck
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-levan committed Oct 6, 2023
1 parent 9a367af commit f5b2a67
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
7 changes: 5 additions & 2 deletions rust/ares/src/jets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ pub mod text;
pub mod tree;

use crate::jets::bits::*;
use crate::jets::crypto::*;
use crate::jets::form::*;
use crate::jets::hash::*;
use crate::jets::math::*;
use crate::jets::nock::*;
use crate::jets::text::*;
use crate::jets::tree::*;
use crate::jets::crypto::ed::*;
use crate::jets::crypto::sha::*;
use crate::mem::NockStack;
use crate::newt::Newt;
use crate::noun::{self, Noun, Slots};
Expand Down Expand Up @@ -89,12 +90,14 @@ pub fn get_jet(jet_name: Noun) -> Option<Jet> {
tas!(b"scow") => Some(jet_scow),
//
tas!(b"mink") => Some(jet_mink),
//
// sha
tas!(b"sha1") => Some(jet_sha1),
tas!(b"shal") => Some(jet_shal),
tas!(b"shas") => Some(jet_shas),
tas!(b"shax") => Some(jet_shax),
tas!(b"shay") => Some(jet_shay),
// ed
tas!(b"puck") => Some(jet_puck),
_ => {
// eprintln!("Unknown jet: {:?}", jet_name);
None
Expand Down
2 changes: 2 additions & 0 deletions rust/ares/src/jets/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod ed;
pub mod sha;
67 changes: 67 additions & 0 deletions rust/ares/src/jets/crypto/ed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use either::{Left, Right};
use crate::jets::util::slot;
use crate::jets::{JetErr, Result};
use crate::mem::NockStack;
use crate::newt::Newt;
use crate::noun::{IndirectAtom, Noun};
use urcrypt_sys::*;

crate::gdb!();

/*
static u3_atom
_cqee_puck(u3_atom sed)
{
c3_y sed_y[32];
if ( 0 != u3r_bytes_fit(32, sed_y, sed) ) {
// hoon explicitly crashes on mis-size
return u3m_bail(c3__exit);
}
else {
c3_y pub_y[32];
urcrypt_ed_puck(sed_y, pub_y);
return u3i_bytes(32, pub_y);
}
}
*/

pub fn jet_puck(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
let sam = slot(subject, 6)?.as_direct()?;

if sam.bit_size() > 32 {
return Err(JetErr::Deterministic); // right?
}

unsafe {
let (mut _seed_ida, seed) = IndirectAtom::new_raw_mut_bytes(stack, 32);
let sam_bytes = sam.data().to_le_bytes();
// copy sam_bytes into seed one by one
for i in 0..sam_bytes.len() {
seed[i] = sam_bytes[i];
}

let (mut pub_ida, pub_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
urcrypt_ed_puck(seed.as_ptr(), pub_key.as_mut_ptr());
Ok(pub_ida.normalize_as_atom().as_noun())
}
}

#[cfg(test)]
mod tests {
use super::*;
use ibig::ubig;
// use crate::noun::{D, T, DIRECT_MAX};
use crate::noun::D;
use crate::jets::util::test::{A, assert_jet, init_stack, assert_jet_err, assert_jet_ubig};
// use crate::jets::JetErr;

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

let ret = A(s, &ubig!(_0xfb099b0acc4d1ce37f9982a2ed331245e0cdfdf6979364b7676a142b8233e53b));
assert_jet(s, jet_puck, D(32), ret);
}

}

0 comments on commit f5b2a67

Please sign in to comment.