Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wasm bindings #4

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,93 @@
name: Build & Test

on:
pull_request:
paths-ignore:
- README.md
push:
branches:
- main
paths-ignore:
- README.md
tags:
- v*

env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Dwarnings"

jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy
override: true
profile: minimal
- run: cargo clippy --all --all-features -- -D warnings

rustfmt:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v1

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: rustfmt
profile: minimal
override: true

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

test:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
rust: 1.57 # MSRV
- target: x86_64-unknown-linux-gnu
rust: stable

steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
- run: ${{ matrix.deps }}
- run: cargo check --all-features
- run: cargo test --release --all-features

wasm-test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.57 # MSRV
- stable
target:
- wasm32-unknown-unknown

steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
- run: cargo install wasm-pack
- run: cd rust-k256 && wasm-pack test --node
6 changes: 6 additions & 0 deletions Cargo.toml
@@ -0,0 +1,6 @@
[workspace]

members = [
# "rust-arkworks", # Doesn't work yet
"rust-k256",
]
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -5,6 +5,10 @@
- `rust-k256`: Rust, using the k256 library
- `rust-arkworks`: Rust, using arkworks

## WASM bindings

Currently, WASM bindings are only available for the `rust-k256` implementation.

## TODO

- zk verifier circuits (WIP Circom here: https://github.com/geometryresearch/secp256k1_hash_to_curve/tree/main/circuits)
Expand All @@ -16,14 +20,17 @@
## Resources

### Paper

https://aayushg.com/thesis.pdf

### Slides

https://docs.google.com/presentation/d/1mKtOI4XgKrWBEPpKFAYkRjxZsBomwhy6Cc2Ia87hAnY/edit#slide=id.g13e97fbcd2c_0_76

### Unpublished Blog Post

https://docs.google.com/document/d/1Q9nUNGaeiKoZYAiN9ndh4iE9e-_ql-Rf7MESTo7UB8s/edit

### Spec
https://hackmd.io/uZQbMHrVSbOHvoI_HrJJlw

https://hackmd.io/uZQbMHrVSbOHvoI_HrJJlw
6 changes: 5 additions & 1 deletion rust-arkworks/Cargo.toml
Expand Up @@ -12,7 +12,7 @@ ark-std = "0.3.0"
ark-serialize = "0.3.0"
ark-serialize-derive = "0.3.0"
thiserror = "1.0.30"
secp256k1 = { git = "https://github.com/geometryresearch/ark-secp256k1.git" }
secp256k1 = { git = "https://github.com/geometryresearch/ark-secp256k1" }
rand_core = {version = "0.6", default-features=false, features = ["getrandom"] }
rand = "0.8.4"
tiny-keccak = { version = "2.0.2", features = [ "shake" ] }
Expand All @@ -21,6 +21,10 @@ elliptic-curve = { version = "0.12.2", features = ["arithmetic"]}
k256 = {version = "0.11.3", features = ["arithmetic", "hash2curve", "expose-field", "sha2"] }
generic-array = { version = "0.14", default-features = false }
hex = "0.4.3"
wasm-bindgen = "0.2.83"
js-sys = "0.3.60"
console_error_panic_hook = "0.1.7"
serde = "1.0.147"

[patch.crates-io]
ark-ec = { git = "https://github.com/FindoraNetwork/ark-algebra" }
Expand Down
2 changes: 1 addition & 1 deletion rust-arkworks/src/error.rs
@@ -1,7 +1,7 @@
use thiserror::Error;

/// This is an error that could occur when running a cryptograhic primitive
#[derive(Error, Debug, PartialEq)]
#[derive(Error, Debug, PartialEq, Eq)]
pub enum CryptoError {
#[error("Cannot hash to curve")]
CannotHashToCurve,
Expand Down
35 changes: 19 additions & 16 deletions rust-arkworks/src/hash_to_curve.rs
@@ -1,33 +1,30 @@
use crate::error::CryptoError;
use ark_ec::short_weierstrass_jacobian::GroupAffine;
use ark_ec::{AffineCurve, ProjectiveCurve};
use tiny_keccak::{Hasher, Shake, Xof};
use ark_ff::FromBytes;
use elliptic_curve::hash2curve::{ExpandMsgXmd, GroupDigest};
use k256::{AffinePoint};
use k256::sha2::Sha256;
use elliptic_curve::sec1::ToEncodedPoint;
use ark_ec::short_weierstrass_jacobian::GroupAffine;
use k256::sha2::Sha256;
use k256::AffinePoint;
use k256::{ProjectivePoint, Secp256k1};
use ark_ff::FromBytes;
use secp256k1::Sec1EncodePoint;
use tiny_keccak::{Hasher, Shake, Xof};

pub fn hash_to_curve<
Fp: ark_ff::PrimeField,
P: ark_ec::SWModelParameters,
>(
pub fn hash_to_curve<Fp: ark_ff::PrimeField, P: ark_ec::SWModelParameters>(
msg: &[u8],
pk: &GroupAffine<P>,
) -> GroupAffine<P> {

let pk_encoded = pk.to_encoded_point(true);
let b = hex::decode(pk_encoded).unwrap();
let x = [msg, b.as_slice()];
let x = x.concat().clone();
let x = x.concat();
let x = x.as_slice();

let pt: ProjectivePoint = Secp256k1::hash_from_bytes::<ExpandMsgXmd<Sha256>>(
&[x],
b"QUUX-V01-CS02-with-secp256k1_XMD:SHA-256_SSWU_RO_"
).unwrap();
b"QUUX-V01-CS02-with-secp256k1_XMD:SHA-256_SSWU_RO_",
)
.unwrap();

let pt_affine = pt.to_affine();

Expand All @@ -36,7 +33,7 @@ pub fn hash_to_curve<

pub fn k256_affine_to_arkworks_secp256k1_affine<
Fp: ark_ff::PrimeField,
P: ark_ec::SWModelParameters
P: ark_ec::SWModelParameters,
>(
k_pt: AffinePoint,
) -> GroupAffine<P> {
Expand All @@ -50,7 +47,10 @@ pub fn k256_affine_to_arkworks_secp256k1_affine<
// pad x bytes
let mut k_pt_x_bytes_vec = vec![0u8; num_field_bytes];
for (i, _) in k_pt_x_bytes.clone().iter().enumerate() {
let _ = std::mem::replace(&mut k_pt_x_bytes_vec[i], k_pt_x_bytes[k_pt_x_bytes.len() - 1 - i]);
let _ = std::mem::replace(
&mut k_pt_x_bytes_vec[i],
k_pt_x_bytes[k_pt_x_bytes.len() - 1 - i],
);
}
let reader = std::io::BufReader::new(k_pt_x_bytes_vec.as_slice());
let g_x = P::BaseField::read(reader).unwrap();
Expand All @@ -61,7 +61,10 @@ pub fn k256_affine_to_arkworks_secp256k1_affine<
// pad y bytes
let mut k_pt_y_bytes_vec = vec![0u8; num_field_bytes];
for (i, _) in k_pt_y_bytes.clone().iter().enumerate() {
let _ = std::mem::replace(&mut k_pt_y_bytes_vec[i], k_pt_y_bytes[k_pt_y_bytes.len() - 1 - i]);
let _ = std::mem::replace(
&mut k_pt_y_bytes_vec[i],
k_pt_y_bytes[k_pt_y_bytes.len() - 1 - i],
);
}

let reader = std::io::BufReader::new(k_pt_y_bytes_vec.as_slice());
Expand Down