gtank / ristretto255

Implements ristretto255, a fast prime-order group.
https://ristretto.group
BSD 3-Clause "New" or "Revised" License
98 stars 22 forks source link

Add property-based tests for field arithmetic. #12

Closed hdevalence closed 5 years ago

hdevalence commented 5 years ago

So far the only test I added failed, not sure why yet.

hdevalence commented 5 years ago
p = 2**255 - 19
Fp = GF(p)
from_radix_51 = lambda xs: sum(xi * (2**(51*i)) for i,xi in enumerate(xs))
# e_test.go:57: #1: failed on input 
# radix51.FieldElement{0x592101fd8643a, 0x25a08467381e1, 0x48cb4dcd5dcf5, 0x1074d52744164, 0x91902aac541b}, 
# radix51.FieldElement{0x165bb67340a7f, 0x52cf7781f4ad6, 0x32534ba21fde4, 0x5b4ba9cbb1736, 0x2e90748c54289}, 
# radix51.FieldElement{0x25a575665ad1e, 0x124e496e3eeaa, 0x433d2180e2561, 0x221c8be3aa11a, 0x7adc8d0adf806}

fail_01_x = from_radix_51([0x592101fd8643a, 0x25a08467381e1, 0x48cb4dcd5dcf5, 0x1074d52744164, 0x91902aac541b])
fail_01_y = from_radix_51([0x165bb67340a7f, 0x52cf7781f4ad6, 0x32534ba21fde4, 0x5b4ba9cbb1736, 0x2e90748c54289])
fail_01_z = from_radix_51([0x25a575665ad1e, 0x124e496e3eeaa, 0x433d2180e2561, 0x221c8be3aa11a, 0x7adc8d0adf806])

Fp((fail_01_x + fail_01_y) * fail_01_z) == Fp(fail_01_x*fail_01_z + fail_01_y*fail_01_z)
True
fail_01_t1 = from_radix_51([0x5f9b66f6d4669, 0x4488b0efa53dd, 0x49cf35f6b1cc9, 0x60db87eec0d68, 0x2badd8351c056])
fail_01_t2 = from_radix_51([0xbd851ffbf314e, 0xa5372f1ddd83c, 0xa2c696988757c, 0x5e09876f7621a, 0xd4240321a90cc])

Fp(fail_01_t1) == Fp(fail_01_t2)
False
Fp(fail_01_t1) == Fp((fail_01_x + fail_01_y) * fail_01_z)
True
Fp(fail_01_t2) == Fp(fail_01_x*fail_01_z + fail_01_y*fail_01_z)
False
hdevalence commented 5 years ago

turns out it's good to look for typos in your own tests

FiloSottile commented 5 years ago

Merging this (manually because we can't override the draft status) into #15 because I want to reuse the Generator for aliasing tests. (Also LGTM!)