gfp_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package bn256
  2. import (
  3. "testing"
  4. )
  5. // Tests that negation works the same way on both assembly-optimized and pure Go
  6. // implementation.
  7. func TestGFpNeg(t *testing.T) {
  8. n := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed}
  9. w := &gfP{0xfedcba9876543211, 0x0123456789abcdef, 0x2152411021524110, 0x0114251201142512}
  10. h := &gfP{}
  11. gfpNeg(h, n)
  12. if *h != *w {
  13. t.Errorf("negation mismatch: have %#x, want %#x", *h, *w)
  14. }
  15. }
  16. // Tests that addition works the same way on both assembly-optimized and pure Go
  17. // implementation.
  18. func TestGFpAdd(t *testing.T) {
  19. a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed}
  20. b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef}
  21. w := &gfP{0xc3df73e9278302b8, 0x687e956e978e3572, 0x254954275c18417f, 0xad354b6afc67f9b4}
  22. h := &gfP{}
  23. gfpAdd(h, a, b)
  24. if *h != *w {
  25. t.Errorf("addition mismatch: have %#x, want %#x", *h, *w)
  26. }
  27. }
  28. // Tests that subtraction works the same way on both assembly-optimized and pure Go
  29. // implementation.
  30. func TestGFpSub(t *testing.T) {
  31. a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed}
  32. b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef}
  33. w := &gfP{0x02468acf13579bdf, 0xfdb97530eca86420, 0xdfc1e401dfc1e402, 0x203e1bfe203e1bfd}
  34. h := &gfP{}
  35. gfpSub(h, a, b)
  36. if *h != *w {
  37. t.Errorf("subtraction mismatch: have %#x, want %#x", *h, *w)
  38. }
  39. }
  40. // Tests that multiplication works the same way on both assembly-optimized and pure Go
  41. // implementation.
  42. func TestGFpMul(t *testing.T) {
  43. a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed}
  44. b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef}
  45. w := &gfP{0xcbcbd377f7ad22d3, 0x3b89ba5d849379bf, 0x87b61627bd38b6d2, 0xc44052a2a0e654b2}
  46. h := &gfP{}
  47. gfpMul(h, a, b)
  48. if *h != *w {
  49. t.Errorf("multiplication mismatch: have %#x, want %#x", *h, *w)
  50. }
  51. }