use crate::impl_midstate_wrapper;
use crate::jet::Jet;
use crate::types::arrow::FinalArrow;
use crate::value::Word;
use crate::{Cmr, Tmr, Value};
use hashes::sha256::Midstate;
use super::{bip340_iv, compact_value, FailEntropy};
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Imr(Midstate);
impl_midstate_wrapper!(Imr);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Ihr(Midstate);
impl_midstate_wrapper!(Ihr);
impl From<Cmr> for Imr {
fn from(cmr: Cmr) -> Self {
Imr::from_byte_array(cmr.to_byte_array())
}
}
impl From<Cmr> for Ihr {
fn from(cmr: Cmr) -> Self {
Ihr::from_byte_array(cmr.to_byte_array())
}
}
impl From<Tmr> for Ihr {
fn from(tmr: Tmr) -> Self {
Ihr::from_byte_array(tmr.to_byte_array())
}
}
impl Imr {
pub const fn iden() -> Self {
Self::IDEN_IV
}
pub const fn unit() -> Self {
Self::UNIT_IV
}
pub fn injl(child: Self) -> Self {
Self::INJL_IV.update_1(child)
}
pub fn injr(child: Self) -> Self {
Self::INJR_IV.update_1(child)
}
pub fn take(child: Self) -> Self {
Self::TAKE_IV.update_1(child)
}
pub fn drop(child: Self) -> Self {
Self::DROP_IV.update_1(child)
}
pub fn comp(left: Self, right: Self) -> Self {
Self::COMP_IV.update(left, right)
}
pub fn case(left: Self, right: Self) -> Self {
Self::CASE_IV.update(left, right)
}
pub fn pair(left: Self, right: Self) -> Self {
Self::PAIR_IV.update(left, right)
}
pub fn disconnect(left: Self, right: Self) -> Self {
Self::DISCONNECT_IV.update(left, right)
}
pub fn witness(ty: &FinalArrow, value: &Value) -> Self {
use hashes::{sha256, HashEngine};
let value_hash = compact_value(value);
let mut engine = sha256::HashEngine::from_midstate(Self::WITNESS_IV.0, 0);
engine.input(&value_hash[..]);
engine.input(ty.target.tmr().as_ref());
Self(engine.midstate())
}
pub fn fail(entropy: FailEntropy) -> Self {
Self::FAIL_IV.update_fail_entropy(entropy)
}
pub fn jet<J: Jet>(jet: J) -> Self {
Cmr::jet(jet).into()
}
pub fn const_word(word: &Word) -> Self {
Cmr::const_word(word).into()
}
#[rustfmt::skip]
const IDEN_IV: Self = Self(Midstate([
0x54, 0x1a, 0x1a, 0x69, 0xbd, 0x4b, 0xcb, 0xda,
0x7f, 0x34, 0x31, 0x0e, 0x30, 0x78, 0xf7, 0x26,
0x44, 0x31, 0x22, 0xfb, 0xcc, 0x1c, 0xb5, 0x36,
0x0c, 0x78, 0x64, 0xec, 0x0d, 0x32, 0x3a, 0xc0,
]));
#[rustfmt::skip]
const UNIT_IV: Self = Self(Midstate([
0xc4, 0x0a, 0x10, 0x26, 0x3f, 0x74, 0x36, 0xb4,
0x16, 0x0a, 0xcb, 0xef, 0x1c, 0x36, 0xfb, 0xa4,
0xbe, 0x4d, 0x95, 0xdf, 0x18, 0x1a, 0x96, 0x8a,
0xfe, 0xab, 0x5e, 0xac, 0x24, 0x7a, 0xdf, 0xf7,
]));
#[rustfmt::skip]
const INJL_IV: Self = Self(Midstate([
0x54, 0xe9, 0x1d, 0x18, 0xd8, 0xf8, 0x1f, 0x6d,
0x29, 0x86, 0xbb, 0x58, 0x47, 0x9a, 0x54, 0xeb,
0x63, 0x0e, 0x95, 0x23, 0xb6, 0x9e, 0xe8, 0x53,
0x29, 0x80, 0xd0, 0x55, 0x58, 0x19, 0x4f, 0x15,
]));
#[rustfmt::skip]
const INJR_IV: Self = Self(Midstate([
0xd7, 0x0f, 0xfd, 0xce, 0x97, 0x77, 0x7b, 0x4d,
0xfe, 0x31, 0xfd, 0x9f, 0xf5, 0xd0, 0x17, 0xa6,
0x30, 0x5d, 0x7e, 0xc6, 0x0d, 0xf3, 0xb1, 0xbf,
0x6d, 0x25, 0xe8, 0x16, 0x33, 0xde, 0xd4, 0xbf,
]));
#[rustfmt::skip]
const TAKE_IV: Self = Self(Midstate([
0x50, 0x5f, 0xc0, 0x81, 0xb5, 0xba, 0x2a, 0xcd,
0x09, 0x50, 0x67, 0xc3, 0xdf, 0xb8, 0xea, 0x12,
0x6f, 0xa1, 0x5d, 0x55, 0xcb, 0x21, 0x1e, 0x6a,
0xed, 0x34, 0xe8, 0xd1, 0xe3, 0x7a, 0xf0, 0xfa,
]));
#[rustfmt::skip]
const DROP_IV: Self = Self(Midstate([
0x8a, 0x30, 0x8d, 0x38, 0xa1, 0x13, 0xa2, 0x60,
0xb4, 0xc7, 0x14, 0x5a, 0xbd, 0xc5, 0x22, 0x4d,
0xeb, 0x70, 0x13, 0x79, 0x59, 0x0e, 0x0c, 0x8c,
0x38, 0x86, 0x0b, 0xab, 0x12, 0x71, 0xa8, 0xa8,
]));
#[rustfmt::skip]
const COMP_IV: Self = Self(Midstate([
0x57, 0xec, 0x23, 0xa2, 0xa4, 0x77, 0x8e, 0x01,
0x58, 0xa6, 0x21, 0x7a, 0xea, 0x3e, 0xf7, 0x42,
0x8b, 0xa0, 0x90, 0x92, 0x73, 0xb9, 0x73, 0xfa,
0x14, 0x32, 0xa9, 0x27, 0x84, 0x3e, 0x92, 0x7a,
]));
#[rustfmt::skip]
const CASE_IV: Self = Self(Midstate([
0x29, 0x5e, 0x2a, 0x6d, 0xc8, 0xc5, 0xce, 0x59,
0xe4, 0xed, 0xcf, 0xe9, 0xb4, 0xd8, 0xf7, 0x64,
0x13, 0x3a, 0xa5, 0x51, 0x4b, 0xd3, 0xee, 0x8b,
0x4b, 0x75, 0xec, 0x8f, 0x4d, 0xeb, 0x08, 0xbe,
]));
#[rustfmt::skip]
const PAIR_IV: Self = Self(Midstate([
0x7d, 0x5e, 0x6d, 0xac, 0x15, 0xb1, 0x42, 0x8a,
0x0d, 0x26, 0x0c, 0x94, 0x29, 0xdb, 0xe8, 0x89,
0x65, 0x93, 0xf3, 0x1f, 0x70, 0x86, 0x27, 0xee,
0x75, 0xb2, 0x7e, 0xee, 0xfd, 0xd0, 0x50, 0x05,
]));
#[rustfmt::skip]
const DISCONNECT_IV: Self = Self(Midstate([
0x4e, 0xb7, 0x99, 0x5f, 0xb5, 0xdd, 0xe5, 0xd0,
0x85, 0xf4, 0x70, 0x85, 0xcd, 0x95, 0x3d, 0x16,
0x84, 0x54, 0x11, 0xed, 0xc6, 0x89, 0xe2, 0x7a,
0xf9, 0xc3, 0xde, 0xa2, 0xfb, 0x12, 0x25, 0xd5,
]));
#[rustfmt::skip]
const WITNESS_IV: Self = Self(Midstate([
0xcb, 0x37, 0xff, 0x70, 0x01, 0xc6, 0x2d, 0x94,
0x42, 0x4f, 0x98, 0x7f, 0x30, 0x23, 0xb3, 0x5e,
0x30, 0xd2, 0x17, 0x23, 0x96, 0x27, 0x6f, 0x89,
0xd0, 0x9f, 0x07, 0xaa, 0x67, 0xb6, 0x21, 0x96,
]));
#[rustfmt::skip]
const FAIL_IV: Self = Self(Midstate([
0x22, 0x83, 0xc1, 0x81, 0x9e, 0x69, 0x2f, 0x96,
0x85, 0xfe, 0x95, 0x40, 0x76, 0xc5, 0x16, 0x7c,
0x03, 0xbd, 0xe7, 0xcc, 0xda, 0xab, 0x00, 0x5e,
0x55, 0x36, 0x12, 0x2e, 0x18, 0xf7, 0x23, 0x7a,
]));
}
impl Ihr {
pub fn from_imr(imr: Imr, ty: &FinalArrow) -> Ihr {
let iv = Ihr(bip340_iv(b"Simplicity\x1fIdentity"));
iv.update_1(Ihr(imr.0))
.update(ty.source.tmr().into(), ty.target.tmr().into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[rustfmt::skip] fn ivs() {
fn check_iv(target: Imr, s: &'static str) {
let name = s
.trim_start_matches("Simplicity\x1f")
.trim_start_matches("Commitment\x1f")
.trim_start_matches("Identity\x1f");
assert_eq!(target, Imr(bip340_iv(s.as_bytes())), "mismatch on IV for {}", name);
}
check_iv(Imr::IDEN_IV, "Simplicity\x1fCommitment\x1fiden");
check_iv(Imr::UNIT_IV, "Simplicity\x1fCommitment\x1funit");
check_iv(Imr::INJL_IV, "Simplicity\x1fCommitment\x1finjl");
check_iv(Imr::INJR_IV, "Simplicity\x1fCommitment\x1finjr");
check_iv(Imr::TAKE_IV, "Simplicity\x1fCommitment\x1ftake");
check_iv(Imr::DROP_IV, "Simplicity\x1fCommitment\x1fdrop");
check_iv(Imr::COMP_IV, "Simplicity\x1fCommitment\x1fcomp");
check_iv(Imr::CASE_IV, "Simplicity\x1fCommitment\x1fcase");
check_iv(Imr::PAIR_IV, "Simplicity\x1fCommitment\x1fpair");
check_iv(Imr::DISCONNECT_IV, "Simplicity\x1fIdentity\x1fdisconnect");
check_iv(Imr::WITNESS_IV, "Simplicity\x1fIdentity\x1fwitness");
check_iv(Imr::FAIL_IV, "Simplicity\x1fCommitment\x1ffail");
}
}