Skip to content

Commit

Permalink
Merge pull request #360 from Chia-Network/nil
Browse files Browse the repository at this point in the history
rename null() to nil() for better alignment with LISP
  • Loading branch information
arvidn authored Jan 9, 2024
2 parents 293e9ab + 8b6d79e commit 4e59b6b
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 146 deletions.
28 changes: 14 additions & 14 deletions benches/run-program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use std::fs::read_to_string;
use std::time::Instant;

fn long_strings(a: &mut Allocator) -> NodePtr {
let mut list = a.null();
let mut list = a.nil();
for _i in 0..1000 {
let item = a
.new_atom(b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
.expect("new_atom");
list = a.new_pair(item, list).expect("new_pair");
}

a.new_pair(list, a.null()).expect("new_pair")
a.new_pair(list, a.nil()).expect("new_pair")
}

fn large_tree_impl(a: &mut Allocator, depth: i32) -> NodePtr {
Expand All @@ -37,11 +37,11 @@ fn long_string(a: &mut Allocator) -> NodePtr {
atom.extend(b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}
let item = a.new_atom(&atom[..]).expect("new_atom");
a.new_pair(item, a.null()).expect("new_pair")
a.new_pair(item, a.nil()).expect("new_pair")
}

fn tuple3<const N: i32, const K: i32>(a: &mut Allocator) -> NodePtr {
let list = a.null();
let list = a.nil();
let item = a.new_number(K.into()).expect("new_atom");
let list = a.new_pair(item, list).expect("new_pair");
let item = a.new_number(N.into()).expect("new_atom");
Expand All @@ -51,7 +51,7 @@ fn tuple3<const N: i32, const K: i32>(a: &mut Allocator) -> NodePtr {
}

fn pair<const N: i32>(a: &mut Allocator) -> NodePtr {
let list = a.null();
let list = a.nil();
let item = a.new_number(N.into()).expect("new_atom");
let list = a.new_pair(item, list).expect("new_pair");
let item = a
Expand All @@ -61,18 +61,18 @@ fn pair<const N: i32>(a: &mut Allocator) -> NodePtr {
}

fn single_value<const N: i32>(a: &mut Allocator) -> NodePtr {
let list = a.null();
let list = a.nil();
let item = a.new_number(N.into()).expect("new_atom");
a.new_pair(item, list).expect("new_pair")
}

fn generate_list<const N: i32>(a: &mut Allocator) -> NodePtr {
let mut list = a.null();
let mut list = a.nil();
for _i in 0..N {
let item = a.new_number(42.into()).expect("new_atom");
list = a.new_pair(item, list).expect("new_pair");
}
a.new_pair(list, a.null()).expect("new_pair")
a.new_pair(list, a.nil()).expect("new_pair")
}

fn large_block(a: &mut Allocator) -> NodePtr {
Expand All @@ -81,7 +81,7 @@ fn large_block(a: &mut Allocator) -> NodePtr {
buffer.push((i & 0xff) as u8);
}

let mut list = a.null();
let mut list = a.nil();
for i in 0..1000 {
let hex_key1 = hex::encode(&buffer[i..i + 32]);
let hex_key2 = hex::encode(&buffer[i / 2..i / 2 + 32]);
Expand Down Expand Up @@ -118,13 +118,13 @@ ff83\
}

fn matrix<const W: i32, const H: i32>(a: &mut Allocator) -> NodePtr {
let mut args = a.null();
let mut args = a.nil();

for _l in 0..2 {
let mut col = a.null();
let mut col = a.nil();

for _k in 0..H {
let mut row = a.null();
let mut row = a.nil();
for _i in 0..W {
let val = a.new_atom(b"ccba9401").expect("new_atom");
row = a.new_pair(val, row).expect("new_pair");
Expand Down Expand Up @@ -177,11 +177,11 @@ ffff01ffff33ffa06b7a83babea1eec790c947db4464ab657dbe9b887fe9acc2\
}

fn none(a: &mut Allocator) -> NodePtr {
a.null()
a.nil()
}

fn point_pow(a: &mut Allocator) -> NodePtr {
let list = a.null();
let list = a.nil();
let item = a.new_number(1337.into()).expect("new_atom");
let list = a.new_pair(item, list).expect("new_pair");
let item = a.new_atom(&hex::decode("b3b8ac537f4fd6bde9b26221d49b54b17a506be147347dae5d081c0a6572b611d8484e338f3432971a9823976c6a232b").expect("invalid point hex")).expect("new_atom");
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/fuzzing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const BUFFER: [u8; 63] = [

pub fn make_tree(a: &mut Allocator, cursor: &mut BitCursor, short_atoms: bool) -> NodePtr {
match cursor.read_bits(1) {
None => a.null(),
None => a.nil(),
Some(0) => {
let first = make_tree(a, cursor, short_atoms);
let second = make_tree(a, cursor, short_atoms);
Expand All @@ -65,12 +65,12 @@ pub fn make_tree(a: &mut Allocator, cursor: &mut BitCursor, short_atoms: bool) -
Some(_) => {
if short_atoms {
match cursor.read_bits(8) {
None => a.null(),
None => a.nil(),
Some(val) => a.new_atom(&[val]).unwrap(),
}
} else {
match cursor.read_bits(6) {
None => a.null(),
None => a.nil(),
Some(len) => a.new_atom(&BUFFER[..len as usize]).unwrap(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fuzz_target!(|data: &[u8]| {
}
Ok(r) => r,
};
let args = allocator.null();
let args = allocator.nil();

let allocator_checkpoint = allocator.checkpoint();

Expand Down
36 changes: 18 additions & 18 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum ObjectType {

// The top 6 bits of the NodePtr indicate what type of object it is
impl NodePtr {
pub fn null() -> Self {
pub fn nil() -> Self {
Self::new(ObjectType::Bytes, 0)
}

Expand Down Expand Up @@ -56,7 +56,7 @@ impl NodePtr {

impl Default for NodePtr {
fn default() -> Self {
Self::null()
Self::nil()
}
}

Expand Down Expand Up @@ -170,11 +170,11 @@ impl Allocator {
pub fn new_atom(&mut self, v: &[u8]) -> Result<NodePtr, EvalErr> {
let start = self.u8_vec.len() as u32;
if (self.heap_limit - start as usize) < v.len() {
return err(self.null(), "out of memory");
return err(self.nil(), "out of memory");
}
let idx = self.atom_vec.len();
if idx == MAX_NUM_ATOMS {
return err(self.null(), "too many atoms");
return err(self.nil(), "too many atoms");
}
self.u8_vec.extend_from_slice(v);
let end = self.u8_vec.len() as u32;
Expand All @@ -197,15 +197,15 @@ impl Allocator {
pub fn new_pair(&mut self, first: NodePtr, rest: NodePtr) -> Result<NodePtr, EvalErr> {
let idx = self.pair_vec.len();
if idx == MAX_NUM_PAIRS {
return err(self.null(), "too many pairs");
return err(self.nil(), "too many pairs");
}
self.pair_vec.push(IntPair { first, rest });
Ok(NodePtr::new(ObjectType::Pair, idx))
}

pub fn new_substr(&mut self, node: NodePtr, start: u32, end: u32) -> Result<NodePtr, EvalErr> {
if self.atom_vec.len() == MAX_NUM_ATOMS {
return err(self.null(), "too many atoms");
return err(self.nil(), "too many atoms");
}
let (ObjectType::Bytes, idx) = node.node_type() else {
return err(node, "(internal error) substr expected atom, got pair");
Expand All @@ -231,11 +231,11 @@ impl Allocator {

pub fn new_concat(&mut self, new_size: usize, nodes: &[NodePtr]) -> Result<NodePtr, EvalErr> {
if self.atom_vec.len() == MAX_NUM_ATOMS {
return err(self.null(), "too many atoms");
return err(self.nil(), "too many atoms");
}
let start = self.u8_vec.len();
if self.heap_limit - start < new_size {
return err(self.null(), "out of memory");
return err(self.nil(), "out of memory");
}
self.u8_vec.reserve(new_size);

Expand All @@ -258,7 +258,7 @@ impl Allocator {
if counter != new_size {
self.u8_vec.truncate(start);
return err(
self.null(),
self.nil(),
"(internal error) concat passed invalid new_size",
);
}
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Allocator {
}
}

pub fn null(&self) -> NodePtr {
pub fn nil(&self) -> NodePtr {
NodePtr::new(ObjectType::Bytes, 0)
}

Expand Down Expand Up @@ -429,7 +429,7 @@ fn test_node_as_index() {
#[test]
fn test_atom_eq() {
let mut a = Allocator::new();
let a0 = a.null();
let a0 = a.nil();
let a1 = a.one();
let a2 = a.new_atom(&[1]).unwrap();
let a3 = a.new_atom(&[0x5, 0x39]).unwrap();
Expand Down Expand Up @@ -473,12 +473,12 @@ fn test_atom_eq() {
}

#[test]
fn test_null() {
fn test_nil() {
let a = Allocator::new();
assert_eq!(a.atom(a.null()), b"");
assert_eq!(a.atom(a.nil()), b"");

let buf = match a.sexp(a.null()) {
SExp::Atom => a.atom(a.null()),
let buf = match a.sexp(a.nil()) {
SExp::Atom => a.atom(a.nil()),
SExp::Pair(_, _) => panic!("unexpected"),
};
assert_eq!(buf, b"");
Expand Down Expand Up @@ -549,7 +549,7 @@ fn test_allocate_heap_limit() {
fn test_allocate_atom_limit() {
let mut a = Allocator::new();
// we can allocate 5 atoms total
// keep in mind that we always have 2 pre-allocated atoms for null and one,
// keep in mind that we always have 2 pre-allocated atoms for nil and one,
// so with a limit of 5, we only have 3 slots left at this point.
let _atom = a.new_atom(b"foo").unwrap();
let _atom = a.new_atom(b"bar").unwrap();
Expand All @@ -563,7 +563,7 @@ fn test_allocate_atom_limit() {
}
assert_eq!(a.new_atom(b"foobar").unwrap_err().1, "too many atoms");

// the pre-allocated null() and one() also count against the limit, and they
// the pre-allocated nil() and one() also count against the limit, and they
// use 0 and 1 bytes respectively
assert_eq!(a.u8_vec.len(), MAX_NUM_ATOMS * 3 - 3 - 2);
}
Expand Down Expand Up @@ -798,7 +798,7 @@ fn test_point_size_error(#[case] fun: TestFun, #[case] size: usize, #[case] expe
#[case(test_g2, "pair found, expected G2 point")]
fn test_point_atom_pair(#[case] fun: TestFun, #[case] expected: &str) {
let mut a = Allocator::new();
let n = a.new_pair(a.null(), a.one()).unwrap();
let n = a.new_pair(a.nil(), a.one()).unwrap();
let r = fun(&a, n);
assert_eq!(r.0, n);
assert_eq!(r.1, expected.to_string());
Expand Down
10 changes: 5 additions & 5 deletions src/bls_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::allocator::{Allocator, NodePtr};
use crate::cost::{check_cost, Cost};
use crate::err_utils::err;
use crate::op_utils::{
atom, first, get_args, get_varargs, int_atom, mod_group_order, new_atom_and_cost, nullp, rest,
atom, first, get_args, get_varargs, int_atom, mod_group_order, new_atom_and_cost, nilp, rest,
MALLOC_COST_PER_BYTE,
};
use crate::reduction::{EvalErr, Reduction, Response};
Expand Down Expand Up @@ -272,7 +272,7 @@ pub fn op_bls_pairing_identity(a: &mut Allocator, input: NodePtr, max_cost: Cost
let mut items = Vec::<(G1Element, G2Element)>::new();

let mut args = input;
while !nullp(a, args) {
while !nilp(a, args) {
cost += BLS_PAIRING_COST_PER_ARG;
check_cost(a, cost, max_cost)?;
let g1 = a.g1(first(a, args)?)?;
Expand All @@ -285,7 +285,7 @@ pub fn op_bls_pairing_identity(a: &mut Allocator, input: NodePtr, max_cost: Cost
if !aggregate_pairing(items) {
err(input, "bls_pairing_identity failed")
} else {
Ok(Reduction(cost, a.null()))
Ok(Reduction(cost, a.nil()))
}
}

Expand All @@ -306,7 +306,7 @@ pub fn op_bls_verify(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respo
args = rest(a, args)?;

let mut items = Vec::<(PublicKey, &[u8])>::new();
while !nullp(a, args) {
while !nilp(a, args) {
let pk = a.g1(first(a, args)?)?;
args = rest(a, args)?;
let msg = atom(a, first(a, args)?, "bls_verify message")?;
Expand All @@ -323,6 +323,6 @@ pub fn op_bls_verify(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respo
if !aggregate_verify(&signature, items) {
err(input, "bls_verify failed")
} else {
Ok(Reduction(cost, a.null()))
Ok(Reduction(cost, a.nil()))
}
}
12 changes: 4 additions & 8 deletions src/core_ops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::allocator::{Allocator, NodePtr, SExp};
use crate::cost::Cost;
use crate::err_utils::err;
use crate::op_utils::{first, get_args, nullp, rest};
use crate::op_utils::{first, get_args, nilp, rest};
use crate::reduction::{EvalErr, Reduction, Response};

const FIRST_COST: Cost = 30;
Expand All @@ -17,11 +17,7 @@ const EQ_COST_PER_BYTE: Cost = 1;

pub fn op_if(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
let [cond, affirmative, negative] = get_args::<3>(a, input, "i")?;
let chosen_node = if nullp(a, cond) {
negative
} else {
affirmative
};
let chosen_node = if nilp(a, cond) { negative } else { affirmative };
Ok(Reduction(IF_COST, chosen_node))
}

Expand All @@ -45,7 +41,7 @@ pub fn op_listp(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response
let [n] = get_args::<1>(a, input, "l")?;
match a.sexp(n) {
SExp::Pair(_, _) => Ok(Reduction(LISTP_COST, a.one())),
_ => Ok(Reduction(LISTP_COST, a.null())),
_ => Ok(Reduction(LISTP_COST, a.nil())),
}
}

Expand Down Expand Up @@ -80,5 +76,5 @@ pub fn op_eq(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
ensure_atom(a, s1, "=")?;
let eq = a.atom_eq(s0, s1);
let cost = EQ_BASE_COST + (a.atom_len(s0) as Cost + a.atom_len(s1) as Cost) * EQ_COST_PER_BYTE;
Ok(Reduction(cost, if eq { a.one() } else { a.null() }))
Ok(Reduction(cost, if eq { a.one() } else { a.nil() }))
}
2 changes: 1 addition & 1 deletion src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub type Cost = u64;

pub fn check_cost(a: &Allocator, cost: Cost, max_cost: Cost) -> Result<(), EvalErr> {
if cost > max_cost {
Err(EvalErr(a.null(), "cost exceeded".into()))
Err(EvalErr(a.nil(), "cost exceeded".into()))
} else {
Ok(())
}
Expand Down
Loading

0 comments on commit 4e59b6b

Please sign in to comment.