Skip to content

Commit

Permalink
Remove const GENERATOR because it should use exists one in rust-secp2…
Browse files Browse the repository at this point in the history
…56k1 crate
  • Loading branch information
rantan committed Feb 20, 2020
1 parent 2335560 commit eed0586
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/util/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ impl PublicKey {
pub fn from_private_key<C: secp256k1::Signing>(secp: &Secp256k1<C>, sk: &PrivateKey) -> PublicKey {
sk.public_key(secp)
}

/// Returns generator point of secp256k1 as PublicKey
pub fn generator() -> PublicKey {
let mut data: Vec<u8> = Vec::with_capacity(33);

if secp256k1::constants::GENERATOR_Y.last().unwrap() & 1 == 1 {
data.extend(&[3]);
} else {
data.extend(&[2]);
}
data.extend(&secp256k1::constants::GENERATOR_X[..]);

PublicKey::from_slice(&data[..]).unwrap()
}
}

impl fmt::Display for PublicKey {
Expand Down Expand Up @@ -491,4 +505,15 @@ mod tests {
let decoded = PublicKey::consensus_decode(&s[..]).unwrap();
assert_eq!(decoded, pk);
}

#[test]
fn test_generator() {
let g = PublicKey::generator();

let mut expected: Vec<u8> = Vec::with_capacity(65);
expected.extend(&[4]);
expected.extend(&secp256k1::constants::GENERATOR_X);
expected.extend(&secp256k1::constants::GENERATOR_Y);
assert_eq!(&expected[..], &g.key.serialize_uncompressed()[..]);
}
}
16 changes: 3 additions & 13 deletions src/util/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,10 @@ use util::key::{PublicKey, PrivateKey};
use util::prime::jacobi;
use util::rfc7969::nonce_rfc6979;


/// Generator for secp256k1 elliptic curve
pub const GENERATOR: [u8; 33] = [
0x02,
0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC,
0x55, 0xA0, 0x62, 0x95, 0xCE, 0x87, 0x0B, 0x07,
0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9,
0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98
];

/// The size of scalar value on secp256k1 curve
pub const SECP256K1_SCALAR_SIZE: usize = 32;

// "SCHNORR + SHA256"
/// "SCHNORR + SHA256"
pub const ALGO16: [u8; 16] = [
83, 67, 72, 78, 79, 82, 82, 32, 43, 32, 83, 72, 65, 50, 53, 54
];
Expand Down Expand Up @@ -84,11 +74,11 @@ impl Signature {
Ok(Signature { r_x, sigma: to_bytes(&sigma) })
}

/// Verify signature
pub fn verify(&self, message: &[u8], pk: &PublicKey) -> Result<(), Error> {
self.verify_inner(message, pk.key.borrow())
}

/// Verify signature
fn verify_inner(&self, message: &[u8], pk: &secp256k1::PublicKey) -> Result<(), Error> {
let ctx = secp256k1::Secp256k1::verification_only();

Expand All @@ -108,7 +98,7 @@ impl Signature {
};

let sg = {
let mut result = secp256k1::PublicKey::from_slice(&GENERATOR[..]).unwrap();
let mut result = PublicKey::generator().key;
result.mul_assign(&ctx, &s[..])?;
result
};
Expand Down

0 comments on commit eed0586

Please sign in to comment.