hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

monty.ha (847B)


      1 
      2 @test fn montyencode() void = {
      3 	let m = fromhex("0010000061");
      4 	let x = fromhexmod("0000010064", m);
      5 
      6 	defer free(x);
      7 	defer free(m);
      8 
      9 	const m0i = ninv31(m[1]);
     10 
     11 	tomonty(x, m);
     12 	frommonty(x, m, m0i);
     13 
     14 	assert(equalshex(x, "010064"));
     15 };
     16 
     17 @test fn montymul() void = {
     18 	let m = fromhex("10000061");
     19 	let x = fromhexmod("00000123", m);
     20 	let y = fromhexmod("000003cf", m);
     21 	let d = fromhexmod("00000000", m);
     22 
     23 	const m0i = ninv31(m[1]);
     24 
     25 	tomonty(x, m);
     26 	tomonty(y, m);
     27 	montymul(d, x, y, m, m0i);
     28 	frommonty(d, m, m0i);
     29 
     30 	assert(equalshex(d, "04544d"));
     31 
     32 	free(d);
     33 	free(x);
     34 	free(y);
     35 
     36 	d = fromhexmod("00000000", m);
     37 	x = fromhexmod("0f98b7cf", m);
     38 	y = fromhexmod("04216b9c", m);
     39 
     40 	tomonty(x, m);
     41 	tomonty(y, m);
     42 	montymul(d, x, y, m, m0i);
     43 	frommonty(d, m, m0i);
     44 
     45 	assert(equalshex(d, "0d031f49"));
     46 
     47 	free(x);
     48 	free(y);
     49 	free(m);
     50 	free(d);
     51 };