Skip to content

Commit

Permalink
Merge branch 'jack/vetkd-demo' into 'master'
Browse files Browse the repository at this point in the history
feat(crypto): CRP-1971 Add initial implementation of VetKD

 

See merge request dfinity-lab/public/ic!11560
  • Loading branch information
randombit committed Jun 23, 2023
2 parents b80eb8d + 6886489 commit 54907b0
Show file tree
Hide file tree
Showing 9 changed files with 1,400 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ members = [
"rs/crypto/internal/crypto_lib/basic_sig/iccsa/test_utils",
"rs/crypto/internal/crypto_lib/basic_sig/rsa_pkcs1",
"rs/crypto/internal/crypto_lib/bls12_381/type",
"rs/crypto/internal/crypto_lib/bls12_381/vetkd",
"rs/crypto/internal/crypto_lib/hmac",
"rs/crypto/internal/crypto_lib/multi_sig/bls12_381",
"rs/crypto/internal/crypto_lib/seed",
Expand Down
36 changes: 34 additions & 2 deletions rs/crypto/internal/crypto_lib/bls12_381/type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ impl Scalar {
Self::from_u64(v as u64)
}

/// Create a scalar from a small integer value + 1
pub fn from_node_index(v: u32) -> Self {
Self::from_u64(v as u64 + 1)
}

/// Create a scalar from a small integer value
pub fn from_isize(v: isize) -> Self {
if v < 0 {
Expand Down Expand Up @@ -371,6 +376,18 @@ impl Scalar {
Self::new(s)
}

/// Decode a scalar as a big-endian byte string, reducing modulo group order
pub fn from_bytes_wide(input: &[u8; 64]) -> Self {
let mut le_bytes = {
let mut buf = *input;
buf.reverse();
buf
};
let s = ic_bls12_381::Scalar::from_bytes_wide(&le_bytes);
le_bytes.zeroize();
Self::new(s)
}

/// Deserialize a scalar from a big-endian byte string
pub fn deserialize<B: AsRef<[u8]>>(bytes: &B) -> Result<Self, PairingInvalidScalar> {
let mut bytes: [u8; Self::BYTES] = bytes
Expand Down Expand Up @@ -915,6 +932,12 @@ macro_rules! define_affine_and_projective_types {
precomputed: Option<Arc<paste! { [<$affine PrecomputedTable>] }>>,
}

impl AsRef<$affine> for $affine {
fn as_ref(&self) -> &Self{
return &self
}
}

impl Eq for $affine {}

impl PartialEq for $affine {
Expand Down Expand Up @@ -1154,6 +1177,12 @@ macro_rules! define_affine_and_projective_types {
value: ic_bls12_381::$projective
}

impl AsRef<$projective> for $projective {
fn as_ref(&self) -> &Self {
return &self
}
}

impl $projective {
/// The size in bytes of this type
pub const BYTES: usize = $size;
Expand Down Expand Up @@ -1673,12 +1702,15 @@ macro_rules! declare_muln_vartime_affine_impl_for {
/// Warning: this function leaks information about the scalars via
/// memory-based side channels. Do not use this function with secret
/// scalars.
pub fn muln_affine_vartime(points: &[$affine], scalars: &[Scalar]) -> Self {
pub fn muln_affine_vartime<T: AsRef<$affine>>(
points: &[T],
scalars: &[Scalar],
) -> Self {
let count = std::cmp::min(points.len(), scalars.len());
let mut proj_points = Vec::with_capacity(count);

for i in 0..count {
proj_points.push(<$proj>::from(&points[i]));
proj_points.push(<$proj>::from(points[i].as_ref()));
}

Self::muln_vartime(&proj_points[..], scalars)
Expand Down
50 changes: 50 additions & 0 deletions rs/crypto/internal/crypto_lib/bls12_381/vetkd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")
load("//bazel:defs.bzl", "rust_bench")

package(default_visibility = ["//rs/crypto:__subpackages__"])

DEPENDENCIES = [
"//rs/crypto/internal/crypto_lib/bls12_381/type",
"@crate_index//:rand_0_8_4",
"@crate_index//:sha3",
"@crate_index//:zeroize",
]

MACRO_DEPENDENCIES = []

DEV_DEPENDENCIES = [
"//rs/crypto/test_utils/reproducible_rng",
"@crate_index//:hex",
]

MACRO_DEV_DEPENDENCIES = []

ALIASES = {}

rust_library(
name = "vetkd",
srcs = glob(["src/**"]),
aliases = ALIASES,
crate_name = "ic_crypto_internal_bls12_381_vetkd",
proc_macro_deps = MACRO_DEPENDENCIES,
version = "0.1.0",
deps = DEPENDENCIES,
)

rust_test_suite(
name = "integration_tests",
srcs = glob(["tests/**/*.rs"]),
proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
deps = [":vetkd"] + DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_bench(
name = "vetkd_bench",
testonly = True,
srcs = ["benches/vetkd.rs"],
deps = [
":vetkd",
"@crate_index//:criterion",
"@crate_index//:rand_0_8_4",
],
)
19 changes: 19 additions & 0 deletions rs/crypto/internal/crypto_lib/bls12_381/vetkd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "ic-crypto-internal-bls12-381-vetkd"
version = "0.1.0"
edition = "2021"

[dependencies]
ic-crypto-internal-bls12-381-type = { path = "../type" }
zeroize = { version = "1", features = ["zeroize_derive"] }
rand = "0.8"
sha3 = "0.9"

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
ic-crypto-test-utils-reproducible-rng = { path = "../../../../test_utils/reproducible_rng" }
hex = "0.4"

[[bench]]
name = "vetkd"
harness = false

0 comments on commit 54907b0

Please sign in to comment.