Skip to content

Commit

Permalink
set timeout to 30
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanKung committed May 29, 2024
1 parent 394d76e commit b4869f7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qaci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

build:
name: Build and test
timeout-minutes: 25
timeout-minutes: 30
strategy:
matrix:
os: ["ubuntu-latest"]
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/ecc/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn affine_to_str(a: &[Affine]) -> Result<String> {
field_to_str(a.iter().map(|x| x.x).collect::<Vec<Field>>().as_slice())
}

pub fn encrypt(s: &str, k: PublicKey) -> Result<Vec<(CurveEle, CurveEle)>> {
pub fn encrypt(s: &str, k: PublicKey<33>) -> Result<Vec<(CurveEle<33>, CurveEle<33>)>> {
let random_sar: Scalar = SecretKey::random().into();
let mut h: Affine = k.try_into()?;
h.y.normalize();
Expand All @@ -143,21 +143,21 @@ pub fn encrypt(s: &str, k: PublicKey) -> Result<Vec<(CurveEle, CurveEle)>> {
(a_c1, a_c2)
})
.collect();
let mut ret: Vec<(CurveEle, CurveEle)> = vec![];
let mut ret: Vec<(CurveEle<33>, CurveEle<33>)> = vec![];
for (c1, c2) in affines {
ret.push((c1.try_into()?, c2.try_into()?))
}
Ok(ret)
}

