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

Feat: Goldilocks field #11

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .github/ISSUE_TEMPLATE/eli15.md

This file was deleted.

51 changes: 27 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --release --all --all-features
args: --verbose --release --all

build:
if: ${{ false }}
name: Build target ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -51,36 +52,37 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
override: false
override: true
toolchain: nightly
# Build benchmarks to prevent bitrot
- name: Build benchmarks
uses: actions-rs/cargo@v1
with:
command: build
args: --benches --examples --all-features

codecov:
name: Code coverage
runs-on: ubuntu-latest
# codecov:
# name: Code coverage
# runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
# Use stable for this to ensure that cargo-tarpaulin can be built.
- uses: actions-rs/toolchain@v1
with:
override: false
- name: Install cargo-tarpaulin
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-tarpaulin
- name: Generate coverage report
uses: actions-rs/cargo@v1
with:
command: tarpaulin
args: --all-features --timeout 600 --out Xml
- name: Upload coverage to Codecov
uses: codecov/[email protected]
# steps:
# - uses: actions/checkout@v3
# # Use stable for this to ensure that cargo-tarpaulin can be built.
# - uses: actions-rs/toolchain@v1
# with:
# override: false
# - name: Install cargo-tarpaulin
# uses: actions-rs/cargo@v1
# with:
# command: install
# args: cargo-tarpaulin
# - name: Generate coverage report
# uses: actions-rs/cargo@v1
# with:
# command: tarpaulin
# args: --all-features --timeout 600 --out Xml
# - name: Upload coverage to Codecov
# uses: codecov/[email protected]

doc-links:
name: Intra-doc links
Expand Down Expand Up @@ -112,7 +114,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
override: false
override: true
toolchain: nightly
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/lints-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,3 @@ jobs:
with:
components: clippy
override: false
- name: Run Clippy (beta)
uses: actions-rs/clippy-check@v1
continue-on-error: true
with:
name: Clippy (beta)
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features --all-targets -- -W clippy::all
23 changes: 0 additions & 23 deletions .github/workflows/lints-stable.yml

This file was deleted.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[workspace]
members = [
"halo2",
"halo2_gadgets",
"halo2_proofs",
"arithmetic/pairing",
"arithmetic/curves",
"arithmetic/pasta_curves",
"primitives/poseidon"
]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# halo2 [![Crates.io](https://img.shields.io/crates/v/halo2.svg)](https://crates.io/crates/halo2) #

Monorepo of halo2 community edition; build on top of the following crates:
- ff: ff-0.12.0
- group: group-0.12.0
- pasta: pasta_curves-0.4.1
- curves: halo2curves-0.2.1
- pairing: pairing-0.22.0
- poseidon: poseidon-0.2.0
- halo2_proofs: halo2_proofs-0.2.0

## [Documentation](https://docs.rs/halo2)

## Minimum Supported Rust Version
Expand Down
45 changes: 45 additions & 0 deletions arithmetic/curves/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "curves"
version = "0.2.1"
authors = [
"Sean Bowe <[email protected]>",
"Jack Grigg <[email protected]>",
"Alex Vlasov <[email protected]>",
"Alex Gluchowski <[email protected]>"
]
license = "MIT/Apache-2.0"
edition = "2018"
repository = "https://github.com/kilic/pairing"
readme = "README.md"
description = "Elliptic curve implementations and wrappers for halo2 library"

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
rand_xorshift = "0.3"
ark-std = { version = "0.3", features = ["print-trace"] }

[dependencies]
subtle = "2.4"
ff = { version = "0.12" }
group = { version = "0.12" }
pasta_curves = { version = "0.4.0", path = "../pasta_curves" }
static_assertions = "1.1.0"
rand = "0.8"
rand_core = { version = "0.6", default-features = false }
lazy_static = { version = "1.4.0"}
num-bigint = "0.4.3"
num-traits = "0.2"

[features]
default = []
asm = []
prefetch = []

[profile.bench]
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false
lto = true
incremental = false
codegen-units = 1
25 changes: 25 additions & 0 deletions arithmetic/curves/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# BN256 pairing

BN256 pairing library that implements original traits from `zkcrypto`,

