diff --git a/cpp/JSI Utils/MGJSIMacros.h b/cpp/JSI Utils/MGJSIMacros.h index 1a73c8b..8455a7a 100644 --- a/cpp/JSI Utils/MGJSIMacros.h +++ b/cpp/JSI Utils/MGJSIMacros.h @@ -19,6 +19,11 @@ return jsi::Function::createFromHostFunction(runtime, propNameID, 0, func); \ }) +#define JSIF(capture) \ + capture(jsi::Runtime &runtime, const jsi::Value &thisValue, \ + const jsi::Value *arguments, size_t count) \ + ->jsi::Value + #define JSI_VALUE(name, body) JSI_VALUE_CAP(name, [=], body) #define JSI_VALUE_CAP(name, capture, body) std::make_pair(name, capture(jsi::Runtime &runtime) \ diff --git a/cpp/JSI Utils/MGSmartHostObject.cpp b/cpp/JSI Utils/MGSmartHostObject.cpp index 05039cd..2df4a2f 100644 --- a/cpp/JSI Utils/MGSmartHostObject.cpp +++ b/cpp/JSI Utils/MGSmartHostObject.cpp @@ -21,6 +21,15 @@ namespace margelo return propertyNames; } + FieldDefinition buildPair(std::string name, jsi::HostFunctionType &&f) { + auto valueBuilder = [f, name](jsi::Runtime &runtime) { + const auto func = f; + auto propNameID = jsi::PropNameID::forAscii(runtime, name); + return jsi::Function::createFromHostFunction(runtime, propNameID, 0, func); + }; + return std::make_pair(name, valueBuilder); + } + // TODO(Szymon) maybe add memoization here jsi::Value MGSmartHostObject::get(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) @@ -37,4 +46,4 @@ namespace margelo return jsi::Value::undefined(); } -} // namespace margelo \ No newline at end of file +} // namespace margelo diff --git a/cpp/JSI Utils/MGSmartHostObject.h b/cpp/JSI Utils/MGSmartHostObject.h index ddbe150..4fe04f2 100644 --- a/cpp/JSI Utils/MGSmartHostObject.h +++ b/cpp/JSI Utils/MGSmartHostObject.h @@ -19,6 +19,8 @@ namespace margelo typedef std::pair FieldDefinition; + FieldDefinition buildPair(std::string name, jsi::HostFunctionType &&f); + class JSI_EXPORT MGSmartHostObject : public MGThreadAwareHostObject { diff --git a/cpp/MGBigNumberHostObject.cpp b/cpp/MGBigNumberHostObject.cpp index 48d2f64..9e8dd7e 100644 --- a/cpp/MGBigNumberHostObject.cpp +++ b/cpp/MGBigNumberHostObject.cpp @@ -1199,7 +1199,7 @@ namespace margelo return jsi::Value(runtime, BN_cmp(thiz->bign, other->bign) >= 0); })); - this->fields.push_back(HOST_LAMBDA("eq", { + this->fields.push_back(buildPair("eq", JSIF([this]) { std::shared_ptr thiz = thisValue.getObject(runtime).getHostObject(runtime); const jsi::Value &otherValue = arguments[0]; if (!otherValue.isObject()) diff --git a/example/babel.config.js b/example/babel.config.js index 26c852a..a8cb51f 100644 --- a/example/babel.config.js +++ b/example/babel.config.js @@ -12,6 +12,7 @@ module.exports = { [pak.name]: path.join(__dirname, '..', pak.source), bn_elliptic: path.join(__dirname, 'src', 'bn-elliptic', 'lib'), stream: 'stream-browserify', + "buffer":"@craftzdog/react-native-buffer" }, }, ], diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index a5b9ccf..ea29ef7 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -330,7 +330,7 @@ PODS: - React-jsinspector (0.71.6) - React-logger (0.71.6): - glog - - react-native-bignumber (0.1.9): + - react-native-bignumber (0.2.1): - OpenSSL-Universal (~> 1.1.180) - React - React-callinvoker @@ -639,7 +639,7 @@ SPEC CHECKSUMS: React-jsiexecutor: 7894956638ff3e00819dd3f9f6f4a84da38f2409 React-jsinspector: d5ce2ef3eb8fd30c28389d0bc577918c70821bd6 React-logger: 9332c3e7b4ef007a0211c0a9868253aac3e1da82 - react-native-bignumber: 055f50ffc80338573d199a560ee6f82a872c1c98 + react-native-bignumber: 9850896db4534ea31e89dfd88182e7412eace199 react-native-quick-base64: e657e9197e61b60a9dec49807843052b830da254 react-native-safe-area-context: 39c2d8be3328df5d437ac1700f4f3a4f75716acc React-perflogger: 43392072a5b867a504e2b4857606f8fc5a403d7f @@ -663,4 +663,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 3321d88aca920fdda50830e7db0b80420de18274 -COCOAPODS: 1.12.0 +COCOAPODS: 1.12.1 diff --git a/example/src/Testing/elliptic-tests/curves.ts b/example/src/Testing/elliptic-tests/curves.ts index e1c3f52..9dccab6 100644 --- a/example/src/Testing/elliptic-tests/curves.ts +++ b/example/src/Testing/elliptic-tests/curves.ts @@ -1,7 +1,9 @@ -import { assert } from 'chai'; +import { assert, expect } from 'chai'; import { describe, it } from '../MochaRNAdapter'; import {BN} from 'react-native-bignumber'; import elliptic from 'elliptic'; +import bn_elliptic from 'bn_elliptic'; +var SLOW_BN = require('bn.js'); export function registerCurvedTests() { describe('Curve', function () { @@ -20,6 +22,40 @@ export function registerCurvedTests() { assert(p.dbl().add(p.dbl()).eq(p.add(p).add(p).add(p))); }); + it('issue #55 umod', function () { + const secp256k1 = new elliptic.ec('secp256k1'); + const bn_secp256k1 = new bn_elliptic.ec('secp256k1'); + + const kI = + '9dc74cbfd383980fb4ae5d2680acddac9dac956dca65a28c80ac9c847c2374e4'; + const n = secp256k1.curve.n; + const G = secp256k1.curve.g; + const Q = G.mul(kI); + + const n_BN = bn_secp256k1.curve.n; + const G_BN = bn_secp256k1.curve.g; + const Q_BN = G_BN.mul(kI); + + expect(`${Q.x.umod(n)}`).to.be.equal(`${Q_BN.x.umod(n_BN)}`); + + //console.log("FAST", Q.x.umod(n)); + // output: e696d0036454d7b7890bd425947329f68d9c7c0e2de44958e5700a30ca98b02c + //console.log("SLOW", Q_BN.x.umod(n_BN)); + // output: 54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed + }) + + it('issue #55 (simpler) umod', function () { + const SLOW_N = new SLOW_BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16); + const SLOW_Q = new SLOW_BN('54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed', 16); + const FAST_Q = new BN('54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed', 16); + const FAST_N = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16); + console.log('slow_n', SLOW_N); + console.log('slow_q', SLOW_Q); + console.log('res1', FAST_Q.umod(FAST_N)); + console.log('res2', SLOW_Q.umod(SLOW_N)); + expect(`${FAST_Q.umod(FAST_N)}`).to.be.equal(`${SLOW_Q.umod(SLOW_N)}`); + }) + it('should dbl points on edwards curve using proj coordinates', function () { var curve = new elliptic.curve.edwards({ p: new BN( diff --git a/src/BigNumber.ts b/src/BigNumber.ts index a231732..8350879 100644 --- a/src/BigNumber.ts +++ b/src/BigNumber.ts @@ -356,7 +356,13 @@ export class BN { } umod(other: BN) { - return new BN(native.umod.call(this.internalBigNum, other.internalBigNum)); + const res = new BN( + native.umod.call(this.internalBigNum, other.internalBigNum) + ); + if (this.red) { + res.forceRed(this._mctx!); + } + return res; } imodn(other: number) {