Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into mike/dkg-tests
  • Loading branch information
merolish committed Aug 19, 2024
2 parents 059c0e0 + f1579d2 commit 55a0971
Show file tree
Hide file tree
Showing 19 changed files with 1,605 additions and 588 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
authors = ["Tristan Britt <[email protected]>", "0xAlcibiades <[email protected]>"]
categories = ["cryptography", "mathematics"]
description = "Implementation of the BLS signature scheme using the alt-bn128 curve."
homepage = "https://github.com/warlock-labs/alt-bn128-bls"
homepage = "https://github.com/warlock-labs/sylow"
keywords = ["alt-bn128", "bls", "cryptography", "elliptic-curve", "pairing"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/warlock-labs/alt-bn128-bls.git"
name = "alt-bn128-bls"
repository = "https://github.com/warlock-labs/sylow.git"
name = "sylow"
version = "0.0.1"
edition = "2021"

Expand All @@ -29,8 +29,6 @@ confy = "0.6.1"
rand = "0.9.0-alpha.2"
serde = { version = "1.0.204", features = ["derive"] }

[lib]
proc-macro = true

[dev-dependencies]
serde = { version = "1.0.204", features = ["derive"] }
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# alt-bn128-bls
# sylow

[![License](https://img.shields.io/crates/l/alt-bn128-bls)](https://choosealicense.com/licenses/mit/)
[![Crates.io](https://img.shields.io/crates/v/alt-bn128-bls)](https://crates.io/crates/alt-bn128-bls)
[![Docs](https://img.shields.io/crates/v/alt-bn128-bls?color=blue&label=docs)](https://docs.rs/alt-bn128-bls/)
![CI](https://github.com/warlock-labs/alt-bn128-bls/actions/workflows/CI.yml/badge.svg)
[![License](https://img.shields.io/crates/l/sylow)](https://choosealicense.com/licenses/mit/)
[![Crates.io](https://img.shields.io/crates/v/sylow)](https://crates.io/crates/sylow)
[![Docs](https://img.shields.io/crates/v/sylow?color=blue&label=docs)](https://docs.rs/sylow/)
![CI](https://github.com/warlock-labs/sylow/actions/workflows/CI.yml/badge.svg)

alt-bn128-bls is a Rust library implementing the BLS (Boneh-Lynn-Shacham) signature scheme using the alt-bn128 (BN254) elliptic curve. It provides threshold signing capabilities and associated utilities, initially developed for use in the Warlock Chaos product.
sylow is a Rust library implementing the BLS (Boneh-Lynn-Shacham) signature scheme using the alt-bn128 (BN254) elliptic curve. It provides threshold signing capabilities and associated utilities, initially developed for use in the Warlock Chaos product.

## Features

Expand All @@ -21,24 +21,27 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
alt-bn128-bls = "0.0.1"
sylow = "0.0.1"
```

Here's a basic example of generating a key pair, signing a message, and verifying the signature:

```rust
use alt_bn128_bls::{KeyPair, sign, verify};
use sylow::{KeyPair, sign, verify};

fn main() {
let key_pair = KeyPair::generate();
let message = b"Hello, World!";

let signature = sign(&key_pair.secret_key, message);
assert!(verify(&key_pair.public_key, message, &signature));
if let Ok(signature) = sign(&key_pair.secret_key, message){
if let Ok(verify) = verify(&key_pair.public_key, message, &signature){
assert!(verify, "Signature verification failed");
}
}
}
```

For more examples and usage details, see the [API documentation](https://docs.rs/alt-bn128-bls).
For more examples and usage details, see the [API documentation](https://docs.rs/sylow).

## Core Concepts

Expand All @@ -64,7 +67,7 @@ The following features and improvements are planned for future releases:

## Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests on the [GitHub repository](https://github.com/warlock-labs/alt-bn128-bls).
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests on the [GitHub repository](https://github.com/warlock-labs/sylow).

## License

Expand All @@ -74,4 +77,4 @@ This project is licensed under the [MIT License](https://choosealicense.com/lice

Warlock Labs - [https://github.com/warlock-labs](https://github.com/warlock-labs)

Project Link: [https://github.com/warlock-labs/alt-bn128-bls](https://github.com/warlock-labs/alt-bn128-bls)
Project Link: [https://github.com/warlock-labs/sylow](https://github.com/warlock-labs/sylow)
5 changes: 5 additions & 0 deletions src/dkg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: to examples dir
use crypto_bigint::U256;
use std::collections::HashMap;
use num_traits::{One, Pow, Zero};
Expand Down Expand Up @@ -42,6 +43,7 @@ struct DealerSecret {
commitments: Vec<Fp>,
}

// TODO: random value generation in Fp
impl DealerSecret {
fn new(quorum: u32, round_id: u64) -> Self {
let coefficients = from_vec_u32(generate_distinct_random_values(quorum as usize, MIN_COEFFICIENT, MAX_COEFFICIENT));
Expand Down Expand Up @@ -162,6 +164,7 @@ fn do_round(round_id: u64, quorum: u32) {
let x_shares = from_vec_u32(generate_distinct_random_values(quorum as usize, MIN_COEFFICIENT, MAX_COEFFICIENT));
let recipient_index = 0;
let mut complaint_count = 0;
// TODO: exclude self
for (recipient_id, recipient) in round_data.participants.iter() {
let x_share = x_shares[recipient_index];
let y_share = dealer_secret.eval_polynomial(x_share);
Expand All @@ -178,10 +181,12 @@ fn do_round(round_id: u64, quorum: u32) {
}
event!(Level::INFO, "round_id: {round_id} dealer_id: {dealer_id} recipient_id: {recipient_id} share_valid: {share_valid}");
}
// TODO: complaint broadcast and validation here
if complaint_count >= n_participants / 2 {
event!(Level::ERROR, "round_id: {round_id} dealer_id: {dealer_id} kicked for {complaint_count}/{n_participants} complaints");
}
}
// TODO: print out computed public key
event!(Level::INFO, "End round {round_id}");
}

Expand Down
53 changes: 39 additions & 14 deletions src/fields/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};
// since the underlying Mul, Add, etc., are not, and const traits are in the works
// https://github.com/rust-lang/rust/issues/67792
#[derive(Copy, Clone, Debug)]
pub(crate) struct FieldExtension<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>>(
pub(crate) [F; N],
);
pub struct FieldExtension<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>>(pub [F; N]);

impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> From<u64>
for FieldExtension<D, N, F>
Expand All @@ -30,12 +28,17 @@ impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> From<u64>
Self::new(&retval)
}
}
#[allow(dead_code)]
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> FieldExtension<D, N, F> {
pub(crate) const fn new(c: &[F; N]) -> Self {
/// This is a const constructor that takes a slice of field elements and returns a field extension
/// The usage of the generics means that it is possible to instantiate any representation of
/// an extension need.
pub const fn new(c: &[F; N]) -> Self {
Self(*c)
}
pub(crate) fn scale(&self, factor: F) -> Self {
/// There is eventually a need to be able to perform multiplication across different field
/// extensions, and more or less this corresponds to a basic scaling, see
/// <https://eprint.iacr.org/2010/354.pdf>
pub fn scale(&self, factor: F) -> Self {
let mut i = 0;
let mut retval = [F::zero(); N];
while i < N {
Expand All @@ -59,16 +62,27 @@ impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> ConstantTimeE
retval
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> Add for FieldExtension<D, N, F> {
type Output = Self;
fn add(self, other: Self) -> Self {
impl<'a, 'b, const D: usize, const N: usize, F: FieldExtensionTrait<D, N>>
Add<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>
{
type Output = FieldExtension<D, N, F>;

fn add(self, other: &'b FieldExtension<D, N, F>) -> Self::Output {
let mut i = 0;
let mut retval = [F::zero(); N];
while i < N {
retval[i] = self.0[i] + other.0[i];
i += 1;
}
Self::new(&retval)
Self::Output::new(&retval)
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> Add<FieldExtension<D, N, F>>
for FieldExtension<D, N, F>
{
type Output = Self;
fn add(self, other: FieldExtension<D, N, F>) -> Self::Output {
&self + &other
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> AddAssign
Expand All @@ -78,16 +92,27 @@ impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> AddAssign
*self = *self + other;
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> Sub for FieldExtension<D, N, F> {
type Output = Self;
fn sub(self, other: Self) -> Self {
impl<'a, 'b, const D: usize, const N: usize, F: FieldExtensionTrait<D, N>>
Sub<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>
{
type Output = FieldExtension<D, N, F>;

fn sub(self, other: &'b FieldExtension<D, N, F>) -> Self::Output {
let mut i = 0;
let mut retval = [F::zero(); N];
while i < N {
retval[i] = self.0[i] - other.0[i];
i += 1;
}
Self::new(&retval)
Self::Output::new(&retval)
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> Sub<FieldExtension<D, N, F>>
for FieldExtension<D, N, F>
{
type Output = Self;
fn sub(self, other: FieldExtension<D, N, F>) -> Self::Output {
&self - &other
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> SubAssign
Expand Down
Loading

0 comments on commit 55a0971

Please sign in to comment.