Skip to content

Commit

Permalink
add parsing dlog SecretKey from SEC-1-encoded bytes;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Sep 30, 2020
1 parent 8a6761c commit 0ef0ce0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bindings/ergo-wallet-lib-wasm/src/secret_key.rs
@@ -1,4 +1,7 @@
//! Secret key
use std::convert::TryInto;

use sigma_tree::sigma_protocol::DlogProverInput;
use sigma_tree::wallet;
use wasm_bindgen::prelude::*;

Expand All @@ -16,6 +19,20 @@ impl SecretKey {
SecretKey(wallet::secret_key::SecretKey::random_dlog())
}

/// Parse dlog secret key from bytes (SEC-1-encoded scalar)
pub fn dlog_from_bytes(bytes: &[u8]) -> Result<SecretKey, JsValue> {
let sized_bytes: &[u8; DlogProverInput::SIZE_BYTES] = bytes.try_into().map_err(|_| {
JsValue::from_str(&format!(
"expected byte array of size {}, found {}",
DlogProverInput::SIZE_BYTES,
bytes.len()
))
})?;
wallet::secret_key::SecretKey::dlog_from_bytes(sized_bytes)
.map(SecretKey)
.ok_or_else(|| JsValue::from_str("failed to parse scalar"))
}

/// Address (encoded public image)
pub fn get_address(&self) -> Address {
self.0.get_address_from_public_image().into()
Expand Down
2 changes: 1 addition & 1 deletion sigma-tree/src/lib.rs
Expand Up @@ -22,8 +22,8 @@ pub mod ast;
pub mod chain;
pub mod eval;
pub mod sigma_protocol;
pub mod util;
pub mod wallet;

pub use ergo_tree::*;

#[cfg(test)]
Expand Down
21 changes: 21 additions & 0 deletions sigma-tree/src/sigma_protocol/private_input.rs
@@ -1,4 +1,7 @@
use crate::util::IntoOption;

use super::{dlog_group, ProveDlog};
use elliptic_curve::FromBytes;
use k256::Scalar;

/// Secret key of discrete logarithm signature protocol
Expand All @@ -9,13 +12,25 @@ pub struct DlogProverInput {
}

impl DlogProverInput {
/// Scalar(secret key) size in bytes
pub const SIZE_BYTES: usize = 32;

/// generates random secret in the range [0, n), where n is DLog group order.
pub fn random() -> DlogProverInput {
DlogProverInput {
w: dlog_group::random_scalar_in_group_range(),
}
}

/// Attempts to parse the given byte array as an SEC-1-encoded scalar(secret key).
/// Returns None if the byte array does not contain a big-endian integer in the range
/// [0, modulus).
pub fn from_bytes(bytes: &[u8; DlogProverInput::SIZE_BYTES]) -> Option<DlogProverInput> {
Scalar::from_bytes(bytes.into())
.into_option()
.map(DlogProverInput::from)
}

/// public key of discrete logarithm signature protocol
pub fn public_image(&self) -> ProveDlog {
// test it, see https://github.com/ergoplatform/sigma-rust/issues/38
Expand All @@ -24,6 +39,12 @@ impl DlogProverInput {
}
}

impl From<Scalar> for DlogProverInput {
fn from(w: Scalar) -> Self {
DlogProverInput { w }
}
}

/// Private inputs (secrets)
pub enum PrivateInput {
/// Discrete logarithm prover input
Expand Down
19 changes: 19 additions & 0 deletions sigma-tree/src/util.rs
@@ -0,0 +1,19 @@
//! Utilities

use elliptic_curve::subtle::CtOption;

/// Convert to Option<T>
pub trait IntoOption<T> {
/// Get Option<T>
fn into_option(self) -> Option<T>;
}

impl<T> IntoOption<T> for CtOption<T> {
fn into_option(self) -> Option<T> {
if self.is_some().into() {
Some(self.unwrap())
} else {
None
}
}
}
5 changes: 5 additions & 0 deletions sigma-tree/src/wallet/secret_key.rs
Expand Up @@ -15,6 +15,11 @@ impl SecretKey {
SecretKey::DlogSecretKey(DlogProverInput::random())
}

/// Parse DlogSecretKey from bytes (SEC-1-encoded scalar)
pub fn dlog_from_bytes(bytes: &[u8; DlogProverInput::SIZE_BYTES]) -> Option<SecretKey> {
DlogProverInput::from_bytes(bytes).map(SecretKey::DlogSecretKey)
}

/// Address (encoded public image)
pub fn get_address_from_public_image(&self) -> Address {
match self {
Expand Down

0 comments on commit 0ef0ce0

Please sign in to comment.