timelock/
lib.rs

1/*
2 * Copyright 2025 by Ideal Labs, LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#![no_std]
18#![warn(unused, future_incompatible, nonstandard_style, rust_2018_idioms, rust_2021_compatibility)]
19#![deny(unsafe_code)]
20
21extern crate alloc;
22
23pub mod block_ciphers;
24pub mod engines;
25pub mod ibe;
26pub mod tlock;
27use crate::engines::EngineBLS;
28
29/// The length of hashes output from sha256
30const HASH_LENGTH: usize = 32;
31type Hash = [u8; HASH_LENGTH];
32// Adapted from: https://github.com/w3f/bls
33/// Internal message hash size.  
34///
35/// We choose 256 bits here so that birthday bound attacks cannot
36/// find messages with the same hash.
37const MESSAGE_SIZE: usize = 32;
38
39pub type MessageDigest = [u8; MESSAGE_SIZE];
40/// Internal message hash type.  Short for frequent rehashing
41/// by `HashMap`, etc.
42#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
43pub struct Message(pub MessageDigest, pub alloc::vec::Vec<u8>);
44
45impl Message {
46	pub fn new(context: &[u8], message: &[u8]) -> Message {
47		use sha3::{
48			digest::{ExtendableOutput, Update, XofReader},
49			Shake128,
50		};
51		let mut h = Shake128::default();
52		h.update(context);
53		let l = message.len() as u64;
54		h.update(&l.to_le_bytes());
55		h.update(message);
56		let mut msg = [0u8; MESSAGE_SIZE];
57		h.finalize_xof().read(&mut msg[..]);
58		Message(msg, [context, message].concat())
59	}
60
61	pub fn hash_to_signature_curve<E: EngineBLS>(&self) -> E::SignatureGroup {
62		E::hash_to_signature_curve(&self.1[..])
63	}
64}
65
66impl From<&[u8]> for Message {
67	fn from(x: &[u8]) -> Message {
68		Message::new(b"", x)
69	}
70}