mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-08-21 07:01:44 +02:00
byuu says: First 32 instructions implemented in the TLCS900H disassembler. Only 992 to go! I removed the use of anonymous namespaces in nall. It was something I rarely used, because it rarely did what I wanted. I updated all nested namespaces to use C++17-style namespace Foo::Bar {} syntax instead of classic C++-style namespace Foo { namespace Bar {}}. I updated ruby::Video::acquire() to return a struct, so we can use C++17 structured bindings. Long term, I want to get away from all functions that take references for output only. Even though C++ botched structured bindings by not allowing you to bind to existing variables, it's even worse to have function calls that take arguments by reference and then write to them. From the caller side, you can't tell the value is being written, nor that the value passed in doesn't matter, which is terrible.
58 lines
1.4 KiB
C++
58 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#if defined(EC_REFERENCE)
|
|
#include <nall/elliptic-curve/modulo25519-reference.hpp>
|
|
#else
|
|
#include <nall/elliptic-curve/modulo25519-optimized.hpp>
|
|
#endif
|
|
|
|
namespace nall::EllipticCurve {
|
|
|
|
struct Curve25519 {
|
|
auto sharedKey(uint256_t secretKey, uint256_t basepoint = 9) const -> uint256_t {
|
|
secretKey &= (1_u256 << 254) - 8;
|
|
secretKey |= (1_u256 << 254);
|
|
basepoint &= ~0_u256 >> 1;
|
|
|
|
point p = scalarMultiply(basepoint % P, secretKey);
|
|
field k = p.x * reciprocal(p.z);
|
|
return k();
|
|
}
|
|
|
|
private:
|
|
using field = Modulo25519;
|
|
struct point { field x, z; };
|
|
const BarrettReduction<256> P = BarrettReduction<256>{EllipticCurve::P};
|
|
|
|
inline auto montgomeryDouble(point p) const -> point {
|
|
field a = square(p.x + p.z);
|
|
field b = square(p.x - p.z);
|
|
field c = a - b;
|
|
field d = a + c * 121665;
|
|
return {a * b, c * d};
|
|
}
|
|
|
|
inline auto montgomeryAdd(point p, point q, field b) const -> point {
|
|
return {
|
|
square(p.x * q.x - p.z * q.z),
|
|
square(p.x * q.z - p.z * q.x) * b
|
|
};
|
|
}
|
|
|
|
inline auto scalarMultiply(field b, uint256_t exponent) const -> point {
|
|
point p{1, 0}, q{b, 1};
|
|
for(uint bit : reverse(range(255))) {
|
|
bool condition = exponent >> bit & 1;
|
|
cswap(condition, p.x, q.x);
|
|
cswap(condition, p.z, q.z);
|
|
q = montgomeryAdd(p, q, b);
|
|
p = montgomeryDouble(p);
|
|
cswap(condition, p.x, q.x);
|
|
cswap(condition, p.z, q.z);
|
|
}
|
|
return p;
|
|
}
|
|
};
|
|
|
|
}
|