Skip to content

Commit

Permalink
Release v0.2.0 (Argyle-Software#14)
Browse files Browse the repository at this point in the history
* Add: wasm compilation feature, npm package and js tests to CI
* Updated deps
* Enforce cargo fmt
* Add: release.md
  • Loading branch information
mberry authored Aug 31, 2023
1 parent d3a2acc commit 56f199b
Show file tree
Hide file tree
Showing 22 changed files with 271 additions and 378 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0 - 2023-8-31

* Added: Wasm compilation feature and npm package

## 0.1.1 - 2023-2-2

* Refactor: Consolidated symmetric modules
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pqc_dilithium"
version = "0.1.1"
version = "0.2.0"
authors = ["Mitchell Berry <foss@mitchellberry.com>"]
description = "A post-quantum cryptographic signature scheme based on the hardness of lattice problems over module lattices"
edition = "2018"
Expand All @@ -21,7 +21,7 @@ version = "0.8.5"
features = ["getrandom"]

[dev-dependencies]
pqc_core = {version = "0.1.0", features = ["load"]}
pqc_core = {version = "0.3.0", features = ["load"]}

[target.'cfg(bench)'.dev-dependencies.criterion]
criterion = "0.4.0"
Expand Down
40 changes: 40 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Release Checklist

`VERSION=v0.0.0`

1. New branch `git checkout -b release_$VERSION`

2. Bump any dependencies in **Cargo.toml**

3. Run tests:
```bash
./tests/test_matrix.sh;
./tests/wasm.js;
```

4. Bump version in **Cargo.toml**

5. Update **changelog.md**

6. `cargo fmt`

7. `wasm-pack build -- --features wasm`

8. Fix autogenerated **pkg/package.json**:
* name - Replace *pqc_dilithium* with *pqc-dilithium*
* description - Revert line back to old description

9. `git commit -a -m "release $VERSION"`

10. `git tag $VERSION -m "Version release"`

11. `git push --atomic origin release_$VERSION $VERSION`

12. Open PR to master, confirm all CI checks pass, merge PR

13. `cargo publish`

14. `npm publish`

15. `git checkout master && git branch -d release_$VERSION`

6 changes: 2 additions & 4 deletions benches/api.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use pqc_dilithium::*;

fn sign_small_msg(c: &mut Criterion)
{
fn sign_small_msg(c: &mut Criterion) {
let keys = Keypair::generate();
let msg = "Hello".as_bytes();
c.bench_function("Sign Small Message", |b| {
b.iter(|| keys.sign(black_box(msg)))
});
}

fn verify_small_msg(c: &mut Criterion)
{
fn verify_small_msg(c: &mut Criterion) {
let keys = Keypair::generate();
let msg = "Hello".as_bytes();
let sig = keys.sign(msg);
Expand Down
4 changes: 3 additions & 1 deletion pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ It is recommended to use Dilithium in a hybrid system alongside a traditional si
---
## Installation

This repository is not available on npm at this moment
```shell
npm i pqc-dilithium
```

## Usage

Expand Down
6 changes: 3 additions & 3 deletions pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "pqc_dilithium",
"name": "pqc-dilithium",
"collaborators": [
"Mitchell Berry <foss@mitchellberry.com>"
],
"description": "A post-quantum cryptographic signature scheme based on the hardness of lattice problems over module lattices",
"version": "0.1.1",
"description": "A post-quantum cryptographic signature scheme written in Rust and compiled to a Wasm binary",
"version": "0.2.0",
"license": "MIT OR Apache-2.0",
"repository": {
"type": "git",
Expand Down
84 changes: 28 additions & 56 deletions src/aes256ctr.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
pub const AES256CTR_BLOCKBYTES: usize = 64;

pub struct Aes256ctrCtx
{
pub struct Aes256ctrCtx {
pub sk_exp: [u64; 120],
pub ivw: [u32; 16],
}

impl Default for Aes256ctrCtx
{
fn default() -> Self
{
impl Default for Aes256ctrCtx {
fn default() -> Self {
Self {
sk_exp: [0u64; 120],
ivw: [0u32; 16],
}
}
}

fn br_dec32le(src: &[u8]) -> u32
{
fn br_dec32le(src: &[u8]) -> u32 {
src[0] as u32
| (src[1] as u32) << 8
| (src[2] as u32) << 16
| (src[3] as u32) << 24
}

fn br_range_dec32le(v: &mut [u32], mut num: usize, src: &[u8])
{
fn br_range_dec32le(v: &mut [u32], mut num: usize, src: &[u8]) {
let mut v_idx: usize = 0;
let mut src_idx: usize = 0;
while num > 0 {
Expand All @@ -37,22 +32,19 @@ fn br_range_dec32le(v: &mut [u32], mut num: usize, src: &[u8])
}
}

fn br_swap32(mut x: u32) -> u32
{
fn br_swap32(mut x: u32) -> u32 {
x = ((x & 0x00FF00FFu32) << 8) | ((x >> 8) & 0x00FF00FFu32);
(x << 16) | (x >> 16)
}

fn br_enc32le(dst: &mut [u8], x: u32)
{
fn br_enc32le(dst: &mut [u8], x: u32) {
dst[0] = x as u8;
dst[1] = (x >> 8) as u8;
dst[2] = (x >> 16) as u8;
dst[3] = (x >> 24) as u8;
}

fn br_range_enc32le(dst: &mut [u8], v: &[u32], mut num: usize)
{
fn br_range_enc32le(dst: &mut [u8], v: &[u32], mut num: usize) {
let mut v_idx = 0;
let mut dst_idx = 0;
while num > 0 {
Expand All @@ -63,8 +55,7 @@ fn br_range_enc32le(dst: &mut [u8], v: &[u32], mut num: usize)
}
}

fn br_aes_ct64_bitslice_sbox(q: &mut [u64])
{
fn br_aes_ct64_bitslice_sbox(q: &mut [u64]) {
// This S-box implementation is a straightforward translation of
// the circuit described by Boyar and Peralta in "A new
// combinational logic minimization technique with applications
Expand Down Expand Up @@ -213,31 +204,26 @@ fn br_aes_ct64_bitslice_sbox(q: &mut [u64])
q[0] = s7;
}

fn swapn(cl: u64, ch: u64, s: usize, x: u64, y: &mut u64) -> u64
{
fn swapn(cl: u64, ch: u64, s: usize, x: u64, y: &mut u64) -> u64 {
let a = x;
let b = *y;
*y = ((a & ch) >> (s)) | (b & ch); // update y
(a & cl) | ((b & cl) << s) // return x
}

fn swap2(x: u64, y: &mut u64) -> u64
{
fn swap2(x: u64, y: &mut u64) -> u64 {
swapn(0x5555555555555555u64, 0xAAAAAAAAAAAAAAAAu64, 1, x, y)
}

fn swap4(x: u64, y: &mut u64) -> u64
{
fn swap4(x: u64, y: &mut u64) -> u64 {
swapn(0x3333333333333333u64, 0xCCCCCCCCCCCCCCCCu64, 2, x, y)
}

fn swap8(x: u64, y: &mut u64) -> u64
{
fn swap8(x: u64, y: &mut u64) -> u64 {
swapn(0x0F0F0F0F0F0F0F0Fu64, 0xF0F0F0F0F0F0F0F0u64, 4, x, y)
}

fn br_aes_ct64_ortho(q: &mut [u64])
{
fn br_aes_ct64_ortho(q: &mut [u64]) {
q[0] = swap2(q[0], &mut q[1]);
q[2] = swap2(q[2], &mut q[3]);
q[4] = swap2(q[4], &mut q[5]);
Expand All @@ -254,8 +240,7 @@ fn br_aes_ct64_ortho(q: &mut [u64])
q[3] = swap8(q[3], &mut q[7]);
}

fn br_aes_ct64_interleave_in(q0: &mut u64, q1: &mut u64, w: &[u32])
{
fn br_aes_ct64_interleave_in(q0: &mut u64, q1: &mut u64, w: &[u32]) {
let mut x0 = w[0] as u64;
let mut x1 = w[1] as u64;
let mut x2 = w[2] as u64;
Expand All @@ -280,8 +265,7 @@ fn br_aes_ct64_interleave_in(q0: &mut u64, q1: &mut u64, w: &[u32])
*q1 = x1 | (x3 << 8);
}

fn br_aes_ct64_interleave_out(w: &mut [u32], q0: u64, q1: u64)
{
fn br_aes_ct64_interleave_out(w: &mut [u32], q0: u64, q1: u64) {
let mut x0 = q0 & 0x00FF00FF00FF00FFu64;
let mut x1 = q1 & 0x00FF00FF00FF00FFu64;
let mut x2 = (q0 >> 8) & 0x00FF00FF00FF00FFu64;
Expand All @@ -300,8 +284,7 @@ fn br_aes_ct64_interleave_out(w: &mut [u32], q0: u64, q1: u64)
w[3] = x3 as u32 | (x3 >> 16) as u32;
}

fn sub_word(x: u32) -> u32
{
fn sub_word(x: u32) -> u32 {
let mut q = [0u64; 8];
q[0] = x as u64;
br_aes_ct64_ortho(&mut q);
Expand All @@ -313,8 +296,7 @@ fn sub_word(x: u32) -> u32
const RCON: [u32; 10] =
[0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36];

fn br_aes_ct64_keysched(comp_skey: &mut [u64], key: &[u8])
{
fn br_aes_ct64_keysched(comp_skey: &mut [u64], key: &[u8]) {
let (mut j, mut k) = (0usize, 0usize);
let mut skey = [0u32; 60];
let key_len = 32usize;
Expand Down Expand Up @@ -363,8 +345,7 @@ fn br_aes_ct64_keysched(comp_skey: &mut [u64], key: &[u8])
}
}

fn br_aes_ct64_skey_expand(skey: &mut [u64], comp_skey: &[u64])
{
fn br_aes_ct64_skey_expand(skey: &mut [u64], comp_skey: &[u64]) {
const N: usize = 15 << 1;
let mut u = 0;
let mut v = 0;
Expand Down Expand Up @@ -393,8 +374,7 @@ fn br_aes_ct64_skey_expand(skey: &mut [u64], comp_skey: &[u64])
}
}

fn add_round_key(q: &mut [u64], sk: &[u64])
{
fn add_round_key(q: &mut [u64], sk: &[u64]) {
q[0] ^= sk[0];
q[1] ^= sk[1];
q[2] ^= sk[2];
Expand All @@ -405,8 +385,7 @@ fn add_round_key(q: &mut [u64], sk: &[u64])
q[7] ^= sk[7];
}

fn shift_rows(q: &mut [u64])
{
fn shift_rows(q: &mut [u64]) {
for x in q.iter_mut() {
*x = (*x & 0x000000000000FFFF)
| ((*x & 0x00000000FFF00000) >> 4)
Expand All @@ -418,13 +397,11 @@ fn shift_rows(q: &mut [u64])
}
}

fn rotr32(x: u64) -> u64
{
fn rotr32(x: u64) -> u64 {
(x << 32) | (x >> 32)
}

fn mix_columns(q: &mut [u64])
{
fn mix_columns(q: &mut [u64]) {
let q0 = q[0];
let q1 = q[1];
let q2 = q[2];
Expand Down Expand Up @@ -452,14 +429,12 @@ fn mix_columns(q: &mut [u64])
q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
}

fn inc4_be(x: u32) -> u32
{
fn inc4_be(x: u32) -> u32 {
let t = br_swap32(x) + 4;
br_swap32(t)
}

fn aes_ctr4x(out: &mut [u8], ivw: &mut [u32], sk_exp: &[u64])
{
fn aes_ctr4x(out: &mut [u8], ivw: &mut [u32], sk_exp: &[u64]) {
let mut w = [0u32; 16];
w.copy_from_slice(&ivw);
let mut q = [0u64; 8];
Expand Down Expand Up @@ -493,15 +468,13 @@ fn aes_ctr4x(out: &mut [u8], ivw: &mut [u32], sk_exp: &[u64])
ivw[15] = inc4_be(ivw[15]);
}

fn br_aes_ct64_ctr_init(sk_exp: &mut [u64], key: &[u8])
{
fn br_aes_ct64_ctr_init(sk_exp: &mut [u64], key: &[u8]) {
let mut skey = [0u64; 30];
br_aes_ct64_keysched(&mut skey, key);
br_aes_ct64_skey_expand(sk_exp, &skey);
}

pub fn aes256ctr_init(s: &mut Aes256ctrCtx, key: &[u8], nonce: [u8; 12])
{
pub fn aes256ctr_init(s: &mut Aes256ctrCtx, key: &[u8], nonce: [u8; 12]) {
br_aes_ct64_ctr_init(&mut s.sk_exp, &key);
br_range_dec32le(&mut s.ivw, 3, &nonce);
let mut slice = [0u32; 3];
Expand All @@ -519,8 +492,7 @@ pub fn aes256ctr_squeezeblocks(
out: &mut [u8],
mut nblocks: u64,
s: &mut Aes256ctrCtx,
)
{
) {
let mut idx = 0;
while nblocks > 0 {
aes_ctr4x(&mut out[idx..], &mut s.ivw, &s.sk_exp);
Expand Down
Loading

0 comments on commit 56f199b

Please sign in to comment.