summaryrefslogtreecommitdiff
path: root/lib/crypto
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-03-31 19:19:21 -0700
committerOri Bernstein <ori@eigenstate.org>2018-03-31 19:19:21 -0700
commit814c5bcd2efc3bb9139a56a1e0eae177437f4f56 (patch)
tree13921034e981b0106e93b2c9d1c240510f5d2fa6 /lib/crypto
parent7ed35649bb5b7ce79c7a01f1b1d8cb850b8d388a (diff)
downloadmc-814c5bcd2efc3bb9139a56a1e0eae177437f4f56.tar.gz
Clip top of ints.
Diffstat (limited to 'lib/crypto')
-rw-r--r--lib/crypto/ctbig.myr40
-rw-r--r--lib/crypto/test/ctbig.myr37
2 files changed, 34 insertions, 43 deletions
diff --git a/lib/crypto/ctbig.myr b/lib/crypto/ctbig.myr
index b053d7d..64c6702 100644
--- a/lib/crypto/ctbig.myr
+++ b/lib/crypto/ctbig.myr
@@ -300,41 +300,11 @@ const checksz = {a, b
}
const clip = {v
-// var mask, edge
-//
-// edge = v.nbit & (Bits - 1)
-// mask = (1 << edge) - 1
-// v.dig[v.dig.len - 1] &= (mask : uint32)
- -> v
-}
-
-const nlz = {a : uint32
- var n
+ var mask, edge
- if a == 0
- -> 32
- ;;
- n = 0
- if a <= 0x0000ffff
- n += 16
- a <<= 16
- ;;
- if a <= 0x00ffffff
- n += 8
- a <<= 8
- ;;
- if a <= 0x0fffffff
- n += 4
- a <<= 4
- ;;
- if a <= 0x3fffffff
- n += 2
- a <<= 2
- ;;
- if a <= 0x7fffffff
- n += 1
- a <<= 1
- ;;
- -> n
+ edge = v.nbit & (Bits - 1)
+ mask = (1 << (32 - edge)) - 1
+ v.dig[v.dig.len - 1] &= (mask : uint32)
+ -> v
}
diff --git a/lib/crypto/test/ctbig.myr b/lib/crypto/test/ctbig.myr
index 35914e0..ec55381 100644
--- a/lib/crypto/test/ctbig.myr
+++ b/lib/crypto/test/ctbig.myr
@@ -3,27 +3,48 @@ use crypto
use testr
const Nbit = 128
+const Nfunky = 79
const main = {
testr.run([
+ /* normal */
[.name="add", .fn={ctx
- do(ctx, crypto.ctadd,
+ do(ctx, crypto.ctadd, Nbit,
"5192296858610368357189246603769160",
"5192296858534810493479828944327220",
"75557863709417659441940")
}],
[.name="sub", .fn={ctx
- do(ctx, crypto.ctsub,
+ do(ctx, crypto.ctsub, Nbit,
"5192296858459252629770411284885280",
"5192296858534810493479828944327220",
"75557863709417659441940")
}],
[.name="mul", .fn={ctx
- do(ctx, crypto.ctmul,
+ do(ctx, crypto.ctmul, Nbit,
"392318858376010676506814412592879878824393346033951606800",
"5192296858534810493479828944327220",
"75557863709417659441940")
}],
+
+ [.name="addfunky", .fn={ctx
+ do(ctx, crypto.ctadd, Nfunky,
+ "75540728658750274549064",
+ "5192296858534810493479828944327220",
+ "75557863709417659441940")
+ }],
+ [.name="subfunky", .fn={ctx
+ do(ctx, crypto.ctsub, Nfunky,
+ "528887911047229543018272",
+ "5192296858534810493479828944327220",
+ "75557863709417659441940")
+ }],
+ [.name="mulfunky", .fn={ctx
+ do(ctx, crypto.ctmul, Nfunky,
+ "434472066238453871708176",
+ "5192296858534810493479828944327220",
+ "75557863709417659441940")
+ }],
//[.name="div", .fn={ctx
// do(ctx, div,
// "75557863709417659441940",
@@ -60,16 +81,16 @@ const main = {
// crypto.ctdivmod(z, r, a, b)
//}
//
-const do = {ctx, op, estr, astr, bstr
+const do = {ctx, op, nbit, estr, astr, bstr
var r, a, ai, b, bi, e, ei
- r = crypto.ctzero(Nbit)
+ r = crypto.ctzero(nbit)
ei = std.get(std.bigparse(estr))
ai = std.get(std.bigparse(astr))
bi = std.get(std.bigparse(bstr))
- e = crypto.big2ct(ei, Nbit)
- a = crypto.big2ct(ai, Nbit)
- b = crypto.big2ct(bi, Nbit)
+ e = crypto.big2ct(ei, nbit)
+ a = crypto.big2ct(ai, nbit)
+ b = crypto.big2ct(bi, nbit)
std.bigfree(ei)
std.bigfree(ai)