Struct rings_core::ecc::SecretKey
source · pub struct SecretKey(_);Expand description
Wrap libsecp256k1::SecretKey.
Implementations§
source§impl SecretKey
impl SecretKey
sourcepub fn random() -> Self
pub fn random() -> Self
Examples found in repository?
src/session.rs (line 176)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
pub fn gen_unsign_info_with_pubkey(
ttl: Option<Ttl>,
signer: Option<Signer>,
pubkey: PublicKey,
) -> Result<(AuthorizedInfo, SecretKey)> {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer {
did: pubkey.address().into(),
pubkey: Some(pubkey),
};
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
Ok((info, key))
}
pub fn gen_unsign_info(
did: Did,
ttl: Option<Ttl>,
signer: Option<Signer>,
) -> (AuthorizedInfo, SecretKey) {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer { did, pubkey: None };
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
(info, key)
}More examples
src/ecc/elgamal.rs (line 121)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
pub fn encrypt(s: &str, k: PublicKey) -> Result<Vec<(CurveEle, CurveEle)>> {
let random_sar: Scalar = SecretKey::random().into();
let mut h: Affine = k.try_into()?;
h.y.normalize();
h.y.normalize();
let affines: Vec<(Affine, Affine)> = str_to_affine(s)
.into_iter()
.map(|c| {
let g_cxt = ECMultGenContext::new_boxed();
let cxt = ECMultContext::new_boxed();
let mut shared_sec = Jacobian::default();
cxt.ecmult_const(&mut shared_sec, &h, &random_sar);
let mut c1 = Jacobian::default();
g_cxt.ecmult_gen(&mut c1, &random_sar);
let mut a_c1 = Affine::from_gej(&c1);
a_c1.x.normalize();
a_c1.y.normalize();
let c2 = shared_sec.add_ge(&c);
let mut a_c2 = Affine::from_gej(&c2);
a_c2.x.normalize();
a_c2.y.normalize();
(a_c1, a_c2)
})
.collect();
let mut ret: Vec<(CurveEle, CurveEle)> = vec![];
for (c1, c2) in affines {
ret.push((c1.try_into()?, c2.try_into()?))
}
Ok(ret)
}sourcepub fn address(&self) -> Address
pub fn address(&self) -> Address
Examples found in repository?
More examples
src/session.rs (line 185)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
pub fn gen_unsign_info_with_pubkey(
ttl: Option<Ttl>,
signer: Option<Signer>,
pubkey: PublicKey,
) -> Result<(AuthorizedInfo, SecretKey)> {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer {
did: pubkey.address().into(),
pubkey: Some(pubkey),
};
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
Ok((info, key))
}
pub fn gen_unsign_info(
did: Did,
ttl: Option<Ttl>,
signer: Option<Signer>,
) -> (AuthorizedInfo, SecretKey) {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer { did, pubkey: None };
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
(info, key)
}
/// sig: Signature of AuthorizedInfo
/// auth_info: generated from `gen_unsign_info`
/// session_key: temp key from gen_unsign_info
pub fn new(sig: &[u8], auth_info: &AuthorizedInfo, session_key: &SecretKey) -> Self {
let inner = SessionWithKey {
session: Session::new(sig, auth_info),
session_key: *session_key,
};
Self {
inner: Arc::new(RwLock::new(inner)),
}
}
/// generate Session with private key
/// only use it for unittest
pub fn new_with_seckey(key: &SecretKey, ttl: Option<Ttl>) -> Result<Self> {
let (auth, s_key) = Self::gen_unsign_info(key.address().into(), ttl, None);
let sig = key.sign(&auth.to_string()?).to_vec();
Ok(Self::new(&sig, &auth, &s_key))
}sourcepub fn sign_hash(&self, message_hash: &[u8; 32]) -> SigBytes
pub fn sign_hash(&self, message_hash: &[u8; 32]) -> SigBytes
Examples found in repository?
src/ecc/signers.rs (line 18)
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
pub fn sign(sec: SecretKey, hash: &[u8; 32]) -> [u8; 65] {
sec.sign_hash(hash)
}
pub fn hash(msg: &str) -> [u8; 32] {
keccak256(msg.as_bytes())
}
pub fn recover(msg: &str, sig: impl AsRef<[u8]>) -> Result<PublicKey> {
let sig_byte: [u8; 65] = sig.as_ref().try_into()?;
crate::ecc::recover(msg, sig_byte)
}
pub fn verify(msg: &str, address: &Address, sig: impl AsRef<[u8]>) -> bool {
if let Ok(p) = recover(msg, sig) {
p.address() == *address
} else {
false
}
}
}
/// eip191.
/// ref <https://eips.ethereum.org/EIPS/eip-191>
pub mod eip191 {
use super::*;
/// sign function passing raw message parameter.
pub fn sign_raw(sec: SecretKey, msg: &str) -> [u8; 65] {
sign(sec, &hash(msg))
}
/// sign function with `hash` data.
pub fn sign(sec: SecretKey, hash: &[u8; 32]) -> [u8; 65] {
let mut sig = sec.sign_hash(hash);
sig[64] += 27;
sig
}More examples
pub fn pubkey(&self) -> PublicKey
Methods from Deref<Target = SecretKey>§
Trait Implementations§
source§impl PartialEq<SecretKey> for SecretKey
impl PartialEq<SecretKey> for SecretKey
impl Copy for SecretKey
impl Eq for SecretKey
impl StructuralEq for SecretKey
impl StructuralPartialEq for SecretKey
Auto Trait Implementations§
impl RefUnwindSafe for SecretKey
impl Send for SecretKey
impl Sync for SecretKey
impl Unpin for SecretKey
impl UnwindSafe for SecretKey
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.