Skip to content

Commit

Permalink
Rectify language fields (#423)
Browse files Browse the repository at this point in the history
* Assign correct FIELD.

* Use obviously-identifying Scalar type aliases.

* Correct fields and use obvious aliases.

* Document field names.

---------

Co-authored-by: porcuquine <[email protected]>
  • Loading branch information
porcuquine and porcuquine committed Jun 6, 2023
1 parent be75f31 commit f4377b4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 50 deletions.
37 changes: 17 additions & 20 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use lurk::{
public_parameters,
store::Store,
};
use pasta_curves::pallas;
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -46,8 +47,7 @@ fn end2end_benchmark(c: &mut Criterion) {
.sample_size(10);

let limit = 1_000_000_000;
let lang_pallas =
Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let reduction_count = DEFAULT_REDUCTION_COUNT;

Expand All @@ -65,7 +65,7 @@ fn end2end_benchmark(c: &mut Criterion) {

group.bench_with_input(benchmark_id, &size, |b, &s| {
b.iter(|| {
let ptr = go_base::<pasta_curves::pallas::Scalar>(&mut store, s.0, s.1);
let ptr = go_base::<pallas::Scalar>(&mut store, s.0, s.1);
let _result = prover
.evaluate_and_prove(&pp, ptr, env, &mut store, limit, lang_pallas_rc.clone())
.unwrap();
Expand All @@ -82,7 +82,7 @@ fn store_benchmark(c: &mut Criterion) {
.sample_size(60);

let mut bls12_store = Store::<Fr>::default();
let mut pallas_store = Store::<pasta_curves::Fp>::default();
let mut pallas_store = Store::<pallas::Scalar>::default();

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
Expand All @@ -100,7 +100,7 @@ fn store_benchmark(c: &mut Criterion) {
let pasta_id = BenchmarkId::new("store_go_base_pallas", &parameter_string);
group.bench_with_input(pasta_id, &size, |b, &s| {
b.iter(|| {
let result = go_base::<pasta_curves::Fp>(&mut pallas_store, s.0, s.1);
let result = go_base::<pallas::Scalar>(&mut pallas_store, s.0, s.1);
black_box(result)
})
});
Expand All @@ -116,7 +116,7 @@ fn hydration_benchmark(c: &mut Criterion) {
.sample_size(60);

let mut bls12_store = Store::<Fr>::default();
let mut pallas_store = Store::<pasta_curves::Fp>::default();
let mut pallas_store = Store::<pallas::Scalar>::default();

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
Expand All @@ -134,7 +134,7 @@ fn hydration_benchmark(c: &mut Criterion) {
{
let benchmark_id = BenchmarkId::new("hydration_go_base_pallas", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let _ptr = go_base::<pasta_curves::Fp>(&mut pallas_store, s.0, s.1);
let _ptr = go_base::<pallas::Scalar>(&mut pallas_store, s.0, s.1);
b.iter(|| pallas_store.hydrate_scalar_cache())
});
}
Expand All @@ -151,9 +151,9 @@ fn eval_benchmark(c: &mut Criterion) {

let limit = 1_000_000_000;
let lang_bls12 = Lang::<Fr, Coproc<Fr>>::new();
let lang_pallas = Lang::<pasta_curves::Fp, Coproc<pasta_curves::Fp>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let mut bls12_store = Store::<Fr>::default();
let mut pallas_store = Store::<pasta_curves::Fp>::default();
let mut pallas_store = Store::<pallas::Scalar>::default();

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
Expand All @@ -180,7 +180,7 @@ fn eval_benchmark(c: &mut Criterion) {
{
let benchmark_id = BenchmarkId::new("eval_go_base_pallas", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pasta_curves::Fp>(&mut pallas_store, s.0, s.1);
let ptr = go_base::<pallas::Scalar>(&mut pallas_store, s.0, s.1);
b.iter(|| {
Evaluator::new(
ptr,
Expand All @@ -206,15 +206,15 @@ fn eval_benchmark(c: &mut Criterion) {

// let limit = 1_000_000_000;
// let _lang_bls = Lang::<Fr, Coproc<Fr>>::new();
// let _lang_pallas = Lang::<pasta_curves::Fp, Coproc<pasta_curves::Fp>>::new();
// let lang_pallas = Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
// let _lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
// let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();

// let reduction_count = DEFAULT_REDUCTION_COUNT;

// group.bench_function("circuit_generation_go_base_10_16_nova", |b| {
// let mut store = Store::default();
// let env = empty_sym_env(&store);
// let ptr = go_base::<pasta_curves::pallas::Scalar>(&mut store, black_box(10), black_box(16));
// let ptr = go_base::<pallas::Scalar>(&mut store, black_box(10), black_box(16));
// let prover = NovaProver::new(reduction_count, lang_pallas.clone());

// let pp = public_parameters::public_params(reduction_count).unwrap();
Expand All @@ -239,8 +239,7 @@ fn prove_benchmark(c: &mut Criterion) {
.sample_size(10);

let limit = 1_000_000_000;
let lang_pallas =
Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
Expand All @@ -249,7 +248,7 @@ fn prove_benchmark(c: &mut Criterion) {
let benchmark_id = BenchmarkId::new("prove_go_base_nova", format!("_{}_{}", size.0, size.1));

group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pasta_curves::pallas::Scalar>(&mut store, s.0, s.1);
let ptr = go_base::<pallas::Scalar>(&mut store, s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas.clone());
let pp = public_parameters::public_params(reduction_count, true, lang_pallas_rc.clone())
.unwrap();
Expand All @@ -273,8 +272,7 @@ fn verify_benchmark(c: &mut Criterion) {
.sample_size(10);

let limit = 1_000_000_000;
let lang_pallas =
Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
Expand Down Expand Up @@ -317,8 +315,7 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
.sample_size(10);

let limit = 1_000_000_000;
let lang_pallas =
Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
Expand Down
16 changes: 8 additions & 8 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use criterion::{
BenchmarkId, Criterion, SamplingMode,
};

use pasta_curves::pallas;

use lurk::{
eval::{
empty_sym_env,
Expand Down Expand Up @@ -41,8 +43,7 @@ fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
#[allow(dead_code)]
fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
let limit: usize = 10_000_000_000;
let lang_pallas =
Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
let reduction_count = DEFAULT_REDUCTION_COUNT;

Expand All @@ -55,7 +56,7 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
|b, iterations| {
let mut store = Store::default();
let env = empty_sym_env(&store);
let ptr = fib::<pasta_curves::pallas::Scalar>(&mut store, black_box(*iterations));
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
let prover = NovaProver::new(reduction_count, lang_pallas.clone());

b.iter_batched(
Expand All @@ -75,14 +76,14 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
#[allow(dead_code)]
fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
let limit = 10_000_000_000;
let lang_pallas = Lang::<pasta_curves::Fp, Coproc<pasta_curves::Fp>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();

c.bench_with_input(
BenchmarkId::new(name.to_string(), iterations),
&(iterations),
|b, iterations| {
let mut store = Store::default();
let ptr = fib::<pasta_curves::Fp>(&mut store, black_box(*iterations));
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
b.iter(|| {
let result =
Evaluator::new(ptr, empty_sym_env(&store), &mut store, limit, &lang_pallas)
Expand All @@ -95,8 +96,7 @@ fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut B

fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
let limit = 10_000_000_000;
let lang_pallas =
Lang::<pasta_curves::pallas::Scalar, Coproc<pasta_curves::pallas::Scalar>>::new();
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
let reduction_count = DEFAULT_REDUCTION_COUNT;
let pp = public_params(reduction_count, true, lang_rc.clone()).unwrap();
Expand All @@ -107,7 +107,7 @@ fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
|b, iterations| {
let mut store = Store::default();
let env = empty_sym_env(&store);
let ptr = fib::<pasta_curves::pallas::Scalar>(&mut store, black_box(*iterations));
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
let prover = NovaProver::new(reduction_count, lang_pallas.clone());

let frames = prover
Expand Down
21 changes: 16 additions & 5 deletions clutch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use clutch::ClutchState;

use lurk::eval::lang::{Coproc, Lang};
use lurk::field::LanguageField;
use lurk::proof::nova;
use lurk::repl::repl_cli;
use pasta_curves::pallas;

fn main() -> Result<()> {
pretty_env_logger::init();
Expand All @@ -24,10 +24,21 @@ fn main() -> Result<()> {

match field {
LanguageField::Pallas => repl_cli::<
nova::S1,
ClutchState<nova::S1, Coproc<nova::S1>>,
Coproc<nova::S1>,
>(Lang::<nova::S1, Coproc<nova::S1>>::new()),
pallas::Scalar,
ClutchState<pallas::Scalar, Coproc<pallas::Scalar>>,
Coproc<pallas::Scalar>,
>(Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new()),
// TODO: Support all LanguageFields.
// LanguageField::BLS12_381 => repl_cli::<
// blstrs::Scalar,
// ClutchState<blstrs::Scalar, Coproc<blstrs::Scalar>>,
// Coproc<blstrs::Scalar>,
// >(Lang::<blstrs::Scalar, Coproc<blstrs::Scalar>>::new()),
// LanguageField::Vesta => repl_cli::<
// vesta::Scalar,
// ClutchState<vesta::Scalar, Coproc<vesta::Scalar>>,
// Coproc<vesta::Scalar>,
// >(Lang::<vesta::Scalar, Coproc<vesta::Scalar>>::new()),
_ => panic!("unsupported field"),
}
}
25 changes: 17 additions & 8 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ use crate::tag::{ContTag, ExprTag, Op1, Op2};

/// The type of finite fields used in the language
/// For Pallas/Vesta see https://electriccoin.co/blog/the-pasta-curves-for-halo-2-and-beyond/
///
/// Please note:
/// - pasta_curves::pallas::Scalar = pasta_curves::Fq
/// - pasta_curves::vesta::Scalar = pasta_curves::Fp
///
/// Because confusion on this point, perhaps combined with cargo-cult copying of incorrect previous usage has led to
/// inconsistencies and inaccuracies in the code base, please prefer the named Scalar forms when correspondence to a
/// named `LanguageField` is important.
pub enum LanguageField {
/// The Pallas field,
Pallas,
Expand Down Expand Up @@ -215,11 +223,11 @@ impl LurkField for blstrs::Scalar {
const FIELD: LanguageField = LanguageField::BLS12_381;
}

impl LurkField for pasta_curves::Fp {
impl LurkField for pasta_curves::pallas::Scalar {
const FIELD: LanguageField = LanguageField::Pallas;
}

impl LurkField for pasta_curves::Fq {
impl LurkField for pasta_curves::vesta::Scalar {
const FIELD: LanguageField = LanguageField::Vesta;
}

Expand Down Expand Up @@ -332,6 +340,7 @@ impl<'de, F: LurkField> Deserialize<'de> for FWrap<F> {
pub mod tests {
use crate::light_data::Encodable;
use blstrs::Scalar as Fr;
use pasta_curves::{pallas, vesta};

use super::*;

Expand All @@ -347,11 +356,11 @@ pub mod tests {
repr_bytes_consistency(f1)
}
#[test]
fn prop_pallas_repr_bytes_consistency(f1 in any::<FWrap<pasta_curves::Fp>>()) {
fn prop_pallas_repr_bytes_consistency(f1 in any::<FWrap<pallas::Scalar>>()) {
repr_bytes_consistency(f1)
}
#[test]
fn prop_vesta_repr_bytes_consistency(f1 in any::<FWrap<pasta_curves::Fq>>()) {
fn prop_vesta_repr_bytes_consistency(f1 in any::<FWrap<vesta::Scalar>>()) {
repr_bytes_consistency(f1)
}
}
Expand Down Expand Up @@ -406,11 +415,11 @@ pub mod tests {
repr_canonicity(f1)
}
#[test]
fn prop_pallas_repr_canonicity(f1 in any::<FWrap<pasta_curves::Fp>>()) {
fn prop_pallas_repr_canonicity(f1 in any::<FWrap<pallas::Scalar>>()) {
repr_canonicity(f1)
}
#[test]
fn prop_vesta_repr_canonicity(f1 in any::<FWrap<pasta_curves::Fq>>()) {
fn prop_vesta_repr_canonicity(f1 in any::<FWrap<vesta::Scalar>>()) {
repr_canonicity(f1)
}
#[test]
Expand All @@ -437,7 +446,7 @@ pub mod tests {
proptest! {
#[test]
fn prop_pallas_tag_roundtrip(x in any::<u64>()){
let f1 = pasta_curves::Fp::from(x);
let f1 = pallas::Scalar::from(x);
let bytes = f1.to_repr().as_ref().to_vec();
let mut bytes_from_u64 = [0u8; 32];
bytes_from_u64[..8].copy_from_slice(&x.to_le_bytes());
Expand All @@ -446,7 +455,7 @@ pub mod tests {

#[test]
fn prop_vesta_tag_roundtrip(x in any::<u64>()){
let f1 = pasta_curves::Fq::from(x);
let f1 = vesta::Scalar::from(x);
let bytes = f1.to_repr().as_ref().to_vec();
let mut bytes_from_u64 = [0u8; 32];
bytes_from_u64[..8].copy_from_slice(&x.to_le_bytes());
Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::Result;

use lurk::eval::lang::{Coproc, Lang};
use lurk::field::LanguageField;
use lurk::proof::nova;
use lurk::repl::{repl_cli, ReplState};
use pasta_curves::{pallas, vesta};

fn main() -> Result<()> {
pretty_env_logger::init();
Expand All @@ -27,14 +27,14 @@ fn main() -> Result<()> {
Coproc<blstrs::Scalar>,
>(Lang::<blstrs::Scalar, Coproc<blstrs::Scalar>>::new()),
LanguageField::Pallas => repl_cli::<
nova::S1,
ReplState<nova::S1, Coproc<nova::S1>>,
Coproc<nova::S1>,
>(Lang::<nova::S1, Coproc<nova::S1>>::new()),
pallas::Scalar,
ReplState<pallas::Scalar, Coproc<pallas::Scalar>>,
Coproc<pallas::Scalar>,
>(Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new()),
LanguageField::Vesta => repl_cli::<
nova::S2,
ReplState<nova::S2, Coproc<nova::S2>>,
Coproc<nova::S2>,
>(Lang::<nova::S2, Coproc<nova::S2>>::new()),
vesta::Scalar,
ReplState<vesta::Scalar, Coproc<vesta::Scalar>>,
Coproc<vesta::Scalar>,
>(Lang::<vesta::Scalar, Coproc<vesta::Scalar>>::new()),
}
}

0 comments on commit f4377b4

Please sign in to comment.