kyber_rs/group/edwards25519/
suite.rs

1use core::fmt::{Display, Formatter};
2use core::ops::{Deref, DerefMut};
3
4use serde::Deserialize;
5use serde::Serialize;
6use sha2::Sha256;
7
8use crate::cipher::Stream;
9use crate::group::edwards25519::curve::Curve;
10use crate::group::edwards25519::scalar::Scalar;
11use crate::group::Group;
12use crate::group::HashFactory;
13use crate::share::vss::suite::Suite;
14use crate::sign::dss;
15use crate::util;
16use crate::util::key::Generator;
17use crate::util::key::KeyError;
18use crate::util::key::Suite as KeySuite;
19use crate::{xof, Random, XOFFactory};
20
21use super::Point;
22
23/// [`SuiteEd25519`] implements some basic functionalities such as [`Group`], [`HashFactory`],
24/// and [`XOFFactory`].
25#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
26pub struct SuiteEd25519 {
27    // TODO: find a way to implement an embedded Stream without breaking everything
28    // r: Box<dyn Stream>,
29    curve: Curve,
30}
31
32impl SuiteEd25519 {
33    /// [`new_blake3_sha256_ed25519()`] returns a cipher suite based on `blake3`,
34    /// `SHA-256`, and the `Ed25519 curve`.It produces cryptographically random
35    /// numbers via crate [`rand`].
36    pub fn new_blake3_sha256_ed25519() -> SuiteEd25519 {
37        SuiteEd25519::default()
38    }
39
40    // TODO: find a way to provide this extended flexibility
41    // func (s *SuiteEd25519) Read(r io.Reader, objs ...interface{}) error {
42    // return fixbuf.Read(r, s, objs...)
43    // }
44    //
45    // func (s *SuiteEd25519) Write(w io.Writer, objs ...interface{}) error {
46    // return fixbuf.Write(w, objs)
47    // }
48    //
49    // /// New implements the kyber.Encoding interface
50    // func (s *SuiteEd25519) New(t reflect.Type) interface{} {
51    // return marshalling.GroupNew(s, t)
52    // }
53
54    // /// NewBlakeSHA256Ed25519WithRand returns a cipher suite based on package
55    // /// go.dedis.ch/kyber/v3/xof/blake2xb, SHA-256, and the Ed25519 curve.
56    // /// It produces cryptographically random numbers via the provided stream r.
57    // func NewBlakeSHA256Ed25519WithRand(r cipher.Stream) *SuiteEd25519 {
58    // suite := new(SuiteEd25519)
59    // suite.r = r
60    // return suite
61    // }
62}
63
64impl Deref for SuiteEd25519 {
65    type Target = Curve;
66
67    fn deref(&self) -> &Self::Target {
68        &self.curve
69    }
70}
71
72impl DerefMut for SuiteEd25519 {
73    fn deref_mut(&mut self) -> &mut Self::Target {
74        &mut self.curve
75    }
76}
77
78impl Generator<Scalar> for SuiteEd25519 {
79    fn new_key<S: crate::cipher::Stream>(
80        &self,
81        stream: &mut S,
82    ) -> Result<Option<Scalar>, KeyError> {
83        self.curve.new_key(stream)
84    }
85}
86
87impl Display for SuiteEd25519 {
88    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
89        write!(f, "{}", self.curve)
90    }
91}
92
93impl Group for SuiteEd25519 {
94    type POINT = Point;
95
96    fn scalar(&self) -> Scalar {
97        self.curve.scalar()
98    }
99
100    fn scalar_len(&self) -> usize {
101        self.curve.scalar_len()
102    }
103
104    fn point(&self) -> Point {
105        self.curve.point()
106    }
107
108    fn point_len(&self) -> usize {
109        self.curve.point_len()
110    }
111
112    fn is_prime_order(&self) -> Option<bool> {
113        self.curve.is_prime_order()
114    }
115}
116
117impl Random for SuiteEd25519 {
118    /// [`random_stream()`] returns a [`Box<Stream>`] that contains a [`Stream`]
119    fn random_stream(&self) -> Box<dyn Stream> {
120        // TODO: add this when the embedded r is added
121        // if self.r != nil {
122        //     return s.r;
123        // }
124        Box::<util::random::random_stream::RandStream>::default()
125    }
126}
127
128impl XOFFactory for SuiteEd25519 {
129    /// [`xof()`] returns an [`XOF`] which is implemented via the `blake3` hash.
130    fn xof(&self, key: Option<&[u8]>) -> Box<dyn crate::XOF> {
131        Box::new(xof::blake3::Xof::new(key))
132    }
133}
134
135impl HashFactory for SuiteEd25519 {
136    type T = Sha256;
137}
138
139impl Suite for SuiteEd25519 {}
140impl dss::Suite for SuiteEd25519 {}
141impl KeySuite for SuiteEd25519 {}