Skip to content

Commit

Permalink
Implement CurveExt::hash_to_curve for {bn256::G1,grumpkin::G1} (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
han0110 authored Jun 21, 2023
1 parent 977c1a5 commit 8cc62b8
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 58 deletions.
25 changes: 4 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ jobs:
strategy:
matrix:
include:
- rust: 1.63.0
feature: default
- rust: nightly
feature: asm

- feature: default
- feature: asm
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
override: true
toolchain: ${{ matrix.rust }}
- name: Build
uses: actions-rs/cargo@v1
with:
Expand All @@ -37,17 +31,11 @@ jobs:
strategy:
matrix:
include:
- rust: 1.63.0
feature: default
- rust: nightly
feature: asm

- feature: default
- feature: asm
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
override: true
toolchain: ${{ matrix.rust }}
- name: Test
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -79,8 +67,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
override: true
toolchain: nightly
components: clippy
- name: Run clippy
uses: actions-rs/cargo@v1
Expand All @@ -96,9 +82,6 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
override: true
toolchain: nightly
- name: Bench arithmetic
uses: actions-rs/cargo@v1
with:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ num-traits = "0.2"
paste = "1.0.11"
serde = { version = "1.0", default-features = false, optional = true }
serde_arrays = { version = "0.1.0", optional = true }
blake2b_simd = "1"

[features]
default = ["reexport", "bits"]
Expand Down
68 changes: 65 additions & 3 deletions src/bn256/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::ff::WithSmallOrderMulGroup;
use crate::ff::{Field, PrimeField};
use crate::group::Curve;
use crate::group::{cofactor::CofactorGroup, prime::PrimeCurveAffine, Group, GroupEncoding};
use crate::hash_to_curve::svdw_hash_to_curve;
use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
Expand Down Expand Up @@ -38,6 +39,7 @@ new_curve_impl!(
G1_A,
G1_B,
"bn256_g1",
|curve_id, domain_prefix| svdw_hash_to_curve(curve_id, domain_prefix, G1::SVDW_Z),
);

new_curve_impl!(
Expand All @@ -51,6 +53,7 @@ new_curve_impl!(
G2_A,
G2_B,
"bn256_g2",
|_, _| unimplemented!(),
);

impl CurveAffineExt for G1Affine {
Expand Down Expand Up @@ -212,17 +215,76 @@ impl CofactorGroup for G2 {
}
}

impl G1 {
const SVDW_Z: Fq = Fq::ONE;
}

#[cfg(test)]
mod tests {

use crate::arithmetic::CurveEndo;
use crate::bn256::{Fr, G1, G2};
use crate::CurveExt;
use ff::Field;
use ff::PrimeField;
use ff::WithSmallOrderMulGroup;
use ff::{PrimeField, WithSmallOrderMulGroup};
use rand_core::OsRng;

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<G1>();
}

#[test]
fn test_map_to_curve() {
crate::tests::curve::svdw_map_to_curve_test::<G1>(
G1::SVDW_Z,
// Precomputed constants taken from https://github.com/ConsenSys/gnark-crypto/blob/441dc0ffe639294b8d09e394f24ba7575577229c/internal/generator/config/bn254.go#L26-L32.
[
"4",
"10944121435919637611123202872628637544348155578648911831344518947322613104291",
"8815841940592487685674414971303048083897117035520822607866",
"7296080957279758407415468581752425029565437052432607887563012631548408736189",
],
// List of (u, (Q.x, Q.y)) taken from https://github.com/ConsenSys/gnark-crypto/blob/441dc0ffe639294b8d09e394f24ba7575577229c/ecc/bn254/hash_vectors_test.go#L4-L28
[
(
"0xcb81538a98a2e3580076eed495256611813f6dae9e16d3d4f8de7af0e9833e1",
(
"0x1bb8810e2ceaf04786d4efd216fc2820ddd9363712efc736ada11049d8af5925",
"0x1efbf8d54c60d865cce08437668ea30f5bf90d287dbd9b5af31da852915e8f11",
),
),
(
"0xba35e127276e9000b33011860904ddee28f1d48ddd3577e2a797ef4a5e62319",
(
"0xda4a96147df1f35b0f820bd35c6fac3b80e8e320de7c536b1e054667b22c332",
"0x189bd3fbffe4c8740d6543754d95c790e44cd2d162858e3b733d2b8387983bb7",
),
),
(
"0x11852286660cd970e9d7f46f99c7cca2b75554245e91b9b19d537aa6147c28fc",
(
"0x2ff727cfaaadb3acab713fa22d91f5fddab3ed77948f3ef6233d7ea9b03f4da1",
"0x304080768fd2f87a852155b727f97db84b191e41970506f0326ed4046d1141aa",
),
),
(
"0x174d1c85d8a690a876cc1deba0166d30569fafdb49cb3ed28405bd1c5357a1cc",
(
"0x11a2eaa8e3e89de056d1b3a288a7f733c8a1282efa41d28e71af065ab245df9b",
"0x60f37c447ac29fd97b9bb83be98ddccf15e34831a9cdf5493b7fede0777ae06",
),
),
(
"0x73b81432b4cf3a8a9076201500d1b94159539f052a6e0928db7f2df74bff672",
(
"0x27409dccc6ee4ce90e24744fda8d72c0bc64e79766f778da0c1c0ef1c186ea84",
"0x1ac201a542feca15e77f30370da183514dc99d8a0b2c136d64ede35cd0b51dc0",
),
),
],
);
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<G1>();
Expand Down
2 changes: 1 addition & 1 deletion src/bn256/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct Gt(pub(crate) Fq12);

impl std::fmt::Display for Gt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ mod test {
let mut rng = ark_std::test_rng();
let base = (0..repeat).map(|_| (rng.next_u32() % (1 << 16)) as u64);

let timer = start_timer!(|| format!("generate {} Bn256 scalar field elements", repeat));
let _res: Vec<_> = base.map(|b| Fr::from(b)).collect();
let timer = start_timer!(|| format!("generate {repeat} Bn256 scalar field elements"));
let _res: Vec<_> = base.map(Fr::from).collect();

end_timer!(timer);
}
Expand Down
5 changes: 3 additions & 2 deletions src/derive/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ macro_rules! new_curve_impl {
$constant_a:expr,
$constant_b:expr,
$curve_id:literal,
$hash_to_curve:expr,
) => {

macro_rules! impl_compressed {
Expand Down Expand Up @@ -615,8 +616,8 @@ macro_rules! new_curve_impl {
}


fn hash_to_curve<'a>(_: &'a str) -> Box<dyn Fn(&[u8]) -> Self + 'a> {
unimplemented!();
fn hash_to_curve<'a>(domain_prefix: &'a str) -> Box<dyn Fn(&[u8]) -> Self + 'a> {
$hash_to_curve($curve_id, domain_prefix)
}

fn is_on_curve(&self) -> Choice {
Expand Down
11 changes: 11 additions & 0 deletions src/grumpkin/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::group::Curve;
use crate::group::{prime::PrimeCurveAffine, Group, GroupEncoding};
use crate::grumpkin::Fq;
use crate::grumpkin::Fr;
use crate::hash_to_curve::svdw_hash_to_curve;
use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
Expand Down Expand Up @@ -31,6 +32,7 @@ new_curve_impl!(
G1_A,
G1_B,
"grumpkin_g1",
|curve_id, domain_prefix| svdw_hash_to_curve(curve_id, domain_prefix, G1::SVDW_Z),
);

impl CurveAffineExt for G1Affine {
Expand Down Expand Up @@ -74,12 +76,21 @@ impl group::cofactor::CofactorGroup for G1 {
}
}

impl G1 {
const SVDW_Z: Fq = Fq::ONE;
}

#[cfg(test)]
mod tests {
use crate::grumpkin::{Fr, G1};
use crate::CurveExt;
use ff::WithSmallOrderMulGroup;

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<G1>();
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<G1>();
Expand Down
Loading

0 comments on commit 8cc62b8

Please sign in to comment.