mirror of
https://github.com/urbit/ares.git
synced 2024-11-30 07:25:44 +03:00
jets: cleanup nasty byte copying
This commit is contained in:
parent
d7f5160a78
commit
91a8565afa
@ -1,4 +1,4 @@
|
|||||||
use crate::jets::util::slot;
|
use crate::jets::util::{met,slot};
|
||||||
use crate::jets::{JetErr, Result};
|
use crate::jets::{JetErr, Result};
|
||||||
use crate::mem::NockStack;
|
use crate::mem::NockStack;
|
||||||
use crate::newt::Newt;
|
use crate::newt::Newt;
|
||||||
@ -9,37 +9,19 @@ crate::gdb!();
|
|||||||
|
|
||||||
pub fn jet_sign(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
pub fn jet_sign(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
||||||
let sam = slot(subject, 6)?;
|
let sam = slot(subject, 6)?;
|
||||||
let message = slot(sam, 2)?.as_atom()?;
|
let msg = slot(sam, 2)?.as_atom()?;
|
||||||
let seed = slot(sam, 3)?.as_atom()?;
|
let sed = slot(sam, 3)?.as_atom()?;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut msg_bytes = message.as_bytes();
|
let msg_bytes = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros
|
||||||
// TODO: absolutely disgusting
|
|
||||||
// remove the leading/trailing 0s
|
|
||||||
while msg_bytes.len() > 0 && msg_bytes[msg_bytes.len() - 1] == 0 {
|
|
||||||
msg_bytes = &msg_bytes[..msg_bytes.len() - 1];
|
|
||||||
}
|
|
||||||
while msg_bytes.len() > 0 && msg_bytes[0] == 0 {
|
|
||||||
msg_bytes = &msg_bytes[1..];
|
|
||||||
}
|
|
||||||
let msg_len = msg_bytes.len();
|
|
||||||
let (mut _msg_ida, msg) = IndirectAtom::new_raw_mut_bytes(stack, msg_len);
|
|
||||||
for i in 0..msg_len {
|
|
||||||
msg[i] = msg_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut _sed_ida, sed) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
let sed_bytes = sed.as_bytes();
|
||||||
let sed_bytes = seed.as_bytes();
|
let (mut _seed_ida, seed) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
||||||
let sed_len = sed_bytes.len();
|
if sed_bytes.len() > 32 { return Err(JetErr::Deterministic) };
|
||||||
if sed_len > 32 {
|
seed.copy_from_slice(sed_bytes);
|
||||||
return Err(JetErr::Deterministic);
|
|
||||||
}
|
|
||||||
for i in 0..sed_len {
|
|
||||||
sed[i] = sed_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut sig_ida, sig) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
let (mut sig_ida, sig) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
||||||
urcrypt_ed_sign(msg.as_ptr(), msg_len, sed.as_ptr(), sig.as_mut_ptr());
|
urcrypt_ed_sign(msg_bytes.as_ptr(), msg_bytes.len(), seed.as_ptr(), sig.as_mut_ptr());
|
||||||
sig.reverse(); // LSB first
|
sig.reverse(); // LSB first
|
||||||
|
|
||||||
Ok(sig_ida.normalize_as_atom().as_noun())
|
Ok(sig_ida.normalize_as_atom().as_noun())
|
||||||
@ -47,55 +29,34 @@ pub fn jet_sign(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: N
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn jet_veri(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
pub fn jet_veri(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
||||||
let signature = slot(subject, 12)?.as_atom()?;
|
let sig = slot(subject, 12)?.as_atom()?;
|
||||||
let message = slot(subject, 26)?.as_atom()?;
|
let msg = slot(subject, 26)?.as_atom()?;
|
||||||
let public_key = slot(subject, 27)?.as_atom()?;
|
let puk = slot(subject, 27)?.as_atom()?;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let (mut _sig_ida, sig) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
let (mut _sig_ida, signature) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
||||||
let sig_bytes = signature.as_bytes();
|
let sig_bytes = sig.as_bytes();
|
||||||
if sig_bytes.len() > 64 {
|
// vere punts; we should do the same in the future
|
||||||
// vere punts; we should do the same in the future
|
if sig_bytes.len() > 64 { return Err(JetErr::NonDeterministic) };
|
||||||
return Err(JetErr::NonDeterministic);
|
signature.copy_from_slice(sig_bytes);
|
||||||
}
|
|
||||||
for i in 0..sig_bytes.len() {
|
|
||||||
sig[i] = sig_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut _pub_ida, pub_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
let (mut _pub_ida, public_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
||||||
let pub_bytes = public_key.as_bytes();
|
let pub_bytes = puk.as_bytes();
|
||||||
if pub_bytes.len() > 32 {
|
// vere punts; we should do the same in the future
|
||||||
// vere punts; we should do the same in the future
|
if pub_bytes.len() > 32 { return Err(JetErr::NonDeterministic) };
|
||||||
return Err(JetErr::NonDeterministic);
|
public_key.copy_from_slice(pub_bytes);
|
||||||
}
|
|
||||||
for i in 0..pub_bytes.len() {
|
|
||||||
pub_key[i] = pub_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut msg_bytes = message.as_bytes();
|
let message = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros
|
||||||
// TODO: absolutely disgusting
|
|
||||||
// remove the leading/trailing 0s
|
let valid = urcrypt_ed_veri(message.as_ptr(), message.len(), public_key.as_ptr(), signature.as_ptr());
|
||||||
while msg_bytes.len() > 0 && msg_bytes[msg_bytes.len() - 1] == 0 {
|
|
||||||
msg_bytes = &msg_bytes[..msg_bytes.len() - 1];
|
|
||||||
}
|
|
||||||
while msg_bytes.len() > 0 && msg_bytes[0] == 0 {
|
|
||||||
msg_bytes = &msg_bytes[1..];
|
|
||||||
}
|
|
||||||
let msg_len = msg_bytes.len();
|
|
||||||
let (mut _msg_ida, msg) = IndirectAtom::new_raw_mut_bytes(stack, msg_len);
|
|
||||||
for i in 0..msg_len {
|
|
||||||
msg[i] = msg_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let valid = urcrypt_ed_veri(msg.as_ptr(), msg_len, pub_key.as_ptr(), sig.as_ptr());
|
|
||||||
Ok(if valid { YES } else { NO })
|
Ok(if valid { YES } else { NO })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jet_shar(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
pub fn jet_shar(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
||||||
let sam = slot(subject, 6)?;
|
let pub_key = slot(subject, 12)?.as_direct()?;
|
||||||
let pub_key = slot(sam, 2)?.as_direct()?;
|
let sec_key = slot(subject, 13)?.as_direct()?;
|
||||||
let sec_key = slot(sam, 3)?.as_direct()?;
|
|
||||||
|
|
||||||
if sec_key.bit_size() > 32 {
|
if sec_key.bit_size() > 32 {
|
||||||
return Err(JetErr::Deterministic);
|
return Err(JetErr::Deterministic);
|
||||||
@ -112,35 +73,31 @@ pub fn jet_shar(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: N
|
|||||||
let pub_bytes = pub_key.data().to_le_bytes();
|
let pub_bytes = pub_key.data().to_le_bytes();
|
||||||
let sec_bytes = sec_key.data().to_le_bytes();
|
let sec_bytes = sec_key.data().to_le_bytes();
|
||||||
|
|
||||||
for i in 0..pub_bytes.len() {
|
public[0..pub_bytes.len()].copy_from_slice(&pub_bytes[..]);
|
||||||
public[i] = pub_bytes[i];
|
secret[0..sec_bytes.len()].copy_from_slice(&sec_bytes[..]);
|
||||||
}
|
|
||||||
for i in 0..sec_bytes.len() {
|
|
||||||
secret[i] = sec_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut shar_ida, shar) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
let (mut shar_ida, shar) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
||||||
urcrypt_ed_shar(public.as_ptr(), secret.as_ptr(), shar.as_mut_ptr());
|
urcrypt_ed_shar(public.as_ptr(), secret.as_ptr(), shar.as_mut_ptr());
|
||||||
|
|
||||||
Ok(shar_ida.normalize_as_atom().as_noun())
|
Ok(shar_ida.normalize_as_atom().as_noun())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jet_puck(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
pub fn jet_puck(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
||||||
let sam = slot(subject, 6)?.as_direct()?;
|
let sed = slot(subject, 6)?.as_direct()?;
|
||||||
|
|
||||||
if sam.bit_size() > 32 {
|
if sed.bit_size() > 32 {
|
||||||
return Err(JetErr::Deterministic);
|
return Err(JetErr::Deterministic);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let (mut _seed_ida, seed) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
let (mut _seed_ida, seed) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
||||||
let sam_bytes = sam.data().to_le_bytes();
|
let sed_bytes = sed.data().to_le_bytes();
|
||||||
for i in 0..sam_bytes.len() {
|
seed[0..sed_bytes.len()].copy_from_slice(&sed_bytes[..]);
|
||||||
seed[i] = sam_bytes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut pub_ida, pub_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
let (mut pub_ida, pub_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
||||||
urcrypt_ed_puck(seed.as_ptr(), pub_key.as_mut_ptr());
|
urcrypt_ed_puck(seed.as_ptr(), pub_key.as_mut_ptr());
|
||||||
|
|
||||||
Ok(pub_ida.normalize_as_atom().as_noun())
|
Ok(pub_ida.normalize_as_atom().as_noun())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ pub fn jet_shal(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: N
|
|||||||
Err(_) => return Err(JetErr::NonDeterministic),
|
Err(_) => return Err(JetErr::NonDeterministic),
|
||||||
};
|
};
|
||||||
|
|
||||||
let message = dat.as_bytes();
|
let message = &(dat.as_bytes())[0..met(3, dat)]; // drop trailing zeros
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
||||||
@ -52,11 +52,11 @@ pub fn jet_shas(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: N
|
|||||||
let sal = slot(sam, 2)?.as_atom()?;
|
let sal = slot(sam, 2)?.as_atom()?;
|
||||||
let ruz= slot(sam, 3)?.as_atom()?;
|
let ruz= slot(sam, 3)?.as_atom()?;
|
||||||
|
|
||||||
let sal_bytes = sal.as_bytes();
|
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()) };
|
let (mut _salt_ida, salt) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) };
|
||||||
salt.copy_from_slice(sal_bytes);
|
salt.copy_from_slice(sal_bytes);
|
||||||
|
|
||||||
let message = ruz.as_bytes();
|
let message = &(ruz.as_bytes())[0..met(3, ruz)]; // drop trailing zeros
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
|
||||||
|
Loading…
Reference in New Issue
Block a user