Skip to content

Commit

Permalink
add comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfnjust committed Dec 26, 2024
1 parent cccba22 commit 56876fb
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 1 deletion.
117 changes: 116 additions & 1 deletion src/contracts/umath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ export class UMath extends SmartContractLib {

@method()
static eqU30(a: U30, b: U30): boolean {
return a.hi == b.hi && b.lo == b.lo
return a.hi == b.hi && a.lo == b.lo
}

@method()
Expand Down Expand Up @@ -753,6 +753,39 @@ export class UMath extends SmartContractLib {
return res
}

@method()
static lteU30(a: U30, b: U30): boolean {
let res = false
if (a.hi == b.hi) {
res = a.lo <= b.lo
} else {
res = a.hi < b.hi
}
return res
}

@method()
static ltU45(a: U45, b: U45): boolean {
let res = false
if (a.hi == b.hi) {
res = UMath.ltU30(a.lo, b.lo)
} else {
res = a.hi < b.hi
}
return res
}

@method()
static lteU45(a: U45, b: U45): boolean {
let res = false
if (a.hi == b.hi) {
res = UMath.lteU30(a.lo, b.lo)
} else {
res = a.hi < b.hi
}
return res
}

@method()
static ltU60(a: U60, b: U60): boolean {
let res = false
Expand All @@ -764,6 +797,17 @@ export class UMath extends SmartContractLib {
return res
}

@method()
static lteU60(a: U60, b: U60): boolean {
let res = false
if (UMath.eqU30(a.hi, b.hi)) {
res = UMath.lteU30(a.lo, b.lo)
} else {
res = UMath.ltU30(a.hi, b.hi)
}
return res
}

@method()
static ltU61(a: U61, b: U61): boolean {
let res = false
Expand All @@ -775,11 +819,27 @@ export class UMath extends SmartContractLib {
return res
}

@method()
static lteU61(a: U61, b: U61): boolean {
let res = false
if (a.hi == b.hi) {
res = UMath.lteU60(a.lo, b.lo)
} else {
res = a.hi == false && b.hi == true
}
return res
}

@method()
static gtU15(a: U15, b: U15): boolean {
return a > b
}

@method()
static gteU15(a: U15, b: U15): boolean {
return a >= b
}

@method()
static gtU30(a: U30, b: U30): boolean {
let res = false
Expand All @@ -791,6 +851,39 @@ export class UMath extends SmartContractLib {
return res
}

@method()
static gteU30(a: U30, b: U30): boolean {
let res = false
if (a.hi == b.hi) {
res = a.lo >= b.lo
} else {
res = a.hi > b.hi
}
return res
}

@method()
static gtU45(a: U45, b: U45): boolean {
let res = false
if (a.hi == b.hi) {
res = UMath.gtU30(a.lo, b.lo)
} else {
res = a.hi > b.hi
}
return res
}

@method()
static gteU45(a: U45, b: U45): boolean {
let res = false
if (a.hi == b.hi) {
res = UMath.gteU30(a.lo, b.lo)
} else {
res = a.hi > b.hi
}
return res
}

@method()
static gtU60(a: U60, b: U60): boolean {
let res = false
Expand All @@ -802,6 +895,17 @@ export class UMath extends SmartContractLib {
return res
}

@method()
static gteU60(a: U60, b: U60): boolean {
let res = false
if (UMath.eqU30(a.hi, b.hi)) {
res = UMath.gteU30(a.lo, b.lo)
} else {
res = UMath.gtU30(a.hi, b.hi)
}
return res
}

@method()
static gtU61(a: U61, b: U61): boolean {
let res = false
Expand All @@ -813,6 +917,17 @@ export class UMath extends SmartContractLib {
return res
}

@method()
static gteU61(a: U61, b: U61): boolean {
let res = false
if (a.hi == b.hi) {
res = UMath.gteU60(a.lo, b.lo)
} else {
res = a.hi == true && b.hi == false
}
return res
}

@method()
static mulU15(a: U15, b: U15): U30 {
const res = OpMul.u15Mul(a, b)
Expand Down
76 changes: 76 additions & 0 deletions tests/contracts/testComparison.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { method, assert, SmartContract } from 'scrypt-ts'
import { UMath, U30, U60, U45 } from '../opmul'

export class TestComparison extends SmartContract {
@method()
public unlockGtU30(a: U30, b: U30, c: boolean) {
const c_ = UMath.gtU30(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockLtU30(a: U30, b: U30, c: boolean) {
const c_ = UMath.ltU30(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockGteU30(a: U30, b: U30, c: boolean) {
const c_ = UMath.gteU30(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockLteU30(a: U30, b: U30, c: boolean) {
const c_ = UMath.lteU30(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockGtU45(a: U45, b: U45, c: boolean) {
const c_ = UMath.gtU45(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockLtU45(a: U45, b: U45, c: boolean) {
const c_ = UMath.ltU45(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockGteU45(a: U45, b: U45, c: boolean) {
const c_ = UMath.gteU45(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockLteU45(a: U45, b: U45, c: boolean) {
const c_ = UMath.lteU45(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockGtU60(a: U60, b: U60, c: boolean) {
const c_ = UMath.gtU60(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockLtU60(a: U60, b: U60, c: boolean) {
const c_ = UMath.ltU60(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockGteU60(a: U60, b: U60, c: boolean) {
const c_ = UMath.gteU60(a, b)
assert(c_ == c, 'result not correct')
}

@method()
public unlockLteU60(a: U60, b: U60, c: boolean) {
const c_ = UMath.lteU60(a, b)
assert(c_ == c, 'result not correct')
}
}
Loading

0 comments on commit 56876fb

Please sign in to comment.