ironfish_frost/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5#![cfg_attr(not(feature = "std"), no_std)]
6#![warn(missing_debug_implementations)]
7#![warn(pointer_structural_match)]
8#![warn(unreachable_pub)]
9#![warn(unused_crate_dependencies)]
10#![warn(unused_qualifications)]
11
12mod serde;
13
14mod checksum;
15
16pub mod error;
17pub mod multienc;
18pub mod participant;
19
20#[cfg(feature = "dkg")]
21pub mod dkg;
22
23#[cfg(feature = "signing")]
24pub mod nonces;
25#[cfg(feature = "signing")]
26pub mod signature_share;
27#[cfg(feature = "signing")]
28pub mod signing_commitment;
29
30pub use ironfish_reddsa::frost::redjubjub as frost;
31
32#[cfg(feature = "std")]
33mod io {
34    pub(crate) use std::io::Error;
35    pub(crate) use std::io::ErrorKind;
36    pub(crate) use std::io::Read;
37    pub(crate) use std::io::Result;
38    pub(crate) use std::io::Write;
39}
40
41#[cfg(not(feature = "std"))]
42#[macro_use]
43#[cfg(not(feature = "std"))]
44extern crate alloc;
45
46#[cfg(not(feature = "std"))]
47mod io {
48    use core::cmp;
49    use core::mem;
50
51    #[derive(Clone, Debug)]
52    pub struct Error;
53
54    impl Error {
55        pub fn other<T>(_: T) -> Self {
56            Self
57        }
58    }
59
60    pub(crate) type Result<T> = core::result::Result<T, Error>;
61
62    pub trait Read {
63        fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
64
65        fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
66            while !buf.is_empty() {
67                match self.read(buf) {
68                    Ok(0) => break,
69                    Ok(n) => {
70                        buf = &mut buf[n..];
71                    }
72                    Err(e) => return Err(e),
73                }
74            }
75            if buf.is_empty() {
76                Ok(())
77            } else {
78                Err(Error)
79            }
80        }
81    }
82
83    impl<R: Read> Read for &mut R {
84        #[inline]
85        fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
86            (*self).read(buf)
87        }
88
89        #[inline]
90        fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
91            (*self).read_exact(buf)
92        }
93    }
94
95    impl Read for &[u8] {
96        fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
97            let n = cmp::min(self.len(), buf.len());
98            let (copy, remaining) = self.split_at(n);
99            buf[..n].copy_from_slice(copy);
100            *self = remaining;
101            Ok(n)
102        }
103    }
104
105    pub trait Write {
106        fn write(&mut self, buf: &[u8]) -> Result<usize>;
107
108        fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
109            while !buf.is_empty() {
110                match self.write(buf) {
111                    Ok(0) => return Err(Error),
112                    Ok(n) => {
113                        buf = &buf[n..];
114                    }
115                    Err(e) => return Err(e),
116                }
117            }
118            Ok(())
119        }
120    }
121
122    impl<W: Write> Write for &mut W {
123        #[inline]
124        fn write(&mut self, buf: &[u8]) -> Result<usize> {
125            (*self).write(buf)
126        }
127
128        #[inline]
129        fn write_all(&mut self, buf: &[u8]) -> Result<()> {
130            (*self).write_all(buf)
131        }
132    }
133
134    impl Write for &mut [u8] {
135        fn write(&mut self, buf: &[u8]) -> Result<usize> {
136            let n = cmp::min(self.len(), buf.len());
137            let (copy, remaining) = mem::take(self).split_at_mut(n);
138            copy.copy_from_slice(&buf[..n]);
139            *self = remaining;
140            Ok(n)
141        }
142    }
143}
144
145#[cfg(not(feature = "std"))]
146use alloc::vec::Vec;
147
148#[cfg(not(feature = "std"))]
149impl io::Write for Vec<u8> {
150    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
151        self.extend_from_slice(buf);
152        Ok(buf.len())
153    }
154
155    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
156        self.extend_from_slice(buf);
157        Ok(())
158    }
159}