-
Notifications
You must be signed in to change notification settings - Fork 65
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
separate affine_t and add some utility to ec/ #29
Open
sandsentinel
wants to merge
2
commits into
supranational:main
Choose a base branch
from
sandsentinel:separate-affine-t
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// Copyright Supranational LLC | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef __SPPARK_EC_AFFINE_T_HPP__ | ||
#define __SPPARK_EC_AFFINE_T_HPP__ | ||
|
||
template<class field_t, class field_h = typename field_t::mem_t> class Affine_t; | ||
template<class field_t, class field_h = typename field_t::mem_t> class Affine_inf_t; | ||
template<class field_t> class jacobian_t; | ||
template<class field_t, class field_h = typename field_t::mem_t> class xyzz_t; | ||
|
||
#ifndef __CUDACC__ | ||
# undef __host__ | ||
# define __host__ | ||
# undef __device__ | ||
# define __device__ | ||
# undef __noinline__ | ||
# define __noinline__ | ||
#endif | ||
|
||
template<class field_t, class field_h> | ||
class Affine_t { | ||
friend class Affine_inf_t<field_t>; | ||
friend class jacobian_t<field_t>; | ||
friend class xyzz_t<field_t>; | ||
|
||
field_t X, Y; | ||
|
||
public: | ||
Affine_t(const field_t& x, const field_t& y) : X(x), Y(y) {} | ||
inline __host__ __device__ Affine_t() {} | ||
|
||
#ifdef __CUDA_ARCH__ | ||
inline __device__ bool is_inf() const | ||
{ return (bool)(X.is_zero(Y)); } | ||
#else | ||
inline __host__ bool is_inf() const | ||
{ return (bool)(X.is_zero() & Y.is_zero()); } | ||
#endif | ||
|
||
inline __host__ Affine_t& operator=(const jacobian_t<field_t>& a) | ||
{ | ||
Y = 1/a.Z; | ||
X = Y^2; // 1/Z^2 | ||
Y *= X; // 1/Z^3 | ||
X *= a.X; // X/Z^2 | ||
Y *= a.Y; // Y/Z^3 | ||
return *this; | ||
} | ||
inline __host__ Affine_t(const jacobian_t<field_t>& a) { *this = a; } | ||
|
||
inline __host__ Affine_t& operator=(const xyzz_t<field_t>& a) | ||
{ | ||
Y = 1/a.ZZZ; | ||
X = Y * a.ZZ; // 1/Z | ||
X = X^2; // 1/Z^2 | ||
X *= a.X; // X/Z^2 | ||
Y *= a.Y; // Y/Z^3 | ||
return *this; | ||
} | ||
inline __host__ Affine_t(const xyzz_t<field_t>& a) { *this = a; } | ||
|
||
inline __host__ __device__ operator jacobian_t<field_t>() const | ||
{ | ||
jacobian_t<field_t> p; | ||
p.X = X; | ||
p.Y = Y; | ||
p.Z = field_t::one(is_inf()); | ||
return p; | ||
} | ||
|
||
inline __host__ __device__ operator xyzz_t<field_t>() const | ||
{ | ||
xyzz_t<field_t> p; | ||
p.X = X; | ||
p.Y = Y; | ||
p.ZZZ = p.ZZ = field_t::one(is_inf()); | ||
return p; | ||
} | ||
|
||
#ifdef __NVCC__ | ||
class mem_t { | ||
field_h X, Y; | ||
|
||
public: | ||
inline __device__ operator Affine_t() const | ||
{ | ||
Affine_t p; | ||
p.X = X; | ||
p.Y = Y; | ||
return p; | ||
} | ||
}; | ||
#else | ||
using mem_t = Affine_t; | ||
#endif | ||
|
||
#ifndef NDEBUG | ||
friend inline __host__ __device__ bool operator==(const Affine_t& a, const Affine_t& b) | ||
{ return (a.X == b.X) && (a.Y == b.Y); } | ||
|
||
friend inline __host__ __device__ bool operator!=(const Affine_t& a, const Affine_t& b) | ||
{ return (a.X != b.X) || (a.Y != b.Y); } | ||
|
||
# if defined(_GLIBCXX_IOSTREAM) || defined(_IOSTREAM_) // non-standard | ||
friend __host__ std::ostream& operator<<(std::ostream& os, const Affine_t& p) | ||
{ | ||
return os << "X: " << p.X << std::endl | ||
<< "Y: " << p.Y; | ||
} | ||
# endif | ||
#endif | ||
}; | ||
|
||
template<class field_t, class field_h> | ||
class Affine_inf_t { | ||
field_t X, Y; | ||
bool inf; | ||
|
||
inline __host__ __device__ bool is_inf() const | ||
{ return inf; } | ||
|
||
public: | ||
inline __host__ __device__ operator Affine_t<field_t>() const | ||
{ | ||
bool inf = is_inf(); | ||
Affine_t<field_t> p; | ||
p.X = czero(X, inf); | ||
p.Y = czero(Y, inf); | ||
return p; | ||
} | ||
|
||
inline __host__ __device__ operator jacobian_t<field_t>() const | ||
{ | ||
jacobian_t<field_t> p; | ||
p.X = X; | ||
p.Y = Y; | ||
p.Z = field_t::one(is_inf()); | ||
return p; | ||
} | ||
|
||
inline __host__ __device__ operator xyzz_t<field_t>() const | ||
{ | ||
xyzz_t<field_t> p; | ||
p.X = X; | ||
p.Y = Y; | ||
p.ZZZ = p.ZZ = field_t::one(is_inf()); | ||
return p; | ||
} | ||
|
||
#ifdef __NVCC__ | ||
class mem_t { | ||
field_h X, Y; | ||
#ifdef __CUDACC__ | ||
int inf[sizeof(field_t)%16 ? 2 : 4]; | ||
|
||
inline __host__ __device__ bool is_inf() const | ||
{ return inf[0]&1 != 0; } | ||
#else | ||
bool inf; | ||
|
||
inline __host__ __device__ bool is_inf() const | ||
{ return inf; } | ||
#endif | ||
public: | ||
inline __device__ operator Affine_t<field_t>() const | ||
{ | ||
bool inf = is_inf(); | ||
Affine_t<field_t> p; | ||
p.X = czero((field_t)X, inf); | ||
p.Y = czero((field_t)Y, inf); | ||
return p; | ||
} | ||
|
||
inline __device__ operator Affine_inf_t() const | ||
{ | ||
bool inf = is_inf(); | ||
Affine_inf_t p; | ||
p.X = czero((field_t)X, inf); | ||
p.Y = czero((field_t)Y, inf); | ||
p.inf = inf; | ||
return p; | ||
} | ||
}; | ||
#else | ||
using mem_t = Affine_inf_t; | ||
#endif | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how you compare points in Jacobian coordinates for equality. See POINT_IS_EQUAL_IMPL for a template. Same naturally applies to the inequality test, which should read just
!(a == b)
. Or maybe compiler does it for you without having to defineoperator!=
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, same thing with xyzz_t. I'll fix both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be fixed now for both
jacobian_t
andxyzz_t
, thanks! I also removed__device__
from==
and!=
because I realisedmont_t
didn't have them and thought it would be better to leave it to a later time.