Skip to content

Commit

Permalink
add SecretKey, DlogProverInput;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jun 10, 2020
1 parent 6ae1b79 commit 206f570
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 122 deletions.
7 changes: 4 additions & 3 deletions sigma-tree-wasm/src/lib.rs
Expand Up @@ -75,13 +75,14 @@ impl Address {

/// TODO: wrap sigma-tree type
#[wasm_bindgen]
pub struct SecretKey(String);
pub struct SecretKey(chain::SecretKey);

#[wasm_bindgen]
impl SecretKey {
/// Decode from string
pub fn parse(_: &str) -> SecretKey {
SecretKey(String::new())
pub fn parse(_: &str) -> Result<SecretKey, JsValue> {
// TODO: implement
Ok(SecretKey(chain::SecretKey::random_dlog()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion sigma-tree-wasm/tests/transaction.rs
Expand Up @@ -11,7 +11,7 @@ wasm_bindgen_test_configure!(run_in_browser);
fn test_signed_p2pk_transaction() {
let tx_inputs = TxInputs::from_boxes(Box::new([]));
let send_change_to = Address::from_testnet_str("").expect("failed");
let sk = SecretKey::parse("");
let sk = SecretKey::parse("").expect("failed");
let recipient = Address::from_testnet_str("").expect("failed");

let outbox = ErgoBoxCandidate::new(1, 0, Contract::pay_to_address(recipient));
Expand Down
10 changes: 8 additions & 2 deletions sigma-tree/src/ast/constant.rs
@@ -1,4 +1,4 @@
use crate::{chain::ErgoBox, data::SigmaProp, types::SType};
use crate::{chain::ErgoBox, sigma_protocol::SigmaProp, types::SType};

#[derive(PartialEq, Eq, Debug, Clone)]
pub enum CollPrim {
Expand Down Expand Up @@ -26,6 +26,12 @@ pub enum ConstantVal {
Tup(Vec<ConstantVal>),
}

impl ConstantVal {
pub fn sigma_prop(prop: SigmaProp) -> ConstantVal {
ConstantVal::SigmaProp(Box::new(prop))
}
}

#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Constant {
pub tpe: SType,
Expand All @@ -36,7 +42,7 @@ impl Constant {
pub fn sigma_prop(prop: SigmaProp) -> Constant {
Constant {
tpe: SType::SSigmaProp,
v: ConstantVal::SigmaProp(Box::new(prop)),
v: ConstantVal::sigma_prop(prop),
}
}
}
2 changes: 2 additions & 0 deletions sigma-tree/src/chain.rs
Expand Up @@ -8,6 +8,7 @@ mod data_input;
mod ergo_box;
mod input;
mod prover_result;
mod secret_key;
mod token;
mod transaction;

Expand All @@ -16,4 +17,5 @@ pub use box_id::*;
pub use contract::*;
pub use ergo_box::*;
pub use input::*;
pub use secret_key::*;
pub use transaction::*;
6 changes: 4 additions & 2 deletions sigma-tree/src/chain/address.rs
@@ -1,7 +1,7 @@
use crate::{
ast::{Constant, Expr},
data::{ProveDlog, SigmaBoolean, SigmaProp},
ecpoint::EcPoint,
sigma_protocol::{ProveDlog, SigmaBoolean, SigmaProofOfKnowledgeTree, SigmaProp},
ErgoTree,
};
use std::fmt;
Expand Down Expand Up @@ -87,7 +87,9 @@ impl Address for P2PKAddress {
}
fn script(&self) -> ErgoTree {
ErgoTree::from_proposition(Rc::new(Expr::Const(Constant::sigma_prop(SigmaProp::new(
SigmaBoolean::ProveDlog(self.pubkey.clone()),
SigmaBoolean::ProofOfKnowledge(SigmaProofOfKnowledgeTree::ProveDlog(
self.pubkey.clone(),
)),
)))))
}
}
Expand Down
14 changes: 14 additions & 0 deletions sigma-tree/src/chain/secret_key.rs
@@ -0,0 +1,14 @@
use crate::sigma_protocol::DlogProverInput;

/// Secrets which do not have a derivation scheme.
pub enum SecretKey {
/// Secret exponent of a group element, i.e. secret w such as h = g^^w, where g is group generator, h is a public key.
DlogSecretKey(DlogProverInput),
}

impl SecretKey {
/// Generates random DlogProverInput
pub fn random_dlog() -> SecretKey {
SecretKey::DlogSecretKey(DlogProverInput::random())
}
}
78 changes: 0 additions & 78 deletions sigma-tree/src/data.rs

This file was deleted.

45 changes: 24 additions & 21 deletions sigma-tree/src/ecpoint.rs
@@ -1,3 +1,4 @@
use crate::sigma_protocol::DlogProverInput;
use k256::{
arithmetic::{AffinePoint, ProjectivePoint, Scalar},
PublicKey,
Expand All @@ -6,7 +7,6 @@ use sigma_ser::{
serializer::{SerializationError, SigmaSerializable},
vlq_encode,
};
use std::convert::TryInto;
use std::io;

#[derive(PartialEq, Debug, Clone)]
Expand All @@ -16,26 +16,29 @@ impl EcPoint {
pub const GROUP_SIZE: usize = 33;

pub fn random() -> EcPoint {
let scalar = loop {
// Generate a new secret key using the operating system's
// cryptographically secure random number generator
let sk = k256::SecretKey::generate();
let bytes: [u8; 32] = sk
.secret_scalar()
.as_ref()
.as_slice()
.try_into()
.expect("expected 32 bytes");
// Returns None if the byte array does not contain
// a big-endian integer in the range [0, n), where n is group order.
let maybe_scalar = Scalar::from_bytes(bytes);
if bool::from(maybe_scalar.is_some()) {
break maybe_scalar.unwrap();
}
};
// we treat EC as a multiplicative group, therefore, exponentiate point is multiply.
let pkp = ProjectivePoint::generator() * &scalar;
EcPoint(pkp)
let sk = DlogProverInput::random();
EcPoint::generator().exponentiate(&sk.w)
}

pub fn generator() -> EcPoint {
EcPoint(ProjectivePoint::generator())
}

pub fn is_infinity(&self) -> bool {
let identity = ProjectivePoint::identity();
self.0 == identity
}

pub fn exponentiate(&self, exponent: &Scalar) -> EcPoint {
if !self.is_infinity() {
// TODO: check if exponent is negative
// see reference impl https://github.com/ScorexFoundation/sigmastate-interpreter/blob/ec71a6f988f7412bc36199f46e7ad8db643478c7/sigmastate/src/main/scala/sigmastate/basics/BcDlogGroup.scala#L201

// we treat EC as a multiplicative group, therefore, exponentiate point is multiply.
EcPoint(self.0 * exponent)
} else {
self.clone()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion sigma-tree/src/ergo_tree.rs
Expand Up @@ -110,7 +110,7 @@ impl<'de> serde::Deserialize<'de> for ErgoTree {
#[cfg(test)]
mod tests {
use super::*;
use crate::{ast::ConstantVal, data::SigmaProp};
use crate::{ast::ConstantVal, sigma_protocol::SigmaProp};
use proptest::prelude::*;
use sigma_ser::test_helpers::*;

Expand Down
2 changes: 1 addition & 1 deletion sigma-tree/src/eval/value.rs
@@ -1,4 +1,4 @@
use crate::{chain::ErgoBox, data::SigmaBoolean};
use crate::{chain::ErgoBox, sigma_protocol::SigmaBoolean};
use std::ops::Add;

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion sigma-tree/src/lib.rs
Expand Up @@ -12,11 +12,11 @@

mod ast;
mod constants;
mod data;
mod ecpoint;
mod ergo_tree;
mod eval;
mod serialization;
mod sigma_protocol;
mod types;

pub mod chain;
Expand Down
11 changes: 3 additions & 8 deletions sigma-tree/src/serialization/data.rs
@@ -1,11 +1,8 @@
use crate::{
ast::CollPrim,
ast::ConstantVal,
ast::ConstantVal::*,
data::{self, SigmaBoolean},
types::SType,
ast::CollPrim, ast::ConstantVal, ast::ConstantVal::*, sigma_protocol, types::SType,
types::SType::*,
};
use sigma_protocol::{SigmaBoolean, SigmaProp};
use sigma_ser::{
serializer::{SerializationError, SigmaSerializable},
vlq_encode::{ReadSigmaVlqExt, WriteSigmaVlqExt},
Expand Down Expand Up @@ -46,9 +43,7 @@ impl DataSerializer {
let c = match tpe {
SAny => todo!(),
SByte => Byte(r.get_i8()?),
SSigmaProp => SigmaProp(Box::new(data::SigmaProp::new(SigmaBoolean::sigma_parse(
r,
)?))),
SSigmaProp => ConstantVal::sigma_prop(SigmaProp::new(SigmaBoolean::sigma_parse(r)?)),
SColl(elem_type) => {
let len = r.get_u16()? as usize;
if **elem_type == SByte {
Expand Down
12 changes: 8 additions & 4 deletions sigma-tree/src/serialization/sigmaboolean.rs
@@ -1,7 +1,7 @@
use super::op_code::OpCode;
use crate::{
data::{ProveDlog, SigmaBoolean},
ecpoint::EcPoint,
sigma_protocol::{ProveDlog, SigmaBoolean, SigmaProofOfKnowledgeTree},
};
use sigma_ser::{
serializer::{SerializationError, SigmaSerializable},
Expand All @@ -13,8 +13,10 @@ impl SigmaSerializable for SigmaBoolean {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: &mut W) -> Result<(), io::Error> {
self.op_code().sigma_serialize(w)?;
match self {
SigmaBoolean::ProveDHTuple { .. } => todo!(),
SigmaBoolean::ProveDlog(v) => v.h.sigma_serialize(w),
SigmaBoolean::ProofOfKnowledge(proof) => match proof {
SigmaProofOfKnowledgeTree::ProveDHTuple { .. } => todo!(),
SigmaProofOfKnowledgeTree::ProveDlog(v) => v.h.sigma_serialize(w),
},
SigmaBoolean::CAND(_) => todo!(),
}
}
Expand All @@ -24,7 +26,9 @@ impl SigmaSerializable for SigmaBoolean {
match op_code {
OpCode::PROVE_DLOG => {
let p = EcPoint::sigma_parse(r)?;
Ok(SigmaBoolean::ProveDlog(ProveDlog { h: Box::new(p) }))
Ok(SigmaBoolean::ProofOfKnowledge(
SigmaProofOfKnowledgeTree::ProveDlog(ProveDlog::new(p)),
))
}
_ => todo!(),
}
Expand Down

0 comments on commit 206f570

Please sign in to comment.