Skip to content
Closed
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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ members = [
"ecdsa",
"transcript"
]


# remove once
# https://github.com/privacy-scaling-explorations/halo2/pull/162
# is merged
[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "halo2-ecc-snark-verifier-0220" }

1 change: 0 additions & 1 deletion ecc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ num-bigint = { version = "0.4", features = ["rand"] }
num-integer = "0.1"
num-traits = "0.2"
rand = "0.8"
group = "0.12"
subtle = { version = "2.3", default-features = false }

[dev-dependencies]
Expand Down
33 changes: 22 additions & 11 deletions ecc/src/base_field_ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,6 @@ impl<C: CurveAffine, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>

#[cfg(test)]
mod tests {
use std::marker::PhantomData;
use std::rc::Rc;

use super::BaseFieldEccChip;
use super::{AssignedPoint, EccConfig, Point};
use crate::curves::bn256::G1Affine as Bn256;
Expand All @@ -351,10 +348,12 @@ mod tests {
use crate::integer::rns::Rns;
use crate::integer::NUMBER_OF_LOOKUP_LIMBS;
use crate::maingate;
use group::{Curve as _, Group};
use halo2::arithmetic::{CurveAffine, FieldExt};
use halo2::arithmetic::CurveAffine;
use halo2::circuit::{Layouter, SimpleFloorPlanner, Value};
use halo2::plonk::{Circuit, ConstraintSystem, Error};
use integer::halo2::ff::{Field, FromUniformBytes, PrimeField};
use integer::halo2::group::Curve;
use integer::halo2::group::Group;
use integer::maingate::RegionCtx;
use maingate::mock_prover_verify;
use maingate::{
Expand All @@ -363,6 +362,8 @@ mod tests {
};
use paste::paste;
use rand_core::OsRng;
use std::marker::PhantomData;
use std::rc::Rc;

const NUMBER_OF_LIMBS: usize = 4;
const BIT_LEN_LIMB: usize = 68;
Expand Down Expand Up @@ -419,7 +420,10 @@ mod tests {
}
}

fn config_range<N: FieldExt>(&self, layouter: &mut impl Layouter<N>) -> Result<(), Error> {
fn config_range<N: PrimeField>(
&self,
layouter: &mut impl Layouter<N>,
) -> Result<(), Error> {
let range_chip = RangeChip::<N>::new(self.range_config.clone());
range_chip.load_table(layouter)?;

Expand Down Expand Up @@ -505,7 +509,10 @@ mod tests {

#[test]
fn test_base_field_ecc_addition_circuit() {
fn run<C: CurveAffine>() {
fn run<C: CurveAffine>()
where
C::Scalar: FromUniformBytes<64>,
{
let circuit = TestEccAddition::<C>::default();
let instance = vec![vec![]];
mock_prover_verify(&circuit, instance);
Expand Down Expand Up @@ -580,7 +587,10 @@ mod tests {

#[test]
fn test_base_field_ecc_public_input() {
fn run<C: CurveAffine>() {
fn run<C: CurveAffine>()
where
C::Scalar: FromUniformBytes<64>,
{
let (rns, _) = setup::<C>(20);
let rns = Rc::new(rns);

Expand Down Expand Up @@ -650,7 +660,6 @@ mod tests {
layouter.assign_region(
|| "region 0",
|region| {
use group::ff::Field;
let offset = 0;
let ctx = &mut RegionCtx::new(region, offset);

Expand All @@ -677,7 +686,10 @@ mod tests {

#[test]
fn test_base_field_ecc_mul_circuit() {
fn run<C: CurveAffine>() {
fn run<C: CurveAffine>()
where
C::Scalar: FromUniformBytes<64>,
{
for window_size in 1..5 {
let aux_generator = <C as CurveAffine>::CurveExt::random(OsRng).to_affine();

Expand Down Expand Up @@ -739,7 +751,6 @@ mod tests {
layouter.assign_region(
|| "region 0",
|region| {
use group::ff::Field;
let offset = 0;
let ctx = &mut RegionCtx::new(region, offset);

Expand Down
5 changes: 2 additions & 3 deletions ecc/src/base_field_ecc/mul.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{AssignedPoint, BaseFieldEccChip};
use crate::maingate::{AssignedCondition, AssignedValue, MainGateInstructions};
use crate::{halo2, Selector, Table, Windowed};
use group::ff::PrimeField;
use halo2::arithmetic::CurveAffine;
use halo2::plonk::Error;
use integer::halo2::ff::{Field, PrimeField};
use integer::maingate::RegionCtx;

impl<C: CurveAffine, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
Expand All @@ -16,14 +16,13 @@ impl<C: CurveAffine, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
bits: &mut Vec<AssignedCondition<C::Scalar>>,
window_size: usize,
) -> Result<(), Error> {
use group::ff::Field;
assert_eq!(bits.len(), C::Scalar::NUM_BITS as usize);

// TODO: This is a tmp workaround. Instead of padding with zeros we can use a
// shorter ending window.
let padding_offset = (window_size - (bits.len() % window_size)) % window_size;
let zeros: Vec<AssignedCondition<C::Scalar>> = (0..padding_offset)
.map(|_| self.main_gate().assign_constant(ctx, C::Scalar::zero()))
.map(|_| self.main_gate().assign_constant(ctx, C::Scalar::ZERO))
.collect::<Result<_, Error>>()?;
bits.extend(zeros);
bits.reverse();
Expand Down
36 changes: 18 additions & 18 deletions ecc/src/ecc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::halo2::arithmetic::{CurveAffine, FieldExt};
use crate::halo2::arithmetic::{CurveAffine, PrimeField};
use crate::integer::chip::IntegerConfig;
use crate::integer::rns::{Integer, Rns};
use crate::integer::AssignedInteger;
use crate::maingate::{big_to_fe, Assigned, AssignedCondition, MainGateConfig, RangeConfig};
use crate::FieldExt;
use crate::PrimeField;
use group::Curve;
use num_bigint::BigUint as big_uint;
use num_traits::One;
Expand All @@ -12,13 +12,13 @@ use std::rc::Rc;

/// Represent a Point in affine coordinates
#[derive(Clone, Debug)]
pub struct Point<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
pub struct Point<W: PrimeField, N: PrimeField, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
{
x: Integer<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
y: Integer<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
}

impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
impl<W: PrimeField, N: PrimeField, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
Point<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>
{
/// Returns `Point` form a point in a EC with W as its base field
Expand Down Expand Up @@ -58,16 +58,16 @@ impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB:
#[derive(Clone)]
/// point that is assumed to be on curve and not infinity
pub struct AssignedPoint<
W: FieldExt,
N: FieldExt,
W: PrimeField,
N: PrimeField,
const NUMBER_OF_LIMBS: usize,
const BIT_LEN_LIMB: usize,
> {
pub(crate) x: AssignedInteger<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
pub(crate) y: AssignedInteger<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
}

impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize> fmt::Debug
impl<W: PrimeField, N: PrimeField, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize> fmt::Debug
for AssignedPoint<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -79,7 +79,7 @@ impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB:
}
}

impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
impl<W: PrimeField, N: PrimeField, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
AssignedPoint<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>
{
/// Returns a new `AssignedPoint` given its coordinates as `AssignedInteger`
Expand Down Expand Up @@ -166,9 +166,9 @@ pub(crate) fn make_mul_aux<C: CurveAffine>(
/// Allows to select values of precomputed table in efficient multiplication
/// algorithm
#[derive(Default)]
pub(crate) struct Selector<F: FieldExt>(Vec<AssignedCondition<F>>);
pub(crate) struct Selector<F: PrimeField>(Vec<AssignedCondition<F>>);

impl<F: FieldExt> fmt::Debug for Selector<F> {
impl<F: PrimeField> fmt::Debug for Selector<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug = f.debug_struct("Selector");
for (i, bit) in self.0.iter().enumerate() {
Expand All @@ -181,9 +181,9 @@ impl<F: FieldExt> fmt::Debug for Selector<F> {

/// Vector of `Selectors` which represent the binary representation of a scalar
/// split in window sized selectors.
pub(crate) struct Windowed<F: FieldExt>(Vec<Selector<F>>);
pub(crate) struct Windowed<F: PrimeField>(Vec<Selector<F>>);

impl<F: FieldExt> fmt::Debug for Windowed<F> {
impl<F: PrimeField> fmt::Debug for Windowed<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug = f.debug_struct("Window");
for (i, selector) in self.0.iter().enumerate() {
Expand All @@ -198,13 +198,13 @@ impl<F: FieldExt> fmt::Debug for Windowed<F> {

/// Table of precomputed values for efficient multiplication algorithm.
pub(crate) struct Table<
W: FieldExt,
N: FieldExt,
W: PrimeField,
N: PrimeField,
const NUMBER_OF_LIMBS: usize,
const BIT_LEN_LIMB: usize,
>(pub(crate) Vec<AssignedPoint<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>>);

impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize> fmt::Debug
impl<W: PrimeField, N: PrimeField, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize> fmt::Debug
for Table<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -223,8 +223,8 @@ impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB:
/// Auxiliary points for efficient multiplication algorithm
/// See: https://hackmd.io/ncuKqRXzR-Cw-Au2fGzsMg
pub(super) struct MulAux<
W: FieldExt,
N: FieldExt,
W: PrimeField,
N: PrimeField,
const NUMBER_OF_LIMBS: usize,
const BIT_LEN_LIMB: usize,
> {
Expand All @@ -233,7 +233,7 @@ pub(super) struct MulAux<
}

/// Constructs `MulAux`
impl<W: FieldExt, N: FieldExt, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
impl<W: PrimeField, N: PrimeField, const NUMBER_OF_LIMBS: usize, const BIT_LEN_LIMB: usize>
MulAux<W, N, NUMBER_OF_LIMBS, BIT_LEN_LIMB>
{
pub(super) fn new(
Expand Down
Loading