Skip to content

Commit 0e91134

Browse files
committed
update cryptographic proof mechanisms
1 parent d07c4d9 commit 0e91134

5 files changed

Lines changed: 402 additions & 173 deletions

File tree

app/node/src/identity.rs

Lines changed: 171 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,138 +2,201 @@ use super::*;
22
use ed25519_dalek::Signer as _;
33
use ed25519::signature::Keypair as _;
44

5-
#[derive(Debug)]
6-
#[derive(Clone)]
7-
#[derive(PartialEq)]
8-
#[derive(Eq)]
9-
#[derive(derive_more::Deref)]
10-
#[derive(derive_more::DerefMut)]
11-
pub struct SignedVerified {
12-
pk: PublicKey,
13-
#[deref]
14-
#[deref_mut]
15-
content: Vec<u8>
5+
pub mod commitment {
6+
167
}
178

18-
impl SignedVerified {
19-
pub fn pk(&self) -> &PublicKey {
20-
&self.pk
9+
pub mod multi_party {
10+
use super::*;
11+
12+
pub struct CompleteVerified(Vec<u8>);
13+
14+
impl TryFrom<Partial> for CompleteVerified {
15+
type Error = Box<dyn std::error::Error>;
16+
17+
fn try_from(value: Partial) -> std::result::Result<Self, Self::Error> {
18+
19+
}
2120
}
2221

23-
pub fn content(&self) -> &[u8] {
24-
&self.content
22+
// all parties have signed
23+
pub struct CompleteUnverified(Vec<u8>);
24+
25+
pub struct CompleteVerified {
26+
signers: Vec<PublicKey>,
27+
content: Vec<u8>
2528
}
2629

27-
pub fn unpack(self) -> (PublicKey, Unsigned) {
28-
let mg: Unsigned = self.content.into();
29-
(self.pk, mg)
30+
#[derive(Debug)]
31+
#[derive(Clone)]
32+
#[derive(PartialEq)]
33+
#[derive(Eq)]
34+
pub struct Partial {
35+
mg: Vec<u8>,
36+
pk_to_sg: std::collections::HashMap<PublicKey, Signature>,
37+
signers: Vec<PublicKey>
3038
}
31-
}
32-
33-
impl TryFrom<SignedUnverified> for SignedVerified {
34-
type Error = Box<dyn std::error::Error>;
3539

36-
fn try_from(value: SignedUnverified) -> std::result::Result<Self, Self::Error> {
37-
let (sg, pk, mg) = value.unpack();
38-
39-
sg.verify(&mg, &pk)?;
40+
impl Partial {
41+
pub fn sign(self, pk: PublicKey, sg: Signature) -> Result<Or<Self, CompleteVerified>> {
42+
sg.verify(mg, &pk)?;
43+
44+
self.pk_to_sg.insert(pk, sg);
45+
Ok(Or::Lhs(self))
46+
}
47+
}
48+
49+
impl TryFrom<Vec<u8>> for Partial {
50+
type Error = Box<dyn std::error::Error>;
4051

41-
let Unsigned(content) = mg;
42-
Ok(Self {
43-
pk: pk.to_owned(),
44-
content
45-
})
52+
fn try_from(value: Vec<u8>) -> std::result::Result<Self, Self::Error> {
53+
54+
}
4655
}
56+
57+
pub struct Unsigned(Vec<u8>);
4758
}
4859

49-
#[derive(Debug)]
50-
#[derive(Clone)]
51-
#[derive(PartialEq)]
52-
#[derive(Eq)]
53-
#[derive(serde::Serialize)]
54-
#[derive(serde::Deserialize)]
55-
#[derive(derive_more::Deref)]
56-
#[derive(derive_more::DerefMut)]
57-
#[serde(try_from = "Vec<u8>")]
58-
pub struct SignedUnverified(Vec<u8>);
59-
60-
impl SignedUnverified {
61-
pub fn verify(self) -> Result<SignedVerified> {
62-
self.try_into()
63-
}
64-
65-
pub fn unpack(self) -> (Signature, PublicKey, Unsigned) {
66-
let Self(mut bytes) = self;
67-
let mut mgpk: Vec<_> = bytes.split_off(64);
68-
let mg: Vec<_> = mgpk.split_off(32);
69-
let mg: Unsigned = mg.try_into().expect("unsigned content slice is guaranteed to be non-empty via constructors");
70-
let pk: [_; _] = mgpk.as_slice().try_into().expect("public key state corruption; must equal 32 bytes");
71-
let pk: PublicKey = pk.into();
72-
let sg: [_; _] = bytes.as_slice().try_into().expect("signature state corruption; remaining bytes must equal 64; guaranteed via constructors");
73-
let sg: Signature = sg.into();
74-
(sg, pk, mg)
60+
pub mod single_party {
61+
use super::*;
62+
63+
#[derive(Debug)]
64+
#[derive(Clone)]
65+
#[derive(PartialEq)]
66+
#[derive(Eq)]
67+
#[derive(derive_more::Deref)]
68+
#[derive(derive_more::DerefMut)]
69+
pub struct SignedVerified {
70+
pk: PublicKey,
71+
#[deref]
72+
#[deref_mut]
73+
content: Vec<u8>
7574
}
76-
}
77-
78-
impl From<(&Unsigned, &SecretKey)> for SignedUnverified {
79-
fn from(value: (&Unsigned, &SecretKey)) -> Self {
80-
let (Unsigned(mg), SecretKey(sk)) = value;
81-
let sk: ed25519_dalek::SigningKey = ed25519_dalek::SigningKey::from_bytes(&sk);
82-
let pk: [_; _] = sk.verifying_key().to_bytes();
83-
let sg: ed25519_dalek::Signature = sk.sign(&mg);
84-
let sg: [_; _] = sg.to_bytes();
85-
let capacity: usize = 64 + 32 + mg.len();
75+
76+
impl SignedVerified {
77+
pub fn pk(&self) -> &PublicKey {
78+
&self.pk
79+
}
8680

87-
let mut bytes: Vec<_> = Vec::with_capacity(capacity);
88-
bytes.extend_from_slice(&sg);
89-
bytes.extend_from_slice(&pk);
90-
bytes.extend_from_slice(&mg);
81+
pub fn content(&self) -> &[u8] {
82+
&self.content
83+
}
84+
}
85+
86+
impl Unpack<(PublicKey, Unsigned)> for SignedVerified {
87+
fn unpack(self) -> (PublicKey, Unsigned) {
88+
let mg: Unsigned = self.content.into();
89+
(self.pk, mg)
90+
}
91+
}
92+
93+
impl TryFrom<SignedUnverified> for SignedVerified {
94+
type Error = Box<dyn std::error::Error>;
9195

92-
Self(bytes)
96+
fn try_from(value: SignedUnverified) -> std::result::Result<Self, Self::Error> {
97+
let (sg, pk, mg) = value.unpack();
98+
99+
sg.verify(&mg, &pk)?;
100+
101+
let Unsigned(content) = mg;
102+
Ok(Self {
103+
pk: pk.to_owned(),
104+
content
105+
})
106+
}
93107
}
94-
}
95-
96-
impl TryFrom<Vec<u8>> for SignedUnverified {
97-
type Error = Box<dyn std::error::Error>;
98108

99-
fn try_from(value: Vec<u8>) -> std::result::Result<Self, Self::Error> {
100-
if value.len() <= 96 {
101-
return Err(<Box<dyn std::error::Error>>::from(String::from("signed message packet is too short")))
109+
#[derive(Debug)]
110+
#[derive(Clone)]
111+
#[derive(PartialEq)]
112+
#[derive(Eq)]
113+
#[derive(serde::Serialize)]
114+
#[derive(serde::Deserialize)]
115+
#[derive(derive_more::Deref)]
116+
#[derive(derive_more::DerefMut)]
117+
#[serde(try_from = "Vec<u8>")]
118+
pub struct SignedUnverified(Vec<u8>);
119+
120+
impl SignedUnverified {
121+
pub fn verify(self) -> Result<SignedVerified> {
122+
self.try_into()
102123
}
103-
let content: &[_] = &value[96..];
104-
if content.is_empty() {
105-
return Err(<Box<dyn std::error::Error>>::from(String::from("signed message content cannot be empty")))
124+
}
125+
126+
impl Unpack<(Signature, PublicKey, Unsigned)> for SignedUnverified {
127+
fn unpack(self) -> (Signature, PublicKey, Unsigned) {
128+
let Self(mut bytes) = self;
129+
let mut mgpk: Vec<_> = bytes.split_off(64);
130+
let mg: Vec<_> = mgpk.split_off(32);
131+
let mg: Unsigned = mg.try_into().expect("unsigned content slice is guaranteed to be non-empty via constructors");
132+
let pk: [_; _] = mgpk.as_slice().try_into().expect("public key state corruption; must equal 32 bytes");
133+
let pk: PublicKey = pk.into();
134+
let sg: [_; _] = bytes.as_slice().try_into().expect("signature state corruption; remaining bytes must equal 64; guaranteed via constructors");
135+
let sg: Signature = sg.into();
136+
(sg, pk, mg)
106137
}
107-
Ok(Self(value))
108138
}
109-
}
110-
111-
#[derive(Debug)]
112-
#[derive(Clone)]
113-
#[derive(PartialEq)]
114-
#[derive(Eq)]
115-
#[derive(serde::Serialize)]
116-
#[derive(serde::Deserialize)]
117-
#[derive(derive_more::Deref)]
118-
#[derive(derive_more::DerefMut)]
119-
#[serde(try_from = "Vec<u8>")]
120-
pub struct Unsigned(Vec<u8>);
121-
122-
impl Unsigned {
123-
pub fn sign(&self, sk: &SecretKey) -> SignedUnverified {
124-
(self, sk).into()
139+
140+
impl From<(&Unsigned, &SecretKey)> for SignedUnverified {
141+
fn from(value: (&Unsigned, &SecretKey)) -> Self {
142+
let (Unsigned(mg), SecretKey(sk)) = value;
143+
let sk: ed25519_dalek::SigningKey = ed25519_dalek::SigningKey::from_bytes(&sk);
144+
let pk: [_; _] = sk.verifying_key().to_bytes();
145+
let sg: ed25519_dalek::Signature = sk.sign(&mg);
146+
let sg: [_; _] = sg.to_bytes();
147+
let capacity: usize = 64 + 32 + mg.len();
148+
149+
let mut bytes: Vec<_> = Vec::with_capacity(capacity);
150+
bytes.extend_from_slice(&sg);
151+
bytes.extend_from_slice(&pk);
152+
bytes.extend_from_slice(&mg);
153+
154+
Self(bytes)
155+
}
125156
}
126-
}
127-
128-
impl TryFrom<Vec<u8>> for Unsigned {
129-
type Error = Box<dyn std::error::Error>;
130157

131-
fn try_from(value: Vec<u8>) -> std::result::Result<Self, Self::Error> {
132-
if value.len() == 0 {
133-
return Err(<Box<dyn std::error::Error>>::from(String::from("message too short")))
158+
impl TryFrom<Vec<u8>> for SignedUnverified {
159+
type Error = Box<dyn std::error::Error>;
160+
161+
fn try_from(value: Vec<u8>) -> std::result::Result<Self, Self::Error> {
162+
if value.len() <= 96 {
163+
return Err(<Box<dyn std::error::Error>>::from(String::from("signed message packet is too short")))
164+
}
165+
let content: &[_] = &value[96..];
166+
if content.is_empty() {
167+
return Err(<Box<dyn std::error::Error>>::from(String::from("signed message content cannot be empty")))
168+
}
169+
Ok(Self(value))
134170
}
135-
Ok(Self(value))
136171
}
172+
173+
#[derive(Debug)]
174+
#[derive(Clone)]
175+
#[derive(PartialEq)]
176+
#[derive(Eq)]
177+
#[derive(serde::Serialize)]
178+
#[derive(serde::Deserialize)]
179+
#[derive(derive_more::Deref)]
180+
#[derive(derive_more::DerefMut)]
181+
#[serde(try_from = "Vec<u8>")]
182+
pub struct Unsigned(Vec<u8>);
183+
184+
impl Unsigned {
185+
pub fn sign(&self, sk: &SecretKey) -> SignedUnverified {
186+
(self, sk).into()
187+
}
188+
}
189+
190+
impl TryFrom<Vec<u8>> for Unsigned {
191+
type Error = Box<dyn std::error::Error>;
192+
193+
fn try_from(value: Vec<u8>) -> std::result::Result<Self, Self::Error> {
194+
if value.len() == 0 {
195+
return Err(<Box<dyn std::error::Error>>::from(String::from("message too short")))
196+
}
197+
Ok(Self(value))
198+
}
199+
}
137200
}
138201

139202
#[derive(Debug)]

0 commit comments

Comments
 (0)