github.com/cloudflare/circl@v1.5.0/sign/ed25519/modular_test.go (about) 1 package ed25519 2 3 import ( 4 "crypto/rand" 5 "testing" 6 7 "github.com/cloudflare/circl/internal/conv" 8 "github.com/cloudflare/circl/internal/test" 9 ) 10 11 func TestCalculateS(t *testing.T) { 12 const testTimes = 1 << 10 13 s := make([]byte, paramB) 14 k := make([]byte, paramB) 15 r := make([]byte, paramB) 16 a := make([]byte, paramB) 17 orderBig := conv.BytesLe2BigInt(order[:]) 18 19 for i := 0; i < testTimes; i++ { 20 _, _ = rand.Read(k[:]) 21 _, _ = rand.Read(r[:]) 22 _, _ = rand.Read(a[:]) 23 bigK := conv.BytesLe2BigInt(k[:]) 24 bigR := conv.BytesLe2BigInt(r[:]) 25 bigA := conv.BytesLe2BigInt(a[:]) 26 27 calculateS(s, r, k, a) 28 got := conv.BytesLe2BigInt(s[:]) 29 30 bigK.Mul(bigK, bigA).Add(bigK, bigR) 31 want := bigK.Mod(bigK, orderBig) 32 33 if got.Cmp(want) != 0 { 34 test.ReportError(t, got, want, k, r, a) 35 } 36 } 37 } 38 39 func TestReduction(t *testing.T) { 40 const testTimes = 1 << 10 41 var x, y [paramB * 2]byte 42 orderBig := conv.BytesLe2BigInt(order[:]) 43 44 for i := 0; i < testTimes; i++ { 45 for _, j := range []int{paramB, 2 * paramB} { 46 _, _ = rand.Read(x[:j]) 47 bigX := conv.BytesLe2BigInt(x[:j]) 48 copy(y[:j], x[:j]) 49 50 reduceModOrder(y[:j], true) 51 got := conv.BytesLe2BigInt(y[:]) 52 53 want := bigX.Mod(bigX, orderBig) 54 55 if got.Cmp(want) != 0 { 56 test.ReportError(t, got, want, x) 57 } 58 } 59 } 60 } 61 62 func TestRangeOrder(t *testing.T) { 63 aboveOrder := [...][paramB]byte{ 64 { // order 65 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 66 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 69 }, 70 { // order+1 71 0xed + 1, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 72 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 75 }, 76 { // all-ones 77 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 78 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 79 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 80 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 81 }, 82 } 83 84 for i := range aboveOrder { 85 got := isLessThanOrder(aboveOrder[i][:]) 86 want := false 87 if got != want { 88 test.ReportError(t, got, want, i, aboveOrder[i]) 89 } 90 } 91 }