mirror of
https://github.com/urbit/ares.git
synced 2024-12-24 13:55:23 +03:00
jets: add ++ed:veri:crypto
This commit is contained in:
parent
d24fa43f16
commit
fa95ca9dab
@ -2,18 +2,68 @@ use crate::jets::util::slot;
|
||||
use crate::jets::{JetErr, Result};
|
||||
use crate::mem::NockStack;
|
||||
use crate::newt::Newt;
|
||||
use crate::noun::{IndirectAtom, Noun};
|
||||
use crate::noun::{IndirectAtom, Noun, YES, NO};
|
||||
use urcrypt_sys::*;
|
||||
|
||||
crate::gdb!();
|
||||
|
||||
pub fn jet_veri(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
||||
let signature = slot(subject, 12)?.as_atom()?;
|
||||
let message = slot(subject, 26)?.as_atom()?;
|
||||
let public_key = slot(subject, 27)?.as_atom()?;
|
||||
|
||||
unsafe {
|
||||
let (mut _sig_ida, sig) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
||||
let sig_bytes = signature.as_bytes();
|
||||
if sig_bytes.len() > 64 {
|
||||
// vere punts; we should do the same in the future
|
||||
return Err(JetErr::NonDeterministic);
|
||||
}
|
||||
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 pub_bytes = public_key.as_bytes();
|
||||
if pub_bytes.len() > 32 {
|
||||
// vere punts; we should do the same in the future
|
||||
return Err(JetErr::NonDeterministic);
|
||||
}
|
||||
for i in 0..pub_bytes.len() {
|
||||
pub_key[i] = pub_bytes[i];
|
||||
}
|
||||
|
||||
let mut msg_bytes = message.as_bytes();
|
||||
// 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 valid = urcrypt_ed_veri(msg.as_ptr(), msg_len, pub_key.as_ptr(), sig.as_ptr());
|
||||
Ok(if valid { YES } else { NO })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn jet_shar(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: Noun) -> Result {
|
||||
let sam = slot(subject, 6)?;
|
||||
let pub_key = slot(sam, 2)?.as_direct()?;
|
||||
let sec_key = slot(sam, 3)?.as_direct()?;
|
||||
|
||||
if pub_key.bit_size() > 32 || sec_key.bit_size() > 32 {
|
||||
return Err(JetErr::Deterministic); // right?
|
||||
if sec_key.bit_size() > 32 {
|
||||
return Err(JetErr::Deterministic);
|
||||
}
|
||||
if pub_key.bit_size() > 32 {
|
||||
// vere punts; we should do the same in the future
|
||||
return Err(JetErr::NonDeterministic);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
@ -40,13 +90,12 @@ pub fn jet_puck(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: N
|
||||
let sam = slot(subject, 6)?.as_direct()?;
|
||||
|
||||
if sam.bit_size() > 32 {
|
||||
return Err(JetErr::Deterministic); // right?
|
||||
return Err(JetErr::Deterministic);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
@ -62,8 +111,7 @@ mod tests {
|
||||
use super::*;
|
||||
use ibig::ubig;
|
||||
use crate::noun::{D, T};
|
||||
use crate::jets::util::test::{A, assert_jet, init_stack, assert_jet_err, assert_jet_ubig};
|
||||
// use crate::jets::JetErr;
|
||||
use crate::jets::util::test::{A, assert_jet, init_stack, assert_jet_err};
|
||||
|
||||
#[test]
|
||||
fn test_puck() {
|
||||
@ -71,6 +119,9 @@ mod tests {
|
||||
|
||||
let ret = A(s, &ubig!(_0xfb099b0acc4d1ce37f9982a2ed331245e0cdfdf6979364b7676a142b8233e53b));
|
||||
assert_jet(s, jet_puck, D(32), ret);
|
||||
|
||||
let sam = A(s, &ubig!(_0xfb099b0acc4d1ce37f9982a2ed331245e0cdfdf6979364b7676a142b8233e53b));
|
||||
assert_jet_err(s, jet_puck, sam, JetErr::Deterministic);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -80,6 +131,43 @@ mod tests {
|
||||
let sam = T(s, &[D(234), D(234)]);
|
||||
let ret = A(s, &ubig!(_0x6ecd5779a47841207a2cd0c9d085796aa646842885a332adac540027d768c1c5));
|
||||
assert_jet(s, jet_shar, sam, ret);
|
||||
|
||||
let sam = A(s, &ubig!(_0xfb099b0acc4d1ce37f9982a2ed331245e0cdfdf6979364b7676a142b8233e53b));
|
||||
assert_jet_err(s, jet_shar, sam, JetErr::Deterministic);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_veri() {
|
||||
let s = &mut init_stack();
|
||||
|
||||
unsafe {
|
||||
let sig_ubig = ubig!(_0x92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00);
|
||||
let sig_bytes = sig_ubig.to_be_bytes();
|
||||
let signature = IndirectAtom::new_raw_bytes(s, sig_bytes.len(), sig_bytes.as_ptr()).as_noun();
|
||||
|
||||
let message = D(0x72);
|
||||
|
||||
let pub_ubig = ubig!(_0x3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c);
|
||||
let pub_bytes = pub_ubig.to_be_bytes();
|
||||
let public_key = IndirectAtom::new_raw_bytes(s, pub_bytes.len(), pub_bytes.as_ptr()).as_noun();
|
||||
|
||||
let sam = T(s, &[signature, message, public_key]);
|
||||
assert_jet(s, jet_veri, sam, YES);
|
||||
|
||||
let sig_ubig = ubig!(_0x6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a);
|
||||
let sig_bytes = sig_ubig.to_be_bytes();
|
||||
let signature = IndirectAtom::new_raw_bytes(s, sig_bytes.len(), sig_bytes.as_ptr()).as_noun();
|
||||
|
||||
let msg_ubig = ubig!(0xaf82);
|
||||
let msg_bytes = msg_ubig.to_be_bytes();
|
||||
let message = IndirectAtom::new_raw_bytes(s, msg_bytes.len(), msg_bytes.as_ptr()).as_noun();
|
||||
|
||||
let pub_ubig = ubig!(_0xfc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025);
|
||||
let pub_bytes = pub_ubig.to_be_bytes();
|
||||
let public_key = IndirectAtom::new_raw_bytes(s, pub_bytes.len(), pub_bytes.as_ptr()).as_noun();
|
||||
|
||||
let sam = T(s, &[signature, message, public_key]);
|
||||
assert_jet(s, jet_veri, sam, YES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,18 +60,7 @@ pub fn jet_shal(stack: &mut NockStack, _newt: &mut Option<&mut Newt>, subject: N
|
||||
}
|
||||
};
|
||||
|
||||
let dat_direct_clone: DirectAtom;
|
||||
let dat_indirect_clone: IndirectAtom;
|
||||
let message = match dat.as_either() {
|
||||
Left(direct) => {
|
||||
dat_direct_clone = direct.clone();
|
||||
dat_direct_clone.as_byteslice()
|
||||
}
|
||||
Right(indirect) => {
|
||||
dat_indirect_clone = indirect.clone();
|
||||
dat_indirect_clone.as_bytes()
|
||||
}
|
||||
};
|
||||
let message = dat.as_bytes();
|
||||
|
||||
unsafe {
|
||||
let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 64);
|
||||
|
@ -196,8 +196,7 @@ impl DirectAtom {
|
||||
}
|
||||
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
let bytes: &[u8; 8] = unsafe { std::mem::transmute(self.0) };
|
||||
&bytes[..]
|
||||
self.as_byteslice()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user