Skip to content

Commit

Permalink
fix: Fix umod not supporting red numbers (#62)
Browse files Browse the repository at this point in the history
* fix #55 (neet to be cleanedup)

* more reliable approach

* cleanup 1

* cleanuup 2

* Update project.pbxproj
  • Loading branch information
Szymon20000 authored Nov 10, 2023
1 parent 4474750 commit aad5944
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 7 deletions.
5 changes: 5 additions & 0 deletions cpp/JSI Utils/MGJSIMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
11 changes: 10 additions & 1 deletion cpp/JSI Utils/MGSmartHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,4 +46,4 @@ namespace margelo
return jsi::Value::undefined();
}

} // namespace margelo
} // namespace margelo
2 changes: 2 additions & 0 deletions cpp/JSI Utils/MGSmartHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace margelo

typedef std::pair<std::string, JSIValueBuilder> FieldDefinition;

FieldDefinition buildPair(std::string name, jsi::HostFunctionType &&f);

class JSI_EXPORT MGSmartHostObject : public MGThreadAwareHostObject
{

Expand Down
2 changes: 1 addition & 1 deletion cpp/MGBigNumberHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MGBigNumber> thiz = thisValue.getObject(runtime).getHostObject<MGBigNumber>(runtime);
const jsi::Value &otherValue = arguments[0];
if (!otherValue.isObject())
Expand Down
1 change: 1 addition & 0 deletions example/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
},
],
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -663,4 +663,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 3321d88aca920fdda50830e7db0b80420de18274

COCOAPODS: 1.12.0
COCOAPODS: 1.12.1
38 changes: 37 additions & 1 deletion example/src/Testing/elliptic-tests/curves.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion src/BigNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit aad5944

Please sign in to comment.