pub fn decrypt(m: &[(CurveEle, CurveEle)], k: SecretKey) -> Result<String> {
pub fn decrypt(m: &[(CurveEle<33>, CurveEle<33>)], k: SecretKey) -> Result<String> {
let sar: Scalar = k.into();
let cxt = ECMultContext::new_boxed();
affine_to_str(
m.iter()
.map(|(c1, c2)| {
let c1: Affine = c1.clone().try_into().expect("bad curve point");
let c2: Affine = c2.clone().try_into().expect("bad curve point");
let c1: Affine = (*c1).try_into().expect("bad curve point");
let c2: Affine = (*c2).try_into().expect("bad curve point");
let mut t = Jacobian::default();
cxt.ecmult_const(&mut t, &c1, &sar);
let a_t = Affine::from_gej(&t).neg();
Expand Down
47 changes: 22 additions & 25 deletions crates/core/src/ecc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use types::PublicKey;
/// length r: 32, length s: 32, length v(recovery_id): 1
pub type SigBytes = [u8; 65];
/// Alias PublicKey.
pub type CurveEle = PublicKey;
pub type CurveEle<const SIZE: usize> = PublicKey<SIZE>;
/// PublicKeyAddress is H160.
pub type PublicKeyAddress = H160;

Expand Down Expand Up @@ -80,26 +80,23 @@ impl From<SecretKey> for libsecp256k1::SecretKey {
}
}

impl TryFrom<PublicKey> for libsecp256k1::PublicKey {
impl TryFrom<PublicKey<33>> for libsecp256k1::PublicKey {
type Error = Error;
fn try_from(key: PublicKey) -> Result<Self> {
let data: [u8; 33] = key
.0
.try_into()
.map_err(|_| Error::ECDSAPublicKeyBadFormat)?;
fn try_from(key: PublicKey<33>) -> Result<Self> {
let data: [u8; 33] = key.0;
Self::parse_compressed(&data).map_err(|_| Error::ECDSAPublicKeyBadFormat)
}
}

impl TryFrom<PublicKey> for ed25519_dalek::PublicKey {
impl TryFrom<PublicKey<33>> for ed25519_dalek::PublicKey {
type Error = Error;
fn try_from(key: PublicKey) -> Result<Self> {
fn try_from(key: PublicKey<33>) -> Result<Self> {
// pubkey[0] == 0
Self::from_bytes(&key.0[1..]).map_err(|_| Error::EdDSAPublicKeyBadFormat)
}
}

impl AffineCoordinates for PublicKey {
impl AffineCoordinates for PublicKey<33> {
type FieldRepr = GenericArray<u8, U32>;

fn x(&self) -> Self::FieldRepr {
Expand All @@ -116,7 +113,7 @@ impl AffineCoordinates for PublicKey {
}
}

impl PublicKey {
impl PublicKey<33> {
/// Map a PublicKey into secp256r1 affine point,
/// This function is an constant-time cryptographic implementations
pub fn ct_into_secp256r1_affine(self) -> CtOption<primeorder::AffinePoint<NistP256>> {
Expand Down Expand Up @@ -145,7 +142,7 @@ impl From<SecretKey> for FieldBytes<NistP256> {
}
}

impl From<ed25519_dalek::PublicKey> for PublicKey {
impl From<ed25519_dalek::PublicKey> for PublicKey<33> {
fn from(key: ed25519_dalek::PublicKey) -> Self {
// [u8;32] here
// ref: https://docs.rs/ed25519-dalek/latest/ed25519_dalek/struct.PublicKey.html
Expand All @@ -154,18 +151,18 @@ impl From<ed25519_dalek::PublicKey> for PublicKey {
s.reverse();
s.push(0);
s.reverse();
Self(s.as_slice().into())
Self(s.as_slice().try_into().unwrap())
}
}

impl TryFrom<PublicKey> for libsecp256k1::curve::Affine {
impl TryFrom<PublicKey<33>> for libsecp256k1::curve::Affine {
type Error = Error;
fn try_from(key: PublicKey) -> Result<Self> {
fn try_from(key: PublicKey<33>) -> Result<Self> {
Ok(TryInto::<libsecp256k1::PublicKey>::try_into(key)?.into())
}
}

impl TryFrom<libsecp256k1::curve::Affine> for PublicKey {
impl TryFrom<libsecp256k1::curve::Affine> for PublicKey<33> {
type Error = Error;
fn try_from(a: libsecp256k1::curve::Affine) -> Result<Self> {
let pubkey: libsecp256k1::PublicKey = a.try_into().map_err(|_| Error::InvalidPublicKey)?;
Expand All @@ -185,13 +182,13 @@ impl From<libsecp256k1::SecretKey> for SecretKey {
}
}

impl From<libsecp256k1::PublicKey> for PublicKey {
impl From<libsecp256k1::PublicKey> for PublicKey<33> {
fn from(key: libsecp256k1::PublicKey) -> Self {
Self(key.serialize_compressed().to_vec())
Self(key.serialize_compressed())
}
}

impl From<SecretKey> for PublicKey {
impl From<SecretKey> for PublicKey<33> {
fn from(secret_key: SecretKey) -> Self {
libsecp256k1::PublicKey::from_secret_key(&secret_key.0).into()
}
Expand Down Expand Up @@ -264,8 +261,8 @@ impl Serialize for SecretKey {
}
}

fn public_key_address(pubkey: &PublicKey) -> PublicKeyAddress {
let hash = match TryInto::<libsecp256k1::PublicKey>::try_into(pubkey.clone()) {
fn public_key_address(pubkey: &PublicKey<33>) -> PublicKeyAddress {
let hash = match TryInto::<libsecp256k1::PublicKey>::try_into(*pubkey) {
// if pubkey is ecdsa key
Ok(pk) => {
let data = pk.serialize();
Expand Down Expand Up @@ -312,7 +309,7 @@ impl SecretKey {
sig_bytes
}

pub fn pubkey(&self) -> PublicKey {
pub fn pubkey(&self) -> PublicKey<33> {
libsecp256k1::PublicKey::from_secret_key(&(*self).into()).into()
}

Expand All @@ -321,22 +318,22 @@ impl SecretKey {
}
}

impl PublicKey {
impl PublicKey<33> {
pub fn address(&self) -> PublicKeyAddress {
public_key_address(self)
}
}

/// Recover PublicKey from RawMessage using signature.
pub fn recover<S>(message: &[u8], signature: S) -> Result<PublicKey>
pub fn recover<S>(message: &[u8], signature: S) -> Result<PublicKey<33>>
where S: AsRef<[u8]> {
let sig_bytes: SigBytes = signature.as_ref().try_into()?;
let message_hash: [u8; 32] = keccak256(message);
recover_hash(&message_hash, &sig_bytes)
}

/// Recover PublicKey from HashMessage using signature.
pub fn recover_hash(message_hash: &[u8; 32], sig: &[u8; 65]) -> Result<PublicKey> {
pub fn recover_hash(message_hash: &[u8; 32], sig: &[u8; 65]) -> Result<PublicKey<33>> {
let r_s_signature: [u8; 64] = sig[..64].try_into()?;
let recovery_id: u8 = sig[64];
Ok(libsecp256k1::recover(
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/ecc/signers/bip137.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::error::Result;
/// | odd | less than n | true | 1 | 32 |
/// | even | more than n | true | 2 | 33 |
/// | odd | more than n | true | 3 | 34 |
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey> {
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey<33>> {
let mut sig = sig.as_ref().to_vec();
sig.rotate_left(1);
let sig = sig.as_mut_slice();
Expand Down
22 changes: 11 additions & 11 deletions crates/core/src/ecc/signers/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ impl TryFrom<G2Projective> for Signature {
}
}

impl TryFrom<G1Projective> for PublicKey {
impl TryFrom<G1Projective> for PublicKey<48> {
type Error = Error;
fn try_from(p: G1Projective) -> Result<Self> {
Ok(PublicKey(to_compressed::<G1Projective, 48>(&p)?.to_vec()))
Ok(PublicKey(to_compressed::<G1Projective, 48>(&p)?))
}
}

impl TryFrom<PublicKey> for G1Projective {
impl TryFrom<PublicKey<48>> for G1Projective {
type Error = Error;
fn try_from(pk: PublicKey) -> Result<Self> {
let data: [u8; 48] = pk.0.try_into().map_err(|_| Error::PublicKeyBadFormat)?;
fn try_from(pk: PublicKey<48>) -> Result<Self> {
let data: [u8; 48] = pk.0;
let ret: Self = from_compressed(&data)?;
Ok(ret)
}
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn sign(sk: SecretKey, msg: &[u8]) -> Result<Signature> {

/// Verifies that the signature is the actual aggregated signature of hashes - pubkeys. Calculated by
/// e(g1, signature) == \prod_{i = 0}^n e(pk_i, hash_i).
pub fn verify_hash(hashes: &[[u8; 96]], sig: &Signature, pks: &[PublicKey]) -> Result<bool> {
pub fn verify_hash(hashes: &[[u8; 96]], sig: &Signature, pks: &[PublicKey<48>]) -> Result<bool> {
let sig: G2Projective = sig.clone().try_into()?;
let g1 = G1Projective::generator();
let e1 = Bls12_381::pairing(g1, sig);
Expand All @@ -149,7 +149,7 @@ pub fn verify_hash(hashes: &[[u8; 96]], sig: &Signature, pks: &[PublicKey]) -> R

let pks: Vec<G1Projective> = pks
.iter()
.map(|pk| pk.clone().try_into())
.map(|pk| (*pk).try_into())
.collect::<Result<Vec<G1Projective>>>()?;

let mm_out = Bls12_381::multi_miller_loop(pks, hashes);
Expand All @@ -162,7 +162,7 @@ pub fn verify_hash(hashes: &[[u8; 96]], sig: &Signature, pks: &[PublicKey]) -> R

/// Verifies that the signature is the actual aggregated signature of messages - pubkeys. Calculated by
/// e(g1, signature) == \prod_{i = 0}^n e(pk_i, hash_to_curve(message_i)).
pub fn verify(msgs: &[&[u8]], sig: &Signature, pks: &[PublicKey]) -> Result<bool> {
pub fn verify(msgs: &[&[u8]], sig: &Signature, pks: &[PublicKey<48>]) -> Result<bool> {
let hashes: Vec<[u8; 96]> = msgs
.iter()
.map(|msg| hash_to_curve(msg))
Expand All @@ -183,7 +183,7 @@ pub fn aggregate(signatures: &[Signature]) -> Result<Signature> {

/// Converts a BLS private key to a BLS public key.
/// Get the public key for this private key. Calculated by pk = g1 * sk.
pub fn public_key(key: &SecretKey) -> Result<PublicKey> {
pub fn public_key(key: &SecretKey) -> Result<PublicKey<48>> {
let sk: Fr = (*key).try_into()?;
let g1 = G1Projective::generator();
(g1 * sk).try_into()
Expand All @@ -200,7 +200,7 @@ mod test {
let pk = public_key(&key).unwrap();
let h = hash_to_curve(msg.as_bytes()).unwrap();
let sig = sign_hash(key, &h).unwrap();
assert!(super::verify_hash(vec![h].as_slice(), &sig, vec![pk.clone()].as_slice()).unwrap());
assert!(super::verify_hash(vec![h].as_slice(), &sig, vec![pk].as_slice()).unwrap());
assert!(super::verify(vec![msg.as_bytes()].as_slice(), &sig, vec![pk].as_slice()).unwrap());
}

Expand Down Expand Up @@ -243,7 +243,7 @@ mod test {
assert!(super::verify_hash(
vec![h1, h2].as_slice(),
&sig_agg,
vec![pk1.clone(), pk2.clone()].as_slice()
vec![pk1, pk2].as_slice()
)
.unwrap());
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/ecc/signers/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn verify(
msg: &[u8],
address: &PublicKeyAddress,
sig: impl AsRef<[u8]>,
pubkey: &PublicKey,
pubkey: &PublicKey<33>,
) -> bool {
if pubkey.address() != *address {
return false;
Expand All @@ -19,7 +19,7 @@ pub fn verify(
}
let sig_data: [u8; 64] = sig.as_ref().try_into().unwrap();
if let (Ok(p), Ok(s)) = (
TryInto::<ed25519_dalek::PublicKey>::try_into(pubkey.clone()),
TryInto::<ed25519_dalek::PublicKey>::try_into(*pubkey),
ed25519_dalek::Signature::from_bytes(&sig_data),
) {
match p.verify(msg, &s) {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/ecc/signers/eip191.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn hash(msg: &[u8]) -> [u8; 32] {
}

/// recover pubkey according to signature.
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey> {
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey<33>> {
let sig_byte: [u8; 65] = sig.as_ref().try_into()?;
let hash = hash(msg);
let mut sig712 = sig_byte;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/ecc/signers/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn hash(msg: &[u8]) -> [u8; 32] {
}

/// recover public key from message and signature.
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey> {
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey<33>> {
let sig_byte: [u8; 65] = sig.as_ref().try_into()?;
crate::ecc::recover(msg, sig_byte)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/ecc/signers/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn verify(
msg: &[u8],
address: &PublicKeyAddress,
sig: impl AsRef<[u8]>,
pubkey: &PublicKey,
pubkey: &PublicKey<33>,
) -> bool {
if pubkey.address() != *address {
return false;
Expand All @@ -103,7 +103,7 @@ pub fn verify(
return false;
}
let ct_pk: CtOption<Result<ecdsa::VerifyingKey<p256::NistP256>>> =
pubkey.clone().ct_try_into_secp256r1_pubkey();
(*pubkey).ct_try_into_secp256r1_pubkey();
let msg_hash = hash(msg);
if ct_pk.is_some().into() {
let res: Result<()> = ct_pk.unwrap().and_then(|pk| {
Expand Down Expand Up @@ -176,7 +176,7 @@ mod test {
/// ```
#[test]
fn test_secp256r1_sign_and_verify() {
let pk: PublicKey = PublicKey::from_hex_string(
let pk: PublicKey<33> = PublicKey::<33>::from_hex_string(
"17a6afd392fcbe4ac9270a599a9c5732c4f838ce35ea2234d389d8f0c367f3f5dcab906352e27289002c7f2c96039ddce7c1b5aad8b87ba94984d4c8b4f95702"
).unwrap();
let sk =
Expand Down
Loading

0 comments on commit b4869f7

Please sign in to comment.