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
use super::{tagged_hash, Signature, CHALLENGE_TAG};
use crate::{AffinePoint, FieldBytes, ProjectivePoint, PublicKey, Scalar};
use ecdsa_core::signature::{DigestVerifier, Error, Result, Verifier};
use elliptic_curve::{
bigint::U256,
ops::{LinearCombination, Reduce},
DecompactPoint,
};
use sha2::{
digest::{consts::U32, FixedOutput},
Digest, Sha256,
};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct VerifyingKey {
inner: PublicKey,
}
impl VerifyingKey {
pub fn verify_prehashed(&self, msg_digest: &[u8; 32], sig: &Signature) -> Result<()> {
let (r, s) = sig.split();
let e = <Scalar as Reduce<U256>>::from_be_bytes_reduced(
tagged_hash(CHALLENGE_TAG)
.chain_update(&sig.bytes[..32])
.chain_update(self.to_bytes())
.chain_update(msg_digest)
.finalize(),
);
let R = ProjectivePoint::lincomb(
&ProjectivePoint::GENERATOR,
&*s,
&self.inner.to_projective(),
&-e,
)
.to_affine();
if R.y.normalize().is_odd().into() || R.x.normalize() != *r {
return Err(Error::new());
}
Ok(())
}
pub fn as_affine(&self) -> &AffinePoint {
self.inner.as_affine()
}
pub fn to_bytes(&self) -> FieldBytes {
self.as_affine().x.to_bytes()
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
let maybe_affine_point = AffinePoint::decompact(FieldBytes::from_slice(bytes));
let affine_point = Option::from(maybe_affine_point).ok_or_else(Error::new)?;
PublicKey::from_affine(affine_point)
.map_err(|_| Error::new())?
.try_into()
}
}
impl<D> DigestVerifier<D, Signature> for VerifyingKey
where
D: Digest + FixedOutput<OutputSize = U32>,
{
fn verify_digest(&self, digest: D, signature: &Signature) -> Result<()> {
self.verify_prehashed(&digest.finalize_fixed().into(), signature)
}
}
impl Verifier<Signature> for VerifyingKey {
fn verify(&self, msg: &[u8], signature: &Signature) -> Result<()> {
self.verify_digest(Sha256::new_with_prefix(msg), signature)
}
}
impl From<VerifyingKey> for AffinePoint {
fn from(vk: VerifyingKey) -> AffinePoint {
*vk.as_affine()
}
}
impl From<&VerifyingKey> for AffinePoint {
fn from(vk: &VerifyingKey) -> AffinePoint {
*vk.as_affine()
}
}
impl From<VerifyingKey> for PublicKey {
fn from(vk: VerifyingKey) -> PublicKey {
vk.inner
}
}
impl From<&VerifyingKey> for PublicKey {
fn from(vk: &VerifyingKey) -> PublicKey {
vk.inner
}
}
impl TryFrom<PublicKey> for VerifyingKey {
type Error = Error;
fn try_from(public_key: PublicKey) -> Result<VerifyingKey> {
if public_key.as_affine().y.normalize().is_even().into() {
Ok(Self { inner: public_key })
} else {
Err(Error::new())
}
}
}
impl TryFrom<&PublicKey> for VerifyingKey {
type Error = Error;
fn try_from(public_key: &PublicKey) -> Result<VerifyingKey> {
Self::try_from(*public_key)
}
}