shithub: mc

Download patch

ref: 814c5bcd2efc3bb9139a56a1e0eae177437f4f56
parent: 7ed35649bb5b7ce79c7a01f1b1d8cb850b8d388a
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Mar 31 15:19:21 EDT 2018

Clip top of ints.

--- a/lib/crypto/ctbig.myr
+++ b/lib/crypto/ctbig.myr
@@ -300,41 +300,11 @@
 }
 
 const clip = {v
-//	var mask, edge
-//
-//	edge = v.nbit & (Bits - 1)
-//	mask = (1 << edge) - 1
-//	v.dig[v.dig.len - 1] &= (mask : uint32)
-	-> v
-}
+	var mask, edge
 
-const nlz = {a : uint32
-	var n
-
-	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
 }
 
--- a/lib/crypto/test/ctbig.myr
+++ b/lib/crypto/test/ctbig.myr
@@ -3,27 +3,48 @@
 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 @@
 //	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)