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

Switch to homogeneous coordinates + Add complete formulae #19

Merged
merged 6 commits into from
Feb 1, 2023

Conversation

davidnevadoc
Copy link
Contributor

@davidnevadoc davidnevadoc commented Jan 26, 2023

This PR contains two main changes:

  1. Complete formulas for addition and doubling.
  2. Switch to homogeneous coordinates instead of jacobian coordinates.

The reference algorithms can be found here: https://eprint.iacr.org/2015/1060.pdf
The implementations for the complete formulas were directly taken from here: https://github.com/zkcrypto/bls12_381/blob/11617d9dd1b67cc6e102c08d507e5e5e11b9fbdf/src/g1.rs#L670

Close #15

- Pasta curves adapted to use homogenous instead of
jacobian coordinates.
- Efficient complete formulas for add and dbl.
@davidnevadoc
Copy link
Contributor Author

NOTE for reviewers: The diff up to the line 400 is just a indentation correction and can be ignored.

@davidnevadoc davidnevadoc self-assigned this Jan 26, 2023
@davidnevadoc davidnevadoc changed the title Switch to homogeneous coordinates + Add complete formulas Switch to homogeneous coordinates + Add complete formulae Jan 26, 2023
Copy link
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I have double checked that the algorithms are identical with the zkcrypto/bls12_381 ones, except the mul_by_3b part (not sure how much performance it affects, might be cool to have some benchmark to see in the future).

Also tried to learn some stuff about different coordinates representation and couldn't realize why the conversion from/to jacobian is like that, so also tried to leave some thoughts here (they might not make sense at all, so appreciate any further resource about them!).

src/derive/curve.rs Outdated Show resolved Hide resolved
src/derive/curve.rs Outdated Show resolved Hide resolved
src/derive/curve.rs Outdated Show resolved Hide resolved
davidnevadoc and others added 4 commits January 30, 2023 17:44
Co-authored-by: Han <tinghan0110@gmail.com>
Co-authored-by: Han <tinghan0110@gmail.com>
Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked against zkcrypto/bls12_381 the formulas as @han0110 did. (2 pairs of eyes might miss less than 1 🤣 )

Everything matches AFAIS. Just added a note regarding the 3mulb fn that I think could be extrapolated as a const so that we get some extra perf maybe (at least more than as a fn).

Finally, thanks @han0110 for your extensive and detailed review!! This thinks take a lot of time to do and you catched everything AFAIS.

Good work @davidnevadoc !!!!!! 👍

Comment on lines 415 to 418
#[inline]
fn curve_constant_3b() -> $base {
$constant_b + $constant_b + $constant_b
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not having this as a const?? It's much better than compute 3 times in 3 places this value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for this approach since it is the one taken for b(). But I agree 100%, I think it would be better to store the value directly so it need not be computed with each call.
However I haven't find a way to do so without introducing a new argument in the macro. I'll look into it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh yeye, there's no way. Unless you introduce a lazy_static call inside of the macro which creates the constant. This could be one approach.
You can also leave an issue filled for this and we can explore it later!

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking the merge until the ASM tests pass.

Comment on lines +567 to +571
let p = $name {
x:p_x,
y:$base::conditional_select(&p_y, &$base::one(), z.is_zero()),
z
};
Copy link
Member

@CPerezz CPerezz Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something was added in e4bf399 that disrupted the ASM tests. You should double check first! See the latest test reports for e4bf399

Copy link
Contributor Author

@davidnevadoc davidnevadoc Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just re-run the workflow, everything looking good.
The error it gave was Illegal instruction so I guess it is related with something else.

Copy link
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nice work!

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

See my suggestion in #19 (comment) and address it if you consider it

@davidnevadoc davidnevadoc merged commit 1a01928 into main Feb 1, 2023
@CPerezz CPerezz deleted the features/homogenous-coords branch February 2, 2023 18:00
contrun pushed a commit to contrun/halo2curves that referenced this pull request Feb 9, 2023
…aling-explorations#19)

* Use homogenous coordinates

- Pasta curves adapted to use homogenous instead of
jacobian coordinates.
- Efficient complete formulas for add and dbl.

* Improve Homogeneous to Jacobian

Co-authored-by: Han <tinghan0110@gmail.com>

* Improve Jacobian to homogeneous

Co-authored-by: Han <tinghan0110@gmail.com>

* Remove leftover

* Fix homogeneous coordinates for inf

* Add static 3b

---------

Co-authored-by: Han <tinghan0110@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimization: implement RCB15 group addition for BN curves
3 participants