1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::{
encryption::{GroupEncryption, GroupEncryptionParameters, GroupEncryptionPublicKey},
signature::{SchnorrOutput, SchnorrParameters, SchnorrPublicKey, SchnorrSignature},
};
use snarkvm_errors::algorithms::SignatureError;
use snarkvm_models::{
algorithms::{EncryptionScheme, SignatureScheme},
curves::{Group, PrimeField, ProjectiveCurve},
};
use snarkvm_utilities::{serialize::*, to_bytes, FromBytes, ToBytes};
use digest::Digest;
use rand::Rng;
use std::{hash::Hash, marker::PhantomData};
fn into_signature_group<G: Group + ProjectiveCurve + CanonicalSerialize, SG: Group + CanonicalDeserialize>(
projective: G,
) -> SG {
let mut bytes = vec![];
CanonicalSerialize::serialize(&projective.into_affine(), &mut bytes).expect("failed to convert to bytes");
CanonicalDeserialize::deserialize(&mut &bytes[..]).expect("failed to convert to signature group")
}
impl<G: Group + ProjectiveCurve + CanonicalSerialize, SG: Group + CanonicalDeserialize, D: Digest>
From<GroupEncryptionParameters<G>> for SchnorrSignature<SG, D>
{
fn from(parameters: GroupEncryptionParameters<G>) -> Self {
let generator_powers: Vec<SG> = parameters
.generator_powers
.iter()
.map(|p| into_signature_group(*p))
.collect();
let parameters = SchnorrParameters {
generator_powers,
salt: parameters.salt,
_hash: PhantomData,
};
Self { parameters }
}
}
impl<G: Group + ProjectiveCurve, SG: Group + CanonicalSerialize + CanonicalDeserialize>
From<GroupEncryptionPublicKey<G>> for SchnorrPublicKey<SG>
{
fn from(public_key: GroupEncryptionPublicKey<G>) -> Self {
Self(into_signature_group(public_key.0))
}
}
impl<G: Group + ProjectiveCurve, SG: Group + Hash + CanonicalSerialize + CanonicalDeserialize, D: Digest + Send + Sync>
SignatureScheme for GroupEncryption<G, SG, D>
where
<G as Group>::ScalarField: PrimeField,
{
type Output = SchnorrOutput<SG>;
type Parameters = GroupEncryptionParameters<G>;
type PrivateKey = <G as Group>::ScalarField;
type PublicKey = GroupEncryptionPublicKey<G>;
fn setup<R: Rng>(rng: &mut R) -> Result<Self, SignatureError> {
Ok(<Self as EncryptionScheme>::setup(rng))
}
fn parameters(&self) -> &Self::Parameters {
&self.parameters
}
fn generate_private_key<R: Rng>(&self, rng: &mut R) -> Result<Self::PrivateKey, SignatureError> {
Ok(<Self as EncryptionScheme>::generate_private_key(self, rng))
}
fn generate_public_key(&self, private_key: &Self::PrivateKey) -> Result<Self::PublicKey, SignatureError> {
Ok(<Self as EncryptionScheme>::generate_public_key(self, private_key).unwrap())
}
fn sign<R: Rng>(
&self,
private_key: &Self::PrivateKey,
message: &[u8],
rng: &mut R,
) -> Result<Self::Output, SignatureError> {
let schnorr_signature: SchnorrSignature<SG, D> = self.parameters.clone().into();
let private_key = <SG as Group>::ScalarField::read(&to_bytes![private_key]?[..])?;
Ok(schnorr_signature.sign(&private_key, message, rng)?)
}
fn verify(
&self,
public_key: &Self::PublicKey,
message: &[u8],
signature: &Self::Output,
) -> Result<bool, SignatureError> {
let schnorr_signature: SchnorrSignature<SG, D> = self.parameters.clone().into();
let schnorr_public_key: SchnorrPublicKey<SG> = (*public_key).into();
Ok(schnorr_signature.verify(&schnorr_public_key, message, signature)?)
}
fn randomize_public_key(
&self,
_public_key: &Self::PublicKey,
_randomness: &[u8],
) -> Result<Self::PublicKey, SignatureError> {
unimplemented!()
}
fn randomize_signature(
&self,
_signature: &Self::Output,
_randomness: &[u8],
) -> Result<Self::Output, SignatureError> {
unimplemented!()
}
}