Skip to content

Commit

Permalink
Add several builtins for strings and bytestring
Browse files Browse the repository at this point in the history
Co-authored-by: rvcas <x@rvcas.dev>
  • Loading branch information
MicroProofs and rvcas committed Aug 8, 2022
1 parent 0e5d25b commit 11ee99c
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 76 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion add_integers.uplc
@@ -1,5 +1,5 @@
(program
1.0.0
(con (list (list integer)) [[7], [5]])
[ (builtin decodeUtf8) [ (builtin encodeUtf8) (con string "hello world") ] ]
)

1 change: 1 addition & 0 deletions crates/uplc/Cargo.toml
Expand Up @@ -13,6 +13,7 @@ exclude = ["test_data/*"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cryptoxide = "0.4.2"
flat-rs = { path = "../flat", version = "0.0.7" }
hex = "0.4.3"
minicbor = { version = "0.18.0", features = ["std"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/uplc/src/machine.rs
Expand Up @@ -334,7 +334,7 @@ impl Machine {

self.spend_budget(cost)?;

runtime.call()
runtime.call(&mut self.logs)
} else {
Ok(Value::Builtin { fun, term, runtime })
}
Expand Down Expand Up @@ -430,7 +430,7 @@ impl Value {
((i.abs() as f64).log2().floor() as i64 / 64) + 1
}
}
Constant::ByteString(b) => (((b.len() - 1) / 8) + 1) as i64,
Constant::ByteString(b) => (((b.len() as i64 - 1) / 8) + 1),
Constant::String(s) => s.chars().count() as i64,
Constant::Unit => 1,
Constant::Bool(_) => 1,
Expand Down
124 changes: 110 additions & 14 deletions crates/uplc/src/machine/cost_model.rs
Expand Up @@ -552,13 +552,40 @@ impl BuiltinCosts {
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::MultiplyInteger => todo!(),
DefaultFunction::MultiplyInteger => ExBudget {
mem: self
.multiply_integer
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.multiply_integer
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::DivideInteger => todo!(),
DefaultFunction::QuotientInteger => todo!(),
DefaultFunction::RemainderInteger => todo!(),
DefaultFunction::ModInteger => todo!(),
DefaultFunction::EqualsInteger => todo!(),
DefaultFunction::LessThanInteger => todo!(),
DefaultFunction::EqualsInteger => ExBudget {
mem: self
.equals_integer
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.equals_integer
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::LessThanInteger => ExBudget {
mem: self
.less_than_integer
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.less_than_integer
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::LessThanEqualsInteger => ExBudget {
mem: self
.less_than_equals_integer
Expand All @@ -569,24 +596,75 @@ impl BuiltinCosts {
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::AppendByteString => todo!(),
DefaultFunction::AppendByteString => ExBudget {
mem: self
.append_byte_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.append_byte_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::ConsByteString => todo!(),
DefaultFunction::SliceByteString => todo!(),
DefaultFunction::LengthOfByteString => todo!(),
DefaultFunction::IndexByteString => todo!(),
DefaultFunction::EqualsByteString => todo!(),
DefaultFunction::EqualsByteString => ExBudget {
mem: self
.equals_byte_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.equals_byte_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::LessThanByteString => todo!(),
DefaultFunction::LessThanEqualsByteString => todo!(),
DefaultFunction::Sha2_256 => todo!(),
DefaultFunction::Sha3_256 => todo!(),
DefaultFunction::Blake2b_256 => todo!(),
DefaultFunction::Sha2_256 => ExBudget {
mem: self.sha2_256.mem.cost(args[0].to_ex_mem()),
cpu: self.sha2_256.cpu.cost(args[0].to_ex_mem()),
},
DefaultFunction::Sha3_256 => ExBudget {
mem: self.sha3_256.mem.cost(args[0].to_ex_mem()),
cpu: self.sha3_256.cpu.cost(args[0].to_ex_mem()),
},
DefaultFunction::Blake2b_256 => ExBudget {
mem: self.blake2b_256.mem.cost(args[0].to_ex_mem()),
cpu: self.blake2b_256.cpu.cost(args[0].to_ex_mem()),
},
DefaultFunction::VerifySignature => todo!(),
DefaultFunction::VerifyEcdsaSecp256k1Signature => todo!(),
DefaultFunction::VerifySchnorrSecp256k1Signature => todo!(),
DefaultFunction::AppendString => todo!(),
DefaultFunction::EqualsString => todo!(),
DefaultFunction::EncodeUtf8 => todo!(),
DefaultFunction::DecodeUtf8 => todo!(),
DefaultFunction::AppendString => ExBudget {
mem: self
.append_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.append_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::EqualsString => ExBudget {
mem: self
.equals_string
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.equals_string
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::EncodeUtf8 => ExBudget {
mem: self.encode_utf8.mem.cost(args[0].to_ex_mem()),
cpu: self.encode_utf8.cpu.cost(args[0].to_ex_mem()),
},
DefaultFunction::DecodeUtf8 => ExBudget {
mem: self.decode_utf8.mem.cost(args[0].to_ex_mem()),
cpu: self.decode_utf8.cpu.cost(args[0].to_ex_mem()),
},
DefaultFunction::IfThenElse => ExBudget {
mem: self.if_then_else.mem.cost(
args[0].to_ex_mem(),
Expand All @@ -599,8 +677,26 @@ impl BuiltinCosts {
args[2].to_ex_mem(),
),
},
DefaultFunction::ChooseUnit => todo!(),
DefaultFunction::Trace => todo!(),
DefaultFunction::ChooseUnit => ExBudget {
mem: self
.choose_unit
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.choose_unit
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::Trace => ExBudget {
mem: self
.trace
.mem
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
cpu: self
.trace
.cpu
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
},
DefaultFunction::FstPair => todo!(),
DefaultFunction::SndPair => todo!(),
DefaultFunction::ChooseList => todo!(),
Expand Down
4 changes: 4 additions & 0 deletions crates/uplc/src/machine/error.rs
@@ -1,3 +1,5 @@
use std::string::FromUtf8Error;

use thiserror::Error;

use crate::ast::{NamedDeBruijn, Term, Type};
Expand Down Expand Up @@ -28,4 +30,6 @@ pub enum Error {
NotAConstant(Value),
#[error("The evaluation never reached a final state")]
MachineNeverReachedDone,
#[error("Decoding utf8")]
Utf8(#[from] FromUtf8Error),
}

0 comments on commit 11ee99c

Please sign in to comment.