Skip to content

Commit

Permalink
Merge pull request #6 from Daniel-Bloom-dfinity/dbloom/agent-rs
Browse files Browse the repository at this point in the history
feat: compatibility for `ic-agent`
  • Loading branch information
blynn committed Jul 25, 2022
2 parents ee1087b + 8c9b9e9 commit 9b48ab9
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 16 deletions.
126 changes: 126 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ readme = "README.md"
include = ["src", "Cargo.toml", "LICENSE", "README.md", "NOTICE.txt"]

[dependencies]
wasm-bindgen = { version = "0.2.67", optional = true }

[dev-dependencies]
hex-literal = "0.3.4"

[features]
default = ['std']
std = []
default = ['std', 'fallback_separator']
allow_alt_compress = []
fallback_separator = []
std = []

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Oz", "--enable-mutable-globals"]

[profile.release]
lto = true
opt-level = 'z'
2 changes: 1 addition & 1 deletion src/bls12381/big.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl BIG {

/* Convert to Hex String */
//#[cfg(feature = "std")]
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
let mut s = String::new();
let mut len = self.nbits();
Expand Down
5 changes: 4 additions & 1 deletion src/bls12381/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ fn hash_to_field(hash: usize,hlen: usize ,u: &mut [FP], dst: &[u8],m: &[u8],ctr:
/* hash a message to an ECP point, using SHA2, random oracle method */
#[allow(non_snake_case)]
pub fn bls_hash_to_point(m: &[u8]) -> ECP {
let dst= "BLS_SIG_BLS12381G1_XMD:SHA-256_SVDW_RO_NUL_";
#[cfg(not(feature = "fallback_separator"))]
let dst = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_";
#[cfg(feature = "fallback_separator")]
let dst = "BLS_SIG_BLS12381G1_XMD:SHA-256_SVDW_RO_NUL_";

let mut u: [FP; 2] = [
FP::new(),
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/dbig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl DBIG {
}

/* Convert to Hex String */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
let mut s = String::new();
let mut len = self.nbits();
Expand Down
4 changes: 2 additions & 2 deletions src/bls12381/ecp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub const G2_TABLE:usize=69;
pub const HTC_ISO:usize=11;
pub const HTC_ISO_G2:usize=3;

pub const ALLOW_ALT_COMPRESS:bool=false;
pub const ALLOW_ALT_COMPRESS:bool=cfg!(feature = "allow_alt_compress");
pub const HASH_TYPE:usize=32;
pub const AESKEY:usize=16;

Expand Down Expand Up @@ -505,7 +505,7 @@ impl ECP {
}

/* convert to hex string */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
let mut W = ECP::new();
W.copy(self);
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/ecp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl ECP2 {
}

/* convert this to hex string */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
let mut W = ECP2::new();
W.copy(self);
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl FP {
}

/* convert to string */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
self.redc().tostring()
}
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ impl FP12 {
}

/* output to hex string */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
format!(
"[{},{},{}]",
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl FP2 {
}

/* output to hex string */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
format!("[{},{}]", self.a.tostring(), self.b.tostring())
}
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/fp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl FP4 {
}

/* output to hex string */
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
pub fn tostring(&self) -> String {
format!("[{},{}]", self.a.tostring(), self.b.tostring())
}
Expand Down
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
* limitations under the License.
*/

//#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

#![allow(clippy::many_single_char_names)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::manual_memcpy)]
#![allow(clippy::new_without_default)]

pub mod arch;
pub mod aes;
pub mod gcm;
Expand All @@ -36,3 +37,33 @@ pub mod sha3;
pub mod nhs;
pub mod x509;
pub mod bls12381;

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
pub fn bls_init() -> isize {
bls12381::bls::init()
}

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
pub fn bls_verify(sig: &[u8], m: &[u8], s: &[u8]) -> isize {
bls12381::bls::core_verify(sig, m, s)
}

#[cfg(test)]
mod tests {
#[cfg(all(feature = "allow_alt_compress", not(feature = "fallback_separator")))]
#[test]
fn bls_verify() {
use hex_literal::hex;
use crate::bls12381::bls::{core_verify, init, BLS_FAIL, BLS_OK};
let pk = hex!("a7623a93cdb56c4d23d99c14216afaab3dfd6d4f9eb3db23d038280b6d5cb2caaee2a19dd92c9df7001dede23bf036bc0f33982dfb41e8fa9b8e96b5dc3e83d55ca4dd146c7eb2e8b6859cb5a5db815db86810b8d12cee1588b5dbf34a4dc9a5");
let sig = hex!("b89e13a212c830586eaa9ad53946cd968718ebecc27eda849d9232673dcd4f440e8b5df39bf14a88048c15e16cbcaabe");
assert_eq!(init(), BLS_OK);
assert_eq!(core_verify(&sig, b"hello".as_ref(), &pk), BLS_OK);
assert_eq!(core_verify(&sig, b"hallo".as_ref(), &pk), BLS_FAIL);
}
}
3 changes: 0 additions & 3 deletions src/main.rs

This file was deleted.

0 comments on commit 9b48ab9

Please sign in to comment.