From e490b7990723b2406ffd0afdc08d335605c6870b Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 23 Aug 2018 23:35:39 +0100 Subject: [PATCH] Transaction serialization --- Cargo.lock | 3 + zcash_primitives/Cargo.toml | 3 + zcash_primitives/src/lib.rs | 12 + .../src/transaction/components.rs | 302 ++++++++++++++++++ zcash_primitives/src/transaction/mod.rs | 179 +++++++++++ zcash_primitives/src/transaction/tests.rs | 94 ++++++ 6 files changed, 593 insertions(+) create mode 100644 zcash_primitives/src/transaction/components.rs create mode 100644 zcash_primitives/src/transaction/mod.rs create mode 100644 zcash_primitives/src/transaction/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 306a9a9..884ef3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -705,6 +705,9 @@ name = "zcash_primitives" version = "0.0.0" dependencies = [ "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pairing 0.14.2", + "sapling-crypto 0.0.1", ] [[package]] diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index dd74c60..a52e1f3 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -7,3 +7,6 @@ authors = [ [dependencies] byteorder = "1" +lazy_static = "1" +pairing = { path = "../pairing" } +sapling-crypto = { path = "../sapling-crypto" } diff --git a/zcash_primitives/src/lib.rs b/zcash_primitives/src/lib.rs index dafaeaf..c024cd8 100644 --- a/zcash_primitives/src/lib.rs +++ b/zcash_primitives/src/lib.rs @@ -1,3 +1,15 @@ +#[macro_use] +extern crate lazy_static; + extern crate byteorder; +extern crate pairing; +extern crate sapling_crypto; + +use sapling_crypto::jubjub::JubjubBls12; mod serialize; +pub mod transaction; + +lazy_static! { + static ref JUBJUB: JubjubBls12 = { JubjubBls12::new() }; +} diff --git a/zcash_primitives/src/transaction/components.rs b/zcash_primitives/src/transaction/components.rs new file mode 100644 index 0000000..93e605d --- /dev/null +++ b/zcash_primitives/src/transaction/components.rs @@ -0,0 +1,302 @@ +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use pairing::{ + bls12_381::{Bls12, Fr, FrRepr}, + PrimeField, PrimeFieldRepr, +}; +use sapling_crypto::{ + jubjub::{edwards, Unknown}, + redjubjub::{PublicKey, Signature}, +}; +use std::io::{self, Read, Write}; + +use serialize::Vector; +use JUBJUB; + +// π_A + π_B + π_C +const GROTH_PROOF_SIZE: usize = (48 + 96 + 48); +// π_A + π_A' + π_B + π_B' + π_C + π_C' + π_K + π_H +const PHGR_PROOF_SIZE: usize = (33 + 33 + 65 + 33 + 33 + 33 + 33 + 33); + +const ZC_NUM_JS_INPUTS: usize = 2; +const ZC_NUM_JS_OUTPUTS: usize = 2; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Amount(pub i64); + +struct Script(Vec); + +impl Script { + pub fn read(mut reader: R) -> io::Result { + let script = Vector::read(&mut reader, |r| r.read_u8())?; + Ok(Script(script)) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + Vector::write(&mut writer, &self.0, |w, e| w.write_u8(*e)) + } +} + +struct OutPoint { + hash: [u8; 32], + n: u32, +} + +impl OutPoint { + pub fn read(mut reader: R) -> io::Result { + let mut hash = [0; 32]; + reader.read_exact(&mut hash)?; + let n = reader.read_u32::()?; + Ok(OutPoint { hash, n }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + writer.write_all(&self.hash)?; + writer.write_u32::(self.n) + } +} + +pub struct TxIn { + prevout: OutPoint, + script_sig: Script, + sequence: u32, +} + +impl TxIn { + pub fn read(mut reader: &mut R) -> io::Result { + let prevout = OutPoint::read(&mut reader)?; + let script_sig = Script::read(&mut reader)?; + let sequence = reader.read_u32::()?; + + Ok(TxIn { + prevout, + script_sig, + sequence, + }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + self.prevout.write(&mut writer)?; + self.script_sig.write(&mut writer)?; + writer.write_u32::(self.sequence) + } +} + +pub struct TxOut { + value: Amount, + script_pubkey: Script, +} + +impl TxOut { + pub fn read(mut reader: &mut R) -> io::Result { + let value = Amount(reader.read_i64::()?); + let script_pubkey = Script::read(&mut reader)?; + + Ok(TxOut { + value, + script_pubkey, + }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + writer.write_i64::(self.value.0)?; + self.script_pubkey.write(&mut writer) + } +} + +pub struct SpendDescription { + pub cv: edwards::Point, + pub anchor: Fr, + pub nullifier: [u8; 32], + pub rk: PublicKey, + pub zkproof: [u8; GROTH_PROOF_SIZE], + pub spend_auth_sig: Signature, +} + +impl SpendDescription { + pub fn read(mut reader: &mut R) -> io::Result { + let cv = edwards::Point::::read(&mut reader, &JUBJUB)?; + let anchor = { + let mut f = FrRepr::default(); + f.read_le(&mut reader)?; + Fr::from_repr(f).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))? + }; + + let mut nullifier = [0; 32]; + reader.read_exact(&mut nullifier)?; + + let rk = PublicKey::::read(&mut reader, &JUBJUB)?; + + let mut zkproof = [0; GROTH_PROOF_SIZE]; + reader.read_exact(&mut zkproof)?; + let spend_auth_sig = Signature::read(&mut reader)?; + + Ok(SpendDescription { + cv, + anchor, + nullifier, + rk, + zkproof, + spend_auth_sig, + }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + self.cv.write(&mut writer)?; + self.anchor.into_repr().write_le(&mut writer)?; + writer.write_all(&self.nullifier)?; + self.rk.write(&mut writer)?; + writer.write_all(&self.zkproof)?; + self.spend_auth_sig.write(&mut writer) + } +} + +pub struct OutputDescription { + pub cv: edwards::Point, + pub cmu: Fr, + pub ephemeral_key: edwards::Point, + pub enc_ciphertext: [u8; 580], + pub out_ciphertext: [u8; 80], + pub zkproof: [u8; GROTH_PROOF_SIZE], +} + +impl OutputDescription { + pub fn read(mut reader: &mut R) -> io::Result { + let cv = edwards::Point::::read(&mut reader, &JUBJUB)?; + let cmu = { + let mut f = FrRepr::default(); + f.read_le(&mut reader)?; + Fr::from_repr(f).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))? + }; + let ephemeral_key = edwards::Point::::read(&mut reader, &JUBJUB)?; + + let mut enc_ciphertext = [0; 580]; + let mut out_ciphertext = [0; 80]; + reader.read_exact(&mut enc_ciphertext)?; + reader.read_exact(&mut out_ciphertext)?; + + let mut zkproof = [0; GROTH_PROOF_SIZE]; + reader.read_exact(&mut zkproof)?; + + Ok(OutputDescription { + cv, + cmu, + ephemeral_key, + enc_ciphertext, + out_ciphertext, + zkproof, + }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + self.cv.write(&mut writer)?; + self.cmu.into_repr().write_le(&mut writer)?; + self.ephemeral_key.write(&mut writer)?; + writer.write_all(&self.enc_ciphertext)?; + writer.write_all(&self.out_ciphertext)?; + writer.write_all(&self.zkproof) + } +} + +enum SproutProof { + Groth([u8; GROTH_PROOF_SIZE]), + PHGR([u8; PHGR_PROOF_SIZE]), +} + +pub struct JSDescription { + vpub_old: Amount, + vpub_new: Amount, + anchor: [u8; 32], + nullifiers: [[u8; 32]; ZC_NUM_JS_INPUTS], + commitments: [[u8; 32]; ZC_NUM_JS_OUTPUTS], + ephemeral_key: [u8; 32], + random_seed: [u8; 32], + macs: [[u8; 32]; ZC_NUM_JS_INPUTS], + proof: SproutProof, + ciphertexts: [[u8; 601]; ZC_NUM_JS_OUTPUTS], +} + +impl JSDescription { + pub fn read(mut reader: R, use_groth: bool) -> io::Result { + let vpub_old = Amount(reader.read_i64::()?); + let vpub_new = Amount(reader.read_i64::()?); + + let mut anchor = [0; 32]; + reader.read_exact(&mut anchor)?; + + let mut nullifiers = [[0; 32]; ZC_NUM_JS_INPUTS]; + nullifiers + .iter_mut() + .map(|nf| reader.read_exact(nf)) + .collect::>()?; + + let mut commitments = [[0; 32]; ZC_NUM_JS_OUTPUTS]; + commitments + .iter_mut() + .map(|cm| reader.read_exact(cm)) + .collect::>()?; + + let mut ephemeral_key = [0; 32]; + let mut random_seed = [0; 32]; + reader.read_exact(&mut ephemeral_key)?; + reader.read_exact(&mut random_seed)?; + + let mut macs = [[0; 32]; ZC_NUM_JS_INPUTS]; + macs.iter_mut() + .map(|mac| reader.read_exact(mac)) + .collect::>()?; + + let proof = match use_groth { + true => { + let mut proof = [0; GROTH_PROOF_SIZE]; + reader.read_exact(&mut proof)?; + SproutProof::Groth(proof) + } + false => { + let mut proof = [0; PHGR_PROOF_SIZE]; + reader.read_exact(&mut proof)?; + SproutProof::PHGR(proof) + } + }; + + let mut ciphertexts = [[0; 601]; ZC_NUM_JS_OUTPUTS]; + ciphertexts + .iter_mut() + .map(|ct| reader.read_exact(ct)) + .collect::>()?; + + Ok(JSDescription { + vpub_old, + vpub_new, + anchor, + nullifiers, + commitments, + ephemeral_key, + random_seed, + macs, + proof, + ciphertexts, + }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + writer.write_i64::(self.vpub_old.0)?; + writer.write_i64::(self.vpub_new.0)?; + writer.write_all(&self.anchor)?; + writer.write_all(&self.nullifiers[0])?; + writer.write_all(&self.nullifiers[1])?; + writer.write_all(&self.commitments[0])?; + writer.write_all(&self.commitments[1])?; + writer.write_all(&self.ephemeral_key)?; + writer.write_all(&self.random_seed)?; + writer.write_all(&self.macs[0])?; + writer.write_all(&self.macs[1])?; + + match &self.proof { + SproutProof::Groth(p) => writer.write_all(p)?, + SproutProof::PHGR(p) => writer.write_all(p)?, + } + + writer.write_all(&self.ciphertexts[0])?; + writer.write_all(&self.ciphertexts[1]) + } +} diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs new file mode 100644 index 0000000..0f894a0 --- /dev/null +++ b/zcash_primitives/src/transaction/mod.rs @@ -0,0 +1,179 @@ +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use sapling_crypto::redjubjub::Signature; +use std::io::{self, Read, Write}; + +use serialize::Vector; + +mod components; + +#[cfg(test)] +mod tests; + +use self::components::{Amount, JSDescription, OutputDescription, SpendDescription, TxIn, TxOut}; + +const OVERWINTER_VERSION_GROUP_ID: u32 = 0x03C48270; +const OVERWINTER_TX_VERSION: u32 = 3; +const SAPLING_VERSION_GROUP_ID: u32 = 0x892F2085; +const SAPLING_TX_VERSION: u32 = 4; + +/// A Zcash transaction. +pub struct Transaction { + overwintered: bool, + version: u32, + version_group_id: u32, + vin: Vec, + vout: Vec, + lock_time: u32, + expiry_height: u32, + value_balance: Amount, + shielded_spends: Vec, + shielded_outputs: Vec, + joinsplits: Vec, + joinsplit_pubkey: [u8; 32], + joinsplit_sig: [u8; 64], + binding_sig: Option, +} + +impl Transaction { + pub fn read(mut reader: R) -> io::Result { + let header = reader.read_u32::()?; + let overwintered = (header >> 31) == 1; + let version = header & 0x7FFFFFFF; + + let version_group_id = match overwintered { + true => reader.read_u32::()?, + false => 0, + }; + + let is_overwinter_v3 = overwintered + && version_group_id == OVERWINTER_VERSION_GROUP_ID + && version == OVERWINTER_TX_VERSION; + let is_sapling_v4 = overwintered + && version_group_id == SAPLING_VERSION_GROUP_ID + && version == SAPLING_TX_VERSION; + if overwintered && !(is_overwinter_v3 || is_sapling_v4) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Unknown transaction format", + )); + } + + let vin = Vector::read(&mut reader, TxIn::read)?; + let vout = Vector::read(&mut reader, TxOut::read)?; + let lock_time = reader.read_u32::()?; + let expiry_height = match is_overwinter_v3 || is_sapling_v4 { + true => reader.read_u32::()?, + false => 0, + }; + + let (value_balance, shielded_spends, shielded_outputs) = if is_sapling_v4 { + let vb = Amount(reader.read_i64::()?); + let ss = Vector::read(&mut reader, SpendDescription::read)?; + let so = Vector::read(&mut reader, OutputDescription::read)?; + (vb, ss, so) + } else { + (Amount(0), vec![], vec![]) + }; + + let mut joinsplit_pubkey = [0; 32]; + let mut joinsplit_sig = [0; 64]; + let joinsplits = if version >= 2 { + let jss = Vector::read(&mut reader, |r| { + JSDescription::read(r, overwintered && version >= SAPLING_TX_VERSION) + })?; + if !jss.is_empty() { + reader.read_exact(&mut joinsplit_pubkey)?; + reader.read_exact(&mut joinsplit_sig)?; + } + jss + } else { + vec![] + }; + + let binding_sig = + match is_sapling_v4 && !(shielded_spends.is_empty() && shielded_outputs.is_empty()) { + true => Some(Signature::read(&mut reader)?), + false => None, + }; + + Ok(Transaction { + overwintered, + version, + version_group_id, + vin, + vout, + lock_time, + expiry_height, + value_balance, + shielded_spends, + shielded_outputs, + joinsplits, + joinsplit_pubkey, + joinsplit_sig, + binding_sig, + }) + } + + pub fn write(&self, mut writer: W) -> io::Result<()> { + writer.write_u32::(self.header())?; + if self.overwintered { + writer.write_u32::(self.version_group_id)?; + } + + let is_overwinter_v3 = self.overwintered + && self.version_group_id == OVERWINTER_VERSION_GROUP_ID + && self.version == OVERWINTER_TX_VERSION; + let is_sapling_v4 = self.overwintered + && self.version_group_id == SAPLING_VERSION_GROUP_ID + && self.version == SAPLING_TX_VERSION; + if self.overwintered && !(is_overwinter_v3 || is_sapling_v4) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Unknown transaction format", + )); + } + + Vector::write(&mut writer, &self.vin, |w, e| e.write(w))?; + Vector::write(&mut writer, &self.vout, |w, e| e.write(w))?; + writer.write_u32::(self.lock_time)?; + if is_overwinter_v3 || is_sapling_v4 { + writer.write_u32::(self.expiry_height)?; + } + + if is_sapling_v4 { + writer.write_i64::(self.value_balance.0)?; + Vector::write(&mut writer, &self.shielded_spends, |w, e| e.write(w))?; + Vector::write(&mut writer, &self.shielded_outputs, |w, e| e.write(w))?; + } + + if self.version >= 2 { + Vector::write(&mut writer, &self.joinsplits, |w, e| e.write(w))?; + if !self.joinsplits.is_empty() { + writer.write_all(&self.joinsplit_pubkey)?; + writer.write_all(&self.joinsplit_sig)?; + } + } + + if is_sapling_v4 && !(self.shielded_spends.is_empty() && self.shielded_outputs.is_empty()) { + match self.binding_sig { + Some(sig) => sig.write(&mut writer)?, + None => { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Missing binding signature", + )) + } + } + } + + Ok(()) + } + + fn header(&self) -> u32 { + let mut header = self.version; + if self.overwintered { + header |= 1 << 31; + } + header + } +} diff --git a/zcash_primitives/src/transaction/tests.rs b/zcash_primitives/src/transaction/tests.rs new file mode 100644 index 0000000..ee19025 --- /dev/null +++ b/zcash_primitives/src/transaction/tests.rs @@ -0,0 +1,94 @@ +use super::Transaction; + +#[test] +fn tx_read_write() { + let data = [ + 0x04, 0x00, 0x00, 0x80, 0x85, 0x20, 0x2f, 0x89, 0x01, 0x23, 0xfc, 0x3c, 0x5d, 0x56, 0xa1, + 0x1e, 0xae, 0xac, 0xdd, 0x39, 0xd0, 0x2b, 0x9d, 0x4e, 0x08, 0xd8, 0xf7, 0x99, 0x6e, 0x09, + 0x40, 0x98, 0x8c, 0x7c, 0x9b, 0x3a, 0xf4, 0x7c, 0x85, 0x45, 0x59, 0x00, 0x00, 0x00, 0x00, + 0x6b, 0x48, 0x30, 0x45, 0x02, 0x21, 0x00, 0xb6, 0x4d, 0x29, 0x65, 0xc6, 0xf6, 0x68, 0x58, + 0x8d, 0xbe, 0xeb, 0x60, 0xaa, 0x1c, 0x37, 0xb9, 0x68, 0xb4, 0x38, 0x17, 0xc3, 0x0d, 0x3d, + 0xf0, 0x65, 0x64, 0x92, 0x9f, 0x84, 0xca, 0x58, 0x60, 0x02, 0x20, 0x6f, 0x51, 0xd8, 0x3b, + 0x10, 0x4d, 0x22, 0x41, 0x0d, 0x29, 0xf1, 0x81, 0xf3, 0x2d, 0x95, 0xff, 0x2f, 0x16, 0xdf, + 0xf5, 0xbc, 0xa8, 0x97, 0x29, 0xfe, 0x6b, 0xca, 0x28, 0xb3, 0x65, 0xb7, 0xe7, 0x01, 0x21, + 0x02, 0x0f, 0x83, 0xff, 0x10, 0x99, 0xb9, 0x5b, 0x95, 0xb4, 0xe1, 0xd4, 0x09, 0xaf, 0x22, + 0xa7, 0xdc, 0xc0, 0xc6, 0x1a, 0x2d, 0x34, 0x20, 0x88, 0x64, 0x01, 0xfa, 0xc8, 0x24, 0x03, + 0x40, 0x4f, 0x51, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf0, 0x05, 0x31, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc0, 0x88, 0xd3, 0x6e, 0xe2, 0xb1, 0x12, 0xce, 0x4b, 0x9c, + 0xec, 0x9d, 0xd4, 0x68, 0x4a, 0x68, 0xa7, 0x07, 0xb8, 0x43, 0x88, 0xac, 0x00, 0x00, 0x00, + 0x00, 0xfe, 0x32, 0x04, 0x00, 0x00, 0x4c, 0x3b, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, + 0xbc, 0x59, 0x9f, 0x29, 0x3a, 0x69, 0xf5, 0x31, 0x54, 0xe0, 0xe9, 0x70, 0x94, 0xc7, 0x78, + 0xa7, 0xb9, 0xf2, 0x58, 0x5c, 0x21, 0x7f, 0x0f, 0x5c, 0x75, 0xc3, 0xb0, 0xc4, 0x32, 0x58, + 0x72, 0xc4, 0xea, 0xd2, 0x13, 0x13, 0x49, 0x69, 0xcf, 0x0e, 0x24, 0x09, 0x1e, 0x5b, 0xc1, + 0x8a, 0x61, 0xb9, 0xac, 0x51, 0xd4, 0x01, 0xf3, 0x7f, 0x4b, 0x6a, 0x69, 0xad, 0xdd, 0xef, + 0x19, 0xde, 0xee, 0x2f, 0xf1, 0x47, 0x25, 0x10, 0x45, 0xd9, 0x0c, 0x6b, 0x63, 0x40, 0xe9, + 0x14, 0x81, 0x06, 0xcb, 0xa2, 0xbd, 0xf7, 0x2b, 0xc4, 0xd4, 0xb2, 0x54, 0xbd, 0x97, 0x88, + 0x40, 0x61, 0x22, 0xa2, 0x73, 0xb7, 0x06, 0x9f, 0x86, 0x65, 0x35, 0xa6, 0xee, 0x20, 0x4b, + 0xd4, 0x50, 0x21, 0x15, 0x9a, 0x05, 0x2a, 0x15, 0xa7, 0x14, 0x63, 0xf3, 0x03, 0x46, 0x9f, + 0x29, 0x1d, 0x96, 0x31,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 0x11, 0x47, 0xf8, + 0x71, 0xd7, 0x89, 0xf6, 0xd2, 0x4a, 0x88, 0x1b, 0xab, 0x64, 0x48, 0xaf, 0x81, 0x22, 0xba, + 0x43, 0xd1, 0x18, 0x92, 0x00, 0x40, 0x18, 0xa6, 0xdc, 0x89, 0xcc, 0x87, 0x2b, 0xd9, 0x85, + 0x37, 0x42, 0x86, 0x18, 0xb8, 0xe3, 0x95, 0x8b, 0x8a, 0x24, 0xdf, 0x1b, 0xd6, 0xc3, 0x03, + 0xb8, 0x65, 0xad, 0xa6, 0x5d, 0xf8, 0x6a, 0x2d, 0xeb, 0xee, 0xef, 0x89, 0xca, 0xab, 0xbc, + 0xb4, 0xe5, 0xb6, 0x4a, 0x60, 0x16, 0x37, 0xa6, 0xc8, 0x95, 0x5d, 0x34, 0xe8, 0xb6, 0xde, + 0x0a, 0x83, 0x7a, 0x01, 0xa9, 0x22, 0x67, 0xdc, 0xc9, 0xc8, 0x3a, 0xe5, 0x84, 0x9f, 0x0d, + 0xc7, 0x2d, 0x02, 0x00, 0x30, 0xc7, 0xe3, 0x00, 0x50, 0xdd, 0x8c, 0x53, 0xd3, 0xcb, 0x83, + 0x3b, 0xb0, 0x27, 0x48, 0x61, 0x2e, 0xd3, 0x4e, 0xeb, 0x42, 0x1f, 0x8c, 0xa7, 0x53, 0x72, + 0x9a, 0xc2, 0x42, 0x70, 0x97, 0x54, 0x4c, 0x29, 0xfd, 0x63, 0x69, 0xf1, 0xd3, 0x66, 0xfc, + 0x51, 0x4a, 0x5a, 0xd1, 0x06, 0xd7, 0x85, 0x91, 0x1f, 0x7b, 0xf7, 0x39, 0xe1, 0x0f, 0x35, + 0xbe, 0x6a, 0x40, 0xbf, 0x54, 0xeb, 0xc1, 0x03, + ]; + let tx = Transaction::read(&data[..]).unwrap(); + + let mut encoded = Vec::with_capacity(data.len()); + tx.write(&mut encoded).unwrap(); + assert_eq!(&data[..], &encoded[..]); +}