|
| 1 | +// Simplicity Bindings |
| 2 | +// Written in 2022 by |
| 3 | +// Christian Lewe <clewe@blockstream.com> |
| 4 | +// |
| 5 | +// To the extent possible under law, the author(s) have dedicated all |
| 6 | +// copyright and related and neighboring rights to this software to |
| 7 | +// the public domain worldwide. This software is distributed without |
| 8 | +// any warranty. |
| 9 | +// |
| 10 | +// You should have received a copy of the CC0 Public Domain Dedication |
| 11 | +// along with this software. |
| 12 | +// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 | +// |
| 14 | + |
| 15 | +use crate::bitstream::Bitstream; |
| 16 | +use crate::bitstring::Bitstring; |
| 17 | +use crate::error::Error; |
| 18 | +use crate::util; |
| 19 | +use libc::{c_void, size_t}; |
| 20 | +use std::fmt; |
| 21 | + |
| 22 | +/// Kind of Simplicity node. |
| 23 | +#[repr(C)] |
| 24 | +#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 25 | +pub enum Tag { |
| 26 | + COMP, |
| 27 | + CASE, |
| 28 | + ASSERTL, |
| 29 | + ASSERTR, |
| 30 | + PAIR, |
| 31 | + DISCONNECT, |
| 32 | + INJL, |
| 33 | + INJR, |
| 34 | + TAKE, |
| 35 | + DROP, |
| 36 | + IDEN, |
| 37 | + UNIT, |
| 38 | + HIDDEN, |
| 39 | + WITNESS, |
| 40 | + JET, |
| 41 | +} |
| 42 | + |
| 43 | +#[repr(C)] |
| 44 | +#[derive(Copy, Clone)] |
| 45 | +union AuxTypes { |
| 46 | + /// scratch space for verifyCanonicalOrder |
| 47 | + aux: size_t, |
| 48 | + /// source type, target type |
| 49 | + types: [size_t; 2], |
| 50 | +} |
| 51 | + |
| 52 | +#[repr(C)] |
| 53 | +#[derive(Copy, Clone)] |
| 54 | +union IndicesChildrenWitness { |
| 55 | + /// source index, target index |
| 56 | + indices: [size_t; 2], |
| 57 | + /// first child, second child |
| 58 | + children: [size_t; 2], |
| 59 | + /// witness |
| 60 | + witness: Bitstring, |
| 61 | +} |
| 62 | + |
| 63 | +#[repr(C)] |
| 64 | +struct PrivateDagNode { |
| 65 | + jet: *const c_void, |
| 66 | + cmr: [u32; 8], |
| 67 | + aux_types: AuxTypes, |
| 68 | + indices_children_witness: IndicesChildrenWitness, |
| 69 | + tag: Tag, |
| 70 | +} |
| 71 | + |
| 72 | +extern "C" { |
| 73 | + fn decodeMallocDag( |
| 74 | + dag: *mut *mut PrivateDagNode, |
| 75 | + combinator_counters: *const c_void, |
| 76 | + stream: *mut Bitstream, |
| 77 | + ) -> i32; |
| 78 | +} |
| 79 | + |
| 80 | +/// Simplicity DAG (typed or untyped). |
| 81 | +/// Uses the Elements extension by default. |
| 82 | +pub struct Dag { |
| 83 | + first: *mut PrivateDagNode, |
| 84 | + len: usize, |
| 85 | +} |
| 86 | + |
| 87 | +impl fmt::Debug for Dag { |
| 88 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 89 | + f.debug_struct("Dag").field("cmr", &self.cmr()).finish() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl Dag { |
| 94 | + /// Decode a Simplicity DAG from bytes. |
| 95 | + pub fn decode(bytes: &[u8]) -> Result<Self, Error> { |
| 96 | + let mut bitstream = Bitstream::from(bytes); |
| 97 | + let mut dag: *mut PrivateDagNode = std::ptr::null_mut(); |
| 98 | + |
| 99 | + unsafe { |
| 100 | + let len = |
| 101 | + Error::get_result(decodeMallocDag(&mut dag, std::ptr::null(), &mut bitstream))?; |
| 102 | + |
| 103 | + if dag.is_null() { |
| 104 | + Err(Error::Malloc) |
| 105 | + } else { |
| 106 | + Ok(Dag { |
| 107 | + first: dag, |
| 108 | + len: len as usize, |
| 109 | + }) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// Return the CMR of the DAG. |
| 115 | + pub fn cmr(&self) -> [u8; 32] { |
| 116 | + unsafe { util::into_u8_merkle_root(&(*self.root_node()).cmr) } |
| 117 | + } |
| 118 | + |
| 119 | + /// Return the root node of the DAG. |
| 120 | + unsafe fn root_node(&self) -> *const PrivateDagNode { |
| 121 | + self.get(self.len - 1) |
| 122 | + } |
| 123 | + |
| 124 | + // TODO: Add notion of `DagNode<'a>` that is spawned by &'a self |
| 125 | + // and that enables read-only access to node fields |
| 126 | + /// Return the node at the given `index` in the DAG. |
| 127 | + /// Panics if `index` is out of bounds. |
| 128 | + unsafe fn get(&self, index: usize) -> *const PrivateDagNode { |
| 129 | + self.first.add(index) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl Drop for Dag { |
| 134 | + fn drop(&mut self) { |
| 135 | + unsafe { |
| 136 | + libc::free(self.first as *mut libc::c_void); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#[cfg(test)] |
| 142 | +mod test { |
| 143 | + use super::*; |
| 144 | + use crate::test::SCHNORR0; |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn decode_cmr() { |
| 148 | + let bytes = SCHNORR0.bytes; |
| 149 | + let dag = Dag::decode(bytes).expect("decoding"); |
| 150 | + assert_eq!(SCHNORR0.cmr(), dag.cmr()); |
| 151 | + } |
| 152 | +} |
0 commit comments