Skip to content

Commit

Permalink
Merge pull request #25 from Chia-Mine/v1.0.3
Browse files Browse the repository at this point in the history
V1.0.3
  • Loading branch information
ChiaMineJP authored Aug 12, 2021
2 parents 277a2c0 + 2106f5e commit a08bf80
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.0.3]
This version is compatible with [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [[email protected]](https://github.com/Chia-Network/clvm)

### Fixed
- Fixed an issue where `op_logand`, `op_logior`, `op_logxor` did not work
- Fixed an issue where result of div/mod against negative `bigint` was not compatible with original `clvm`

## [1.0.2]
This version is compatible with [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [[email protected]](https://github.com/Chia-Network/clvm)

Expand Down Expand Up @@ -180,6 +187,7 @@ At this version, I've managed to improve test complete time to `79s` -> `2s` by
Initial (beta) release.

<!--[Unreleased]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.1...v0.0.2-->
[1.0.3]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.2...v1.0.3
[1.0.2]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.1...v1.0.2
[1.0.1]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.19...v1.0.0
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ yarn add clvm

## Compatibility
This code is compatible with:
- [`1a5cb17895d8707f784a85180bc97d3c6ebe71a0`](https://github.com/Chia-Network/clvm/tree/1a5cb17895d8707f784a85180bc97d3c6ebe71a0) of [[email protected]](https://github.com/Chia-Network/clvm)
- [Diff to the latest clvm](https://github.com/Chia-Network/clvm/compare/1a5cb17895d8707f784a85180bc97d3c6ebe71a0...main)
- [`ab4560900cf475ff515054bec0ca9a4491aca366`](https://github.com/Chia-Network/clvm/tree/ab4560900cf475ff515054bec0ca9a4491aca366) of [[email protected]](https://github.com/Chia-Network/clvm)
- [Diff to the latest clvm](https://github.com/Chia-Network/clvm/compare/ab4560900cf475ff515054bec0ca9a4491aca366...main)

## Example
```javascript
Expand Down Expand Up @@ -58,7 +58,7 @@ If so, you can make your code fully synchronous.


## Differences with Python's `clvm`
Although I try hard to make it look like Python's `clvm`, there are things users should take it into account.
Although I try hard to make it look like Python's `clvm`, there are things users should be aware of.
I put the code which absorbs language incompatibility into `src/__type_compaibility__.ts`, so if you're interested take a look at it.

### There are no build-in `Tuple` type in Javascript
Expand Down Expand Up @@ -152,6 +152,12 @@ SExp.to([1, [2, 3]]).toString(); // 'ff01ffff02ff038080'
str(SExp.to([1, [2, 3]])); // You can use str() function as well as Python by the way.
```

### Calculation of division/modulo against negative number is different
|Python|JavaScript|
|------|----------|
|`-8 // 5 == -2`|`-8n / 5n === -1n`|
|`-8 % 5 == 2`|`-8n % 5n === -3n`|

## clvm license
`clvm-js` is based on [clvm](https://github.com/Chia-Network/clvm) with the
[Apache license 2.0](https://github.com/Chia-Network/clvm/blob/main/LICENSE)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clvm",
"version": "1.0.2",
"version": "1.0.3",
"author": "Admin ChiaMineJP <[email protected]>",
"description": "Javascript implementation of chia lisp",
"license": "MIT",
Expand Down
26 changes: 25 additions & 1 deletion src/__type_compatibility__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,28 @@ export class Stream {
public getValue(): Bytes {
return new Bytes(this._buffer.subarray(0, this.length));
}
}
}

/**
* Python's style division.
* In javascript, `-8 / 5 === -1` while `-8 / 5 == -2` in Python
*/
export function division(a: bigint, b: bigint): bigint {
const div = a / b;
if(a === div*b){
return div;
}
if(div >= BigInt(0)){
return div;
}
return div - BigInt(1);
}

/**
* Python's style modulo.
* In javascript, `-8 % 5 === -3` while `-8 / 5 == 2` in Python
*/
export function modulo(a: bigint, b: bigint): bigint {
const div = division(a, b);
return a - b*div;
}
30 changes: 14 additions & 16 deletions src/more_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
STRLEN_BASE_COST,
STRLEN_COST_PER_BYTE
} from "./costs";
import {Bytes, list, Stream, t} from "./__type_compatibility__";
import {Bytes, list, Stream, t, division, modulo} from "./__type_compatibility__";
import {EvalError} from "./EvalError";
import {bigint_from_bytes, bigint_to_bytes, limbs_for_int} from "./casts";
import {isAtom} from "./CLVMObject";
Expand Down Expand Up @@ -190,8 +190,8 @@ export function op_divmod(args: SExp){
throw new EvalError("divmod with 0", SExp.to(i0));
}
cost += (l0+l1)*DIVMOD_COST_PER_BYTE;
const q = i0 / i1;
const r = i0 % i1;
const q = division(i0, i1); // i0 / i1
const r = modulo(i0, i1); // i0 % i1
const q1 = SExp.to(q);
const r1 = SExp.to(r);
cost += ((q1.atom as Bytes).length + (r1.atom as Bytes).length) * MALLOC_COST_PER_BYTE;
Expand All @@ -207,7 +207,7 @@ export function op_div(args: SExp){
throw new EvalError("div with 0", SExp.to(i0));
}
cost += (l0+l1)*DIV_COST_PER_BYTE;
const q = i0 / i1;
const q = division(i0, i1); // i0 / i1
return malloc_cost(cost, SExp.to(q));
}

Expand Down Expand Up @@ -240,7 +240,7 @@ export function op_pubkey_for_exp(args: SExp){
const t0 = args_as_int_list("pubkey_for_exp", args, 1)[0] as [bigint, number];
let i0 = t0[0];
const l0 = t0[1];
i0 = i0 % BigInt("0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001");
i0 = modulo(i0, BigInt("0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001")); // i0 % BigInt("0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001")
const {PrivateKey} = getBLSModule();
const bytes = new Uint8Array(32);
const u0 =bigint_to_bytes(i0).raw();
Expand Down Expand Up @@ -390,7 +390,7 @@ export function op_lsh(args: SExp){
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function binop_reduction(op_name: string, initial_value: number, args: SExp, op_f: Function){
export function binop_reduction(op_name: string, initial_value: bigint, args: SExp, op_f: (a: bigint, b: bigint) => bigint){
let total = initial_value;
let arg_size = 0;
let cost = LOG_BASE_COST;
Expand All @@ -405,30 +405,30 @@ export function binop_reduction(op_name: string, initial_value: number, args: SE
}

export function op_logand(args: SExp){
const binop = (a: number, b: number) => {
const binop = (a: bigint, b: bigint) => {
a &= b;
return a;
}

return binop_reduction("logand", -1, args, binop);
return binop_reduction("logand", BigInt(-1), args, binop);
}

export function op_logior(args: SExp){
const binop = (a: number, b: number) => {
const binop = (a: bigint, b: bigint) => {
a |= b;
return a;
}

return binop_reduction("logior", 0, args, binop);
return binop_reduction("logior", BigInt(0), args, binop);
}

export function op_logxor(args: SExp){
const binop = (a: number, b: number) => {
const binop = (a: bigint, b: bigint) => {
a ^= b;
return a;
}

return binop_reduction("logxor", 0, args, binop);
return binop_reduction("logxor", BigInt(0), args, binop);
}

export function op_lognot(args: SExp){
Expand All @@ -455,8 +455,7 @@ export function op_not(args: SExp){
}

export function op_any(args: SExp){
const items: SExp[] = [];
for(const _ of args_as_bools("any", args)) items.push(_);
const items = list(args_as_bools("any", args));
const cost = BOOL_BASE_COST + items.length * BOOL_COST_PER_ARG;
let r = SExp.FALSE;
for(const v of items){
Expand All @@ -472,8 +471,7 @@ export function op_any(args: SExp){
}

export function op_all(args: SExp){
const items: SExp[] = [];
for(const _ of args_as_bools("all", args)) items.push(_);
const items = list(args_as_bools("all", args));
const cost = BOOL_BASE_COST + items.length * BOOL_COST_PER_ARG;
let r = SExp.TRUE;
for(const v of items){
Expand Down

0 comments on commit a08bf80

Please sign in to comment.