* [`zkcrypto/ff`](https://github.com/zkcrypto/ff)
* [`zkcrypto/group`](https://github.com/zkcrypto/group)
* [`zkcrypto/pairing`](https://github.com/zkcrypto/pairing)

and plus

`FieldExt`, `CurveExt` [traits](https://github.com/zcash/pasta_curves/tree/main/src/arithmetic) that are used in `halo2` library.

This implementation is mostly ported from [matterlabs/pairing](https://github.com/matter-labs/pairing/tree/master/src/bn256) and [zkcrypto/bls12-381](https://github.com/zkcrypto/bls12_381).

## Bench

None Assembly
```
$ cargo test --profile bench test_field -- --nocapture
```

Assembly
```
$ cargo test --profile bench test_field --features asm -- --nocapture
```
7 changes: 7 additions & 0 deletions arithmetic/curves/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
#[cfg(feature = "asm")]
if std::env::consts::ARCH != "x86_64" {
eprintln!("Currently feature `asm` can only be enabled on x86_64 arch.");
std::process::exit(1);
}
}
116 changes: 116 additions & 0 deletions arithmetic/curves/src/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! This module provides common utilities, traits and structures for group and
//! field arithmetic.
//!
//! This module is temporary, and the extension traits defined here are expected
//! to be upstreamed into the `ff` and `group` crates after some refactoring.

use subtle::{Choice, ConditionallySelectable, CtOption};

pub trait CurveAffineExt: pasta_curves::arithmetic::CurveAffine {
fn batch_add<const COMPLETE: bool, const LOAD_POINTS: bool>(
points: &mut [Self],
output_indices: &[u32],
num_points: usize,
offset: usize,
bases: &[Self],
base_positions: &[u32],
);
}

pub(crate) fn sqrt_tonelli_shanks<F: ff::PrimeField, S: AsRef<[u64]>>(
f: &F,
tm1d2: S,
) -> CtOption<F> {
use subtle::ConstantTimeEq;

// w = self^((t - 1) // 2)
let w = f.pow_vartime(tm1d2);

let mut v = F::S;
let mut x = w * f;
let mut b = x * w;

// Initialize z as the 2^S root of unity.
let mut z = F::root_of_unity();

for max_v in (1..=F::S).rev() {
let mut k = 1;
let mut tmp = b.square();
let mut j_less_than_v: Choice = 1.into();

for j in 2..max_v {
let tmp_is_one = tmp.ct_eq(&F::one());
let squared = F::conditional_select(&tmp, &z, tmp_is_one).square();
tmp = F::conditional_select(&squared, &tmp, tmp_is_one);
let new_z = F::conditional_select(&z, &squared, tmp_is_one);
j_less_than_v &= !j.ct_eq(&v);
k = u32::conditional_select(&j, &k, tmp_is_one);
z = F::conditional_select(&z, &new_z, j_less_than_v);
}

let result = x * z;
x = F::conditional_select(&result, &x, b.ct_eq(&F::one()));
z = z.square();
b *= z;
v = k;
}

CtOption::new(
x,
(x * x).ct_eq(f), // Only return Some if it's the square root.
)
}

/// Compute a + b + carry, returning the result and the new carry over.
#[inline(always)]
pub(crate) const fn adc(a: u64, b: u64, carry: u64) -> (u64, u64) {
let ret = (a as u128) + (b as u128) + (carry as u128);
(ret as u64, (ret >> 64) as u64)
}

/// Compute a - (b + borrow), returning the result and the new borrow.
#[inline(always)]
pub(crate) const fn sbb(a: u64, b: u64, borrow: u64) -> (u64, u64) {
let ret = (a as u128).wrapping_sub((b as u128) + ((borrow >> 63) as u128));
(ret as u64, (ret >> 64) as u64)
}

/// Compute a + (b * c) + carry, returning the result and the new carry over.
#[inline(always)]
pub(crate) const fn mac(a: u64, b: u64, c: u64, carry: u64) -> (u64, u64) {
let ret = (a as u128) + ((b as u128) * (c as u128)) + (carry as u128);
(ret as u64, (ret >> 64) as u64)
}

/// Compute a + (b * c), returning the result and the new carry over.
#[inline(always)]
pub(crate) const fn macx(a: u64, b: u64, c: u64) -> (u64, u64) {
let res = (a as u128) + ((b as u128) * (c as u128));
(res as u64, (res >> 64) as u64)
}

/// Compute a * b, returning the result.
#[inline(always)]
pub(crate) fn mul_512(a: [u64; 4], b: [u64; 4]) -> [u64; 8] {
let (r0, carry) = macx(0, a[0], b[0]);
let (r1, carry) = macx(carry, a[0], b[1]);
let (r2, carry) = macx(carry, a[0], b[2]);
let (r3, carry_out) = macx(carry, a[0], b[3]);

let (r1, carry) = macx(r1, a[1], b[0]);
let (r2, carry) = mac(r2, a[1], b[1], carry);
let (r3, carry) = mac(r3, a[1], b[2], carry);
let (r4, carry_out) = mac(carry_out, a[1], b[3], carry);

let (r2, carry) = macx(r2, a[2], b[0]);
let (r3, carry) = mac(r3, a[2], b[1], carry);
let (r4, carry) = mac(r4, a[2], b[2], carry);
let (r5, carry_out) = mac(carry_out, a[2], b[3], carry);

let (r3, carry) = macx(r3, a[3], b[0]);
let (r4, carry) = mac(r4, a[3], b[1], carry);
let (r5, carry) = mac(r5, a[3], b[2], carry);
let (r6, carry_out) = mac(carry_out, a[3], b[3], carry);

[r0, r1, r2, r3, r4, r5, r6, carry_out]
}
Loading