KeyPair

Struct KeyPair 

Source
pub struct KeyPair {
    pub sk: Scalar,
    pub pk: Point,
}
Expand description

Cryptographic key pair containing a secret key and corresponding public key This represents original (non-proxy) keys that can create delegations

Fields§

§sk: Scalar

Secret scalar value

§pk: Point

Public point value

Implementations§

Source§

impl KeyPair

Source

pub fn new() -> Result<Self>

Generate a new random key pair

§Returns

A new KeyPair with randomly generated secret and public keys

§Errors

Returns an error if scalar multiplication fails

Examples found in repository?
examples/demo.rs (line 11)
6fn main() -> Result<()> {
7    println!("=== Ristretto255 Proxy Signatures Demo ===\n");
8
9    // 1) Key generation
10    println!("1. Generating keys...");
11    let a = KeyPair::new()?; // Original signer
12    let b = KeyPair::new()?; // Proxy signer
13    println!(
14        "   A's public key: {}",
15        Hex::encode_to_string(a.pk).unwrap()
16    );
17    println!(
18        "   B's public key: {}",
19        Hex::encode_to_string(b.pk).unwrap()
20    );
21
22    // 2) Delegation with warrant
23    println!("\n2. Creating delegation...");
24    let warrant = b"Proxy: B may sign for A for service XYZ until 2026-12-31";
25    let token = DelegationToken::create(&a, &b.pk, warrant)?;
26    if !token.verify(&a.pk)? {
27        return Err(proxy_signatures::ProxySignatureError::InvalidDelegation);
28    }
29    println!("   Warrant: {}", std::str::from_utf8(warrant)?);
30    println!("   Delegation token created and verified");
31
32    // 3) Proxy key derivation
33    println!("\n3. Deriving proxy keys...");
34    let keys = proxy_signatures::derive_proxy_keys(&b, &a.pk, &token)?;
35    let ctx = ProxyPublicContext {
36        warrant: warrant.to_vec(),
37        rw: token.rw,
38        yp: keys.pk,
39    };
40    println!(
41        "   Proxy public key YP: {}",
42        Hex::encode_to_string(keys.pk).unwrap()
43    );
44
45    // 4) Proxy signs
46    println!("\n4. Creating proxy signature...");
47    let msg = b"Pay 10 units to Carol";
48    let sig = proxy_signatures::proxy_sign(&keys.sk, msg)?;
49    println!("   Message: {}", std::str::from_utf8(msg)?);
50    println!("   Signature created");
51
52    // 5) Verify proxy signature
53    println!("\n5. Verifying proxy signature...");
54    let valid = proxy_signatures::proxy_verify(&a.pk, &b.pk, msg, &sig, &ctx)?;
55    println!("   Proxy signature valid? {}", valid);
56
57    // 6) Test with wrong message
58    println!("\n6. Testing with wrong message...");
59    let wrong_msg = b"Pay 100 units to Carol";
60    let invalid = proxy_signatures::proxy_verify(&a.pk, &b.pk, wrong_msg, &sig, &ctx)?;
61    println!(
62        "   Wrong message signature valid? {} (should be false)",
63        invalid
64    );
65
66    // 7) Demonstrate revocation by tampering with warrant
67    println!("\n7. Testing revocation (modified warrant)...");
68    let revoked_ctx = ProxyPublicContext {
69        warrant: b"REVOKED: Original warrant no longer valid".to_vec(),
70        rw: ctx.rw,
71        yp: ctx.yp,
72    };
73    let revoked = proxy_signatures::proxy_verify(&a.pk, &b.pk, msg, &sig, &revoked_ctx)?;
74    println!(
75        "   Signature with revoked warrant valid? {} (should be false)",
76        revoked
77    );
78
79    println!("\n✅ All operations completed successfully!");
80
81    Ok(())
82}
Source

pub fn public_key(&self) -> &Point

Get a reference to the public key

Source

pub fn secret_key(&self) -> &Scalar

Get a reference to the secret key

Source

pub fn from_secret_key(sk: Scalar) -> Result<Self>

Create a KeyPair from existing secret key

§Arguments
  • sk - Secret key scalar
§Returns

A new KeyPair with the given secret key and derived public key

§Errors

Returns an error if scalar multiplication fails

Trait Implementations§

Source§

impl AsRef<[u8; 32]> for KeyPair

Source§

fn as_ref(&self) -> &Point

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for KeyPair

Source§

fn clone(&self) -> KeyPair

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyPair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for KeyPair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&KeyPair> for Point

Source§

fn from(keypair: &KeyPair) -> Self

Converts to this type from the input type.
Source§

impl Hash for KeyPair

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for KeyPair

Source§

fn eq(&self, other: &KeyPair) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for KeyPair

Source§

impl StructuralPartialEq for KeyPair

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.