From 758f97c769c7792d2ba5f59c8c567701abc14a36 Mon Sep 17 00:00:00 2001 From: armfazh Date: Sat, 4 Jun 2022 18:16:46 -0700 Subject: [PATCH] Format templates using gtfmt tool --- dh/sidh/internal/templates/arith_decl.gotemp | 20 ++-- .../internal/templates/arith_generic.gotemp | 44 ++++---- dh/sidh/internal/templates/arith_test.gotemp | 30 ++--- dh/sidh/internal/templates/core.gotemp | 2 +- dh/sidh/internal/templates/curve.gotemp | 2 +- dh/sidh/internal/templates/curve_test.gotemp | 2 +- dh/sidh/internal/templates/fp2.gotemp | 104 +++++++++--------- dh/sidh/internal/templates/fp2_test.gotemp | 12 +- kem/kyber/templates/pkg.templ.go | 2 +- pke/kyber/templates/params.templ.go | 10 +- pke/kyber/templates/pkg.templ.go | 12 +- sign/dilithium/templates/mode.templ.go | 72 ++++++------ sign/dilithium/templates/modePkg.templ.go | 14 +-- sign/dilithium/templates/params.templ.go | 20 ++-- 14 files changed, 173 insertions(+), 173 deletions(-) diff --git a/dh/sidh/internal/templates/arith_decl.gotemp b/dh/sidh/internal/templates/arith_decl.gotemp index afed62c04..2815fadba 100644 --- a/dh/sidh/internal/templates/arith_decl.gotemp +++ b/dh/sidh/internal/templates/arith_decl.gotemp @@ -4,7 +4,7 @@ //go:build {{if .OPT_ARM}}({{end}}amd64 && !noasm{{if .OPT_ARM}}) || (arm64 && !noasm){{end}} // +build amd64,!noasm{{if .OPT_ARM}} arm64,!noasm{{end}} -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( . "github.com/cloudflare/circl/dh/sidh/internal/common" @@ -14,39 +14,39 @@ import ( // If choice is neither 0 nor 1 then behaviour is undefined. // This function executes in constant time. //go:noescape -func cmov{{ .FIELD}}(x, y *Fp, choice uint8) +func cmov{{.FIELD}}(x, y *Fp, choice uint8) // If choice = 0, leave x,y unchanged. If choice = 1, set x,y = y,x. // If choice is neither 0 nor 1 then behaviour is undefined. // This function executes in constant time. //go:noescape -func cswap{{ .FIELD}}(x, y *Fp, choice uint8) +func cswap{{.FIELD}}(x, y *Fp, choice uint8) // Compute z = x + y (mod p). //go:noescape -func add{{ .FIELD}}(z, x, y *Fp) +func add{{.FIELD}}(z, x, y *Fp) // Compute z = x - y (mod p). //go:noescape -func sub{{ .FIELD}}(z, x, y *Fp) +func sub{{.FIELD}}(z, x, y *Fp) // Compute z = x + y, without reducing mod p. //go:noescape -func adl{{ .FIELD}}(z, x, y *FpX2) +func adl{{.FIELD}}(z, x, y *FpX2) // Compute z = x - y, without reducing mod p. //go:noescape -func sul{{ .FIELD}}(z, x, y *FpX2) +func sul{{.FIELD}}(z, x, y *FpX2) // Reduce a field element in [0, 2*p) to one in [0,p). //go:noescape -func mod{{ .FIELD}}(x *Fp) +func mod{{.FIELD}}(x *Fp) // Computes z = x * y. //go:noescape -func mul{{ .FIELD}}(z *FpX2, x, y *Fp) +func mul{{.FIELD}}(z *FpX2, x, y *Fp) // Computes the Montgomery reduction z = x R^{-1} (mod 2*p). On return value // of x may be changed. z=x not allowed. //go:noescape -func rdc{{ .FIELD}}(z *Fp, x *FpX2) +func rdc{{.FIELD}}(z *Fp, x *FpX2) diff --git a/dh/sidh/internal/templates/arith_generic.gotemp b/dh/sidh/internal/templates/arith_generic.gotemp index d7a872107..a497da89e 100644 --- a/dh/sidh/internal/templates/arith_generic.gotemp +++ b/dh/sidh/internal/templates/arith_generic.gotemp @@ -4,7 +4,7 @@ //go:build {{if .OPT_ARM}}noasm || (!amd64 && !arm64){{else}}noasm || !amd64{{end}} // +build {{if .OPT_ARM}}noasm !amd64,!arm64{{else}}noasm !amd64{{end}} -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( "math/bits" @@ -13,30 +13,30 @@ import ( ) // Compute z = x + y (mod p). -func add{{ .FIELD }}(z, x, y *common.Fp) { +func add{{.FIELD}}(z, x, y *common.Fp) { var carry uint64 - // z=x+y % {{ .FIELD }} + // z=x+y % {{.FIELD}} for i := 0; i < FpWords; i++ { z[i], carry = bits.Add64(x[i], y[i], carry) } - // z = z - {{ .FIELD}}x2 + // z = z - {{.FIELD}}x2 carry = 0 for i := 0; i < FpWords; i++ { - z[i], carry = bits.Sub64(z[i], {{ .FIELD}}x2[i], carry) + z[i], carry = bits.Sub64(z[i], {{.FIELD}}x2[i], carry) } - // if z<0 add {{ .FIELD}}x2 back + // if z<0 add {{.FIELD}}x2 back mask := uint64(0 - carry) carry = 0 for i := 0; i < FpWords; i++ { - z[i], carry = bits.Add64(z[i], {{ .FIELD}}x2[i]&mask, carry) + z[i], carry = bits.Add64(z[i], {{.FIELD}}x2[i]&mask, carry) } } // Compute z = x - y (mod p). -func sub{{ .FIELD }}(z, x, y *common.Fp) { +func sub{{.FIELD}}(z, x, y *common.Fp) { var borrow uint64 for i := 0; i < FpWords; i++ { @@ -47,14 +47,14 @@ func sub{{ .FIELD }}(z, x, y *common.Fp) { borrow = 0 for i := 0; i < FpWords; i++ { - z[i], borrow = bits.Add64(z[i], {{ .FIELD}}x2[i]&mask, borrow) + z[i], borrow = bits.Add64(z[i], {{.FIELD}}x2[i]&mask, borrow) } } // If choice = 0, leave x unchanged. If choice = 1, sets x to y. // If choice is neither 0 nor 1 then behaviour is undefined. // This function executes in constant time. -func cmov{{ .FIELD }}(x, y *common.Fp, choice uint8) { +func cmov{{.FIELD}}(x, y *common.Fp, choice uint8) { mask := 0 - uint64(choice) for i := 0; i < FpWords; i++ { x[i] ^= mask & (x[i] ^ y[i]) @@ -66,7 +66,7 @@ func cmov{{ .FIELD }}(x, y *common.Fp, choice uint8) { // For details see "Hackers Delight, 2.20" // // Implementation doesn't actually depend on a prime field. -func cswap{{ .FIELD }}(x, y *common.Fp, mask uint8) { +func cswap{{.FIELD}}(x, y *common.Fp, mask uint8) { var tmp, mask64 uint64 mask64 = 0 - uint64(mask) @@ -79,17 +79,17 @@ func cswap{{ .FIELD }}(x, y *common.Fp, mask uint8) { // Perform Montgomery reduction: set z = x R^{-1} (mod 2*p) // with R=2^(FpWords*64). Destroys the input value. -func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) { +func rdc{{.FIELD}}(z *common.Fp, x *common.FpX2) { var carry, t, u, v uint64 var hi, lo uint64 var count int - count = {{ .FIELD}}p1Zeros + count = {{.FIELD}}p1Zeros for i := 0; i < FpWords; i++ { for j := 0; j < i; j++ { if j < (i - count + 1) { - hi, lo = bits.Mul64(z[j], {{ .FIELD }}p1[i-j]) + hi, lo = bits.Mul64(z[j], {{.FIELD}}p1[i-j]) v, carry = bits.Add64(lo, v, 0) u, carry = bits.Add64(hi, u, carry) t += carry @@ -111,7 +111,7 @@ func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) { } for j := i - FpWords + 1; j < FpWords; j++ { if j < (FpWords - count) { - hi, lo = bits.Mul64(z[j], {{ .FIELD }}p1[i-j]) + hi, lo = bits.Mul64(z[j], {{.FIELD}}p1[i-j]) v, carry = bits.Add64(lo, v, 0) u, carry = bits.Add64(hi, u, carry) t += carry @@ -131,7 +131,7 @@ func rdc{{ .FIELD }}(z *common.Fp, x *common.FpX2) { } // Compute z = x * y. -func mul{{ .FIELD }}(z *common.FpX2, x, y *common.Fp) { +func mul{{.FIELD}}(z *common.FpX2, x, y *common.Fp) { var u, v, t uint64 var hi, lo uint64 var carry uint64 @@ -165,7 +165,7 @@ func mul{{ .FIELD }}(z *common.FpX2, x, y *common.Fp) { } // Compute z = x + y, without reducing mod p. -func adl{{ .FIELD }}(z, x, y *common.FpX2) { +func adl{{.FIELD}}(z, x, y *common.FpX2) { var carry uint64 for i := 0; i < 2*FpWords; i++ { z[i], carry = bits.Add64(x[i], y[i], carry) @@ -173,22 +173,22 @@ func adl{{ .FIELD }}(z, x, y *common.FpX2) { } // Reduce a field element in [0, 2*p) to one in [0,p). -func mod{{ .FIELD }}(x *common.Fp) { +func mod{{.FIELD}}(x *common.Fp) { var borrow, mask uint64 for i := 0; i < FpWords; i++ { - x[i], borrow = bits.Sub64(x[i], {{ .FIELD }}[i], borrow) + x[i], borrow = bits.Sub64(x[i], {{.FIELD}}[i], borrow) } // Sets all bits if borrow = 1 mask = 0 - borrow borrow = 0 for i := 0; i < FpWords; i++ { - x[i], borrow = bits.Add64(x[i], {{ .FIELD }}[i]&mask, borrow) + x[i], borrow = bits.Add64(x[i], {{.FIELD}}[i]&mask, borrow) } } // Compute z = x - y, without reducing mod p. -func sul{{ .FIELD }}(z, x, y *common.FpX2) { +func sul{{.FIELD}}(z, x, y *common.FpX2) { var borrow, mask uint64 for i := 0; i < 2*FpWords; i++ { z[i], borrow = bits.Sub64(x[i], y[i], borrow) @@ -198,6 +198,6 @@ func sul{{ .FIELD }}(z, x, y *common.FpX2) { mask = 0 - borrow borrow = 0 for i := FpWords; i < 2*FpWords; i++ { - z[i], borrow = bits.Add64(z[i], {{ .FIELD }}[i-FpWords]&mask, borrow) + z[i], borrow = bits.Add64(z[i], {{.FIELD}}[i-FpWords]&mask, borrow) } } diff --git a/dh/sidh/internal/templates/arith_test.gotemp b/dh/sidh/internal/templates/arith_test.gotemp index 777e517a1..dca3cb992 100644 --- a/dh/sidh/internal/templates/arith_test.gotemp +++ b/dh/sidh/internal/templates/arith_test.gotemp @@ -1,7 +1,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots. -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( "testing" @@ -26,14 +26,14 @@ func TestFpCswap(t *testing.T) { x := one y := two - cswap{{ .FIELD}}(&x, &y, 0) + cswap{{.FIELD}}(&x, &y, 0) for i := 0; i < FpWords; i++ { if (x[i] != one[i]) || (y[i] != two[i]) { t.Error("Found", x, "expected", one) } } - cswap{{ .FIELD}}(&x, &y, 1) + cswap{{.FIELD}}(&x, &y, 1) for i := 0; i < FpWords; i++ { if (x[i] != two[i]) || (y[i] != one[i]) { t.Error("Found", x, "expected", two) @@ -48,7 +48,7 @@ func TestFpCmov(t *testing.T) { x := one y := two - cmov{{ .FIELD}}(&x, &y, 0) + cmov{{.FIELD}}(&x, &y, 0) for i := 0; i < FpWords; i++ { if x[i] != one[i] { t.Error("Found", x, "expected", one) @@ -58,7 +58,7 @@ func TestFpCmov(t *testing.T) { } } - cmov{{ .FIELD}}(&x, &y, 1) + cmov{{.FIELD}}(&x, &y, 1) for i := 0; i < FpWords; i++ { if x[i] != two[i] { t.Error("Found", x, "expected", two) @@ -72,7 +72,7 @@ func TestFpCmov(t *testing.T) { // Benchmarking for field arithmetic func BenchmarkMul(b *testing.B) { for n := 0; n < b.N; n++ { - mul{{ .FIELD}}(&benchmarkFpX2, &bench_x, &bench_y) + mul{{.FIELD}}(&benchmarkFpX2, &bench_x, &bench_y) } } @@ -80,50 +80,50 @@ func BenchmarkRdc(b *testing.B) { z := bench_z // This benchmark actually computes garbage, because - // rdc{{ .FIELD}} mangles its input, but since it's + // rdc{{.FIELD}} mangles its input, but since it's // constant-time that shouldn't matter for the benchmarks. for n := 0; n < b.N; n++ { - rdc{{ .FIELD}}(&benchmarkFp, &z) + rdc{{.FIELD}}(&benchmarkFp, &z) } } func BenchmarkAdd(b *testing.B) { for n := 0; n < b.N; n++ { - add{{ .FIELD}}(&benchmarkFp, &bench_x, &bench_y) + add{{.FIELD}}(&benchmarkFp, &bench_x, &bench_y) } } func BenchmarkSub(b *testing.B) { for n := 0; n < b.N; n++ { - sub{{ .FIELD}}(&benchmarkFp, &bench_x, &bench_y) + sub{{.FIELD}}(&benchmarkFp, &bench_x, &bench_y) } } func BenchmarkCswap(b *testing.B) { x, y := bench_x, bench_y for n := 0; n < b.N; n++ { - cswap{{ .FIELD}}(&x, &y, 1) - cswap{{ .FIELD}}(&x, &y, 0) + cswap{{.FIELD}}(&x, &y, 1) + cswap{{.FIELD}}(&x, &y, 0) } } func BenchmarkMod(b *testing.B) { x := bench_x for n := 0; n < b.N; n++ { - mod{{ .FIELD}}(&x) + mod{{.FIELD}}(&x) } } func BenchmarkX2AddLazy(b *testing.B) { x, y, z := bench_z, bench_z, bench_z for n := 0; n < b.N; n++ { - adl{{ .FIELD}}(&x, &y, &z) + adl{{.FIELD}}(&x, &y, &z) } } func BenchmarkX2SubLazy(b *testing.B) { x, y, z := bench_z, bench_z, bench_z for n := 0; n < b.N; n++ { - sul{{ .FIELD}}(&x, &y, &z) + sul{{.FIELD}}(&x, &y, &z) } } diff --git a/dh/sidh/internal/templates/core.gotemp b/dh/sidh/internal/templates/core.gotemp index 6e0472de7..7a8486bdb 100644 --- a/dh/sidh/internal/templates/core.gotemp +++ b/dh/sidh/internal/templates/core.gotemp @@ -1,7 +1,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots. -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( . "github.com/cloudflare/circl/dh/sidh/internal/common" diff --git a/dh/sidh/internal/templates/curve.gotemp b/dh/sidh/internal/templates/curve.gotemp index 6161a8311..c548d836a 100644 --- a/dh/sidh/internal/templates/curve.gotemp +++ b/dh/sidh/internal/templates/curve.gotemp @@ -1,7 +1,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots. -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( "crypto/rand" diff --git a/dh/sidh/internal/templates/curve_test.gotemp b/dh/sidh/internal/templates/curve_test.gotemp index 46bd5a2a5..01b865986 100644 --- a/dh/sidh/internal/templates/curve_test.gotemp +++ b/dh/sidh/internal/templates/curve_test.gotemp @@ -1,7 +1,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots. -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( "bytes" diff --git a/dh/sidh/internal/templates/fp2.gotemp b/dh/sidh/internal/templates/fp2.gotemp index c018cc8b9..6f5df26b5 100644 --- a/dh/sidh/internal/templates/fp2.gotemp +++ b/dh/sidh/internal/templates/fp2.gotemp @@ -1,7 +1,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots. -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( "github.com/cloudflare/circl/dh/sidh/internal/common" @@ -11,8 +11,8 @@ import ( // in Montgomery domain. func mulP(dest, lhs, rhs *common.Fp) { var ab common.FpX2 - mul{{ .FIELD}}(&ab, lhs, rhs) // = a*b*R*R - rdc{{ .FIELD}}(dest, &ab) // = a*b*R mod p + mul{{.FIELD}}(&ab, lhs, rhs) // = a*b*R*R + rdc{{.FIELD}}(dest, &ab) // = a*b*R mod p } // Set dest = x^((p-3)/4). If x is square, this is 1/sqrt(x). @@ -28,9 +28,9 @@ func p34(dest, x *common.Fp) { // This performs sum(powStrategy) + 1 squarings and len(lookup) + len(mulStrategy) // multiplications. - powStrategy := {{ .P34_POW_STRATEGY}} - mulStrategy := {{ .P34_MUL_STRATEGY}} - initialMul := uint8({{ .P34_INITIAL_MUL}}) + powStrategy := {{.P34_POW_STRATEGY}} + mulStrategy := {{.P34_MUL_STRATEGY}} + initialMul := uint8({{.P34_INITIAL_MUL}}) // Precompute lookup table of odd multiples of x for window // size k=5. @@ -55,13 +55,13 @@ func p34(dest, x *common.Fp) { } func add(dest, lhs, rhs *common.Fp2) { - add{{ .FIELD}}(&dest.A, &lhs.A, &rhs.A) - add{{ .FIELD}}(&dest.B, &lhs.B, &rhs.B) + add{{.FIELD}}(&dest.A, &lhs.A, &rhs.A) + add{{.FIELD}}(&dest.B, &lhs.B, &rhs.B) } func sub(dest, lhs, rhs *common.Fp2) { - sub{{ .FIELD}}(&dest.A, &lhs.A, &rhs.A) - sub{{ .FIELD}}(&dest.B, &lhs.B, &rhs.B) + sub{{.FIELD}}(&dest.A, &lhs.A, &rhs.A) + sub{{.FIELD}}(&dest.B, &lhs.B, &rhs.B) } func mul(dest, lhs, rhs *common.Fp2) { @@ -79,16 +79,16 @@ func mul(dest, lhs, rhs *common.Fp2) { // (b - a)*(c - d) = (b*c + a*d) - a*c - b*d // // so (a*d + b*c) = (b-a)*(c-d) + a*c + b*d. - mul{{ .FIELD}}(&ac, &lhs.A, &rhs.A) // = a*c*R*R - mul{{ .FIELD}}(&bd, &lhs.B, &rhs.B) // = b*d*R*R - sub{{ .FIELD}}(&bMinA, &lhs.B, &lhs.A) // = (b-a)*R - sub{{ .FIELD}}(&cMinD, &rhs.A, &rhs.B) // = (c-d)*R - mul{{ .FIELD}}(&adPlusBc, &bMinA, &cMinD) // = (b-a)*(c-d)*R*R - adl{{ .FIELD}}(&adPlusBc, &adPlusBc, &ac) // = ((b-a)*(c-d) + a*c)*R*R - adl{{ .FIELD}}(&adPlusBc, &adPlusBc, &bd) // = ((b-a)*(c-d) + a*c + b*d)*R*R - rdc{{ .FIELD}}(&dest.B, &adPlusBc) // = (a*d + b*c)*R mod p - sul{{ .FIELD}}(&acMinBd, &ac, &bd) // = (a*c - b*d)*R*R - rdc{{ .FIELD}}(&dest.A, &acMinBd) // = (a*c - b*d)*R mod p + mul{{.FIELD}}(&ac, &lhs.A, &rhs.A) // = a*c*R*R + mul{{.FIELD}}(&bd, &lhs.B, &rhs.B) // = b*d*R*R + sub{{.FIELD}}(&bMinA, &lhs.B, &lhs.A) // = (b-a)*R + sub{{.FIELD}}(&cMinD, &rhs.A, &rhs.B) // = (c-d)*R + mul{{.FIELD}}(&adPlusBc, &bMinA, &cMinD) // = (b-a)*(c-d)*R*R + adl{{.FIELD}}(&adPlusBc, &adPlusBc, &ac) // = ((b-a)*(c-d) + a*c)*R*R + adl{{.FIELD}}(&adPlusBc, &adPlusBc, &bd) // = ((b-a)*(c-d) + a*c + b*d)*R*R + rdc{{.FIELD}}(&dest.B, &adPlusBc) // = (a*d + b*c)*R mod p + sul{{.FIELD}}(&acMinBd, &ac, &bd) // = (a*c - b*d)*R*R + rdc{{.FIELD}}(&dest.A, &acMinBd) // = (a*c - b*d)*R mod p } // Set dest = 1/x @@ -110,10 +110,10 @@ func inv(dest, x *common.Fp2) { // // 1/(a+bi) = a*c - b*ci. - mul{{ .FIELD}}(&e1, &x.A, &x.A) // = a*a*R*R - mul{{ .FIELD}}(&e2, &x.B, &x.B) // = b*b*R*R - adl{{ .FIELD}}(&e1, &e1, &e2) // = (a^2 + b^2)*R*R - rdc{{ .FIELD}}(&f1, &e1) // = (a^2 + b^2)*R mod p + mul{{.FIELD}}(&e1, &x.A, &x.A) // = a*a*R*R + mul{{.FIELD}}(&e2, &x.B, &x.B) // = b*b*R*R + adl{{.FIELD}}(&e1, &e1, &e2) // = (a^2 + b^2)*R*R + rdc{{.FIELD}}(&f1, &e1) // = (a^2 + b^2)*R mod p // Now f1 = a^2 + b^2 mulP(&f2, &f1, &f1) @@ -121,12 +121,12 @@ func inv(dest, x *common.Fp2) { mulP(&f2, &f2, &f2) mulP(&f2, &f2, &f1) - mul{{ .FIELD}}(&e1, &x.A, &f2) - rdc{{ .FIELD}}(&dest.A, &e1) + mul{{.FIELD}}(&e1, &x.A, &f2) + rdc{{.FIELD}}(&dest.A, &e1) - sub{{ .FIELD}}(&f1, &common.Fp{}, &x.B) - mul{{ .FIELD}}(&e1, &f1, &f2) - rdc{{ .FIELD}}(&dest.B, &e1) + sub{{.FIELD}}(&f1, &common.Fp{}, &x.B) + mul{{.FIELD}}(&e1, &f1, &f2) + rdc{{.FIELD}}(&dest.B, &e1) } func sqr(dest, x *common.Fp2) { @@ -137,13 +137,13 @@ func sqr(dest, x *common.Fp2) { b := &x.B // (a + bi)*(a + bi) = (a^2 - b^2) + 2abi. - add{{ .FIELD}}(&a2, a, a) // = a*R + a*R = 2*a*R - add{{ .FIELD}}(&aPlusB, a, b) // = a*R + b*R = (a+b)*R - sub{{ .FIELD}}(&aMinusB, a, b) // = a*R - b*R = (a-b)*R - mul{{ .FIELD}}(&a2MinB2, &aPlusB, &aMinusB) // = (a+b)*(a-b)*R*R = (a^2 - b^2)*R*R - mul{{ .FIELD}}(&ab2, &a2, b) // = 2*a*b*R*R - rdc{{ .FIELD}}(&dest.A, &a2MinB2) // = (a^2 - b^2)*R mod p - rdc{{ .FIELD}}(&dest.B, &ab2) // = 2*a*b*R mod p + add{{.FIELD}}(&a2, a, a) // = a*R + a*R = 2*a*R + add{{.FIELD}}(&aPlusB, a, b) // = a*R + b*R = (a+b)*R + sub{{.FIELD}}(&aMinusB, a, b) // = a*R - b*R = (a-b)*R + mul{{.FIELD}}(&a2MinB2, &aPlusB, &aMinusB) // = (a+b)*(a-b)*R*R = (a^2 - b^2)*R*R + mul{{.FIELD}}(&ab2, &a2, b) // = 2*a*b*R*R + rdc{{.FIELD}}(&dest.A, &a2MinB2) // = (a^2 - b^2)*R mod p + rdc{{.FIELD}}(&dest.B, &ab2) // = 2*a*b*R mod p } // In case choice == 1, performs following swap in constant time: @@ -151,10 +151,10 @@ func sqr(dest, x *common.Fp2) { // xPz <-> xQz // Otherwise returns xPx, xPz, xQx, xQz unchanged func cswap(xPx, xPz, xQx, xQz *common.Fp2, choice uint8) { - cswap{{ .FIELD}}(&xPx.A, &xQx.A, choice) - cswap{{ .FIELD}}(&xPx.B, &xQx.B, choice) - cswap{{ .FIELD}}(&xPz.A, &xQz.A, choice) - cswap{{ .FIELD}}(&xPz.B, &xQz.B, choice) + cswap{{.FIELD}}(&xPx.A, &xQx.A, choice) + cswap{{.FIELD}}(&xPx.B, &xQx.B, choice) + cswap{{.FIELD}}(&xPz.A, &xQz.A, choice) + cswap{{.FIELD}}(&xPz.B, &xQz.B, choice) } // In case choice == 1, performs following moves in constant time: @@ -162,10 +162,10 @@ func cswap(xPx, xPz, xQx, xQz *common.Fp2, choice uint8) { // xPz <- xQz // Otherwise returns xPx, xPz, xQx, xQz unchanged func cmov(xPx, xPz, xQx, xQz *common.Fp2, choice uint8) { - cmov{{ .FIELD}}(&xPx.A, &xQx.A, choice) - cmov{{ .FIELD}}(&xPx.B, &xQx.B, choice) - cmov{{ .FIELD}}(&xPz.A, &xQz.A, choice) - cmov{{ .FIELD}}(&xPz.B, &xQz.B, choice) + cmov{{.FIELD}}(&xPx.A, &xQx.A, choice) + cmov{{.FIELD}}(&xPx.B, &xQx.B, choice) + cmov{{.FIELD}}(&xPz.A, &xQz.A, choice) + cmov{{.FIELD}}(&xPz.B, &xQz.B, choice) } func isZero(x *common.Fp2) uint8 { @@ -189,11 +189,11 @@ func ToMontgomery(out, in *common.Fp2) { var aRR common.FpX2 // a*R*R - mul{{ .FIELD}}(&aRR, &in.A, &{{ .FIELD}}R2) + mul{{.FIELD}}(&aRR, &in.A, &{{.FIELD}}R2) // a*R mod p - rdc{{ .FIELD}}(&out.A, &aRR) - mul{{ .FIELD}}(&aRR, &in.B, &{{ .FIELD}}R2) - rdc{{ .FIELD}}(&out.B, &aRR) + rdc{{.FIELD}}(&out.A, &aRR) + mul{{.FIELD}}(&aRR, &in.B, &{{.FIELD}}R2) + rdc{{.FIELD}}(&out.B, &aRR) } // Converts in.A and in.B from Montgomery domain and stores @@ -207,12 +207,12 @@ func FromMontgomery(out, in *common.Fp2) { // convert from montgomery domain copy(aR[:], in.A[:]) - rdc{{ .FIELD}}(&out.A, &aR) // = a mod p in [0, 2p) - mod{{ .FIELD}}(&out.A) // = a mod p in [0, p) + rdc{{.FIELD}}(&out.A, &aR) // = a mod p in [0, 2p) + mod{{.FIELD}}(&out.A) // = a mod p in [0, p) for i := range aR { aR[i] = 0 } copy(aR[:], in.B[:]) - rdc{{ .FIELD}}(&out.B, &aR) - mod{{ .FIELD}}(&out.B) + rdc{{.FIELD}}(&out.B, &aR) + mod{{.FIELD}}(&out.B) } diff --git a/dh/sidh/internal/templates/fp2_test.gotemp b/dh/sidh/internal/templates/fp2_test.gotemp index 8ad23791f..140359c4f 100644 --- a/dh/sidh/internal/templates/fp2_test.gotemp +++ b/dh/sidh/internal/templates/fp2_test.gotemp @@ -1,7 +1,7 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots. -package {{ .PACKAGE}} +package {{.PACKAGE}} import ( "math/rand" @@ -23,10 +23,10 @@ func vartimeEqFp2(lhs, rhs *common.Fp2) bool { a := *lhs b := *rhs - mod{{ .FIELD}}(&a.A) - mod{{ .FIELD}}(&a.B) - mod{{ .FIELD}}(&b.A) - mod{{ .FIELD}}(&b.B) + mod{{.FIELD}}(&a.A) + mod{{.FIELD}}(&a.B) + mod{{.FIELD}}(&b.A) + mod{{.FIELD}}(&b.B) eq := true for i := 0; i < FpWords && eq; i++ { @@ -50,7 +50,7 @@ func (testParams) generateFp2(rand *rand.Rand) common.Fp2 { // This still allows generating values >= 2p, but hopefully that // excess is OK (and if it's not, we'll find out, because it's for // testing...) - highLimb := rand.Uint64() % {{ .FIELD}}x2[FpWords-1] + highLimb := rand.Uint64() % {{.FIELD}}x2[FpWords-1] fpElementGen := func() (fp common.Fp) { for i := 0; i < (FpWords - 1); i++ { fp[i] = rand.Uint64() diff --git a/kem/kyber/templates/pkg.templ.go b/kem/kyber/templates/pkg.templ.go index f3e20530d..8fc5b3d97 100644 --- a/kem/kyber/templates/pkg.templ.go +++ b/kem/kyber/templates/pkg.templ.go @@ -271,7 +271,7 @@ var sch kem.Scheme = &scheme{} // Scheme returns a KEM interface. func Scheme() kem.Scheme { return sch } -func (*scheme) Name() string { return "{{ .Name }}" } +func (*scheme) Name() string { return "{{.Name}}" } func (*scheme) PublicKeySize() int { return PublicKeySize } func (*scheme) PrivateKeySize() int { return PrivateKeySize } func (*scheme) SeedSize() int { return KeySeedSize } diff --git a/pke/kyber/templates/params.templ.go b/pke/kyber/templates/params.templ.go index 77594285d..5c913bfbc 100644 --- a/pke/kyber/templates/params.templ.go +++ b/pke/kyber/templates/params.templ.go @@ -11,15 +11,15 @@ import ( ) const ( - K = {{ .K }} - Eta1 = {{ .Eta1 }} - DU = {{ .DU }} - DV = {{ .DV }} + K = {{.K}} + Eta1 = {{.Eta1}} + DU = {{.DU}} + DV = {{.DV}} PublicKeySize = 32 + K*common.PolySize PrivateKeySize = K * common.PolySize PlaintextSize = common.PlaintextSize SeedSize = 32 - CiphertextSize = {{ .CiphertextSize }} + CiphertextSize = {{.CiphertextSize}} ) diff --git a/pke/kyber/templates/pkg.templ.go b/pke/kyber/templates/pkg.templ.go index ef680885f..4afec38b9 100644 --- a/pke/kyber/templates/pkg.templ.go +++ b/pke/kyber/templates/pkg.templ.go @@ -4,18 +4,18 @@ // Code generated from modePkg.templ.go. DO NOT EDIT. -// {{ .Pkg }} implements the IND-CPA-secure Public Key Encryption -// scheme {{ .Name }}.CPAPKE as submitted to round 3 of the NIST PQC competition +// {{.Pkg}} implements the IND-CPA-secure Public Key Encryption +// scheme {{.Name}}.CPAPKE as submitted to round 3 of the NIST PQC competition // and described in // // https://pq-crystals.org/kyber/data/kyber-specification-round3.pdf -package {{ .Pkg }} +package {{.Pkg}} import ( cryptoRand "crypto/rand" "io" - "github.com/cloudflare/circl/pke/kyber/{{ .Pkg }}/internal" + "github.com/cloudflare/circl/pke/kyber/{{.Pkg}}/internal" ) const ( @@ -38,10 +38,10 @@ const ( PlaintextSize = internal.PlaintextSize ) -// PublicKey is the type of {{ .Name }}.CPAPKE public key +// PublicKey is the type of {{.Name}}.CPAPKE public key type PublicKey internal.PublicKey -// PrivateKey is the type of {{ .Name }}.CPAPKE private key +// PrivateKey is the type of {{.Name}}.CPAPKE private key type PrivateKey internal.PrivateKey // GenerateKey generates a public/private key pair using entropy from rand. diff --git a/sign/dilithium/templates/mode.templ.go b/sign/dilithium/templates/mode.templ.go index b43c54657..8c76b2512 100644 --- a/sign/dilithium/templates/mode.templ.go +++ b/sign/dilithium/templates/mode.templ.go @@ -11,84 +11,84 @@ import ( "io" "github.com/cloudflare/circl/sign/dilithium/internal/common" - "github.com/cloudflare/circl/sign/dilithium/{{ .Pkg }}" + "github.com/cloudflare/circl/sign/dilithium/{{.Pkg}}" ) -// {{ .Impl }} implements the mode.Mode interface for {{ .Name }}. -type {{ .Impl }} struct{} +// {{.Impl}} implements the mode.Mode interface for {{.Name}}. +type {{.Impl}} struct{} -// {{ .Mode }} is Dilithium in mode "{{ .Name }}". -var {{ .Mode }} Mode = &{{ .Impl }}{} +// {{.Mode}} is Dilithium in mode "{{.Name}}". +var {{.Mode}} Mode = &{{.Impl}}{} -func (m *{{ .Impl }}) GenerateKey(rand io.Reader) ( +func (m *{{.Impl}}) GenerateKey(rand io.Reader) ( PublicKey, PrivateKey, error) { - return {{ .Pkg }}.GenerateKey(rand) + return {{.Pkg}}.GenerateKey(rand) } -func (m *{{ .Impl }}) NewKeyFromSeed(seed []byte) (PublicKey, +func (m *{{.Impl}}) NewKeyFromSeed(seed []byte) (PublicKey, PrivateKey) { if len(seed) != common.SeedSize { panic(fmt.Sprintf("seed must be of length %d", common.SeedSize)) } seedBuf := [common.SeedSize]byte{} copy(seedBuf[:], seed) - return {{ .Pkg}}.NewKeyFromSeed(&seedBuf) + return {{.Pkg}}.NewKeyFromSeed(&seedBuf) } -func (m *{{ .Impl }}) Sign(sk PrivateKey, msg []byte) []byte { - isk := sk.(*{{ .Pkg }}.PrivateKey) - ret := [{{ .Pkg }}.SignatureSize]byte{} - {{ .Pkg}}.SignTo(isk, msg, ret[:]) +func (m *{{.Impl}}) Sign(sk PrivateKey, msg []byte) []byte { + isk := sk.(*{{.Pkg}}.PrivateKey) + ret := [{{.Pkg}}.SignatureSize]byte{} + {{.Pkg}}.SignTo(isk, msg, ret[:]) return ret[:] } -func (m *{{ .Impl }}) Verify(pk PublicKey, msg []byte, signature []byte) bool { - ipk := pk.(*{{ .Pkg }}.PublicKey) - return {{ .Pkg }}.Verify(ipk, msg, signature) +func (m *{{.Impl}}) Verify(pk PublicKey, msg []byte, signature []byte) bool { + ipk := pk.(*{{.Pkg}}.PublicKey) + return {{.Pkg}}.Verify(ipk, msg, signature) } -func (m *{{ .Impl }}) PublicKeyFromBytes(data []byte) PublicKey { - var ret {{ .Pkg }}.PublicKey - if len(data) != {{ .Pkg }}.PublicKeySize { - panic("packed public key must be of {{ .Pkg }}.PublicKeySize bytes") +func (m *{{.Impl}}) PublicKeyFromBytes(data []byte) PublicKey { + var ret {{.Pkg}}.PublicKey + if len(data) != {{.Pkg}}.PublicKeySize { + panic("packed public key must be of {{.Pkg}}.PublicKeySize bytes") } - var buf [{{ .Pkg }}.PublicKeySize]byte + var buf [{{.Pkg}}.PublicKeySize]byte copy(buf[:], data) ret.Unpack(&buf) return &ret } -func (m *{{ .Impl }}) PrivateKeyFromBytes(data []byte) PrivateKey { - var ret {{ .Pkg }}.PrivateKey - if len(data) != {{ .Pkg }}.PrivateKeySize { - panic("packed public key must be of {{ .Pkg }}.PrivateKeySize bytes") +func (m *{{.Impl}}) PrivateKeyFromBytes(data []byte) PrivateKey { + var ret {{.Pkg}}.PrivateKey + if len(data) != {{.Pkg}}.PrivateKeySize { + panic("packed public key must be of {{.Pkg}}.PrivateKeySize bytes") } - var buf [{{ .Pkg }}.PrivateKeySize]byte + var buf [{{.Pkg}}.PrivateKeySize]byte copy(buf[:], data) ret.Unpack(&buf) return &ret } -func (m *{{ .Impl }}) SeedSize() int { +func (m *{{.Impl}}) SeedSize() int { return common.SeedSize } -func (m *{{ .Impl }}) PublicKeySize() int { - return {{ .Pkg }}.PublicKeySize +func (m *{{.Impl}}) PublicKeySize() int { + return {{.Pkg}}.PublicKeySize } -func (m *{{ .Impl }}) PrivateKeySize() int { - return {{ .Pkg }}.PrivateKeySize +func (m *{{.Impl}}) PrivateKeySize() int { + return {{.Pkg}}.PrivateKeySize } -func (m *{{ .Impl }}) SignatureSize() int { - return {{ .Pkg }}.SignatureSize +func (m *{{.Impl}}) SignatureSize() int { + return {{.Pkg}}.SignatureSize } -func (m *{{ .Impl }}) Name() string { - return "{{ .Name }}" +func (m *{{.Impl}}) Name() string { + return "{{.Name}}" } func init() { - modes["{{ .Name }}"] = {{ .Mode }} + modes["{{.Name}}"] = {{.Mode}} } diff --git a/sign/dilithium/templates/modePkg.templ.go b/sign/dilithium/templates/modePkg.templ.go index 74a6a9f8d..29dc73556 100644 --- a/sign/dilithium/templates/modePkg.templ.go +++ b/sign/dilithium/templates/modePkg.templ.go @@ -4,11 +4,11 @@ // Code generated from modePkg.templ.go. DO NOT EDIT. -// {{ .Pkg }} implements the CRYSTALS-Dilithium signature scheme {{ .Name }} +// {{.Pkg}} implements the CRYSTALS-Dilithium signature scheme {{.Name}} // as submitted to round3 of the NIST PQC competition and described in // // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf -package {{ .Pkg }} +package {{.Pkg}} import ( "crypto" @@ -16,7 +16,7 @@ import ( "io" "github.com/cloudflare/circl/sign/dilithium/internal/common" - "github.com/cloudflare/circl/sign/dilithium/{{ .Pkg }}/internal" + "github.com/cloudflare/circl/sign/dilithium/{{.Pkg}}/internal" ) const ( @@ -33,10 +33,10 @@ const ( SignatureSize = internal.SignatureSize ) -// PublicKey is the type of {{ .Name }} public key +// PublicKey is the type of {{.Name}} public key type PublicKey internal.PublicKey -// PrivateKey is the type of {{ .Name }} private key +// PrivateKey is the type of {{.Name}} private key type PrivateKey internal.PrivateKey // GenerateKey generates a public/private key pair using entropy from rand. @@ -118,7 +118,7 @@ func (sk *PrivateKey) MarshalBinary() ([]byte, error) { // Unpacks the public key from data. func (pk *PublicKey) UnmarshalBinary(data []byte) error { if len(data) != PublicKeySize { - return errors.New("packed public key must be of {{ .Pkg }}.PublicKeySize bytes") + return errors.New("packed public key must be of {{.Pkg}}.PublicKeySize bytes") } var buf [PublicKeySize]byte copy(buf[:], data) @@ -129,7 +129,7 @@ func (pk *PublicKey) UnmarshalBinary(data []byte) error { // Unpacks the private key from data. func (sk *PrivateKey) UnmarshalBinary(data []byte) error { if len(data) != PrivateKeySize { - return errors.New("packed private key must be of {{ .Pkg }}.PrivateKeySize bytes") + return errors.New("packed private key must be of {{.Pkg}}.PrivateKeySize bytes") } var buf [PrivateKeySize]byte copy(buf[:], data) diff --git a/sign/dilithium/templates/params.templ.go b/sign/dilithium/templates/params.templ.go index ee40632b8..1e0331c25 100644 --- a/sign/dilithium/templates/params.templ.go +++ b/sign/dilithium/templates/params.templ.go @@ -7,14 +7,14 @@ package internal const ( - Name = "{{ .Name }}" - UseAES = {{ .UseAES }} - K = {{ .K }} - L = {{ .L }} - Eta = {{ .Eta }} - DoubleEtaBits = {{ .DoubleEtaBits }} - Omega = {{ .Omega }} - Tau = {{ .Tau }} - Gamma1Bits = {{ .Gamma1Bits }} - Gamma2 = {{ .Gamma2 }} + Name = "{{.Name}}" + UseAES = {{.UseAES}} + K = {{.K}} + L = {{.L}} + Eta = {{.Eta}} + DoubleEtaBits = {{.DoubleEtaBits}} + Omega = {{.Omega}} + Tau = {{.Tau}} + Gamma1Bits = {{.Gamma1Bits}} + Gamma2 = {{.Gamma2}} )